Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Fixes #9 Bridge error when no file is selected #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion Factory/DiactorosFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ private function getFiles(array $uploadedFiles)
$files = array();

foreach ($uploadedFiles as $key => $value) {
if ($value === null) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

YODA: null === $value

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've always been curious why the Symfony source prefers null to come first. What's the reasoning?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$files[$key] = new DiactorosUploadedFile(null, 0, UPLOAD_ERR_NO_FILE, null, null);
continue;
}
if ($value instanceof UploadedFile) {
$files[$key] = $this->createUploadedFile($value);
} else {
Expand All @@ -107,7 +111,7 @@ private function createUploadedFile(UploadedFile $symfonyUploadedFile)
{
return new DiactorosUploadedFile(
$symfonyUploadedFile->getRealPath(),
$symfonyUploadedFile->getSize(),
$symfonyUploadedFile->getClientSize(),
$symfonyUploadedFile->getError(),
$symfonyUploadedFile->getClientOriginalName(),
$symfonyUploadedFile->getClientMimeType()
Expand Down
34 changes: 34 additions & 0 deletions Tests/Factory/DiactorosFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,4 +161,38 @@ public function testCreateResponseFromBinaryFile()

$this->assertEquals('Binary', $psrResponse->getBody()->__toString());
}

public function testUploadErrNoFile()
{
$file = new UploadedFile(null, null, null, 0, UPLOAD_ERR_NO_FILE, true);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the $path parameter must be a string according to phpdoc, so passing null is invalid

$this->assertEquals(0,$file->getSize());
$this->assertEquals(UPLOAD_ERR_NO_FILE,$file->getError());
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing space after comma


// SplFile returns false on error
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be the 3rd argument to assertFalse

$this->assertEquals('boolean',gettype(($file->getSize())));
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not needed as the assertion below is enough

$this->assertFalse($file->getSize());

// This is an integer, oddly enough internally size is declared as a string
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this comment use useless here as you are not testing UploadedFile here, thus the assertion below is awkward as well

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$this->assertTrue(is_int($file->getClientSize()));
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please use assertInternalType


$request = new Request(array(),array(),array(),array(),
array(
'f1' => $file,
'f2' => array('name' => null, 'type' => null, 'tmp_name' => null, 'error' => UPLOAD_ERR_NO_FILE, 'size' => 0),
),
array(
'REQUEST_METHOD' => 'POST',
'HTTP_HOST' => 'dunglas.fr',
'HTTP_X_SYMFONY' => '2.8',
),
'Content'
);

$psrRequest = $this->factory->createRequest($request);

$uploadedFiles = $psrRequest->getUploadedFiles();

$this->assertEquals(UPLOAD_ERR_NO_FILE, $uploadedFiles['f1']->getError());
$this->assertEquals(UPLOAD_ERR_NO_FILE, $uploadedFiles['f2']->getError());
}
}