Skip to content

Commit fef287b

Browse files
committed
test: improve unit concerns\relationships
1 parent 1ab3d68 commit fef287b

File tree

2 files changed

+116
-26
lines changed

2 files changed

+116
-26
lines changed

tests/Support/Reflect.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,25 @@
22

33
namespace Test\Support;
44

5+
use ReflectionClass;
6+
57
class Reflect
68
{
79
public static function invoke($object, string $method, ...$args)
810
{
9-
$reflect = new \ReflectionClass($object);
11+
$reflect = new ReflectionClass($object);
1012
$reflectMethod = $reflect->getMethod($method);
1113
$reflectMethod->setAccessible(true);
1214

1315
return $reflectMethod->invoke($object, ...$args);
1416
}
1517

18+
public static function set($object, string $property, $value)
19+
{
20+
$reflect = new ReflectionClass($object);
21+
$reflectProperty = $reflect->getProperty($property);
22+
$reflectProperty->setAccessible(true);
23+
24+
$reflectProperty->setValue($object, $value);
25+
}
1626
}

tests/Unit/Concerns/RelationshipsTest.php

Lines changed: 105 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,21 @@
66
use Ark4ne\JsonApi\Resource\Relationship;
77
use Ark4ne\JsonApi\Resource\Resourceable;
88
use Ark4ne\JsonApi\Resource\Support\Includes;
9+
use Illuminate\Database\Eloquent\Model;
910
use Illuminate\Http\Request;
1011
use Illuminate\Http\Resources\Json\JsonResource;
1112
use Test\Support\Reflect;
1213
use Test\TestCase;
1314

1415
class RelationshipsTest extends TestCase
1516
{
16-
public function testMapRelationship()
17+
public function testMapRelationshipMinimal()
1718
{
1819
$stub = new class(null) extends JsonResource {
1920
use Relationships;
2021
};
2122

22-
$minimal = false;
23+
$minimal = true;
2324
$request = new Request();
2425
$relationship = $this->createMock(Relationship::class);
2526
$relationship
@@ -32,16 +33,6 @@ public function testMapRelationship()
3233
'links' => ['self' => "://api.com/child/abc-123"],
3334
'meta' => ['total' => 1],
3435
],
35-
'with' => [
36-
'with-some' => 'some-value'
37-
],
38-
'included' => [
39-
[
40-
'id' => 'abc-123',
41-
'type' => 'child',
42-
'foo' => 'bar'
43-
]
44-
],
4536
]);
4637

4738
$actual = Reflect::invoke($stub, 'mapRelationship', $minimal, $request, $relationship);
@@ -51,22 +42,17 @@ public function testMapRelationship()
5142
'links' => ['self' => "://api.com/child/abc-123"],
5243
'meta' => ['total' => 1],
5344
], $actual);
54-
$this->assertEquals([
55-
'with-some' => ['some-value'],
56-
'included' => [
57-
[
58-
'id' => 'abc-123',
59-
'type' => 'child',
60-
'foo' => 'bar'
61-
]
62-
]
63-
], $stub->with($request));
6445

46+
$this->assertEquals([], $stub->with($request));
47+
}
48+
49+
public function testMapRelationshipFull()
50+
{
6551
$stub = new class(null) extends JsonResource {
6652
use Relationships;
6753
};
6854

69-
$minimal = true;
55+
$minimal = false;
7056
$request = new Request();
7157
$relationship = $this->createMock(Relationship::class);
7258
$relationship
@@ -79,6 +65,16 @@ public function testMapRelationship()
7965
'links' => ['self' => "://api.com/child/abc-123"],
8066
'meta' => ['total' => 1],
8167
],
68+
'with' => [
69+
'with-some' => 'some-value'
70+
],
71+
'included' => [
72+
[
73+
'id' => 'abc-123',
74+
'type' => 'child',
75+
'foo' => 'bar'
76+
]
77+
],
8278
]);
8379

