From 40b3c9e0e09e676c8f30deba61102ca76b27e19f Mon Sep 17 00:00:00 2001 From: Cees-Jan Kiewiet Date: Wed, 31 May 2017 17:44:21 +0200 Subject: [PATCH 1/4] UploadedFile implementing UploadedFileInterface --- src/UploadedFile.php | 119 +++++++++++++++++++++++++++++++++++++ tests/UploadedFileTest.php | 61 +++++++++++++++++++ 2 files changed, 180 insertions(+) create mode 100644 src/UploadedFile.php create mode 100644 tests/UploadedFileTest.php diff --git a/src/UploadedFile.php b/src/UploadedFile.php new file mode 100644 index 00000000..49248fe2 --- /dev/null +++ b/src/UploadedFile.php @@ -0,0 +1,119 @@ +stream = $stream; + $this->size = $size; + + if (!is_int($error) || !in_array($error, [ + UPLOAD_ERR_OK, + UPLOAD_ERR_INI_SIZE, + UPLOAD_ERR_FORM_SIZE, + UPLOAD_ERR_PARTIAL, + UPLOAD_ERR_NO_FILE, + UPLOAD_ERR_NO_TMP_DIR, + UPLOAD_ERR_CANT_WRITE, + UPLOAD_ERR_EXTENSION, + ])) { + throw new InvalidArgumentException( + 'Invalid error code, must be an UPLOAD_ERR_* constant' + ); + } + $this->error = $error; + $this->filename = $filename; + $this->mediaType = $mediaType; + } + + /** + * {@inheritdoc} + */ + public function getStream() + { + if ($this->error !== UPLOAD_ERR_OK) { + throw new RuntimeException('Cannot retrieve stream due to upload error'); + } + + return $this->stream; + } + + /** + * {@inheritdoc} + */ + public function moveTo($targetPath) + { + throw new RuntimeException('Not implemented'); + } + + /** + * {@inheritdoc} + */ + public function getSize() + { + return $this->size; + } + + /** + * {@inheritdoc} + */ + public function getError() + { + return $this->error; + } + + /** + * {@inheritdoc} + */ + public function getClientFilename() + { + return $this->filename; + } + + /** + * {@inheritdoc} + */ + public function getClientMediaType() + { + return $this->mediaType; + } +} diff --git a/tests/UploadedFileTest.php b/tests/UploadedFileTest.php new file mode 100644 index 00000000..0b16e25d --- /dev/null +++ b/tests/UploadedFileTest.php @@ -0,0 +1,61 @@ +moveTo('bar.foo'); + } + + public function testGetters() + { + $stream = new BufferStream(); + $uploadedFile = new UploadedFile($stream, 0, UPLOAD_ERR_OK, 'foo.bar', 'foo/bar'); + self::assertSame($stream, $uploadedFile->getStream()); + self::assertSame(0, $uploadedFile->getSize()); + self::assertSame(UPLOAD_ERR_OK, $uploadedFile->getError()); + self::assertSame('foo.bar', $uploadedFile->getClientFilename()); + self::assertSame('foo/bar', $uploadedFile->getClientMediaType()); + } + + public function testGetStreamOnFailedUpload() + { + self::expectException('\RuntimeException'); + self::expectExceptionMessage('Cannot retrieve stream due to upload error'); + $stream = new BufferStream(); + $uploadedFile = new UploadedFile($stream, 0, UPLOAD_ERR_NO_FILE, 'foo.bar', 'foo/bar'); + $uploadedFile->getStream(); + } +} From 056eea404a76b7f2100a47ff679f81f5f4bd703c Mon Sep 17 00:00:00 2001 From: Cees-Jan Kiewiet Date: Wed, 31 May 2017 19:36:02 +0200 Subject: [PATCH 2/4] Use annotations instead of expect exception methods --- tests/UploadedFileTest.php | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/tests/UploadedFileTest.php b/tests/UploadedFileTest.php index 0b16e25d..abd58a35 100644 --- a/tests/UploadedFileTest.php +++ b/tests/UploadedFileTest.php @@ -21,19 +21,21 @@ public function failtyErrorProvider() /** * @dataProvider failtyErrorProvider + * @expectedException \InvalidArgumentException + * @expectedExceptionMessage Invalid error code, must be an UPLOAD_ERR_* constant */ public function testFailtyError($error) { - self::expectException('\InvalidArgumentException'); - self::expectExceptionMessage('Invalid error code, must be an UPLOAD_ERR_* constant'); $stream = new BufferStream(); new UploadedFile($stream, 0, $error, 'foo.bar', 'foo/bar'); } + /** + * @expectedException \RuntimeException + * @expectedExceptionMessage Not implemented + */ public function testNoMoveFile() { - self::expectException('\RuntimeException'); - self::expectExceptionMessage('Not implemented'); $stream = new BufferStream(); $uploadedFile = new UploadedFile($stream, 0, UPLOAD_ERR_OK, 'foo.bar', 'foo/bar'); $uploadedFile->moveTo('bar.foo'); @@ -50,10 +52,12 @@ public function testGetters() self::assertSame('foo/bar', $uploadedFile->getClientMediaType()); } + /** + * @expectedException \RuntimeException + * @expectedExceptionMessage Cannot retrieve stream due to upload error + */ public function testGetStreamOnFailedUpload() { - self::expectException('\RuntimeException'); - self::expectExceptionMessage('Cannot retrieve stream due to upload error'); $stream = new BufferStream(); $uploadedFile = new UploadedFile($stream, 0, UPLOAD_ERR_NO_FILE, 'foo.bar', 'foo/bar'); $uploadedFile->getStream(); From 88450cf038bc8b947256de10408959f04498a8d8 Mon Sep 17 00:00:00 2001 From: Cees-Jan Kiewiet Date: Wed, 31 May 2017 19:40:14 +0200 Subject: [PATCH 3/4] PHP 5.3 style arrays --- src/UploadedFile.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/UploadedFile.php b/src/UploadedFile.php index 49248fe2..f7c7223d 100644 --- a/src/UploadedFile.php +++ b/src/UploadedFile.php @@ -46,7 +46,7 @@ public function __construct(StreamInterface $stream, $size, $error, $filename, $ $this->stream = $stream; $this->size = $size; - if (!is_int($error) || !in_array($error, [ + if (!is_int($error) || !in_array($error, array( UPLOAD_ERR_OK, UPLOAD_ERR_INI_SIZE, UPLOAD_ERR_FORM_SIZE, @@ -55,7 +55,7 @@ public function __construct(StreamInterface $stream, $size, $error, $filename, $ UPLOAD_ERR_NO_TMP_DIR, UPLOAD_ERR_CANT_WRITE, UPLOAD_ERR_EXTENSION, - ])) { + ))) { throw new InvalidArgumentException( 'Invalid error code, must be an UPLOAD_ERR_* constant' ); From 7cbb5264cc5bb9301a83eea49a9eb1a8a33b9ae2 Mon Sep 17 00:00:00 2001 From: Cees-Jan Kiewiet Date: Thu, 8 Jun 2017 10:14:39 +0200 Subject: [PATCH 4/4] Marked UploadedFile internal --- src/UploadedFile.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/UploadedFile.php b/src/UploadedFile.php index f7c7223d..b1794a27 100644 --- a/src/UploadedFile.php +++ b/src/UploadedFile.php @@ -7,6 +7,9 @@ use Psr\Http\Message\UploadedFileInterface; use RuntimeException; +/** + * @internal + */ final class UploadedFile implements UploadedFileInterface { /**