From 62adac7c76803adcbbf9ce3eeb78e416c2d6ad0d Mon Sep 17 00:00:00 2001 From: Adrian Suter Date: Tue, 30 Apr 2019 21:46:58 +0200 Subject: [PATCH 1/4] Create PhpFactoryFunctionOverrides.php --- tests/Assets/PhpFactoryFunctionOverrides.php | 1 + 1 file changed, 1 insertion(+) create mode 100644 tests/Assets/PhpFactoryFunctionOverrides.php diff --git a/tests/Assets/PhpFactoryFunctionOverrides.php b/tests/Assets/PhpFactoryFunctionOverrides.php new file mode 100644 index 0000000..b3d9bbc --- /dev/null +++ b/tests/Assets/PhpFactoryFunctionOverrides.php @@ -0,0 +1 @@ + Date: Tue, 30 Apr 2019 22:42:45 +0200 Subject: [PATCH 2/4] Test case for non-readable or invalid uri metadata --- tests/Assets/PhpFactoryFunctionOverrides.php | 25 ++++++++++++ tests/Factory/UploadedFileFactoryTest.php | 40 ++++++++++++++++++++ tests/bootstrap.php | 1 + 3 files changed, 66 insertions(+) diff --git a/tests/Assets/PhpFactoryFunctionOverrides.php b/tests/Assets/PhpFactoryFunctionOverrides.php index b3d9bbc..990fc8b 100644 --- a/tests/Assets/PhpFactoryFunctionOverrides.php +++ b/tests/Assets/PhpFactoryFunctionOverrides.php @@ -1 +1,26 @@ createStreamFromResource($resource); } + + /** + * Prophesize a `\Psr\Http\Message\StreamInterface` with a `getMetadata` method prophecy. + * + * @param string $argKey Argument for the method prophecy. + * @param mixed $returnValue Return value of the `getMetadata` method. + * + * @return \Psr\Http\Message\StreamInterface + */ + protected function prophesizeStreamInterfaceWithGetMetadataMethod(string $argKey, $returnValue): StreamInterface + { + $prophecy = $this->prophesize(StreamInterface::class); + $mp = new MethodProphecy($prophecy, 'getMetadata', [new ExactValueToken($argKey)]); + $mp->shouldBeCalled(); + $mp->willReturn($returnValue); + + /** @var StreamInterface $upload */ + $upload = $prophecy->reveal(); + return $upload; + } + + /** + * @expectedException \InvalidArgumentException + * @expectedExceptionMessage File is not readable. + */ + public function testCreateUploadedFileWithInvalidUri() + { + $this->factory->createUploadedFile($this->prophesizeStreamInterfaceWithGetMetadataMethod('uri', null)); + } + + /** + * @expectedException \InvalidArgumentException + * @expectedExceptionMessage File is not readable. + */ + public function testCreateUploadedFileWithNonReadableFile() + { + $this->factory->createUploadedFile($this->prophesizeStreamInterfaceWithGetMetadataMethod('uri', 'non-readable')); + } } diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 6de35e5..b0970a1 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -11,3 +11,4 @@ require __DIR__ . '/Assets/PhpGetAllHeaders.php'; require __DIR__ . '/Assets/PhpFunctionOverrides.php'; +require __DIR__ . '/Assets/PhpFactoryFunctionOverrides.php'; From 72cc91ee89eaccb5e9e82d2c276a24dd3f095a3e Mon Sep 17 00:00:00 2001 From: Adrian Suter Date: Tue, 30 Apr 2019 22:55:08 +0200 Subject: [PATCH 3/4] Reformat because of line lengths --- tests/Assets/PhpFactoryFunctionOverrides.php | 3 ++- tests/Factory/UploadedFileFactoryTest.php | 8 ++++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/tests/Assets/PhpFactoryFunctionOverrides.php b/tests/Assets/PhpFactoryFunctionOverrides.php index 990fc8b..5642e25 100644 --- a/tests/Assets/PhpFactoryFunctionOverrides.php +++ b/tests/Assets/PhpFactoryFunctionOverrides.php @@ -10,7 +10,8 @@ namespace Slim\Psr7\Factory; /** - * Return `false` if the parameter is "non-readable", otherwise the function would return the value from the php built-in function. + * Return `false` if the parameter is "non-readable", otherwise the function would return the value from + * the php built-in function. * * @param string $filename * diff --git a/tests/Factory/UploadedFileFactoryTest.php b/tests/Factory/UploadedFileFactoryTest.php index 6527a31..77268d8 100644 --- a/tests/Factory/UploadedFileFactoryTest.php +++ b/tests/Factory/UploadedFileFactoryTest.php @@ -65,7 +65,9 @@ protected function prophesizeStreamInterfaceWithGetMetadataMethod(string $argKey */ public function testCreateUploadedFileWithInvalidUri() { - $this->factory->createUploadedFile($this->prophesizeStreamInterfaceWithGetMetadataMethod('uri', null)); + $this->factory->createUploadedFile( + $this->prophesizeStreamInterfaceWithGetMetadataMethod('uri', null) + ); } /** @@ -74,6 +76,8 @@ public function testCreateUploadedFileWithInvalidUri() */ public function testCreateUploadedFileWithNonReadableFile() { - $this->factory->createUploadedFile($this->prophesizeStreamInterfaceWithGetMetadataMethod('uri', 'non-readable')); + $this->factory->createUploadedFile( + $this->prophesizeStreamInterfaceWithGetMetadataMethod('uri', 'non-readable') + ); } } From 10dc4e4ef23f80e50f0ecbdf20382be25f437c3b Mon Sep 17 00:00:00 2001 From: Adrian Suter Date: Tue, 30 Apr 2019 22:58:35 +0200 Subject: [PATCH 4/4] Fix code style --- tests/Factory/UploadedFileFactoryTest.php | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/tests/Factory/UploadedFileFactoryTest.php b/tests/Factory/UploadedFileFactoryTest.php index 77268d8..90a8bd6 100644 --- a/tests/Factory/UploadedFileFactoryTest.php +++ b/tests/Factory/UploadedFileFactoryTest.php @@ -10,6 +10,7 @@ namespace Slim\Tests\Psr7\Factory; use Interop\Http\Factory\UploadedFileFactoryTestCase; +use InvalidArgumentException; use Prophecy\Argument\Token\ExactValueToken; use Prophecy\Prophecy\MethodProphecy; use Psr\Http\Message\StreamInterface; @@ -42,15 +43,15 @@ protected function createStream($content) /** * Prophesize a `\Psr\Http\Message\StreamInterface` with a `getMetadata` method prophecy. * - * @param string $argKey Argument for the method prophecy. - * @param mixed $returnValue Return value of the `getMetadata` method. + * @param string $argKey Argument for the method prophecy. + * @param mixed $returnValue Return value of the `getMetadata` method. * - * @return \Psr\Http\Message\StreamInterface + * @return StreamInterface */ protected function prophesizeStreamInterfaceWithGetMetadataMethod(string $argKey, $returnValue): StreamInterface { $prophecy = $this->prophesize(StreamInterface::class); - $mp = new MethodProphecy($prophecy, 'getMetadata', [new ExactValueToken($argKey)]); + $mp = new MethodProphecy($prophecy, 'getMetadata', [new ExactValueToken($argKey)]); $mp->shouldBeCalled(); $mp->willReturn($returnValue); @@ -60,24 +61,24 @@ protected function prophesizeStreamInterfaceWithGetMetadataMethod(string $argKey } /** - * @expectedException \InvalidArgumentException + * @expectedException InvalidArgumentException * @expectedExceptionMessage File is not readable. */ public function testCreateUploadedFileWithInvalidUri() { $this->factory->createUploadedFile( - $this->prophesizeStreamInterfaceWithGetMetadataMethod('uri', null) + $this->prophesizeStreamInterfaceWithGetMetadataMethod('uri', null) ); } /** - * @expectedException \InvalidArgumentException + * @expectedException InvalidArgumentException * @expectedExceptionMessage File is not readable. */ public function testCreateUploadedFileWithNonReadableFile() { $this->factory->createUploadedFile( - $this->prophesizeStreamInterfaceWithGetMetadataMethod('uri', 'non-readable') + $this->prophesizeStreamInterfaceWithGetMetadataMethod('uri', 'non-readable') ); } }