From 43c050ec2e6295b933e629b7dbc638200e99228b Mon Sep 17 00:00:00 2001 From: PJ Dietz Date: Sun, 26 Apr 2015 15:32:33 -0400 Subject: [PATCH] Implement ServerRequest::withUploadedFiles --- src/Message/ServerRequest.php | 47 ++++++++++- test/tests/unit/Message/ServerRequestTest.php | 79 +++++++++++++++++++ 2 files changed, 124 insertions(+), 2 deletions(-) diff --git a/src/Message/ServerRequest.php b/src/Message/ServerRequest.php index 84b3855..28de84c 100644 --- a/src/Message/ServerRequest.php +++ b/src/Message/ServerRequest.php @@ -2,7 +2,6 @@ namespace WellRESTed\Message; -use Psr\Http\Message\An; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Message\StreamInterface; @@ -167,7 +166,14 @@ class ServerRequest extends Request implements ServerRequestInterface */ public function withUploadedFiles(array $uploadedFiles) { - // TODO: Implement withUploadedFiles() method. + if (!$this->isValidUploadedFilesTree($uploadedFiles)) { + throw new \InvalidArgumentException( + "withUploadedFiles expects an array with string keys and UploadedFileInterface[] values"); + } + + $request = clone $this; + $request->uploadedFiles = $uploadedFiles; + return $request; } /** @@ -409,4 +415,41 @@ class ServerRequest extends Request implements ServerRequestInterface return $headers; } + private function isValidUploadedFilesTree(array $uploadedFiles) + { + // Ensure all keys are strings. + $keys = array_keys($uploadedFiles); + if (count($keys) !== count(array_filter($keys, "is_string"))) { + return false; + } + + // All values must be UploadedFileInterface[]. + + // Ensure all values are arrays. + $values = array_values($uploadedFiles); + if (count($values) !== count(array_filter($values, "is_array"))) { + return false; + } + + $isUploadedFileInterface = function ($object) { + return is_object($object) && in_array('Psr\Http\Message\UploadedFileInterface', class_implements($object)); + }; + + foreach ($values as $items) { + + // Ensure values are list arrays. + if (array_keys($items) !== range(0, count($items) - 1)) { + return false; + } + + // Ensure all items are UploadedFileInterfaces + $itemValues = array_values($items); + if (count($itemValues) !== count(array_filter($itemValues, $isUploadedFileInterface))) { + return false; + } + } + + return true; + } + } diff --git a/test/tests/unit/Message/ServerRequestTest.php b/test/tests/unit/Message/ServerRequestTest.php index c642a59..d69f971 100644 --- a/test/tests/unit/Message/ServerRequestTest.php +++ b/test/tests/unit/Message/ServerRequestTest.php @@ -301,6 +301,85 @@ class ServerRequestTest extends \PHPUnit_Framework_TestCase ]; } + /** + * @covers WellRESTed\Message\ServerRequest::withUploadedFiles + * @covers WellRESTed\Message\ServerRequest::isValidUploadedFilesTree + */ + public function testWithUploadedFilesCreatesNewInstance() + { + $uploadedFiles = [ + "file" => [new UploadedFile("index.html", "text/html", 524, "/tmp/php9hNlHe", 0)] + ]; + $request = new ServerRequest(); + $request1 = $request->withUploadedFiles([]); + $request2 = $request1->withUploadedFiles($uploadedFiles); + $this->assertNotSame($request2, $request1); + } + + /** + * @covers WellRESTed\Message\ServerRequest::withUploadedFiles + * @covers WellRESTed\Message\ServerRequest::isValidUploadedFilesTree + */ + public function testWithUploadedFilesReturnsPassedUploadedFiles() + { + $uploadedFiles = [ + "file" => [new UploadedFile("index.html", "text/html", 524, "/tmp/php9hNlHe", 0)] + ]; + $request = new ServerRequest(); + $request = $request->withUploadedFiles($uploadedFiles); + $this->assertSame($uploadedFiles, $request->getUploadedFiles()); + } + + /** + * @covers WellRESTed\Message\ServerRequest::withUploadedFiles + * @covers WellRESTed\Message\ServerRequest::isValidUploadedFilesTree + * @expectedException \InvalidArgumentException + * @dataProvider invalidUploadedFilesProvider + */ + public function testWithUploadedFilesThrowsExceptionWithInvalidTree($uploadedFiles) + { + $request = new ServerRequest(); + $request->withUploadedFiles($uploadedFiles); + } + + public function invalidUploadedFilesProvider() + { + return [ + // All keys must be strings + [[new UploadedFile("index.html", "text/html", 524, "/tmp/php9hNlHe", 0)]], + + // All values must be arrays. + [["file" => new UploadedFile("index.html", "text/html", 524, "/tmp/php9hNlHe", 0)]], + + // All values must be list arrays. + [ + [ + "file" => + [ + "file1" => new UploadedFile("index.html", "text/html", 524, "/tmp/php9hNlHe", 0) + ] + ] + ], + [ + [ + "file" => [ + 0 => new UploadedFile("index.html", "text/html", 524, "/tmp/php9hNlHe", 0), + 2 => new UploadedFile("index.html", "text/html", 524, "/tmp/php9hNlHe", 0) + ] + ] + ], + [ + [ + "file" => [ + new UploadedFile("index.html", "text/html", 524, "/tmp/php9hNlHe", 0), + new UploadedFile("index.html", "text/html", 524, "/tmp/php9hNlHe", 0), + "index.html" + ] + ] + ] + ]; + } + // ------------------------------------------------------------------------ // Parsed Body