8480
$actual = Reflect::invoke($stub, 'mapRelationship', $minimal, $request, $relationship);
@@ -88,11 +84,32 @@ public function testMapRelationship()
8884
'links' => ['self' => "://api.com/child/abc-123"],
8985
'meta' => ['total' => 1],
9086
], $actual);
87+
$this->assertEquals([
88+
'with-some' => ['some-value'],
89+
'included' => [
90+
[
91+
'id' => 'abc-123',
92+
'type' => 'child',
93+
'foo' => 'bar'
94+
]
95+
]
96+
], $stub->with($request));
97+
}
9198

92-
$this->assertEquals([], $stub->with($request));
99+
public function testRequestedRelationshipsNoRelations()
100+
{
101+
Includes::flush();
102+
$resource = new class(collect()) extends JsonResource {
103+
use Relationships;
104+
};
105+
106+
$actual = Reflect::invoke($resource, 'requestedRelationships', new Request());
107+
108+
$this->assertEquals([], $actual);
109+
$this->assertEquals([], $resource->with);
93110
}
94111

95-
public function testRequestedRelationships()
112+
public function testRequestedRelationshipsNoInclude()
96113
{
97114
Includes::flush();
98115
$stub = $this->getStub();
@@ -109,7 +126,10 @@ public function testRequestedRelationships()
109126
],
110127
], $actual);
111128
$this->assertEquals([], $stub->with);
129+
}
112130

131+
public function testRequestedRelationshipsOneDepthInclude()
132+
{
113133
Includes::flush();
114134
$stub = $this->getStub();
115135

@@ -144,7 +164,10 @@ public function testRequestedRelationships()
144164
]
145165
]
146166
], $stub->with);
167+
}
147168

169+
public function testRequestedRelationshipsTwoDepthInclude()
170+
{
148171
Includes::flush();
149172
$stub = $this->getStub();
150173

@@ -195,6 +218,63 @@ public function testRequestedRelationships()
195218
], $stub->with);
196219
}
197220

221+
public function testRequestedRelationshipsWithMissingValue()
222+
{
223+
$model = new class(['id' => 1]) extends Model {
224+
protected $fillable = ['id'];
225+
};
226+
227+
Reflect::set($model, 'relations', ['loadedRelation' => (object)['id' => 3]]);
228+
229+
$resource = new class($model) extends JsonResource {
230+
use Relationships;
231+
232+
public function toRelationships(Request $request): iterable
233+
{
234+
return [
235+
'foo' => new class((object)['id' => 2]) extends JsonResource implements Resourceable {
236+
public function toArray($request, bool $minimal = false): array
237+
{
238+
return [
239+
'id' => $this->id,
240+
'type' => 'foo'
241+
];
242+
}
243+
},
244+
'bar' => $this->when(false, fn() => JsonResource::make(collect())),
245+
'baz' => JsonResource::collection($this->whenLoaded('not-loaded-relation')),
246+
'tar' => new class($this->whenLoaded('loadedRelation')) extends JsonResource implements
247+
Resourceable {
248+
public function toArray($request, bool $minimal = false): array
249+
{
250+
return [
251+
'id' => $this->id,
252+
'type' => 'tar'
253+
];
254+
}
255+
},
256+
];
257+
}
258+
};
259+
260+
$actual = Reflect::invoke($resource, 'requestedRelationships', new Request());
261+
262+
$this->assertEquals([
263+
'foo' => [
264+
'data' => [
265+
'id' => 2,
266+
'type' => 'foo'
267+
]
268+
],
269+
'tar' => [
270+
'data' => [
271+
'id' => 3,
272+
'type' => 'tar'
273+
]
274+
]
275+
], $actual);
276+
}
277+
198278
private function getStub()
199279
{
200280
return new class((object)[]) extends JsonResource implements Resourceable {

0 commit comments

Comments
 (0)