Skip to content

Commit b886e94

Browse files
author
Greg Bowler
committed
Testing withUploadedFiles is immutable
1 parent b313273 commit b886e94

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

test/unit/ServerRequestTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Gt\Http\Uri;
1010
use Gt\Input\Input;
1111
use Gt\Input\InputData\Datum\FileUpload;
12+
use Gt\Input\InputData\Datum\InputDatum;
1213
use Gt\Input\InputData\FileUploadInputData;
1314
use PHPUnit\Framework\MockObject\MockObject;
1415
use PHPUnit\Framework\TestCase;
@@ -151,6 +152,28 @@ public function testWithUploadedFilesFullToEmpty() {
151152
self::assertEmpty($sutEmpty->getUploadedFiles());
152153
}
153154

155+
public function testWithUploadedFilesEmptyToFull() {
156+
$sutEmpty = self::getServerRequest(
157+
"post",
158+
"/example",
159+
[],
160+
[],
161+
[
162+
"files" => [],
163+
]
164+
);
165+
self::assertEmpty($sutEmpty->getUploadedFiles());
166+
167+
$file1 = self::createMock(FileUpload::class);
168+
$file2 = self::createMock(FileUpload::class);
169+
170+
$sutFull = $sutEmpty->withUploadedFiles([
171+
$file1,
172+
$file2,
173+
]);
174+
self::assertCount(2, $sutFull->getUploadedFiles());
175+
}
176+
154177
protected function getServerRequest(
155178
string $method = null,
156179
string $uri = null,
@@ -245,6 +268,15 @@ protected function getMockInput(
245268
$mock->method("getAll")
246269
->with(Input::DATA_FILES)
247270
->willReturn($fileUploadParameters);
271+
$mock->method("add")
272+
->willReturnCallback(function(string $key, InputDatum $datum, string $method)use(&$fileArray) {
273+
if($method === Input::DATA_FILES) {
274+
/** @var FileUpload $datum */
275+
$fileArray[$key] = [
276+
"name" => $datum->getClientFilename()
277+
];
278+
}
279+
});
248280
return $mock;
249281
}
250282

0 commit comments

Comments
 (0)