From 101b6080ffef23b9a3c17f4aca172f2d14871dd5 Mon Sep 17 00:00:00 2001 From: Artx Hundiak Date: Wed, 19 Aug 2015 10:01:54 -0500 Subject: [PATCH 1/3] Handles null file in createrequest bridge. Fixed PHP 5.3.3 array syntax --- Factory/DiactorosFactory.php | 6 ++++- Tests/Factory/DiactorosFactoryTest.php | 34 ++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) 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()); + } } From e5d62e62720e935d548999aaf367875467ea2e3b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Str=C3=B8m?= Date: Thu, 2 Jun 2016 10:54:14 +0200 Subject: [PATCH 2/3] Fixes based on code-review --- Factory/DiactorosFactory.php | 2 +- Factory/HttpFoundationFactory.php | 10 +++++++--- Tests/Factory/DiactorosFactoryTest.php | 15 +++++---------- 3 files changed, 13 insertions(+), 14 deletions(-) diff --git a/Factory/DiactorosFactory.php b/Factory/DiactorosFactory.php index f0fc0d3..4777e7a 100644 --- a/Factory/DiactorosFactory.php +++ b/Factory/DiactorosFactory.php @@ -86,7 +86,7 @@ private function getFiles(array $uploadedFiles) $files = array(); foreach ($uploadedFiles as $key => $value) { - if ($value === null) { + if (null === $value) { $files[$key] = new DiactorosUploadedFile(null, 0, UPLOAD_ERR_NO_FILE, null, null); continue; } diff --git a/Factory/HttpFoundationFactory.php b/Factory/HttpFoundationFactory.php index 2c356fd..1f52545 100644 --- a/Factory/HttpFoundationFactory.php +++ b/Factory/HttpFoundationFactory.php @@ -80,10 +80,14 @@ private function getFiles(array $uploadedFiles) */ private function createUploadedFile(UploadedFileInterface $psrUploadedFile) { - $temporaryPath = $this->getTemporaryPath(); - $psrUploadedFile->moveTo($temporaryPath); + $temporaryPath = ''; + $clientFileName = ''; + if (UPLOAD_ERR_NO_FILE !== $psrUploadedFile->getError()) { + $temporaryPath = $this->getTemporaryPath(); + $psrUploadedFile->moveTo($temporaryPath); - $clientFileName = $psrUploadedFile->getClientFilename(); + $clientFileName = $psrUploadedFile->getClientFilename(); + } return new UploadedFile( $temporaryPath, diff --git a/Tests/Factory/DiactorosFactoryTest.php b/Tests/Factory/DiactorosFactoryTest.php index c6ab6dc..d36ad1d 100644 --- a/Tests/Factory/DiactorosFactoryTest.php +++ b/Tests/Factory/DiactorosFactoryTest.php @@ -164,18 +164,13 @@ public function testCreateResponseFromBinaryFile() 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()))); + $file = new UploadedFile('', '', null, 0, UPLOAD_ERR_NO_FILE, true); + $this->assertEquals(0, $file->getSize()); + $this->assertEquals(UPLOAD_ERR_NO_FILE, $file->getError()); $this->assertFalse($file->getSize()); + $this->assertInternalType('integer', $file->getClientSize()); - // 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(), + $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), From a1a631a92686388f3076a90844d0a8cefe3e8ff9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Str=C3=B8m?= Date: Thu, 18 Aug 2016 12:54:08 +0200 Subject: [PATCH 3/3] Update assert error message --- Tests/Factory/DiactorosFactoryTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tests/Factory/DiactorosFactoryTest.php b/Tests/Factory/DiactorosFactoryTest.php index d36ad1d..94ea5e4 100644 --- a/Tests/Factory/DiactorosFactoryTest.php +++ b/Tests/Factory/DiactorosFactoryTest.php @@ -167,7 +167,7 @@ public function testUploadErrNoFile() $file = new UploadedFile('', '', null, 0, UPLOAD_ERR_NO_FILE, true); $this->assertEquals(0, $file->getSize()); $this->assertEquals(UPLOAD_ERR_NO_FILE, $file->getError()); - $this->assertFalse($file->getSize()); + $this->assertFalse($file->getSize(), 'SplFile::getSize() returns false on error'); $this->assertInternalType('integer', $file->getClientSize()); $request = new Request(array(), array(), array(), array(),