From b1363202ed23b5f3d7fac2f6173596b73e79594b Mon Sep 17 00:00:00 2001 From: Amrouche Hamza Date: Tue, 5 Dec 2017 07:43:39 +0100 Subject: [PATCH] [HttpFoundation] Incorrect documentation and method name for UploadedFile::getClientSize() --- .../Component/Form/Tests/CompoundFormTest.php | 12 ++--- .../Extension/Core/Type/FileTypeTest.php | 4 +- .../HttpFoundation/File/UploadedFile.php | 9 +++- .../Tests/File/UploadedFileTest.php | 45 +++++++++++++------ .../HttpFoundation/Tests/FileBagTest.php | 14 +++--- src/Symfony/Component/HttpKernel/Client.php | 4 +- .../Component/HttpKernel/Tests/ClientTest.php | 9 ++-- .../Tests/Constraints/FileValidatorTest.php | 2 +- 8 files changed, 61 insertions(+), 38 deletions(-) diff --git a/src/Symfony/Component/Form/Tests/CompoundFormTest.php b/src/Symfony/Component/Form/Tests/CompoundFormTest.php index acdd004d425a0..83dbdc1ebad56 100644 --- a/src/Symfony/Component/Form/Tests/CompoundFormTest.php +++ b/src/Symfony/Component/Form/Tests/CompoundFormTest.php @@ -609,7 +609,7 @@ public function testSubmitPostOrPutRequest($method) 'author' => array( 'error' => array('image' => UPLOAD_ERR_OK), 'name' => array('image' => 'upload.png'), - 'size' => array('image' => 123), + 'size' => array('image' => null), 'tmp_name' => array('image' => $path), 'type' => array('image' => 'image/png'), ), @@ -630,7 +630,7 @@ public function testSubmitPostOrPutRequest($method) $form->handleRequest($request); - $file = new UploadedFile($path, 'upload.png', 'image/png', 123, UPLOAD_ERR_OK); + $file = new UploadedFile($path, 'upload.png', 'image/png', null, UPLOAD_ERR_OK); $this->assertEquals('Bernhard', $form['name']->getData()); $this->assertEquals($file, $form['image']->getData()); @@ -655,7 +655,7 @@ public function testSubmitPostOrPutRequestWithEmptyRootFormName($method) 'image' => array( 'error' => UPLOAD_ERR_OK, 'name' => 'upload.png', - 'size' => 123, + 'size' => null, 'tmp_name' => $path, 'type' => 'image/png', ), @@ -676,7 +676,7 @@ public function testSubmitPostOrPutRequestWithEmptyRootFormName($method) $form->handleRequest($request); - $file = new UploadedFile($path, 'upload.png', 'image/png', 123, UPLOAD_ERR_OK); + $file = new UploadedFile($path, 'upload.png', 'image/png', null, UPLOAD_ERR_OK); $this->assertEquals('Bernhard', $form['name']->getData()); $this->assertEquals($file, $form['image']->getData()); @@ -697,7 +697,7 @@ public function testSubmitPostOrPutRequestWithSingleChildForm($method) 'image' => array( 'error' => UPLOAD_ERR_OK, 'name' => 'upload.png', - 'size' => 123, + 'size' => null, 'tmp_name' => $path, 'type' => 'image/png', ), @@ -714,7 +714,7 @@ public function testSubmitPostOrPutRequestWithSingleChildForm($method) $form->handleRequest($request); - $file = new UploadedFile($path, 'upload.png', 'image/png', 123, UPLOAD_ERR_OK); + $file = new UploadedFile($path, 'upload.png', 'image/png', null, UPLOAD_ERR_OK); $this->assertEquals($file, $form->getData()); diff --git a/src/Symfony/Component/Form/Tests/Extension/Core/Type/FileTypeTest.php b/src/Symfony/Component/Form/Tests/Extension/Core/Type/FileTypeTest.php index 5ba1dc5a5101d..e1502130712aa 100644 --- a/src/Symfony/Component/Form/Tests/Extension/Core/Type/FileTypeTest.php +++ b/src/Symfony/Component/Form/Tests/Extension/Core/Type/FileTypeTest.php @@ -188,7 +188,7 @@ public function requestHandlerProvider() private function createUploadedFileMock(RequestHandlerInterface $requestHandler, $path, $originalName) { if ($requestHandler instanceof HttpFoundationRequestHandler) { - return new UploadedFile($path, $originalName, null, 10, null, true); + return new UploadedFile($path, $originalName, null, null, null, true); } return array( @@ -196,7 +196,7 @@ private function createUploadedFileMock(RequestHandlerInterface $requestHandler, 'error' => 0, 'type' => 'text/plain', 'tmp_name' => $path, - 'size' => 10, + 'size' => null, ); } } diff --git a/src/Symfony/Component/HttpFoundation/File/UploadedFile.php b/src/Symfony/Component/HttpFoundation/File/UploadedFile.php index 678c4f0afc6ef..d040c73373c2c 100644 --- a/src/Symfony/Component/HttpFoundation/File/UploadedFile.php +++ b/src/Symfony/Component/HttpFoundation/File/UploadedFile.php @@ -60,6 +60,9 @@ public function __construct(string $path, string $originalName, string $mimeType $this->originalName = $this->getName($originalName); $this->mimeType = $mimeType ?: 'application/octet-stream'; $this->size = $size; + if (null !== $size) { + @trigger_error('Passing a size in the constructor is deprecated since 4.1 and will be removed in 5.0. Use getSize() instead.', E_USER_DEPRECATED); + } $this->error = $error ?: UPLOAD_ERR_OK; $this->test = $test; @@ -141,10 +144,14 @@ public function guessClientExtension() * It is extracted from the request from which the file has been uploaded. * Then it should not be considered as a safe value. * - * @return int|null The file size + * @deprecated since 4.1 will be removed in 5.0 use getSize() instead. + * + * @return int|null The file sizes */ public function getClientSize() { + @trigger_error(sprintf('"%s" is deprecated since 4.1 and will be removed in 5.0. Use getSize() instead.', __METHOD__), E_USER_DEPRECATED); + return $this->size; } diff --git a/src/Symfony/Component/HttpFoundation/Tests/File/UploadedFileTest.php b/src/Symfony/Component/HttpFoundation/Tests/File/UploadedFileTest.php index 36f122fe79223..4e439297d0678 100644 --- a/src/Symfony/Component/HttpFoundation/Tests/File/UploadedFileTest.php +++ b/src/Symfony/Component/HttpFoundation/Tests/File/UploadedFileTest.php @@ -40,7 +40,7 @@ public function testFileUploadsWithNoMimeType() __DIR__.'/Fixtures/test.gif', 'original.gif', null, - filesize(__DIR__.'/Fixtures/test.gif'), + null, UPLOAD_ERR_OK ); @@ -57,7 +57,7 @@ public function testFileUploadsWithUnknownMimeType() __DIR__.'/Fixtures/.unknownextension', 'original.gif', null, - filesize(__DIR__.'/Fixtures/.unknownextension'), + null, UPLOAD_ERR_OK ); @@ -70,7 +70,7 @@ public function testGuessClientExtension() __DIR__.'/Fixtures/test.gif', 'original.gif', 'image/gif', - filesize(__DIR__.'/Fixtures/test.gif'), + null, null ); @@ -83,7 +83,7 @@ public function testGuessClientExtensionWithIncorrectMimeType() __DIR__.'/Fixtures/test.gif', 'original.gif', 'image/jpeg', - filesize(__DIR__.'/Fixtures/test.gif'), + null, null ); @@ -96,7 +96,7 @@ public function testErrorIsOkByDefault() __DIR__.'/Fixtures/test.gif', 'original.gif', 'image/gif', - filesize(__DIR__.'/Fixtures/test.gif'), + null, null ); @@ -109,7 +109,7 @@ public function testGetClientOriginalName() __DIR__.'/Fixtures/test.gif', 'original.gif', 'image/gif', - filesize(__DIR__.'/Fixtures/test.gif'), + null, null ); @@ -122,7 +122,7 @@ public function testGetClientOriginalExtension() __DIR__.'/Fixtures/test.gif', 'original.gif', 'image/gif', - filesize(__DIR__.'/Fixtures/test.gif'), + null, null ); @@ -138,7 +138,7 @@ public function testMoveLocalFileIsNotAllowed() __DIR__.'/Fixtures/test.gif', 'original.gif', 'image/gif', - filesize(__DIR__.'/Fixtures/test.gif'), + null, UPLOAD_ERR_OK ); @@ -158,7 +158,7 @@ public function testMoveLocalFileIsAllowedInTestMode() $path, 'original.gif', 'image/gif', - filesize($path), + null, UPLOAD_ERR_OK, true ); @@ -178,7 +178,7 @@ public function testGetClientOriginalNameSanitizeFilename() __DIR__.'/Fixtures/test.gif', '../../original.gif', 'image/gif', - filesize(__DIR__.'/Fixtures/test.gif'), + null, null ); @@ -191,7 +191,7 @@ public function testGetSize() __DIR__.'/Fixtures/test.gif', 'original.gif', 'image/gif', - filesize(__DIR__.'/Fixtures/test.gif'), + null, null ); @@ -206,6 +206,23 @@ public function testGetSize() $this->assertEquals(filesize(__DIR__.'/Fixtures/test'), $file->getSize()); } + /** + * @group legacy + * @expectedDeprecation Passing a size in the constructor is deprecated since 4.1 and will be removed in 5.0. Use getSize instead. + */ + public function testConstructDeprecatedSize() + { + $file = new UploadedFile( + __DIR__.'/Fixtures/test.gif', + 'original.gif', + 'image/gif', + filesize(__DIR__.'/Fixtures/test.gif'), + null + ); + + $this->assertEquals(filesize(__DIR__.'/Fixtures/test.gif'), $file->getSize()); + } + public function testGetExtension() { $file = new UploadedFile( @@ -223,7 +240,7 @@ public function testIsValid() __DIR__.'/Fixtures/test.gif', 'original.gif', null, - filesize(__DIR__.'/Fixtures/test.gif'), + null, UPLOAD_ERR_OK, true ); @@ -240,7 +257,7 @@ public function testIsInvalidOnUploadError($error) __DIR__.'/Fixtures/test.gif', 'original.gif', null, - filesize(__DIR__.'/Fixtures/test.gif'), + null, $error ); @@ -264,7 +281,7 @@ public function testIsInvalidIfNotHttpUpload() __DIR__.'/Fixtures/test.gif', 'original.gif', null, - filesize(__DIR__.'/Fixtures/test.gif'), + null, UPLOAD_ERR_OK ); diff --git a/src/Symfony/Component/HttpFoundation/Tests/FileBagTest.php b/src/Symfony/Component/HttpFoundation/Tests/FileBagTest.php index b1bbba0d3f57c..4e859a868ca39 100644 --- a/src/Symfony/Component/HttpFoundation/Tests/FileBagTest.php +++ b/src/Symfony/Component/HttpFoundation/Tests/FileBagTest.php @@ -34,14 +34,14 @@ public function testFileMustBeAnArrayOrUploadedFile() public function testShouldConvertsUploadedFiles() { $tmpFile = $this->createTempFile(); - $file = new UploadedFile($tmpFile, basename($tmpFile), 'text/plain', 100, 0); + $file = new UploadedFile($tmpFile, basename($tmpFile), 'text/plain', null, 0); $bag = new FileBag(array('file' => array( 'name' => basename($tmpFile), 'type' => 'text/plain', 'tmp_name' => $tmpFile, 'error' => 0, - 'size' => 100, + 'size' => null, ))); $this->assertEquals($file, $bag->get('file')); @@ -89,7 +89,7 @@ public function testShouldNotRemoveEmptyUploadedFilesForAssociativeArray() public function testShouldConvertUploadedFilesWithPhpBug() { $tmpFile = $this->createTempFile(); - $file = new UploadedFile($tmpFile, basename($tmpFile), 'text/plain', 100, 0); + $file = new UploadedFile($tmpFile, basename($tmpFile), 'text/plain', null, 0); $bag = new FileBag(array( 'child' => array( @@ -106,7 +106,7 @@ public function testShouldConvertUploadedFilesWithPhpBug() 'file' => 0, ), 'size' => array( - 'file' => 100, + 'file' => null, ), ), )); @@ -118,7 +118,7 @@ public function testShouldConvertUploadedFilesWithPhpBug() public function testShouldConvertNestedUploadedFilesWithPhpBug() { $tmpFile = $this->createTempFile(); - $file = new UploadedFile($tmpFile, basename($tmpFile), 'text/plain', 100, 0); + $file = new UploadedFile($tmpFile, basename($tmpFile), 'text/plain', null, 0); $bag = new FileBag(array( 'child' => array( @@ -135,7 +135,7 @@ public function testShouldConvertNestedUploadedFilesWithPhpBug() 'sub' => array('file' => 0), ), 'size' => array( - 'sub' => array('file' => 100), + 'sub' => array('file' => null), ), ), )); @@ -147,7 +147,7 @@ public function testShouldConvertNestedUploadedFilesWithPhpBug() public function testShouldNotConvertNestedUploadedFiles() { $tmpFile = $this->createTempFile(); - $file = new UploadedFile($tmpFile, basename($tmpFile), 'text/plain', 100, 0); + $file = new UploadedFile($tmpFile, basename($tmpFile), 'text/plain', null, 0); $bag = new FileBag(array('image' => array('file' => $file))); $files = $bag->all(); diff --git a/src/Symfony/Component/HttpKernel/Client.php b/src/Symfony/Component/HttpKernel/Client.php index d682ff72fafb6..0c96163e838a9 100644 --- a/src/Symfony/Component/HttpKernel/Client.php +++ b/src/Symfony/Component/HttpKernel/Client.php @@ -168,7 +168,7 @@ protected function filterFiles(array $files) '', $value->getClientOriginalName(), $value->getClientMimeType(), - 0, + null, UPLOAD_ERR_INI_SIZE, true ); @@ -177,7 +177,7 @@ protected function filterFiles(array $files) $value->getPathname(), $value->getClientOriginalName(), $value->getClientMimeType(), - $value->getClientSize(), + null, $value->getError(), true ); diff --git a/src/Symfony/Component/HttpKernel/Tests/ClientTest.php b/src/Symfony/Component/HttpKernel/Tests/ClientTest.php index 1ac72c73595c2..b612ce63c6f43 100644 --- a/src/Symfony/Component/HttpKernel/Tests/ClientTest.php +++ b/src/Symfony/Component/HttpKernel/Tests/ClientTest.php @@ -104,8 +104,8 @@ public function testUploadedFile() $client = new Client($kernel); $files = array( - array('tmp_name' => $source, 'name' => 'original', 'type' => 'mime/original', 'size' => 123, 'error' => UPLOAD_ERR_OK), - new UploadedFile($source, 'original', 'mime/original', 123, UPLOAD_ERR_OK, true), + array('tmp_name' => $source, 'name' => 'original', 'type' => 'mime/original', 'size' => null, 'error' => UPLOAD_ERR_OK), + new UploadedFile($source, 'original', 'mime/original', null, UPLOAD_ERR_OK, true), ); $file = null; @@ -120,7 +120,6 @@ public function testUploadedFile() $this->assertEquals('original', $file->getClientOriginalName()); $this->assertEquals('mime/original', $file->getClientMimeType()); - $this->assertEquals('123', $file->getClientSize()); $this->assertTrue($file->isValid()); } @@ -154,7 +153,7 @@ public function testUploadedFileWhenSizeExceedsUploadMaxFileSize() $file = $this ->getMockBuilder('Symfony\Component\HttpFoundation\File\UploadedFile') - ->setConstructorArgs(array($source, 'original', 'mime/original', 123, UPLOAD_ERR_OK, true)) + ->setConstructorArgs(array($source, 'original', 'mime/original', null, UPLOAD_ERR_OK, true)) ->setMethods(array('getSize')) ->getMock() ; @@ -176,7 +175,7 @@ public function testUploadedFileWhenSizeExceedsUploadMaxFileSize() $this->assertEquals(UPLOAD_ERR_INI_SIZE, $file->getError()); $this->assertEquals('mime/original', $file->getClientMimeType()); $this->assertEquals('original', $file->getClientOriginalName()); - $this->assertEquals(0, $file->getClientSize()); + $this->assertEquals(0, $file->getSize()); unlink($source); } diff --git a/src/Symfony/Component/Validator/Tests/Constraints/FileValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/FileValidatorTest.php index 4cf62a6215507..c9574fbcc8a17 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/FileValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/FileValidatorTest.php @@ -411,7 +411,7 @@ public function testDisallowEmpty() */ public function testUploadedFileError($error, $message, array $params = array(), $maxSize = null) { - $file = new UploadedFile('/path/to/file', 'originalName', 'mime', 0, $error); + $file = new UploadedFile('/path/to/file', 'originalName', 'mime', null, $error); $constraint = new File(array( $message => 'myMessage',