Skip to content

Commit 655307c

Browse files
author
Greg Bowler
committed
Implement and test body get/with functions
1 parent b886e94 commit 655307c

File tree

2 files changed

+104
-26
lines changed

2 files changed

+104
-26
lines changed

src/ServerRequest.php

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
use Gt\Http\Header\RequestHeaders;
77
use Gt\Input\Input;
88
use Gt\Input\InputData\Datum\FileUpload;
9+
use Gt\Input\InputData\Datum\InputDatum;
10+
use Gt\Input\InputData\InputData;
911
use Psr\Http\Message\ServerRequestInterface;
1012
use Psr\Http\Message\UploadedFileInterface;
1113

@@ -152,30 +154,15 @@ public function getUploadedFiles():array {
152154
* immutability of the message, and MUST return an instance that has the
153155
* updated body parameters.
154156
*
155-
* @param array $uploadedFiles An array tree of UploadedFileInterface instances.
157+
* @param FileUpload[] $uploadedFiles An array tree of UploadedFileInterface instances.
156158
* @return static
157159
* @throws \InvalidArgumentException if an invalid structure is provided.
158160
*/
159161
public function withUploadedFiles(array $uploadedFiles):self {
160-
$clone = clone $this;
161-
162-
$fileUploadParameters = $clone->input->getAll(
162+
return $this->withInputData(
163+
$uploadedFiles,
163164
Input::DATA_FILES
164165
);
165-
$fileUploadParameters->remove(
166-
...$fileUploadParameters->getKeys()
167-
);
168-
169-
/** @var FileUpload[] $uploadedFiles */
170-
foreach($uploadedFiles as $key => $file) {
171-
$clone->input->add(
172-
$key,
173-
$file,
174-
Input::DATA_FILES
175-
);
176-
}
177-
178-
return $clone;
179166
}
180167

181168
/**
@@ -194,7 +181,7 @@ public function withUploadedFiles(array $uploadedFiles):self {
194181
* These will typically be an array or object.
195182
*/
196183
public function getParsedBody() {
197-
// TODO: Implement getParsedBody() method.
184+
return $this->input->getAll(Input::DATA_BODY);
198185
}
199186

200187
/**
@@ -219,14 +206,17 @@ public function getParsedBody() {
219206
* immutability of the message, and MUST return an instance that has the
220207
* updated body parameters.
221208
*
222-
* @param null|array|object $data The deserialized body data. This will
209+
* @param InputDatum[] $inputDatumArray The deserialized body data. This will
223210
* typically be in an array or object.
224211
* @return static
225212
* @throws \InvalidArgumentException if an unsupported argument type is
226213
* provided.
227214
*/
228-
public function withParsedBody($data):self {
229-
// TODO: Implement withParsedBody() method.
215+
public function withParsedBody($inputDatumArray):self {
216+
return $this->withInputData(
217+
$inputDatumArray,
218+
Input::DATA_BODY
219+
);
230220
}
231221

232222
/**
@@ -303,4 +293,29 @@ public function withoutAttribute($name):self {
303293
unset($clone->attributes[$name]);
304294
return $clone;
305295
}
296+
297+
protected function withInputData(
298+
array $inputDatumArray,
299+
string $method
300+
):self {
301+
$clone = clone $this;
302+
303+
$parameters = $clone->input->getAll(
304+
$method
305+
);
306+
$parameters->remove(
307+
...$parameters->getKeys()
308+
);
309+
310+
/** @var InputDatum[] $inputDatumArray */
311+
foreach($inputDatumArray as $key => $datum) {
312+
$clone->input->add(
313+
$key,
314+
$datum,
315+
$method
316+
);
317+
}
318+
319+
return $clone;
320+
}
306321
}

test/unit/ServerRequestTest.php

Lines changed: 67 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@
88
use Gt\Http\ServerRequest;
99
use Gt\Http\Uri;
1010
use Gt\Input\Input;
11+
use Gt\Input\InputData\BodyInputData;
1112
use Gt\Input\InputData\Datum\FileUpload;
1213
use Gt\Input\InputData\Datum\InputDatum;
1314
use Gt\Input\InputData\FileUploadInputData;
15+
use Gt\Input\InputData\InputData;
1416
use PHPUnit\Framework\MockObject\MockObject;
1517
use PHPUnit\Framework\TestCase;
1618

@@ -174,6 +176,47 @@ public function testWithUploadedFilesEmptyToFull() {
174176
self::assertCount(2, $sutFull->getUploadedFiles());
175177
}
176178

179+
public function testGetParsedBodyEmpty() {
180+
$sut = self::getServerRequest(
181+
"get",
182+
"/example"
183+
);
184+
self::assertEmpty($sut->getParsedBody());
185+
}
186+
187+
public function testGetParsedBody() {
188+
$sut = self::getServerRequest(
189+
"post",
190+
"/example",
191+
[],
192+
[],
193+
[
194+
"post" => [
195+
"key1" => "value1",
196+
"key2" => "value2",
197+
]
198+
]
199+
);
200+
201+
/** @var InputData $inputData */
202+
$inputData = $sut->getParsedBody();
203+
self::assertInstanceOf(InputData::class, $inputData);
204+
self::assertCount(2, $inputData->asArray());
205+
}
206+
207+
public function testWithParsedBody() {
208+
$sut = self::getServerRequest();
209+
$inputData = $sut->getParsedBody();
210+
self::assertEmpty($inputData->asArray());
211+
212+
$inputDatumArray = [];
213+
$inputDatumArray []= self::createMock(InputDatum::class);
214+
$inputDatumArray []= self::createMock(InputDatum::class);
215+
216+
$sut = $sut->withParsedBody($inputDatumArray);
217+
self::assertCount(2, $sut->getParsedBody()->asArray());
218+
}
219+
177220
protected function getServerRequest(
178221
string $method = null,
179222
string $uri = null,
@@ -243,9 +286,19 @@ protected function getMockInput(
243286
array $post = [],
244287
array $files = []
245288
):MockObject {
289+
$bodyArray = [];
290+
foreach($post as $key => $value) {
291+
$bodyArray []= self::createMock(InputDatum::class);
292+
}
293+
$bodyParameters = self::createMock(BodyInputData::class);
294+
$bodyParameters->method("asArray")
295+
->willReturnCallback(function()use(&$bodyArray) {
296+
return $bodyArray;
297+
});
298+
246299
$fileArray = [];
247300
foreach($files as $file) {
248-
$fileArray []= $this->createMock(FileUpload::class);
301+
$fileArray []= self::createMock(FileUpload::class);
249302
}
250303
$fileUploadParameters = self::createMock(FileUploadInputData::class);
251304
$fileUploadParameters->method("getKeys")
@@ -266,16 +319,26 @@ protected function getMockInput(
266319

267320
$mock = self::createMock(Input::class);
268321
$mock->method("getAll")
269-
->with(Input::DATA_FILES)
270-
->willReturn($fileUploadParameters);
322+
->willReturnCallback(function($method)use($bodyParameters, $fileUploadParameters) {
323+
if($method === Input::DATA_FILES) {
324+
return $fileUploadParameters;
325+
}
326+
if($method === Input::DATA_BODY) {
327+
return $bodyParameters;
328+
}
329+
});
271330
$mock->method("add")
272-
->willReturnCallback(function(string $key, InputDatum $datum, string $method)use(&$fileArray) {
331+
->willReturnCallback(function(string $key, InputDatum $datum, string $method)use(&$bodyArray, &$fileArray) {
273332
if($method === Input::DATA_FILES) {
274333
/** @var FileUpload $datum */
275334
$fileArray[$key] = [
276335
"name" => $datum->getClientFilename()
277336
];
278337
}
338+
if($method === INPUT::DATA_BODY) {
339+
/** @var InputDatum $datum */
340+
$bodyArray[$key] = $datum;
341+
}
279342
});
280343
return $mock;
281344
}

0 commit comments

Comments
 (0)