Skip to content

Commit

Permalink
Handles null file in createrequest bridge.
Browse files Browse the repository at this point in the history
Fixed PHP 5.3.3 array syntax
  • Loading branch information
ahundiak authored and Danielss89 committed Jun 3, 2016
1 parent 3664dc0 commit 101b608
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
6 changes: 5 additions & 1 deletion Factory/DiactorosFactory.php
Expand Up @@ -86,6 +86,10 @@ private function getFiles(array $uploadedFiles)
$files = array();

foreach ($uploadedFiles as $key => $value) {
if ($value === null) {
$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
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);
$this->assertEquals(0,$file->getSize());
$this->assertEquals(UPLOAD_ERR_NO_FILE,$file->getError());

// SplFile returns false on error
$this->assertEquals('boolean',gettype(($file->getSize())));
$this->assertFalse($file->getSize());

// This is an integer, oddly enough internally size is declared as a string
$this->assertTrue(is_int($file->getClientSize()));

$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());
}
}

0 comments on commit 101b608

Please sign in to comment.