Skip to content

Commit

Permalink
Implement and test body get/with functions
Browse files Browse the repository at this point in the history
  • Loading branch information
g105b committed Jan 27, 2019
1 parent b886e94 commit 655307c
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 26 deletions.
59 changes: 37 additions & 22 deletions src/ServerRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
use Gt\Http\Header\RequestHeaders;
use Gt\Input\Input;
use Gt\Input\InputData\Datum\FileUpload;
use Gt\Input\InputData\Datum\InputDatum;
use Gt\Input\InputData\InputData;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\UploadedFileInterface;

Expand Down Expand Up @@ -152,30 +154,15 @@ public function getUploadedFiles():array {
* immutability of the message, and MUST return an instance that has the
* updated body parameters.
*
* @param array $uploadedFiles An array tree of UploadedFileInterface instances.
* @param FileUpload[] $uploadedFiles An array tree of UploadedFileInterface instances.
* @return static
* @throws \InvalidArgumentException if an invalid structure is provided.
*/
public function withUploadedFiles(array $uploadedFiles):self {
$clone = clone $this;

$fileUploadParameters = $clone->input->getAll(
return $this->withInputData(
$uploadedFiles,
Input::DATA_FILES
);
$fileUploadParameters->remove(
...$fileUploadParameters->getKeys()
);

/** @var FileUpload[] $uploadedFiles */
foreach($uploadedFiles as $key => $file) {
$clone->input->add(
$key,
$file,
Input::DATA_FILES
);
}

return $clone;
}

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

/**
Expand All @@ -219,14 +206,17 @@ public function getParsedBody() {
* immutability of the message, and MUST return an instance that has the
* updated body parameters.
*
* @param null|array|object $data The deserialized body data. This will
* @param InputDatum[] $inputDatumArray The deserialized body data. This will
* typically be in an array or object.
* @return static
* @throws \InvalidArgumentException if an unsupported argument type is
* provided.
*/
public function withParsedBody($data):self {
// TODO: Implement withParsedBody() method.
public function withParsedBody($inputDatumArray):self {
return $this->withInputData(
$inputDatumArray,
Input::DATA_BODY
);
}

/**
Expand Down Expand Up @@ -303,4 +293,29 @@ public function withoutAttribute($name):self {
unset($clone->attributes[$name]);
return $clone;
}

protected function withInputData(
array $inputDatumArray,
string $method
):self {
$clone = clone $this;

$parameters = $clone->input->getAll(
$method
);
$parameters->remove(
...$parameters->getKeys()
);

/** @var InputDatum[] $inputDatumArray */
foreach($inputDatumArray as $key => $datum) {
$clone->input->add(
$key,
$datum,
$method
);
}

return $clone;
}
}
71 changes: 67 additions & 4 deletions test/unit/ServerRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
use Gt\Http\ServerRequest;
use Gt\Http\Uri;
use Gt\Input\Input;
use Gt\Input\InputData\BodyInputData;
use Gt\Input\InputData\Datum\FileUpload;
use Gt\Input\InputData\Datum\InputDatum;
use Gt\Input\InputData\FileUploadInputData;
use Gt\Input\InputData\InputData;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

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

public function testGetParsedBodyEmpty() {
$sut = self::getServerRequest(
"get",
"/example"
);
self::assertEmpty($sut->getParsedBody());
}

public function testGetParsedBody() {
$sut = self::getServerRequest(
"post",
"/example",
[],
[],
[
"post" => [
"key1" => "value1",
"key2" => "value2",
]
]
);

/** @var InputData $inputData */
$inputData = $sut->getParsedBody();
self::assertInstanceOf(InputData::class, $inputData);
self::assertCount(2, $inputData->asArray());
}

public function testWithParsedBody() {
$sut = self::getServerRequest();
$inputData = $sut->getParsedBody();
self::assertEmpty($inputData->asArray());

$inputDatumArray = [];
$inputDatumArray []= self::createMock(InputDatum::class);
$inputDatumArray []= self::createMock(InputDatum::class);

$sut = $sut->withParsedBody($inputDatumArray);
self::assertCount(2, $sut->getParsedBody()->asArray());
}

protected function getServerRequest(
string $method = null,
string $uri = null,
Expand Down Expand Up @@ -243,9 +286,19 @@ protected function getMockInput(
array $post = [],
array $files = []
):MockObject {
$bodyArray = [];
foreach($post as $key => $value) {
$bodyArray []= self::createMock(InputDatum::class);
}
$bodyParameters = self::createMock(BodyInputData::class);
$bodyParameters->method("asArray")
->willReturnCallback(function()use(&$bodyArray) {
return $bodyArray;
});

$fileArray = [];
foreach($files as $file) {
$fileArray []= $this->createMock(FileUpload::class);
$fileArray []= self::createMock(FileUpload::class);
}
$fileUploadParameters = self::createMock(FileUploadInputData::class);
$fileUploadParameters->method("getKeys")
Expand All @@ -266,16 +319,26 @@ protected function getMockInput(

$mock = self::createMock(Input::class);
$mock->method("getAll")
->with(Input::DATA_FILES)
->willReturn($fileUploadParameters);
->willReturnCallback(function($method)use($bodyParameters, $fileUploadParameters) {
if($method === Input::DATA_FILES) {
return $fileUploadParameters;
}
if($method === Input::DATA_BODY) {
return $bodyParameters;
}
});
$mock->method("add")
->willReturnCallback(function(string $key, InputDatum $datum, string $method)use(&$fileArray) {
->willReturnCallback(function(string $key, InputDatum $datum, string $method)use(&$bodyArray, &$fileArray) {
if($method === Input::DATA_FILES) {
/** @var FileUpload $datum */
$fileArray[$key] = [
"name" => $datum->getClientFilename()
];
}
if($method === INPUT::DATA_BODY) {
/** @var InputDatum $datum */
$bodyArray[$key] = $datum;
}
});
return $mock;
}
Expand Down

0 comments on commit 655307c

Please sign in to comment.