Skip to content

Commit 1cc3c56

Browse files
committed
Prepare POST tests
1 parent 47d1c0e commit 1cc3c56

File tree

5 files changed

+107
-50
lines changed

5 files changed

+107
-50
lines changed

src/Controller/POST.php

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@
1717
namespace Phramework\JSONAPI\Controller;
1818

1919
use Phramework\JSONAPI\Controller\POST\QueueItem;
20+
use Phramework\JSONAPI\Util;
2021
use \Phramework\Models\Request;
2122
use \Phramework\Exceptions\RequestException;
2223
use \Phramework\JSONAPI\Relationship;
24+
use Phramework\Validate\ObjectValidator;
2325

2426
/**
2527
* POST related methods
@@ -44,6 +46,7 @@ abstract class POST extends \Phramework\JSONAPI\Controller\GET
4446
* so that any exceptions can be thrown, and finally invoke the execution of the queue.
4547
* @uses $modelClass::post method to create resources
4648
* @return int[]
49+
* @todo validate type
4750
*/
4851
protected static function handlePOST(
4952
$params,
@@ -59,7 +62,7 @@ protected static function handlePOST(
5962
$data = static::getRequestData($params);
6063

6164
//Treat single requests as an array of resources
62-
if (!is_array($data)) {
65+
if (is_object($data) || is_array($data) && Util::isArrayAssoc($data)) {
6366
$data = [$data];
6467
}
6568

@@ -68,7 +71,11 @@ protected static function handlePOST(
6871
$resource = (object)$resource;
6972
}
7073

71-
$requestAttributes = $resource->attributes;
74+
$requestAttributes = (
75+
isset($resource->attributes) && $resource->attributes
76+
? $resource->attributes
77+
: new \stdClass()
78+
);
7279

7380
if (property_exists($resource, 'relationships')) {
7481
$requestRelationships = $resource->relationships;
@@ -126,6 +133,8 @@ protected static function handlePOST(
126133
$ids[] = $id;
127134
}
128135

136+
return $ids;
137+
129138
if (count($ids) === 1) {
130139
//Prepare response with 201 Created status code
131140
\Phramework\Models\Response::created(
@@ -170,10 +179,17 @@ private static function handlePOSTResource(
170179
$validationCallbacks,
171180
$requestRelationships
172181
) {
173-
$validationModel = $modelClass::getValidationModel();
182+
$validator = $modelClass::getValidationModel();
183+
184+
$attributesValidator = (
185+
isset($validator->attributes) && $validator->attributes
186+
? $validator->attributes
187+
: new ObjectValidator()
188+
);
189+
174190

175191
//Parse request attributes using $validationModel to validate the data
176-
$attributes = $validationModel->attributes->parse($requestAttributes);
192+
$attributes = $attributesValidator->parse($requestAttributes);
177193

178194
$relationships = $modelClass::getRelationships();
179195

@@ -193,6 +209,11 @@ private static function handlePOSTResource(
193209
* - copy ids to $relationshipAttributes object
194210
*/
195211
foreach ($requestRelationships as $relationshipKey => $relationshipValue) {
212+
//Work with objects
213+
if (is_array($relationshipValue)) {
214+
$relationshipValue = (object) $relationshipValue;
215+
}
216+
196217
if (!isset($relationshipValue->data)) {
197218
throw new RequestException(sprintf(
198219
'Relationship "%s" must have a member data defined',
@@ -202,6 +223,10 @@ private static function handlePOSTResource(
202223

203224
$relationshipData = $relationshipValue->data;
204225

226+
if (is_array($relationshipData) && Util::isArrayAssoc($relationshipData)) {
227+
$relationshipData = (object) $relationshipData;
228+
}
229+
205230
//Check if relationship exists
206231
static::exists(
207232
$modelClass::relationshipExists($relationshipKey),
@@ -252,25 +277,32 @@ private static function handlePOSTResource(
252277
}
253278

254279
foreach ($relationshipData as $value) {
280+
if (is_array($value) && Util::isArrayAssoc($value)) {
281+
$value = (object) $value;
282+
}
283+
255284
if (!is_object($value)) {
256285
throw new RequestException(sprintf(
257286
'Expected data properties to be object for relationship "%s"',
258287
$relationshipKey
259288
));
260289
}
290+
261291
if (!isset($value->id) || !isset($value->type)) {
262292
throw new RequestException(sprintf(
263293
'Attributes "id" and "type" required for relationship "%s"',
264294
$relationshipKey
265295
));
266296
}
297+
267298
if ($value->type !== $relationshipResourceType) {
268299
throw new RequestException(sprintf(
269300
'Invalid resource type "%s" for relationship "%s"',
270301
$relationshipResourceType,
271302
$relationshipKey
272303
));
273304
}
305+
274306
$parsedValues[] = $value->id;
275307
}
276308

@@ -284,8 +316,8 @@ private static function handlePOSTResource(
284316
//Parse attributes using relationship's validation model
285317
$parsedRelationshipAttributes =
286318
(
287-
isset($validationModel->relationships)
288-
? $validationModel->relationships->parse(
319+
isset($validator->relationships)
320+
? $validator->relationships->parse(
289321
$relationshipAttributes
290322
)
291323
: new \stdClass()
@@ -409,9 +441,8 @@ private static function handlePOSTResource(
409441
);
410442
}
411443

412-
413444
//Push to queueRelationships
414-
$queueRelationships->{$relationshipKey} = (object)[
445+
$queueRelationships->{$relationshipKey} = (object) [
415446
'callback' => $relationshipCallMethod, //callable
416447
'resources' => $parsedRelationshipValue //array
417448
];

tests/APP/Models/Article.php

Lines changed: 52 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -125,18 +125,63 @@ public static function get(
125125
Fields $fields = null,
126126
...$additionalParameters
127127
) {
128-
$records = [
128+
return self::collection(
129+
self::handleGetWithArrayOfRecords(
130+
static::getRecords(),
131+
$page,
132+
$filter,
133+
$sort,
134+
$fields,
135+
...$additionalParameters
136+
),
137+
$fields
138+
);
139+
}
140+
141+
public static function getRelationships()
142+
{
143+
return (object)[
144+
'creator' => new Relationship(
145+
'creator-user_id',
146+
'user',
147+
Relationship::TYPE_TO_ONE,
148+
User::class,
149+
'id'
150+
),
151+
'tag' => new Relationship(
152+
'tag-id',
153+
'tag',
154+
Relationship::TYPE_TO_MANY,
155+
Tag::class,
156+
'id'
157+
)
158+
];
159+
}
160+
161+
public static function post(
162+
$attributes,
163+
$return = \Phramework\Database\Operations\Create::RETURN_ID
164+
) {
165+
$records = static::getRecords();
166+
167+
return $records[0]['id'];
168+
}
169+
170+
171+
public static function getRecords()
172+
{
173+
return [
129174
[
130175
'id' => '1',
131176
'creator-user_id' => '1',
132177
'status' => 1,
133178
'title' => 'First post',
134179
'updated' => null,
135-
'meta' => (object) [
180+
'meta' => (object)[
136181
'keywords' => 'blog'
137182
],
138-
Resource::META_MEMBER => (object) [
139-
'view' => 1000,
183+
Resource::META_MEMBER => (object)[
184+
'view' => 1000,
140185
'unique' => 100
141186
]
142187
],
@@ -149,7 +194,9 @@ public static function get(
149194
'meta' => null,
150195
Resource::META_MEMBER => [
151196
'some_key' => [
152-
1, 2, 3
197+
1,
198+
2,
199+
3
153200
]
154201
]
155202
],
@@ -170,37 +217,5 @@ public static function get(
170217
'meta' => null
171218
]
172219
];
173-
174-
return self::collection(
175-
self::handleGetWithArrayOfRecords(
176-
$records,
177-
$page,
178-
$filter,
179-
$sort,
180-
$fields,
181-
...$additionalParameters
182-
),
183-
$fields
184-
);
185-
}
186-
187-
public static function getRelationships()
188-
{
189-
return (object)[
190-
'creator' => new Relationship(
191-
'creator-user_id',
192-
'user',
193-
Relationship::TYPE_TO_ONE,
194-
User::class,
195-
'id'
196-
),
197-
'tag' => new Relationship(
198-
'tag-id',
199-
'tag',
200-
Relationship::TYPE_TO_MANY,
201-
Tag::class,
202-
'id'
203-
)
204-
];
205220
}
206221
}

tests/APP/Models/Tag.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public static function postRelationshipByArticle(
106106
$additionalAttributes = null,
107107
$return = \Phramework\Database\Operations\Create::RETURN_NUMBER_OF_RECORDS
108108
) {
109-
return \Phramework\Database\Operations\Create::create(
109+
/*return \Phramework\Database\Operations\Create::create(
110110
[
111111
'tag-id' => $tagId,
112112
'article-id' => $articleId,
@@ -115,7 +115,8 @@ public static function postRelationshipByArticle(
115115
'article-tag',
116116
static::getSchema(),
117117
$return
118-
);
118+
);*/
119+
return 1;
119120
}
120121

121122
/**

tests/src/Controllers/POSTTest.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ protected function prepare()
5252
$_SERVER['REQUEST_METHOD'] = Phramework::METHOD_POST;
5353

5454
$_POST['data'] = [
55+
'type' => 'article',
5556
'attributes' => [
5657
'title' => 'omg'
5758
],
@@ -90,6 +91,7 @@ function (
9091
$that->params = $parameters;
9192
}
9293
);
94+
9395
// clean the output buffer
9496
//ob_clean();
9597
//$this->phramework->invoke();
@@ -108,6 +110,14 @@ protected function tearDown()
108110
//}
109111
}
110112

113+
/**
114+
* @covers \Phramework\JSONAPI\Controller\POST::handlePOSTResource
115+
*/
116+
public function testHandlePOSTResource()
117+
{
118+
$this->testPOSTSuccess();
119+
}
120+
111121
/**
112122
* @covers \Phramework\JSONAPI\Controller\POST::handlePOST
113123
*/
@@ -121,7 +131,7 @@ public function testPOSTSuccess()
121131
//Access parameters written by invoked phramework's viewer
122132
$params = $this->params;
123133

124-
var_dump($params);
134+
print_r([$params]);
125135

126136
$this->assertInternalType('object', $params);
127137

tests/src/Model/RelationshipTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,14 @@ public function testGetRelationshipData()
3434
{
3535
$articles = Article::get(new Page(1, 1))[0];
3636

37-
print_r($articles);
37+
//print_r($articles);
3838

3939
$relationship = Tag::getRelationshipByArticle(
4040
'1',
4141
'tag',
4242
null
4343
);
4444

45-
print_r($relationship);
45+
//print_r($relationship);
4646
}
4747
}

0 commit comments

Comments
 (0)