10000 Merge branch 'develop' into async-with-cn · Abid0Hasan/laravel-json-api@8ea33b9 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8ea33b9

Browse files
committed
Merge branch 'develop' into async-with-cn
2 parents c7f3474 + c2dcb65 commit 8ea33b9

File tree

5 files changed

+109
-31
lines changed

5 files changed

+109
-31
lines changed

docs/basics/validators.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,14 @@ Content-Type: application/vnd.api+json
4141
4242
{
4343
"errors": [
44-
[
44+
{
4545
"title": "Non-Compliant JSON API Document",
4646
"status": "400",
4747
"detail": "The member id must be a string.",
4848
"source": {
4949
"pointer": "/data/id"
5050
}
51-
]
51+
}
5252
]
5353
}
5454
```
@@ -273,9 +273,9 @@ class Validators extends AbstractValidators
273273

274274
/**
275275
* @param \App\Post $record
276-
* @return \Illuminate\Support\Collection
276+
* @return iterable
277277
*/
278-
protected function existingRelationships($record): Collection
278+
protected function existingRelationships($record): iterable
279279
{
280280
return [
281281
'author' => [

src/Document/ResourceObject.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ public function withoutRelationships(): self
351351
public function getRelations(): Collection
352352
{
353353
return $this->getRelationships()->filter(function (array $relation) {
354-
return isset($relation['data']);
354+
return array_key_exists('data', $relation);
355355
})->map(function (array $relation) {
356356
return $relation['data'];
357357
});

src/Testing/TestResponse.php

Lines changed: 61 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -107,20 +107,30 @@ public function assertFetchedPage(
107107
bool $strict = true
108108
): self
109109
{
110-
if (empty($links) && empty($meta)) {
111-
throw new \InvalidArgumentException('Expecting links or meta to ensure response is a page.');
112-
}
113-
114-
$this->assertFetchedMany($expected, $strict);
110+
$this->assertPage($expected, $links, $meta, $metaKey, $strict);
115111

116-
if ($links) {
117-
$this->assertLinks($links, $strict);
118-
}
112+
return $this;
113+
}
119114

120-
if ($meta) {
121-
$meta = $metaKey ? [$metaKey => $meta] : $meta;
122-
$this->assertMeta($meta, $strict);
123-
}
115+
/**
116+
* Assert the response is a JSON API page with expected resources in the specified order.
117+
*
118+
* @param $expected
119+
* @param array|null $links
120+
* @param array|null $meta
121+
* @param string|null $metaKey
122+
* @param bool $strict
123+
* @return TestResponse
124+
*/
125+
public function assertFetchedPageInOrder(
126+
$expected,
127+
?array $links,
128+
?array $meta,
129+
string $metaKey = 'page',
130+
bool $strict = true
131+
): self
132+
{
133+
$this->assertPage($expected, $links, $meta, $metaKey, $strict, true);
124134

125135
return $this;
126136
}
@@ -449,4 +459,43 @@ protected function normalizeId($id)
449459
return (string) $id;
450460
}
451461

462+
/**
463+
* Assert the response is a JSON API page.
464+
*
465+
* @param $expected
466+
* @param array|null $links
467+
* @param array|null $meta
468+
* @param string|null $metaKey
469+
* @param bool $strict
470+
* @param bool $order
471+
* @return void
472+
*/
473+
private function assertPage(
474+
$expected,
475+
?array $links,
476+
?array $meta,
477+
string $metaKey = 'page',
478+
bool $strict = true,
479+
bool $order = false
480+
): void
481+
{
482+
if (empty($links) && empty($meta)) {
483+
throw new \InvalidArgumentException('Expecting links or meta to ensure response is a page.');
484+
}
485+
486+
if ($order) {
487+
$this->assertFetchedManyInOrder($expected, $strict);
488+
} else {
489+
$this->assertFetchedMany($expected, $strict);
490+
}
491+
492+
if ($links) {
493+
$this->assertLinks($links, $strict);
494+
}
495+
496+
if ($meta) {
497+
$meta = $metaKey ? [$metaKey => $meta] : $meta;
498+
$this->assertMeta($meta, $strict);
499+
}
500+
}
452501
}

src/Validation/AbstractValidators.php

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -330,12 +330,12 @@ protected function updateData($record, array $document): array
330330
$resource['attributes'] = $this->extractAttributes(
331331
$record,
332332
$resource['attributes'] ?? []
333-
)->all();
333+
);
334334

335335
$resource['relationships'] = $this->extractRelationships(
336336
$record,
337337
$resource['relationships'] ?? []
338-
)->all();
338+
);
339339
}
340340

341341
return $resource;
@@ -363,34 +363,34 @@ protected function mustValidateExisting($record, array $document): bool
363363
*
364364
* @param $record
365365
* @param array $new
366-
* @return Collection
366+
* @return array
367367
*/
368-
protected function extractAttributes($record, array $new): Collection
368+
protected function extractAttributes($record, array $new): array
369369
{
370-
return $this->existingAttributes($record)->merge($new);
370+
return collect($this->existingAttributes($record))->merge($new)->all();
371371
}
372372

373373
/**
374374
* Get any existing attributes for the provided record.
375375
*
376376
* @param $record
377-
* @return Collection
377+
* @return iterable
378378
*/
379-
protected function existingAttributes($record): Collection
379+
protected function existingAttributes($record): iterable
380380
{
381-
return collect($this->container->getSchema($record)->getAttributes($record));
381+
return $this->container->getSchema($record)->getAttributes($record);
382382
}
383383

384384
/**
385385
* Extract relationships for a resource update.
386386
*
387387
* @param $record
388388
* @param array $new
389-
* @return Collection
389+
* @return array
390390
*/
391-
protected function extractRelationships($record, array $new): Collection
391+
protected function extractRelationships($record, array $new): array
392392
{
393-
return $this->existingRelationships($record)->merge($new);
393+
return collect($this->existingRelationships($record))->merge($new)->all();
394394
}
395395

396396
/**
@@ -401,11 +401,11 @@ protected function extractRelationships($record, array $new): Collection
401401
* to add existing relationship data.
402402
*
403403
* @param $record
404-
* @return Collection
404+
* @return iterable
405405
*/
406-
protected function existingRelationships($record): Collection
406+
protected function existingRelationships($record): iterable
407407
{
408-
return collect();
408+
return [];
409409
}
410410

411411
/**

tests/lib/Unit/Document/ResourceObjectTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,35 @@ public function testFields(): array
122122
return $expected;
123123
}
124124

125+
public function testFieldsWithEmptyToOne(): void
126+
{
127+
$this->values['relationships']['author']['data'] = null;
128+
129+
$expected = [
130+
'author' => null,
131+
'content' => '...',
132+
'id' => '1',
133+
'published' => null,
134+
'tags' => [
135+
[
136+
'type' => 'tags',
137+
'id' => '4',
138+
],
139+
[
140+
'type' => 'tags',
141+
'id' => '5',
142+
],
143+
],
144+
'title' => 'Hello World',
145+
'type' => 'posts',
146+
];
147+
148+
$resource = ResourceObject::create($this->values);
149+
$this->assertSame($expected, $resource->all());
150+
$this->assertNull($resource['author']);
151+
$this->assertNull($resource->get('author', true));
152+
}
153+
125154
/**
126155
* @param array $expected
127156
* @depends testFields

0 commit comments

Comments
 (0)
0