From b886e94bae0f03ea7ac87ca2f5f87c5423f9633d Mon Sep 17 00:00:00 2001 From: Greg Bowler Date: Sun, 27 Jan 2019 10:28:00 +0000 Subject: [PATCH] Testing withUploadedFiles is immutable --- test/unit/ServerRequestTest.php | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/test/unit/ServerRequestTest.php b/test/unit/ServerRequestTest.php index 45efd95..d005e1d 100644 --- a/test/unit/ServerRequestTest.php +++ b/test/unit/ServerRequestTest.php @@ -9,6 +9,7 @@ use Gt\Http\Uri; use Gt\Input\Input; use Gt\Input\InputData\Datum\FileUpload; +use Gt\Input\InputData\Datum\InputDatum; use Gt\Input\InputData\FileUploadInputData; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; @@ -151,6 +152,28 @@ public function testWithUploadedFilesFullToEmpty() { self::assertEmpty($sutEmpty->getUploadedFiles()); } + public function testWithUploadedFilesEmptyToFull() { + $sutEmpty = self::getServerRequest( + "post", + "/example", + [], + [], + [ + "files" => [], + ] + ); + self::assertEmpty($sutEmpty->getUploadedFiles()); + + $file1 = self::createMock(FileUpload::class); + $file2 = self::createMock(FileUpload::class); + + $sutFull = $sutEmpty->withUploadedFiles([ + $file1, + $file2, + ]); + self::assertCount(2, $sutFull->getUploadedFiles()); + } + protected function getServerRequest( string $method = null, string $uri = null, @@ -245,6 +268,15 @@ protected function getMockInput( $mock->method("getAll") ->with(Input::DATA_FILES) ->willReturn($fileUploadParameters); + $mock->method("add") + ->willReturnCallback(function(string $key, InputDatum $datum, string $method)use(&$fileArray) { + if($method === Input::DATA_FILES) { + /** @var FileUpload $datum */ + $fileArray[$key] = [ + "name" => $datum->getClientFilename() + ]; + } + }); return $mock; }