Skip to content

Commit ae4cb8e

Browse files
committed
Write phpunit tests for handlePost
1 parent 748aff3 commit ae4cb8e

File tree

8 files changed

+594
-21
lines changed

8 files changed

+594
-21
lines changed

src/Controller/Controller.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,6 @@ function ($d) {
217217
$model
218218
);
219219

220-
221220
if ($parsed !== null) {
222221
//overwrite
223222
if ($overwrite
@@ -238,6 +237,12 @@ function ($d) {
238237
return $directives;
239238
}
240239

240+
/**
241+
* @param $object
242+
* @param ISource|null $source
243+
* @param string[] ...$properties
244+
* @throws MissingParametersException
245+
*/
241246
public static function requireProperties(
242247
$object,
243248
ISource $source = null,

src/Controller/Helper/RequestBodyQueue.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
trait RequestBodyQueue
3838
{
3939
/**
40-
* What is resource ?
4140
* @todo
4241
* @param \stdClass $resource Primary data resource
4342
*/
@@ -47,8 +46,7 @@ public static function handleResource(
4746
ResourceModel $model,
4847
ValidationModel $validationModel,
4948
array $validationCallbacks = []
50-
)
51-
{
49+
) {
5250
//Fetch request attributes
5351
$requestAttributes = $resource->attributes ?? new \stdClass();
5452
$requestRelationships = $resource->relationships ?? new \stdClass();
@@ -164,7 +162,7 @@ function (\stdClass $p) {
164162

165163
//Convert to array
166164
$tempIds = (
167-
is_array($rValue)
165+
is_array($rValue)
168166
? $rValue
169167
: [$rValue]
170168
);
@@ -204,7 +202,8 @@ function (\stdClass $p) {
204202
$callback(
205203
$resource,
206204
$parsedAttributes, //parsed
207-
$parsedRelationships //parsed
205+
$parsedRelationships, //parsed
206+
$source
208207
);
209208
}
210209

@@ -214,4 +213,3 @@ function (\stdClass $p) {
214213
);
215214
}
216215
}
217-

src/Controller/Post.php

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
use Phramework\Exceptions\ForbiddenException;
2121
use Phramework\Exceptions\IncorrectParameterException;
22+
use Phramework\Exceptions\MissingParametersException;
2223
use Phramework\Exceptions\RequestException;
2324
use Phramework\Exceptions\ServerException;
2425
use Phramework\Exceptions\Source\Pointer;
@@ -43,19 +44,29 @@ trait Post
4344
{
4445
use RequestBodyQueue;
4546

46-
//prototype
4747
/**
4848
* Handle HTTP POST request method to create new resources
4949
* @param ServerRequestInterface $request
5050
* @param ResponseInterface $response
5151
* @param ResourceModel $model
52-
* @param array $validationCallbacks
53-
* @param callable|null $viewCallback
52+
* @param array $validationCallbacks function of
53+
* - \stdClass $resource
54+
* - \stdClass $parsedAttributes
55+
* - \stdClass $parsedRelationships
56+
* - ISource $source
57+
* returning void
58+
* @param callable|null $viewCallback function of
59+
* - ServerRequestInterface $request,
60+
* - ResponseInterface $response,
61+
* - string[] $ids
62+
* - returning ResponseInterface
5463
* @param int|null $bulkLimit
5564
* @param array $directives
5665
* @return ResponseInterface
5766
* @throws ForbiddenException
5867
* @throws RequestException
68+
* @throws MissingParametersException
69+
* @throws ServerException
5970
*/
6071
public static function handlePost(
6172
ServerRequestInterface $request,
@@ -67,10 +78,12 @@ public static function handlePost(
6778
array $directives = []
6879
) : ResponseInterface {
6980
//todo figure out a permanent solution to have body as object instead of array, for every framework
70-
$body = json_decode(json_encode($request->getParsedBody()));
81+
$body = json_decode(json_encode($request->getParsedBody())) ?? new \stdClass();
82+
83+
Controller::requireProperties($body, new Pointer('/'), 'data');
7184

7285
//Access request body primary data
73-
$data = $body->data ?? [new \stdClass()];
86+
$data = $body->data;
7487

7588
/**
7689
* @var bool
@@ -88,7 +101,7 @@ public static function handlePost(
88101
//check bulk limit
89102
if ($bulkLimit !== null && count($data) > $bulkLimit) {
90103
throw new RequestException(sprintf(
91-
'Number of batch requests is exceeding the maximum of %s',
104+
'Number of bulk requests is exceeding the maximum of %s',
92105
$bulkLimit
93106
));
94107
}
@@ -143,6 +156,7 @@ public static function handlePost(
143156
$item = static::handleResource(
144157
$resource,
145158
$source,
159+
$model,
146160
$validationModel,
147161
$validationCallbacks
148162
);
@@ -233,13 +247,25 @@ public static function handlePost(
233247
);
234248
}
235249

236-
/*if (count($ids) === 1) {
250+
return Post::defaultViewCallback(
251+
$request,
252+
$response,
253+
$ids
254+
);
255+
}
256+
257+
public static function defaultViewCallback(
258+
ServerRequestInterface $request,
259+
ResponseInterface $response,
260+
array $ids
261+
) : ResponseInterface {
262+
if (count($ids) === 1) {
237263
//Prepare response with 201 Created status code
238-
return Response::created(
264+
$response = Response::created(
239265
$response,
240-
'link' . $ids[0] // location
266+
'link/' . $ids[0] // location //todo
241267
);
242-
}*/ //see https://stackoverflow.com/questions/11309444/can-the-location-header-be-used-for-multiple-resource-locations-in-a-201-created
268+
} //see https://stackoverflow.com/questions/11309444/can-the-location-header-be-used-for-multiple-resource-locations-in-a-201-created
243269

244270
//Return 204 No Content
245271
return Response::noContent($response);

src/Directive/FilterAttribute.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ public static function parse($filterKey, $filterValue)
118118
}
119119

120120
//@todo is this required?
121-
$singleFilterValue = urldecode($singleFilterValue);
121+
$singleFilterValue = urldecode((string) $singleFilterValue);
122122

123123
list($operator, $operand) = Operator::parse($singleFilterValue);
124124

tests/APP/DataSource/MemoryDataSource.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ public function post(
159159

160160
if (!property_exists($attributes, $idAttribute)) {
161161
//generate an id
162-
$attributes->{$idAttribute} = md5(mt_rand());
162+
$attributes->{$idAttribute} = md5((string) mt_rand());
163163
}
164164

165165
$table = $this->resourceModel->getVariable('table');

tests/APP/Models/Tag.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
use Phramework\JSONAPI\ResourceModel;
2323
use Phramework\JSONAPI\Model;
2424
use Phramework\JSONAPI\ModelTrait;
25+
use Phramework\JSONAPI\ValidationModel;
26+
use Phramework\Validate\ObjectValidator;
27+
use Phramework\Validate\StringValidator;
2528

2629
/**
2730
* @since 3.0.0
@@ -38,7 +41,19 @@ class Tag extends Model
3841
protected static function defineModel() : ResourceModel
3942
{
4043
$model = (new ResourceModel('tag', new MemoryDataSource()))
41-
->addVariable('table', 'tag');
44+
->addVariable('table', 'tag')
45+
->setValidationModel(
46+
new ValidationModel(
47+
new ObjectValidator(
48+
(object) [
49+
'name' => new StringValidator(2, 10)
50+
],
51+
['name'],
52+
false
53+
)
54+
),
55+
'POST'
56+
);
4257

4358
return $model;
4459
}

0 commit comments

Comments
 (0)