diff --git a/Factory/DiactorosFactory.php b/Factory/DiactorosFactory.php index 31726f1..f0fc0d3 100644 --- a/Factory/DiactorosFactory.php +++ b/Factory/DiactorosFactory.php @@ -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 { @@ -107,7 +111,7 @@ private function createUploadedFile(UploadedFile $symfonyUploadedFile) { return new DiactorosUploadedFile( $symfonyUploadedFile->getRealPath(), - $symfonyUploadedFile->getSize(), + $symfonyUploadedFile->getClientSize(), $symfonyUploadedFile->getError(), $symfonyUploadedFile->getClientOriginalName(), $symfonyUploadedFile->getClientMimeType() diff --git a/Tests/Factory/DiactorosFactoryTest.php b/Tests/Factory/DiactorosFactoryTest.php index a4c32bd..c6ab6dc 100644 --- a/Tests/Factory/DiactorosFactoryTest.php +++ b/Tests/Factory/DiactorosFactoryTest.php @@ -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()); + } }