From 7b8c6437d267489c10a0c6f185a15bfcfffeb59a Mon Sep 17 00:00:00 2001 From: Sasha Alex Romanenko Date: Tue, 18 Dec 2018 19:55:53 -0500 Subject: [PATCH 1/5] Inconsistent parameter naming and instance type caused misunderstandings. --- docs/book/file.md | 4 ++-- src/File/RenameUpload.php | 8 ++++---- test/File/RenameUploadTest.php | 4 ++++ 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/docs/book/file.md b/docs/book/file.md index aa160a3f..7b7fbf2d 100644 --- a/docs/book/file.md +++ b/docs/book/file.md @@ -241,7 +241,7 @@ The following set of options are supported: Required when passing a [PSR-7 UploadedFileInterface](https://www.php-fig.org/psr/psr-7/#36-psrhttpmessageuploadedfileinterface) to the filter; used to create a new stream representing the renamed file. (Since 2.9.0) -- `upload_file_factory` (`Psr\Http\Message\UploadedFileFactoryInterface`; default: +- `uploaded_file_factory` (`Psr\Http\Message\UploadedFileFactoryInterface`; default: `null`): Required when passing a [PSR-7 UploadedFileInterface](https://www.php-fig.org/psr/psr-7/#36-psrhttpmessageuploadedfileinterface) to the filter; used to create a new uploaded file representation of the renamed file. (Since 2.9.0) @@ -328,7 +328,7 @@ $filter = new \Zend\Filter\File\RenameUpload([ // @var StreamFactoryInterface $streamFactory 'stream_factory' => $streamFactory, // @var UploadedFileFactoryInterface $uploadedFileFactory - 'upload_file_factory' => $uploadedFileFactory, + 'uploaded_file_factory' => $uploadedFileFactory, ]); // @var ServerRequestInterface $request diff --git a/src/File/RenameUpload.php b/src/File/RenameUpload.php index 6ed19cc4..6c96ef6e 100644 --- a/src/File/RenameUpload.php +++ b/src/File/RenameUpload.php @@ -26,7 +26,7 @@ class RenameUpload extends AbstractFilter 'overwrite' => false, 'randomize' => false, 'stream_factory' => null, - 'upload_file_factory' => null, + 'uploaded_file_factory' => null, ]; /** @@ -101,7 +101,7 @@ public function getTarget() */ public function setUploadFileFactory(UploadedFileFactoryInterface $factory) { - $this->options['upload_file_factory'] = $factory; + $this->options['uploaded_file_factory'] = $factory; return $this; } @@ -110,7 +110,7 @@ public function setUploadFileFactory(UploadedFileFactoryInterface $factory) */ public function getUploadFileFactory() { - return $this->options['upload_file_factory']; + return $this->options['uploaded_file_factory']; } /** @@ -424,7 +424,7 @@ private function filterPsr7UploadedFile(UploadedFileInterface $uploadedFile) $uploadedFileFactory = $this->getUploadFileFactory(); if (! $uploadedFileFactory) { throw new Exception\RuntimeException(sprintf( - 'No PSR-17 %s present; cannot filter file. Please pass the upload_file_factory' + 'No PSR-17 %s present; cannot filter file. Please pass the uploaded_file_factory' . ' option with a %s instance when creating the filter for use with PSR-7.', UploadedFileFactoryInterface::class, UploadedFileFactoryInterface::class diff --git a/test/File/RenameUploadTest.php b/test/File/RenameUploadTest.php index a4eb5e13..1d5e00d6 100644 --- a/test/File/RenameUploadTest.php +++ b/test/File/RenameUploadTest.php @@ -133,11 +133,15 @@ public function testOptions() 'use_upload_name' => true, 'overwrite' => true, 'randomize' => true, + 'stream_factory' => $this->prophesize(StreamFactoryInterface::class)->reveal(), + 'uploaded_file_factory' => $this->prophesize(UploadedFileFactoryInterface::class)->reveal() ]); $this->assertEquals($this->sourceFile, $filter->getTarget()); $this->assertTrue($filter->getUseUploadName()); $this->assertTrue($filter->getOverwrite()); $this->assertTrue($filter->getRandomize()); + $this->assertInstanceOf(StreamFactoryInterface::class, $filter->getStreamFactory()); + $this->assertInstanceOf(UploadedFileFactoryInterface::class, $filter->getUploadFileFactory()); } /** From 13394fabc3ed6ee6d341b333526c6c02459c0306 Mon Sep 17 00:00:00 2001 From: Sasha Alex Romanenko Date: Tue, 18 Dec 2018 19:56:04 -0500 Subject: [PATCH 2/5] Inconsistent parameter naming and instance type caused misunderstandings. --- docs/book/file.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/docs/book/file.md b/docs/book/file.md index 7b7fbf2d..b907c262 100644 --- a/docs/book/file.md +++ b/docs/book/file.md @@ -356,6 +356,18 @@ foreach ($request->getUploadedFiles() as $uploadedFile) { > provides a PSR-17 implementation, but requires PHP 7.1. If you are still on > PHP 7.0, either upgrade, or find a compatible psr/http-factory-implementation. +If already using `zend-diactoros` that comes with expressive, instances for `$streamFactory` and `$uploadedFileFactory` +can be: +```php +use Zend\Diactoros\StreamFactory; +use Zend\Diactoros\UploadedFileFactory; + +[ + 'stream_factory' => new StreamFactory(), + 'upload_file_factory' => new UploadedFileFactory(), +] +``` + ## Uppercase `Zend\Filter\File\Uppercase` can be used to convert all file contents to From 22eb616a362b628c80d4420b55a3769cdc70d361 Mon Sep 17 00:00:00 2001 From: Sasha Alex Romanenko Date: Wed, 19 Dec 2018 09:32:25 -0500 Subject: [PATCH 3/5] - Getter/Setter for new option name was not renamed. Fixed. - Only test this one new property. - Make documentation example syntax complete. --- docs/book/file.md | 13 +++++++++---- src/File/RenameUpload.php | 4 ++-- test/File/RenameUploadTest.php | 1 - 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/docs/book/file.md b/docs/book/file.md index b907c262..25b627d9 100644 --- a/docs/book/file.md +++ b/docs/book/file.md @@ -359,13 +359,18 @@ foreach ($request->getUploadedFiles() as $uploadedFile) { If already using `zend-diactoros` that comes with expressive, instances for `$streamFactory` and `$uploadedFileFactory` can be: ```php +use Psr\Http\Message\ServerRequestInterface; +use Psr\Http\Message\StreamFactoryInterface; use Zend\Diactoros\StreamFactory; use Zend\Diactoros\UploadedFileFactory; +use Zend\Filter\File\RenameUpload; -[ - 'stream_factory' => new StreamFactory(), - 'upload_file_factory' => new UploadedFileFactory(), -] +$filter = new \Zend\Filter\File\RenameUpload([ + 'target' => './data/uploads/', + 'randomize' => true, + 'stream_factory' => new StreamFactory(), + 'uploaded_file_factory' => new UploadedFileFactory(), +]); ``` ## Uppercase diff --git a/src/File/RenameUpload.php b/src/File/RenameUpload.php index 6c96ef6e..3d69db85 100644 --- a/src/File/RenameUpload.php +++ b/src/File/RenameUpload.php @@ -99,7 +99,7 @@ public function getTarget() * filtered PSR-7 UploadedFileInterface instances. * @return self */ - public function setUploadFileFactory(UploadedFileFactoryInterface $factory) + public function setUploadedFileFactory(UploadedFileFactoryInterface $factory) { $this->options['uploaded_file_factory'] = $factory; return $this; @@ -108,7 +108,7 @@ public function setUploadFileFactory(UploadedFileFactoryInterface $factory) /** * @return null|UploadedFileFactoryInterface */ - public function getUploadFileFactory() + public function getUploadedFileFactory() { return $this->options['uploaded_file_factory']; } diff --git a/test/File/RenameUploadTest.php b/test/File/RenameUploadTest.php index 1d5e00d6..020ad074 100644 --- a/test/File/RenameUploadTest.php +++ b/test/File/RenameUploadTest.php @@ -133,7 +133,6 @@ public function testOptions() 'use_upload_name' => true, 'overwrite' => true, 'randomize' => true, - 'stream_factory' => $this->prophesize(StreamFactoryInterface::class)->reveal(), 'uploaded_file_factory' => $this->prophesize(UploadedFileFactoryInterface::class)->reveal() ]); $this->assertEquals($this->sourceFile, $filter->getTarget()); From d4ba8051f909111ecfc337732c287978c43e9c66 Mon Sep 17 00:00:00 2001 From: Sasha Alex Romanenko Date: Wed, 19 Dec 2018 09:48:30 -0500 Subject: [PATCH 4/5] Catch more instances of upload_factory vs uploaded_factory --- src/File/RenameUpload.php | 2 +- test/File/RenameUploadTest.php | 5 ++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/File/RenameUpload.php b/src/File/RenameUpload.php index 3d69db85..db2fee18 100644 --- a/src/File/RenameUpload.php +++ b/src/File/RenameUpload.php @@ -421,7 +421,7 @@ private function filterPsr7UploadedFile(UploadedFileInterface $uploadedFile) $stream = $streamFactory->createStreamFromFile($targetFile); - $uploadedFileFactory = $this->getUploadFileFactory(); + $uploadedFileFactory = $this->getUploadedFileFactory(); if (! $uploadedFileFactory) { throw new Exception\RuntimeException(sprintf( 'No PSR-17 %s present; cannot filter file. Please pass the uploaded_file_factory' diff --git a/test/File/RenameUploadTest.php b/test/File/RenameUploadTest.php index 020ad074..7d9079d9 100644 --- a/test/File/RenameUploadTest.php +++ b/test/File/RenameUploadTest.php @@ -139,8 +139,7 @@ public function testOptions() $this->assertTrue($filter->getUseUploadName()); $this->assertTrue($filter->getOverwrite()); $this->assertTrue($filter->getRandomize()); - $this->assertInstanceOf(StreamFactoryInterface::class, $filter->getStreamFactory()); - $this->assertInstanceOf(UploadedFileFactoryInterface::class, $filter->getUploadFileFactory()); + $this->assertInstanceOf(UploadedFileFactoryInterface::class, $filter->getUploadedFileFactory()); } /** @@ -221,7 +220,7 @@ public function testStringConstructorWithPsrFile() $this->assertEquals($targetFile, $filter->getTarget()); $filter->setStreamFactory($streamFactory->reveal()); - $filter->setUploadFileFactory($fileFactory->reveal()); + $filter->setUploadedFileFactory($fileFactory->reveal()); $moved = $filter($originalFile->reveal()); From e6f1f5527992ddf36dd972300074dfe57b0cec36 Mon Sep 17 00:00:00 2001 From: Sasha Alex Romanenko Date: Wed, 19 Dec 2018 10:13:57 -0500 Subject: [PATCH 5/5] Prophecy tests do not work with PHP5 --- test/File/RenameUploadTest.php | 1 + 1 file changed, 1 insertion(+) diff --git a/test/File/RenameUploadTest.php b/test/File/RenameUploadTest.php index 7d9079d9..69c061d2 100644 --- a/test/File/RenameUploadTest.php +++ b/test/File/RenameUploadTest.php @@ -118,6 +118,7 @@ public function testThrowsExceptionWithNonUploadedFile() } /** + * * @requires PHP 7 * @return void */ public function testOptions()