Skip to content

Commit 752481b

Browse files
committed
test: unit relationship
1 parent d037ec7 commit 752481b

File tree

3 files changed

+209
-1
lines changed

3 files changed

+209
-1
lines changed

src/Relationship.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Closure;
66
use Illuminate\Http\Resources\Json\JsonResource;
7+
use Illuminate\Http\Resources\Json\ResourceCollection;
78
use Illuminate\Http\Resources\PotentiallyMissing;
89

910
use function value;
@@ -39,6 +40,7 @@ public function toArray($request, bool $minimal = false): array
3940
'meta' => value($this->meta, $this->resource),
4041
];
4142

43+
/** @var Resourceable $resource */
4244
$resource = value($this->resource);
4345

4446
if ($this->isMissing($resource)) {
@@ -51,7 +53,7 @@ public function toArray($request, bool $minimal = false): array
5153

5254
$datum = $resource->toArray($request, $minimal);
5355

54-
if ($resource instanceof JsonApiCollection) {
56+
if ($resource instanceof ResourceCollection) {
5557
foreach ($datum as $value) {
5658
$data['data'][] = [
5759
'type' => $value['type'],

src/Resourceable.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,11 @@
44

55
interface Resourceable
66
{
7+
/**
8+
* @param $request
9+
* @param bool $minimal
10+
*
11+
* @return array{type: string, id: string}|array<int, array{type: string, id: string}>
12+
*/
713
public function toArray($request, bool $minimal = false): array;
814
}

tests/Unit/RelationshipTest.php

Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
<?php
2+
3+
namespace Test\Unit;
4+
5+
use Ark4ne\JsonApi\Resource\Relationship;
6+
use Ark4ne\JsonApi\Resource\Resourceable;
7+
use Illuminate\Http\Request;
8+
use Illuminate\Http\Resources\Json\JsonResource;
9+
use Illuminate\Http\Resources\Json\ResourceCollection;
10+
use Illuminate\Http\Resources\MissingValue;
11+
use Test\TestCase;
12+
13+
class RelationshipTest extends TestCase
14+
{
15+
public function testToArrayResource()
16+
{
17+
$resource = $this->getJsonResource();
18+
19+
$relation = new Relationship($resource);
20+
21+
$this->assertEquals([
22+
'data' => [
23+
'data' => [
24+
'type' => 'my-model',
25+
'id' => 1,
26+
]
27+
],
28+
'included' => [
29+
[
30+
'type' => 'my-model',
31+
'id' => 1,
32+
'attributes' => [
33+
'foo' => 'bar'
34+
]
35+
]
36+
]
37+
], $relation->toArray(new Request()));
38+
}
39+
40+
public function testToArrayResourceMinimal()
41+
{
42+
$resource = $this->getJsonResource();
43+
44+
$relation = (new Relationship($resource))->withLinks([
45+
'self' => 'link'
46+
])->withMeta([
47+
'hash' => 'azerty'
48+
]);
49+
50+
$this->assertEquals([
51+
'data' => [
52+
'data' => [
53+
'type' => 'my-model',
54+
'id' => 1,
55+
],
56+
'links' => ['self' => 'link'],
57+
'meta' => ['hash' => 'azerty'],
58+
],
59+
], $relation->toArray(new Request(), true));
60+
}
61+
62+
public function testToArrayCollection()
63+
{
64+
$resource = $this->getResourceCollection();
65+
$relation = new Relationship($resource);
66+
67+
$this->assertEquals([
68+
'data' => [
69+
'data' => [
70+
[
71+
'type' => 'my-model',
72+
'id' => 1,
73+
],
74+
[
75+
'type' => 'my-model',
76+
'id' => 2,
77+
]
78+
]
79+
],
80+
'included' => [
81+
[
82+
'type' => 'my-model',
83+
'id' => 1,
84+
'attributes' => [
85+
'foo' => 'bar'
86+
]
87+
],
88+
[
89+
'type' => 'my-model',
90+
'id' => 2,
91+
'attributes' => [
92+
'baz' => 'tar'
93+
]
94+
]
95+
]
96+
], $relation->toArray(new Request()));
97+
}
98+
99+
public function testToArrayCollectionMinimal()
100+
{
101+
$resource = $this->getResourceCollection();
102+
$relation = (new Relationship($resource))->withLinks([
103+
'self' => 'link'
104+
])->withMeta([
105+
'total' => 2
106+
]);
107+
108+
$this->assertEquals([
109+
'data' => [
110+
'data' => [
111+
[
112+
'type' => 'my-model',
113+
'id' => 1,
114+
],
115+
[
116+
'type' => 'my-model',
117+
'id' => 2,
118+
]
119+
],
120+
'links' => ['self' => 'link'],
121+
'meta' => ['total' => 2],
122+
],
123+
], $relation->toArray(new Request(), true));
124+
}
125+
126+
public function testToArrayMissingValue()
127+
{
128+
$resource = $this->getJsonResourceMissingValue();
129+
$relation = (new Relationship($resource))->withLinks([
130+
'self' => 'link'
131+
])->withMeta([
132+
'hash' => 'azerty'
133+
]);
134+
135+
$this->assertEquals([
136+
'data' => [
137+
'links' => ['self' => 'link'],
138+
'meta' => ['hash' => 'azerty'],
139+
],
140+
], $relation->toArray(new Request(), true));
141+
}
142+
143+
private function getJsonResource()
144+
{
145+
return new class((object)['id' => 1]) extends JsonResource implements Resourceable {
146+
public function toArray($request, bool $minimal = false): array
147+
{
148+
return [
149+
'type' => 'my-model',
150+
'id' => $this->id,
151+
'attributes' => [
152+
'foo' => 'bar'
153+
]
154+
];
155+
}
156+
};
157+
}
158+
159+
private function getJsonResourceMissingValue()
160+
{
161+
return new class(new MissingValue()) extends JsonResource implements Resourceable {
162+
public function toArray($request, bool $minimal = false): array
163+
{
164+
return [
165+
'type' => 'my-model',
166+
'id' => $this->id,
167+
'attributes' => [
168+
'foo' => 'bar'
169+
]
170+
];
171+
}
172+
};
173+
}
174+
175+
private function getResourceCollection()
176+
{
177+
return new class(collect()) extends ResourceCollection implements Resourceable {
178+
public function toArray($request, bool $minimal = false): array
179+
{
180+
return [
181+
[
182+
'type' => 'my-model',
183+
'id' => 1,
184+
'attributes' => [
185+
'foo' => 'bar'
186+
]
187+
],
188+
[
189+
'type' => 'my-model',
190+
'id' => 2,
191+
'attributes' => [
192+
'baz' => 'tar'
193+
]
194+
]
195+
];
196+
}
197+
};
198+
}
199+
200+
}

0 commit comments

Comments
 (0)