1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace League\Fractal\Test; |
6
|
|
|
|
7
|
|
|
use InvalidArgumentException; |
8
|
|
|
use League\Fractal\Manager; |
9
|
|
|
use League\Fractal\Resource\Collection; |
10
|
|
|
use League\Fractal\Resource\Item; |
11
|
|
|
use League\Fractal\Scope; |
12
|
|
|
use League\Fractal\Serializer\JsonApiSerializer; |
13
|
|
|
use League\Fractal\Test\Stub\Transformer\JsonApiAuthorTransformer; |
14
|
|
|
use League\Fractal\Test\Stub\Transformer\JsonApiBookTransformer; |
15
|
|
|
use League\Fractal\Test\Stub\Transformer\JsonApiEmptyTransformer; |
16
|
|
|
use Mockery; |
17
|
|
|
|
18
|
|
|
class JsonApiSerializerTest extends TestCase |
19
|
|
|
{ |
20
|
|
|
/** @var Manager */ |
21
|
|
|
private $manager; |
22
|
|
|
|
23
|
|
|
public function setUp() : void |
24
|
|
|
{ |
25
|
|
|
$this->manager = new Manager(); |
26
|
|
|
$this->manager->setSerializer(new JsonApiSerializer()); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function testSerializeCollectionWithExtraMeta() : void |
30
|
|
|
{ |
31
|
|
|
$booksData = [ |
32
|
|
|
[ |
33
|
|
|
'id' => 1, |
34
|
|
|
'title' => 'Foo', |
35
|
|
|
'year' => '1991', |
36
|
|
|
'_author' => [ |
37
|
|
|
'id' => 1, |
38
|
|
|
'name' => 'Dave', |
39
|
|
|
], |
40
|
|
|
'meta' => [ |
41
|
|
|
'foo' => 'bar' |
42
|
|
|
] |
43
|
|
|
], |
44
|
|
|
[ |
45
|
|
|
'id' => 2, |
46
|
|
|
'title' => 'Bar', |
47
|
|
|
'year' => '1997', |
48
|
|
|
'_author' => [ |
49
|
|
|
'id' => 2, |
50
|
|
|
'name' => 'Bob', |
51
|
|
|
], |
52
|
|
|
'meta' => [ |
53
|
|
|
'bar' => 'baz' |
54
|
|
|
] |
55
|
|
|
], |
56
|
|
|
]; |
57
|
|
|
|
58
|
|
|
$resource = new Collection($booksData, new JsonApiBookTransformer(), 'books'); |
59
|
|
|
$scope = new Scope($this->manager, $resource); |
60
|
|
|
|
61
|
|
|
$expected = [ |
62
|
|
|
'data' => [ |
63
|
|
|
[ |
64
|
|
|
'type' => 'books', |
65
|
|
|
'id' => '1', |
66
|
|
|
'attributes' => [ |
67
|
|
|
'title' => 'Foo', |
68
|
|
|
'year' => 1991, |
69
|
|
|
], |
70
|
|
|
'meta' => [ |
71
|
|
|
'foo' => 'bar' |
72
|
|
|
] |
73
|
|
|
], |
74
|
|
|
[ |
75
|
|
|
'type' => 'books', |
76
|
|
|
'id' => '2', |
77
|
|
|
'attributes' => [ |
78
|
|
|
'title' => 'Bar', |
79
|
|
|
'year' => 1997, |
80
|
|
|
], |
81
|
|
|
'meta' => [ |
82
|
|
|
'bar' => 'baz' |
83
|
|
|
] |
84
|
|
|
], |
85
|
|
|
], |
86
|
|
|
]; |
87
|
|
|
|
88
|
|
|
$this->assertSame($expected, $scope->toArray()); |
89
|
|
|
|
90
|
|
|
$expectedJson = '{"data":[{"type":"books","id":"1","attributes":{"title":"Foo","year":1991},"meta":{"foo":"bar"}},{"type":"books","id":"2","attributes":{"title":"Bar","year":1997},"meta":{"bar":"baz"}}]}'; |
91
|
|
|
$this->assertSame($expectedJson, $scope->toJson()); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function testSerializingItemResourceWithHasOneInclude() : void |
95
|
|
|
{ |
96
|
|
|
$this->manager->parseIncludes('author'); |
97
|
|
|
|
98
|
|
|
$bookData = [ |
99
|
|
|
'id' => 1, |
100
|
|
|
'title' => 'Foo', |
101
|
|
|
'year' => '1991', |
102
|
|
|
'_author' => [ |
103
|
|
|
'id' => 1, |
104
|
|
|
'name' => 'Dave', |
105
|
|
|
], |
106
|
|
|
]; |
107
|
|
|
|
108
|
|
|
$resource = new Item($bookData, new JsonApiBookTransformer(), 'books'); |
109
|
|
|
|
110
|
|
|
$scope = new Scope($this->manager, $resource); |
111
|
|
|
|
112
|
|
|
$expected = [ |
113
|
|
|
'data' => [ |
114
|
|
|
'type' => 'books', |
115
|
|
|
'id' => '1', |
116
|
|
|
'attributes' => [ |
117
|
|
|
'title' => 'Foo', |
118
|
|
|
'year' => 1991, |
119
|
|
|
], |
120
|
|
|
'relationships' => [ |
121
|
|
|
'author' => [ |
122
|
|
|
'data' => [ |
123
|
|
|
'type' => 'people', |
124
|
|
|
'id' => '1', |
125
|
|
|
], |
126
|
|
|
], |
127
|
|
|
], |
128
|
|
|
], |
129
|
|
|
'included' => [ |
130
|
|
|
[ |
131
|
|
|
'type' => 'people', |
132
|
|
|
'id' => '1', |
133
|
|
|
'attributes' => [ |
134
|
|
|
'name' => 'Dave', |
135
|
|
|
], |
136
|
|
|
], |
137
|
|
|
], |
138
|
|
|
]; |
139
|
|
|
|
140
|
|
|
$this->assertSame($expected, $scope->toArray()); |
141
|
|
|
|
142
|
|
|
$expectedJson = '{"data":{"type":"books","id":"1","attributes":{"title":"Foo","year":1991},"relationships":{"author":{"data":{"type":"people","id":"1"}}}},"included":[{"type":"people","id":"1","attributes":{"name":"Dave"}}]}'; |
143
|
|
|
$this->assertSame($expectedJson, $scope->toJson()); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
public function testSerializingItemResourceWithMetaOnRelationship() : void |
147
|
|
|
{ |
148
|
|
|
$this->manager->parseIncludes('author-with-meta'); |
149
|
|
|
|
150
|
|
|
$bookData = [ |
151
|
|
|
'id' => 1, |
152
|
|
|
'title' => 'Foo', |
153
|
|
|
'year' => '1991', |
154
|
|
|
'_author' => [ |
155
|
|
|
'id' => 1, |
156
|
|
|
'name' => 'Dave', |
157
|
|
|
], |
158
|
|
|
]; |
159
|
|
|
|
160
|
|
|
$resource = new Item($bookData, new JsonApiBookTransformer(), 'books'); |
161
|
|
|
|
162
|
|
|
$scope = new Scope($this->manager, $resource); |
163
|
|
|
|
164
|
|
|
$expected = [ |
165
|
|
|
'data' => [ |
166
|
|
|
'type' => 'books', |
167
|
|
|
'id' => '1', |
168
|
|
|
'attributes' => [ |
169
|
|
|
'title' => 'Foo', |
170
|
|
|
'year' => 1991, |
171
|
|
|
], |
172
|
|
|
'relationships' => [ |
173
|
|
|
'author-with-meta' => [ |
174
|
|
|
'data' => [ |
175
|
|
|
'type' => 'people', |
176
|
|
|
'id' => '1', |
177
|
|
|
], |
178
|
|
|
'meta' => [ 'foo' => 'bar' ], |
179
|
|
|
], |
180
|
|
|
], |
181
|
|
|
], |
182
|
|
|
'included' => [ |
183
|
|
|
[ |
184
|
|
|
'type' => 'people', |
185
|
|
|
'id' => '1', |
186
|
|
|
'attributes' => [ |
187
|
|
|
'name' => 'Dave', |
188
|
|
|
], |
189
|
|
|
], |
190
|
|
|
], |
191
|
|
|
]; |
192
|
|
|
|
193
|
|
|
$this->assertSame($expected, $scope->toArray()); |
194
|
|
|
|
195
|
|
|
$expectedJson = '{"data":{"type":"books","id":"1","attributes":{"title":"Foo","year":1991},"relationships":{"author-with-meta":{"data":{"type":"people","id":"1"},"meta":{"foo":"bar"}}}},"included":[{"type":"people","id":"1","attributes":{"name":"Dave"}}]}'; |
196
|
|
|
$this->assertSame($expectedJson, $scope->toJson()); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
View Code Duplication |
public function testSerializingItemResourceWithHasOneDasherizedInclude() : void |
|
|
|
|
200
|
|
|
{ |
201
|
|
|
$this->manager->parseIncludes('co-author'); |
202
|
|
|
|
203
|
|
|
$bookData = [ |
204
|
|
|
'id' => 1, |
205
|
|
|
'title' => 'Foo', |
206
|
|
|
'year' => '1991', |
207
|
|
|
'_author' => [ |
208
|
|
|
'id' => 1, |
209
|
|
|
'name' => 'Dave', |
210
|
|
|
], |
211
|
|
|
'_co_author' => [ |
212
|
|
|
'id' => 2, |
213
|
|
|
'name' => 'Jim', |
214
|
|
|
], |
215
|
|
|
]; |
216
|
|
|
|
217
|
|
|
$resource = new Item($bookData, new JsonApiBookTransformer(), 'books'); |
218
|
|
|
|
219
|
|
|
$scope = new Scope($this->manager, $resource); |
220
|
|
|
|
221
|
|
|
$expected = [ |
222
|
|
|
'data' => [ |
223
|
|
|
'type' => 'books', |
224
|
|
|
'id' => '1', |
225
|
|
|
'attributes' => [ |
226
|
|
|
'title' => 'Foo', |
227
|
|
|
'year' => 1991, |
228
|
|
|
], |
229
|
|
|
'relationships' => [ |
230
|
|
|
'co-author' => [ |
231
|
|
|
'data' => [ |
232
|
|
|
'type' => 'people', |
233
|
|
|
'id' => '2', |
234
|
|
|
], |
235
|
|
|
], |
236
|
|
|
], |
237
|
|
|
], |
238
|
|
|
'included' => [ |
239
|
|
|
[ |
240
|
|
|
'type' => 'people', |
241
|
|
|
'id' => '2', |
242
|
|
|
'attributes' => [ |
243
|
|
|
'name' => 'Jim', |
244
|
|
|
], |
245
|
|
|
], |
246
|
|
|
], |
247
|
|
|
]; |
248
|
|
|
|
249
|
|
|
$this->assertSame($expected, $scope->toArray()); |
250
|
|
|
|
251
|
|
|
$expectedJson = '{"data":{"type":"books","id":"1","attributes":{"title":"Foo","year":1991},"relationships":{"co-author":{"data":{"type":"people","id":"2"}}}},"included":[{"type":"people","id":"2","attributes":{"name":"Jim"}}]}'; |
252
|
|
|
$this->assertSame($expectedJson, $scope->toJson()); |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
public function testSerializingItemResourceWithEmptyHasOneInclude() : void |
256
|
|
|
{ |
257
|
|
|
$this->manager->parseIncludes('author'); |
258
|
|
|
|
259
|
|
|
$bookData = [ |
260
|
|
|
'id' => 1, |
261
|
|
|
'title' => 'Foo', |
262
|
|
|
'year' => '1991', |
263
|
|
|
'_author' => null, |
264
|
|
|
]; |
265
|
|
|
|
266
|
|
|
$resource = new Item($bookData, new JsonApiBookTransformer(), 'books'); |
267
|
|
|
|
268
|
|
|
$scope = new Scope($this->manager, $resource); |
269
|
|
|
|
270
|
|
|
$expected = [ |
271
|
|
|
'data' => [ |
272
|
|
|
'type' => 'books', |
273
|
|
|
'id' => '1', |
274
|
|
|
'attributes' => [ |
275
|
|
|
'title' => 'Foo', |
276
|
|
|
'year' => 1991, |
277
|
|
|
], |
278
|
|
|
'relationships' => [ |
279
|
|
|
'author' => [ |
280
|
|
|
'data' => null, |
281
|
|
|
], |
282
|
|
|
], |
283
|
|
|
], |
284
|
|
|
]; |
285
|
|
|
|
286
|
|
|
$this->assertSame($expected, $scope->toArray()); |
287
|
|
|
|
288
|
|
|
$expectedJson = '{"data":{"type":"books","id":"1","attributes":{"title":"Foo","year":1991},"relationships":{"author":{"data":null}}}}'; |
289
|
|
|
$this->assertSame($expectedJson, $scope->toJson()); |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
public function testSerializingItemResourceWithHasManyInclude() |
293
|
|
|
{ |
294
|
|
|
$this->manager->parseIncludes('published'); |
295
|
|
|
|
296
|
|
|
$authorData = [ |
297
|
|
|
'id' => 1, |
298
|
|
|
'name' => 'Dave', |
299
|
|
|
'_published' => [ |
300
|
|
|
[ |
301
|
|
|
'id' => 1, |
302
|
|
|
'title' => 'Foo', |
303
|
|
|
'year' => '1991', |
304
|
|
|
], |
305
|
|
|
[ |
306
|
|
|
'id' => 2, |
307
|
|
|
'title' => 'Bar', |
308
|
|
|
'year' => '2015', |
309
|
|
|
], |
310
|
|
|
], |
311
|
|
|
]; |
312
|
|
|
|
313
|
|
|
$resource = new Item($authorData, new JsonApiAuthorTransformer(), 'people'); |
314
|
|
|
|
315
|
|
|
$scope = new Scope($this->manager, $resource); |
316
|
|
|
|
317
|
|
|
$expected = [ |
318
|
|
|
'data' => [ |
319
|
|
|
'type' => 'people', |
320
|
|
|
'id' => '1', |
321
|
|
|
'attributes' => [ |
322
|
|
|
'name' => 'Dave', |
323
|
|
|
], |
324
|
|
|
'relationships' => [ |
325
|
|
|
'published' => [ |
326
|
|
|
'data' => [ |
327
|
|
|
[ |
328
|
|
|
'type' => 'books', |
329
|
|
|
'id' => 1, |
330
|
|
|
], |
331
|
|
|
[ |
332
|
|
|
'type' => 'books', |
333
|
|
|
'id' => 2, |
334
|
|
|
], |
335
|
|
|
], |
336
|
|
|
], |
337
|
|
|
], |
338
|
|
|
], |
339
|
|
|
'included' => [ |
340
|
|
|
[ |
341
|
|
|
'type' => 'books', |
342
|
|
|
'id' => '1', |
343
|
|
|
'attributes' => [ |
344
|
|
|
'title' => 'Foo', |
345
|
|
|
'year' => 1991, |
346
|
|
|
], |
347
|
|
|
], |
348
|
|
|
[ |
349
|
|
|
'type' => 'books', |
350
|
|
|
'id' => '2', |
351
|
|
|
'attributes' => [ |
352
|
|
|
'title' => 'Bar', |
353
|
|
|
'year' => 2015, |
354
|
|
|
], |
355
|
|
|
], |
356
|
|
|
], |
357
|
|
|
]; |
358
|
|
|
|
359
|
|
|
$this->assertEquals($expected, $scope->toArray()); |
360
|
|
|
|
361
|
|
|
$expectedJson = '{"data":{"type":"people","id":"1","attributes":{"name":"Dave"},"relationships":{"published":{"data":[{"type":"books","id":"1"},{"type":"books","id":"2"}]}}},"included":[{"type":"books","id":"1","attributes":{"title":"Foo","year":1991}},{"type":"books","id":"2","attributes":{"title":"Bar","year":2015}}]}'; |
362
|
|
|
$this->assertSame($expectedJson, $scope->toJson()); |
363
|
|
|
} |
364
|
|
|
|
365
|
|
|
public function testSerializingItemResourceWithEmptyHasManyInclude() : void |
366
|
|
|
{ |
367
|
|
|
$this->manager->parseIncludes('published'); |
368
|
|
|
|
369
|
|
|
$authorData = [ |
370
|
|
|
'id' => 1, |
371
|
|
|
'name' => 'Dave', |
372
|
|
|
'_published' => [], |
373
|
|
|
]; |
374
|
|
|
|
375
|
|
|
$resource = new Item($authorData, new JsonApiAuthorTransformer(), 'people'); |
376
|
|
|
|
377
|
|
|
$scope = new Scope($this->manager, $resource); |
378
|
|
|
|
379
|
|
|
$expected = [ |
380
|
|
|
'data' => [ |
381
|
|
|
'type' => 'people', |
382
|
|
|
'id' => '1', |
383
|
|
|
'attributes' => [ |
384
|
|
|
'name' => 'Dave', |
385
|
|
|
], |
386
|
|
|
'relationships' => [ |
387
|
|
|
'published' => [ |
388
|
|
|
'data' => [], |
389
|
|
|
], |
390
|
|
|
], |
391
|
|
|
], |
392
|
|
|
]; |
393
|
|
|
|
394
|
|
|
$this->assertSame($expected, $scope->toArray()); |
395
|
|
|
|
396
|
|
|
$expectedJson = '{"data":{"type":"people","id":"1","attributes":{"name":"Dave"},"relationships":{"published":{"data":[]}}}}'; |
397
|
|
|
$this->assertSame($expectedJson, $scope->toJson()); |
398
|
|
|
} |
399
|
|
|
|
400
|
|
|
public function testSerializingItemResourceWithoutIncludes() |
401
|
|
|
{ |
402
|
|
|
$bookData = [ |
403
|
|
|
'id' => 1, |
404
|
|
|
'title' => 'Foo', |
405
|
|
|
'year' => '1991', |
406
|
|
|
'_author' => [ |
407
|
|
|
'id' => 1, |
408
|
|
|
'name' => 'Dave', |
409
|
|
|
], |
410
|
|
|
]; |
411
|
|
|
|
412
|
|
|
$resource = new Item($bookData, new JsonApiBookTransformer(), 'books'); |
413
|
|
|
|
414
|
|
|
$scope = new Scope($this->manager, $resource); |
415
|
|
|
|
416
|
|
|
$expected = [ |
417
|
|
|
'data' => [ |
418
|
|
|
'type' => 'books', |
419
|
|
|
'id' => '1', |
420
|
|
|
'attributes' => [ |
421
|
|
|
'title' => 'Foo', |
422
|
|
|
'year' => 1991, |
423
|
|
|
], |
424
|
|
|
], |
425
|
|
|
]; |
426
|
|
|
|
427
|
|
|
$this->assertSame($expected, $scope->toArray()); |
428
|
|
|
|
429
|
|
|
$expectedJson = '{"data":{"type":"books","id":"1","attributes":{"title":"Foo","year":1991}}}'; |
430
|
|
|
$this->assertSame($expectedJson, $scope->toJson()); |
431
|
|
|
} |
432
|
|
|
|
433
|
|
|
public function testSerializingItemResourceWithMeta() |
434
|
|
|
{ |
435
|
|
|
$bookData = [ |
436
|
|
|
'id' => 1, |
437
|
|
|
'title' => 'Foo', |
438
|
|
|
'year' => '1991', |
439
|
|
|
'_author' => [ |
440
|
|
|
'id' => 1, |
441
|
|
|
'name' => 'Dave', |
442
|
|
|
], |
443
|
|
|
]; |
444
|
|
|
|
445
|
|
|
$resource = new Item($bookData, new JsonApiBookTransformer(), 'books'); |
446
|
|
|
$resource->setMetaValue('foo', 'bar'); |
447
|
|
|
|
448
|
|
|
$scope = new Scope($this->manager, $resource); |
449
|
|
|
|
450
|
|
|
$expected = [ |
451
|
|
|
'data' => [ |
452
|
|
|
'type' => 'books', |
453
|
|
|
'id' => '1', |
454
|
|
|
'attributes' => [ |
455
|
|
|
'title' => 'Foo', |
456
|
|
|
'year' => 1991, |
457
|
|
|
], |
458
|
|
|
], |
459
|
|
|
'meta' => [ |
460
|
|
|
'foo' => 'bar', |
461
|
|
|
], |
462
|
|
|
]; |
463
|
|
|
|
464
|
|
|
$this->assertSame($expected, $scope->toArray()); |
465
|
|
|
|
466
|
|
|
$expectedJson = '{"data":{"type":"books","id":"1","attributes":{"title":"Foo","year":1991}},"meta":{"foo":"bar"}}'; |
467
|
|
|
$this->assertSame($expectedJson, $scope->toJson()); |
468
|
|
|
} |
469
|
|
|
|
470
|
|
|
public function testSerializingItemResourceWithMetaInBody() : void |
471
|
|
|
{ |
472
|
|
|
$bookData = [ |
473
|
|
|
'id' => 1, |
474
|
|
|
'title' => 'Foo', |
475
|
|
|
'year' => '1991', |
476
|
|
|
'_author' => [ |
477
|
|
|
'id' => 1, |
478
|
|
|
'name' => 'Dave', |
479
|
|
|
], |
480
|
|
|
'meta' => [ |
481
|
|
|
'something' => 'something' |
482
|
|
|
] |
483
|
|
|
]; |
484
|
|
|
|
485
|
|
|
$resource = new Item($bookData, new JsonApiBookTransformer(), 'books'); |
486
|
|
|
$resource->setMetaValue('foo', 'bar'); |
487
|
|
|
|
488
|
|
|
$scope = new Scope($this->manager, $resource); |
489
|
|
|
|
490
|
|
|
$expected = [ |
491
|
|
|
'data' => [ |
492
|
|
|
'type' => 'books', |
493
|
|
|
'id' => '1', |
494
|
|
|
'attributes' => [ |
495
|
|
|
'title' => 'Foo', |
496
|
|
|
'year' => 1991, |
497
|
|
|
], |
498
|
|
|
'meta' => [ |
499
|
|
|
'something' => 'something' |
500
|
|
|
] |
501
|
|
|
], |
502
|
|
|
'meta' => [ |
503
|
|
|
'foo' => 'bar' |
504
|
|
|
], |
505
|
|
|
]; |
506
|
|
|
|
507
|
|
|
$this->assertSame($expected, $scope->toArray()); |
508
|
|
|
|
509
|
|
|
$expectedJson = '{"data":{"type":"books","id":"1","attributes":{"title":"Foo","year":1991},"meta":{"something":"something"}},"meta":{"foo":"bar"}}'; |
510
|
|
|
$this->assertSame($expectedJson, $scope->toJson()); |
511
|
|
|
} |
512
|
|
|
|
513
|
|
View Code Duplication |
public function testSerializingCollectionResourceWithoutIncludes() : void |
|
|
|
|
514
|
|
|
{ |
515
|
|
|
$booksData = [ |
516
|
|
|
[ |
517
|
|
|
'id' => 1, |
518
|
|
|
'title' => 'Foo', |
519
|
|
|
'year' => '1991', |
520
|
|
|
'_author' => [ |
521
|
|
|
'id' => 1, |
522
|
|
|
'name' => 'Dave', |
523
|
|
|
], |
524
|
|
|
], |
525
|
|
|
[ |
526
|
|
|
'id' => 2, |
527
|
|
|
'title' => 'Bar', |
528
|
|
|
'year' => '1997', |
529
|
|
|
'_author' => [ |
530
|
|
|
'id' => 2, |
531
|
|
|
'name' => 'Bob', |
532
|
|
|
], |
533
|
|
|
], |
534
|
|
|
]; |
535
|
|
|
|
536
|
|
|
$resource = new Collection($booksData, new JsonApiBookTransformer(), 'books'); |
537
|
|
|
$scope = new Scope($this->manager, $resource); |
538
|
|
|
|
539
|
|
|
$expected = [ |
540
|
|
|
'data' => [ |
541
|
|
|
[ |
542
|
|
|
'type' => 'books', |
543
|
|
|
'id' => '1', |
544
|
|
|
'attributes' => [ |
545
|
|
|
'title' => 'Foo', |
546
|
|
|
'year' => 1991, |
547
|
|
|
], |
548
|
|
|
], |
549
|
|
|
[ |
550
|
|
|
'type' => 'books', |
551
|
|
|
'id' => '2', |
552
|
|
|
'attributes' => [ |
553
|
|
|
'title' => 'Bar', |
554
|
|
|
'year' => 1997, |
555
|
|
|
], |
556
|
|
|
], |
557
|
|
|
], |
558
|
|
|
]; |
559
|
|
|
|
560
|
|
|
$this->assertSame($expected, $scope->toArray()); |
561
|
|
|
|
562
|
|
|
$expectedJson = '{"data":[{"type":"books","id":"1","attributes":{"title":"Foo","year":1991}},{"type":"books","id":"2","attributes":{"title":"Bar","year":1997}}]}'; |
563
|
|
|
$this->assertSame($expectedJson, $scope->toJson()); |
564
|
|
|
} |
565
|
|
|
|
566
|
|
View Code Duplication |
public function testSerializingCollectionResourceWithHasOneInclude() : void |
|
|
|
|
567
|
|
|
{ |
568
|
|
|
$this->manager->parseIncludes('author'); |
569
|
|
|
|
570
|
|
|
$booksData = [ |
571
|
|
|
[ |
572
|
|
|
'id' => 1, |
573
|
|
|
'title' => 'Foo', |
574
|
|
|
'year' => '1991', |
575
|
|
|
'_author' => [ |
576
|
|
|
'id' => 1, |
577
|
|
|
'name' => 'Dave', |
578
|
|
|
], |
579
|
|
|
], |
580
|
|
|
[ |
581
|
|
|
'id' => 2, |
582
|
|
|
'title' => 'Bar', |
583
|
|
|
'year' => '1997', |
584
|
|
|
'_author' => [ |
585
|
|
|
'id' => 2, |
586
|
|
|
'name' => 'Bob', |
587
|
|
|
], |
588
|
|
|
], |
589
|
|
|
]; |
590
|
|
|
|
591
|
|
|
$resource = new Collection($booksData, new JsonApiBookTransformer(), 'books'); |
592
|
|
|
$scope = new Scope($this->manager, $resource); |
593
|
|
|
|
594
|
|
|
$expected = [ |
595
|
|
|
'data' => [ |
596
|
|
|
[ |
597
|
|
|
'type' => 'books', |
598
|
|
|
'id' => '1', |
599
|
|
|
'attributes' => [ |
600
|
|
|
'title' => 'Foo', |
601
|
|
|
'year' => 1991, |
602
|
|
|
], |
603
|
|
|
'relationships' => [ |
604
|
|
|
'author' => [ |
605
|
|
|
'data' => [ |
606
|
|
|
'type' => 'people', |
607
|
|
|
'id' => '1', |
608
|
|
|
], |
609
|
|
|
], |
610
|
|
|
], |
611
|
|
|
], |
612
|
|
|
[ |
613
|
|
|
'type' => 'books', |
614
|
|
|
'id' => '2', |
615
|
|
|
'attributes' => [ |
616
|
|
|
'title' => 'Bar', |
617
|
|
|
'year' => 1997, |
618
|
|
|
], |
619
|
|
|
'relationships' => [ |
620
|
|
|
'author' => [ |
621
|
|
|
'data' => [ |
622
|
|
|
'type' => 'people', |
623
|
|
|
'id' => '2', |
624
|
|
|
], |
625
|
|
|
], |
626
|
|
|
], |
627
|
|
|
], |
628
|
|
|
], |
629
|
|
|
'included' => [ |
630
|
|
|
[ |
631
|
|
|
'type' => 'people', |
632
|
|
|
'id' => '1', |
633
|
|
|
'attributes' => [ |
634
|
|
|
'name' => 'Dave', |
635
|
|
|
], |
636
|
|
|
], |
637
|
|
|
[ |
638
|
|
|
'type' => 'people', |
639
|
|
|
'id' => '2', |
640
|
|
|
'attributes' => [ |
641
|
|
|
'name' => 'Bob', |
642
|
|
|
], |
643
|
|
|
], |
644
|
|
|
], |
645
|
|
|
]; |
646
|
|
|
|
647
|
|
|
$this->assertSame($expected, $scope->toArray()); |
648
|
|
|
|
649
|
|
|
$expectedJson = '{"data":[{"type":"books","id":"1","attributes":{"title":"Foo","year":1991},"relationships":{"author":{"data":{"type":"people","id":"1"}}}},{"type":"books","id":"2","attributes":{"title":"Bar","year":1997},"relationships":{"author":{"data":{"type":"people","id":"2"}}}}],"included":[{"type":"people","id":"1","attributes":{"name":"Dave"}},{"type":"people","id":"2","attributes":{"name":"Bob"}}]}'; |
650
|
|
|
$this->assertSame($expectedJson, $scope->toJson()); |
651
|
|
|
} |
652
|
|
|
|
653
|
|
|
public function testSerializingCollectionResourceWithEmptyHasOneInclude() : void |
654
|
|
|
{ |
655
|
|
|
$this->manager->parseIncludes('author'); |
656
|
|
|
|
657
|
|
|
$booksData = [ |
658
|
|
|
[ |
659
|
|
|
'id' => 1, |
660
|
|
|
'title' => 'Foo', |
661
|
|
|
'year' => '1991', |
662
|
|
|
'_author' => null, |
663
|
|
|
], |
664
|
|
|
[ |
665
|
|
|
'id' => 2, |
666
|
|
|
'title' => 'Bar', |
667
|
|
|
'year' => '1997', |
668
|
|
|
'_author' => [ |
669
|
|
|
'id' => 2, |
670
|
|
|
'name' => 'Bob', |
671
|
|
|
], |
672
|
|
|
], |
673
|
|
|
]; |
674
|
|
|
|
675
|
|
|
$resource = new Collection($booksData, new JsonApiBookTransformer(), 'books'); |
676
|
|
|
$scope = new Scope($this->manager, $resource); |
677
|
|
|
|
678
|
|
|
$expected = [ |
679
|
|
|
'data' => [ |
680
|
|
|
[ |
681
|
|
|
'type' => 'books', |
682
|
|
|
'id' => '1', |
683
|
|
|
'attributes' => [ |
684
|
|
|
'title' => 'Foo', |
685
|
|
|
'year' => 1991, |
686
|
|
|
], |
687
|
|
|
'relationships' => [ |
688
|
|
|
'author' => [ |
689
|
|
|
'data' => null, |
690
|
|
|
], |
691
|
|
|
], |
692
|
|
|
], |
693
|
|
|
[ |
694
|
|
|
'type' => 'books', |
695
|
|
|
'id' => '2', |
696
|
|
|
'attributes' => [ |
697
|
|
|
'title' => 'Bar', |
698
|
|
|
'year' => 1997, |
699
|
|
|
], |
700
|
|
|
'relationships' => [ |
701
|
|
|
'author' => [ |
702
|
|
|
'data' => [ |
703
|
|
|
'type' => 'people', |
704
|
|
|
'id' => '2', |
705
|
|
|
], |
706
|
|
|
], |
707
|
|
|
], |
708
|
|
|
], |
709
|
|
|
], |
710
|
|
|
'included' => [ |
711
|
|
|
[ |
712
|
|
|
'type' => 'people', |
713
|
|
|
'id' => '2', |
714
|
|
|
'attributes' => [ |
715
|
|
|
'name' => 'Bob', |
716
|
|
|
], |
717
|
|
|
], |
718
|
|
|
], |
719
|
|
|
]; |
720
|
|
|
|
721
|
|
|
$this->assertSame($expected, $scope->toArray()); |
722
|
|
|
|
723
|
|
|
$expectedJson = '{"data":[{"type":"books","id":"1","attributes":{"title":"Foo","year":1991},"relationships":{"author":{"data":null}}},{"type":"books","id":"2","attributes":{"title":"Bar","year":1997},"relationships":{"author":{"data":{"type":"people","id":"2"}}}}],"included":[{"type":"people","id":"2","attributes":{"name":"Bob"}}]}'; |
724
|
|
|
$this->assertSame($expectedJson, $scope->toJson()); |
725
|
|
|
} |
726
|
|
|
|
727
|
|
|
public function testSerializingCollectionResourceWithHasManyInclude() : void |
728
|
|
|
{ |
729
|
|
|
$this->manager->parseIncludes('published'); |
730
|
|
|
|
731
|
|
|
$authorsData = [ |
732
|
|
|
[ |
733
|
|
|
'id' => 1, |
734
|
|
|
'name' => 'Dave', |
735
|
|
|
'_published' => [ |
736
|
|
|
[ |
737
|
|
|
'id' => 1, |
738
|
|
|
'title' => 'Foo', |
739
|
|
|
'year' => '1991', |
740
|
|
|
], |
741
|
|
|
[ |
742
|
|
|
'id' => 2, |
743
|
|
|
'title' => 'Bar', |
744
|
|
|
'year' => '2015', |
745
|
|
|
], |
746
|
|
|
], |
747
|
|
|
], |
748
|
|
|
[ |
749
|
|
|
'id' => 2, |
750
|
|
|
'name' => 'Bob', |
751
|
|
|
'_published' => [ |
752
|
|
|
[ |
753
|
|
|
'id' => 3, |
754
|
|
|
'title' => 'Baz', |
755
|
|
|
'year' => '1995', |
756
|
|
|
], |
757
|
|
|
[ |
758
|
|
|
'id' => 4, |
759
|
|
|
'title' => 'Quux', |
760
|
|
|
'year' => '2000', |
761
|
|
|
], |
762
|
|
|
], |
763
|
|
|
], |
764
|
|
|
]; |
765
|
|
|
|
766
|
|
|
$resource = new Collection($authorsData, new JsonApiAuthorTransformer(), 'people'); |
767
|
|
|
$scope = new Scope($this->manager, $resource); |
768
|
|
|
|
769
|
|
|
$expected = [ |
770
|
|
|
'data' => [ |
771
|
|
|
[ |
772
|
|
|
'type' => 'people', |
773
|
|
|
'id' => '1', |
774
|
|
|
'attributes' => [ |
775
|
|
|
'name' => 'Dave', |
776
|
|
|
], |
777
|
|
|
'relationships' => [ |
778
|
|
|
'published' => [ |
779
|
|
|
'data' => [ |
780
|
|
|
[ |
781
|
|
|
'type' => 'books', |
782
|
|
|
'id' => 1, |
783
|
|
|
], |
784
|
|
|
[ |
785
|
|
|
'type' => 'books', |
786
|
|
|
'id' => 2, |
787
|
|
|
], |
788
|
|
|
], |
789
|
|
|
], |
790
|
|
|
], |
791
|
|
|
], |
792
|
|
|
[ |
793
|
|
|
'type' => 'people', |
794
|
|
|
'id' => '2', |
795
|
|
|
'attributes' => [ |
796
|
|
|
'name' => 'Bob', |
797
|
|
|
], |
798
|
|
|
'relationships' => [ |
799
|
|
|
'published' => [ |
800
|
|
|
'data' => [ |
801
|
|
|
[ |
802
|
|
|
'type' => 'books', |
803
|
|
|
'id' => 3, |
804
|
|
|
], |
805
|
|
|
[ |
806
|
|
|
'type' => 'books', |
807
|
|
|
'id' => 4, |
808
|
|
|
], |
809
|
|
|
], |
810
|
|
|
], |
811
|
|
|
], |
812
|
|
|
], |
813
|
|
|
], |
814
|
|
|
'included' => [ |
815
|
|
|
[ |
816
|
|
|
'type' => 'books', |
817
|
|
|
'id' => '1', |
818
|
|
|
'attributes' => [ |
819
|
|
|
'title' => 'Foo', |
820
|
|
|
'year' => 1991, |
821
|
|
|
], |
822
|
|
|
], |
823
|
|
|
[ |
824
|
|
|
'type' => 'books', |
825
|
|
|
'id' => '2', |
826
|
|
|
'attributes' => [ |
827
|
|
|
'title' => 'Bar', |
828
|
|
|
'year' => 2015, |
829
|
|
|
], |
830
|
|
|
], |
831
|
|
|
[ |
832
|
|
|
'type' => 'books', |
833
|
|
|
'id' => '3', |
834
|
|
|
'attributes' => [ |
835
|
|
|
'title' => 'Baz', |
836
|
|
|
'year' => 1995, |
837
|
|
|
], |
838
|
|
|
], |
839
|
|
|
[ |
840
|
|
|
'type' => 'books', |
841
|
|
|
'id' => '4', |
842
|
|
|
'attributes' => [ |
843
|
|
|
'title' => 'Quux', |
844
|
|
|
'year' => 2000, |
845
|
|
|
], |
846
|
|
|
], |
847
|
|
|
], |
848
|
|
|
]; |
849
|
|
|
|
850
|
|
|
$this->assertEquals($expected, $scope->toArray()); |
851
|
|
|
|
852
|
|
|
$expectedJson = '{"data":[{"type":"people","id":"1","attributes":{"name":"Dave"},"relationships":{"published":{"data":[{"type":"books","id":"1"},{"type":"books","id":"2"}]}}},{"type":"people","id":"2","attributes":{"name":"Bob"},"relationships":{"published":{"data":[{"type":"books","id":"3"},{"type":"books","id":"4"}]}}}],"included":[{"type":"books","id":"1","attributes":{"title":"Foo","year":1991}},{"type":"books","id":"2","attributes":{"title":"Bar","year":2015}},{"type":"books","id":"3","attributes":{"title":"Baz","year":1995}},{"type":"books","id":"4","attributes":{"title":"Quux","year":2000}}]}'; |
853
|
|
|
$this->assertSame($expectedJson, $scope->toJson()); |
854
|
|
|
} |
855
|
|
|
|
856
|
|
|
public function testSerializingCollectionResourceWithEmptyHasManyInclude() : void |
857
|
|
|
{ |
858
|
|
|
$this->manager->parseIncludes('published'); |
859
|
|
|
|
860
|
|
|
$authorsData = [ |
861
|
|
|
[ |
862
|
|
|
'id' => 1, |
863
|
|
|
'name' => 'Dave', |
864
|
|
|
'_published' => [], |
865
|
|
|
], |
866
|
|
|
[ |
867
|
|
|
'id' => 2, |
868
|
|
|
'name' => 'Bob', |
869
|
|
|
'_published' => [ |
870
|
|
|
[ |
871
|
|
|
'id' => 3, |
872
|
|
|
'title' => 'Baz', |
873
|
|
|
'year' => '1995', |
874
|
|
|
], |
875
|
|
|
], |
876
|
|
|
], |
877
|
|
|
]; |
878
|
|
|
|
879
|
|
|
$resource = new Collection($authorsData, new JsonApiAuthorTransformer(), 'people'); |
880
|
|
|
$scope = new Scope($this->manager, $resource); |
881
|
|
|
|
882
|
|
|
$expected = [ |
883
|
|
|
'data' => [ |
884
|
|
|
[ |
885
|
|
|
'type' => 'people', |
886
|
|
|
'id' => '1', |
887
|
|
|
'attributes' => [ |
888
|
|
|
'name' => 'Dave', |
889
|
|
|
], |
890
|
|
|
'relationships' => [ |
891
|
|
|
'published' => [ |
892
|
|
|
'data' => [], |
893
|
|
|
], |
894
|
|
|
], |
895
|
|
|
], |
896
|
|
|
[ |
897
|
|
|
'type' => 'people', |
898
|
|
|
'id' => '2', |
899
|
|
|
'attributes' => [ |
900
|
|
|
'name' => 'Bob', |
901
|
|
|
], |
902
|
|
|
'relationships' => [ |
903
|
|
|
'published' => [ |
904
|
|
|
'data' => [ |
905
|
|
|
[ |
906
|
|
|
'type' => 'books', |
907
|
|
|
'id' => 3, |
908
|
|
|
], |
909
|
|
|
], |
910
|
|
|
], |
911
|
|
|
], |
912
|
|
|
], |
913
|
|
|
], |
914
|
|
|
'included' => [ |
915
|
|
|
[ |
916
|
|
|
'type' => 'books', |
917
|
|
|
'id' => '3', |
918
|
|
|
'attributes' => [ |
919
|
|
|
'title' => 'Baz', |
920
|
|
|
'year' => 1995, |
921
|
|
|
], |
922
|
|
|
], |
923
|
|
|
], |
924
|
|
|
]; |
925
|
|
|
|
926
|
|
|
$this->assertEquals($expected, $scope->toArray()); |
927
|
|
|
|
928
|
|
|
$expectedJson = '{"data":[{"type":"people","id":"1","attributes":{"name":"Dave"},"relationships":{"published":{"data":[]}}},{"type":"people","id":"2","attributes":{"name":"Bob"},"relationships":{"published":{"data":[{"type":"books","id":"3"}]}}}],"included":[{"type":"books","id":"3","attributes":{"title":"Baz","year":1995}}]}'; |
929
|
|
|
$this->assertSame($expectedJson, $scope->toJson()); |
930
|
|
|
} |
931
|
|
|
|
932
|
|
View Code Duplication |
public function testSerializingCollectionResourceWithMeta() : void |
|
|
|
|
933
|
|
|
{ |
934
|
|
|
$booksData = [ |
935
|
|
|
[ |
936
|
|
|
'id' => 1, |
937
|
|
|
'title' => 'Foo', |
938
|
|
|
'year' => '1991', |
939
|
|
|
'_author' => [ |
940
|
|
|
'name' => 'Dave', |
941
|
|
|
], |
942
|
|
|
], |
943
|
|
|
[ |
944
|
|
|
'id' => 2, |
945
|
|
|
'title' => 'Bar', |
946
|
|
|
'year' => '1997', |
947
|
|
|
'_author' => [ |
948
|
|
|
'name' => 'Bob', |
949
|
|
|
], |
950
|
|
|
], |
951
|
|
|
]; |
952
|
|
|
|
953
|
|
|
$resource = new Collection($booksData, new JsonApiBookTransformer(), 'books'); |
954
|
|
|
$resource->setMetaValue('foo', 'bar'); |
955
|
|
|
|
956
|
|
|
$scope = new Scope($this->manager, $resource); |
957
|
|
|
|
958
|
|
|
$expected = [ |
959
|
|
|
'data' => [ |
960
|
|
|
[ |
961
|
|
|
'type' => 'books', |
962
|
|
|
'id' => '1', |
963
|
|
|
'attributes' => [ |
964
|
|
|
'title' => 'Foo', |
965
|
|
|
'year' => 1991, |
966
|
|
|
], |
967
|
|
|
], |
968
|
|
|
[ |
969
|
|
|
'type' => 'books', |
970
|
|
|
'id' => '2', |
971
|
|
|
'attributes' => [ |
972
|
|
|
'title' => 'Bar', |
973
|
|
|
'year' => 1997, |
974
|
|
|
], |
975
|
|
|
], |
976
|
|
|
], |
977
|
|
|
'meta' => [ |
978
|
|
|
'foo' => 'bar', |
979
|
|
|
], |
980
|
|
|
]; |
981
|
|
|
|
982
|
|
|
$this->assertSame($expected, $scope->toArray()); |
983
|
|
|
|
984
|
|
|
$expectedJson = '{"data":[{"type":"books","id":"1","attributes":{"title":"Foo","year":1991}},{"type":"books","id":"2","attributes":{"title":"Bar","year":1997}}],"meta":{"foo":"bar"}}'; |
985
|
|
|
$this->assertSame($expectedJson, $scope->toJson()); |
986
|
|
|
} |
987
|
|
|
|
988
|
|
|
public function testSerializingCollectionResourceWithDuplicatedIncludeData() : void |
989
|
|
|
{ |
990
|
|
|
$this->manager->parseIncludes('author'); |
991
|
|
|
|
992
|
|
|
$booksData = [ |
993
|
|
|
[ |
994
|
|
|
'id' => 1, |
995
|
|
|
'title' => 'Foo', |
996
|
|
|
'year' => '1991', |
997
|
|
|
'_author' => [ |
998
|
|
|
'id' => 1, |
999
|
|
|
'name' => 'Dave', |
1000
|
|
|
], |
1001
|
|
|
], |
1002
|
|
|
[ |
1003
|
|
|
'id' => 2, |
1004
|
|
|
'title' => 'Bar', |
1005
|
|
|
'year' => '1997', |
1006
|
|
|
'_author' => [ |
1007
|
|
|
'id' => 1, |
1008
|
|
|
'name' => 'Dave', |
1009
|
|
|
], |
1010
|
|
|
], |
1011
|
|
|
]; |
1012
|
|
|
|
1013
|
|
|
$resource = new Collection($booksData, new JsonApiBookTransformer(), 'books'); |
1014
|
|
|
$scope = new Scope($this->manager, $resource); |
1015
|
|
|
|
1016
|
|
|
$expected = [ |
1017
|
|
|
'data' => [ |
1018
|
|
|
[ |
1019
|
|
|
'type' => 'books', |
1020
|
|
|
'id' => '1', |
1021
|
|
|
'attributes' => [ |
1022
|
|
|
'title' => 'Foo', |
1023
|
|
|
'year' => 1991, |
1024
|
|
|
], |
1025
|
|
|
'relationships' => [ |
1026
|
|
|
'author' => [ |
1027
|
|
|
'data' => [ |
1028
|
|
|
'type' => 'people', |
1029
|
|
|
'id' => '1', |
1030
|
|
|
], |
1031
|
|
|
], |
1032
|
|
|
], |
1033
|
|
|
], |
1034
|
|
|
[ |
1035
|
|
|
'type' => 'books', |
1036
|
|
|
'id' => '2', |
1037
|
|
|
'attributes' => [ |
1038
|
|
|
'title' => 'Bar', |
1039
|
|
|
'year' => 1997, |
1040
|
|
|
], |
1041
|
|
|
'relationships' => [ |
1042
|
|
|
'author' => [ |
1043
|
|
|
'data' => [ |
1044
|
|
|
'type' => 'people', |
1045
|
|
|
'id' => '1', |
1046
|
|
|
], |
1047
|
|
|
], |
1048
|
|
|
], |
1049
|
|
|
], |
1050
|
|
|
], |
1051
|
|
|
'included' => [ |
1052
|
|
|
[ |
1053
|
|
|
'type' => 'people', |
1054
|
|
|
'id' => '1', |
1055
|
|
|
'attributes' => [ |
1056
|
|
|
'name' => 'Dave', |
1057
|
|
|
], |
1058
|
|
|
], |
1059
|
|
|
], |
1060
|
|
|
]; |
1061
|
|
|
|
1062
|
|
|
$this->assertSame($expected, $scope->toArray()); |
1063
|
|
|
|
1064
|
|
|
$expectedJson = '{"data":[{"type":"books","id":"1","attributes":{"title":"Foo","year":1991},"relationships":{"author":{"data":{"type":"people","id":"1"}}}},{"type":"books","id":"2","attributes":{"title":"Bar","year":1997},"relationships":{"author":{"data":{"type":"people","id":"1"}}}}],"included":[{"type":"people","id":"1","attributes":{"name":"Dave"}}]}'; |
1065
|
|
|
$this->assertSame($expectedJson, $scope->toJson()); |
1066
|
|
|
} |
1067
|
|
|
|
1068
|
|
View Code Duplication |
public function testSerializingItemResourceWithNestedIncludes() : void |
|
|
|
|
1069
|
|
|
{ |
1070
|
|
|
$this->manager->parseIncludes(['author', 'author.published']); |
1071
|
|
|
|
1072
|
|
|
$bookData = [ |
1073
|
|
|
'id' => 1, |
1074
|
|
|
'title' => 'Foo', |
1075
|
|
|
'year' => '1991', |
1076
|
|
|
'_author' => [ |
1077
|
|
|
'id' => 1, |
1078
|
|
|
'name' => 'Dave', |
1079
|
|
|
'_published' => [ |
1080
|
|
|
[ |
1081
|
|
|
'id' => 1, |
1082
|
|
|
'title' => 'Foo', |
1083
|
|
|
'year' => '1991', |
1084
|
|
|
], |
1085
|
|
|
[ |
1086
|
|
|
'id' => 2, |
1087
|
|
|
'title' => 'Bar', |
1088
|
|
|
'year' => '2015', |
1089
|
|
|
], |
1090
|
|
|
], |
1091
|
|
|
], |
1092
|
|
|
]; |
1093
|
|
|
|
1094
|
|
|
$resource = new Item($bookData, new JsonApiBookTransformer(), 'books'); |
1095
|
|
|
|
1096
|
|
|
$scope = new Scope($this->manager, $resource); |
1097
|
|
|
|
1098
|
|
|
$expected = [ |
1099
|
|
|
'data' => [ |
1100
|
|
|
'type' => 'books', |
1101
|
|
|
'id' => '1', |
1102
|
|
|
'attributes' => [ |
1103
|
|
|
'title' => 'Foo', |
1104
|
|
|
'year' => 1991, |
1105
|
|
|
], |
1106
|
|
|
'relationships' => [ |
1107
|
|
|
'author' => [ |
1108
|
|
|
'data' => [ |
1109
|
|
|
'type' => 'people', |
1110
|
|
|
'id' => '1', |
1111
|
|
|
], |
1112
|
|
|
], |
1113
|
|
|
], |
1114
|
|
|
], |
1115
|
|
|
'included' => [ |
1116
|
|
|
[ |
1117
|
|
|
'type' => 'books', |
1118
|
|
|
'id' => '2', |
1119
|
|
|
'attributes' => [ |
1120
|
|
|
'title' => 'Bar', |
1121
|
|
|
'year' => 2015, |
1122
|
|
|
], |
1123
|
|
|
], |
1124
|
|
|
[ |
1125
|
|
|
'type' => 'people', |
1126
|
|
|
'id' => '1', |
1127
|
|
|
'attributes' => [ |
1128
|
|
|
'name' => 'Dave', |
1129
|
|
|
], |
1130
|
|
|
'relationships' => [ |
1131
|
|
|
'published' => [ |
1132
|
|
|
'data' => [ |
1133
|
|
|
[ |
1134
|
|
|
'type' => 'books', |
1135
|
|
|
'id' => '1', |
1136
|
|
|
], |
1137
|
|
|
[ |
1138
|
|
|
'type' => 'books', |
1139
|
|
|
'id' => '2', |
1140
|
|
|
], |
1141
|
|
|
], |
1142
|
|
|
], |
1143
|
|
|
], |
1144
|
|
|
], |
1145
|
|
|
], |
1146
|
|
|
]; |
1147
|
|
|
|
1148
|
|
|
$this->assertSame($expected, $scope->toArray()); |
1149
|
|
|
|
1150
|
|
|
$expectedJson = '{"data":{"type":"books","id":"1","attributes":{"title":"Foo","year":1991},"relationships":{"author":{"data":{"type":"people","id":"1"}}}},"included":[{"type":"books","id":"2","attributes":{"title":"Bar","year":2015}},{"type":"people","id":"1","attributes":{"name":"Dave"},"relationships":{"published":{"data":[{"type":"books","id":"1"},{"type":"books","id":"2"}]}}}]}'; |
1151
|
|
|
$this->assertSame($expectedJson, $scope->toJson()); |
1152
|
|
|
} |
1153
|
|
|
|
1154
|
|
|
public function testSerializingItemResourceWithSelfLink() : void |
1155
|
|
|
{ |
1156
|
|
|
$baseUrl = 'http://example.com'; |
1157
|
|
|
$this->manager->setSerializer(new JsonApiSerializer($baseUrl)); |
1158
|
|
|
|
1159
|
|
|
$bookData = [ |
1160
|
|
|
'id' => 1, |
1161
|
|
|
'title' => 'Foo', |
1162
|
|
|
'year' => '1991', |
1163
|
|
|
'_author' => [ |
1164
|
|
|
'id' => 1, |
1165
|
|
|
'name' => 'Dave', |
1166
|
|
|
], |
1167
|
|
|
]; |
1168
|
|
|
|
1169
|
|
|
$resource = new Item($bookData, new JsonApiBookTransformer(), 'books'); |
1170
|
|
|
|
1171
|
|
|
$scope = new Scope($this->manager, $resource); |
1172
|
|
|
|
1173
|
|
|
$expected = [ |
1174
|
|
|
'data' => [ |
1175
|
|
|
'type' => 'books', |
1176
|
|
|
'id' => '1', |
1177
|
|
|
'attributes' => [ |
1178
|
|
|
'title' => 'Foo', |
1179
|
|
|
'year' => 1991, |
1180
|
|
|
], |
1181
|
|
|
'links' => [ |
1182
|
|
|
'self' => 'http://example.com/books/1', |
1183
|
|
|
], |
1184
|
|
|
'relationships' => [ |
1185
|
|
|
'author' => [ |
1186
|
|
|
'links' => [ |
1187
|
|
|
'self' => 'http://example.com/books/1/relationships/author', |
1188
|
|
|
'related' => 'http://example.com/books/1/author', |
1189
|
|
|
], |
1190
|
|
|
], |
1191
|
|
|
'co-author' => [ |
1192
|
|
|
'links' => [ |
1193
|
|
|
'self' => 'http://example.com/books/1/relationships/co-author', |
1194
|
|
|
'related' => 'http://example.com/books/1/co-author', |
1195
|
|
|
], |
1196
|
|
|
], |
1197
|
|
|
'author-with-meta' => [ |
1198
|
|
|
'links' => [ |
1199
|
|
|
'self' => 'http://example.com/books/1/relationships/author-with-meta', |
1200
|
|
|
'related' => 'http://example.com/books/1/author-with-meta', |
1201
|
|
|
], |
1202
|
|
|
], |
1203
|
|
|
], |
1204
|
|
|
], |
1205
|
|
|
]; |
1206
|
|
|
|
1207
|
|
|
$this->assertSame($expected, $scope->toArray()); |
1208
|
|
|
|
1209
|
|
|
$expectedJson = '{"data":{"type":"books","id":"1","attributes":{"title":"Foo","year":1991},"links":{"self":"http:\/\/example.com\/books\/1"},"relationships":{"author":{"links":{"self":"http:\/\/example.com\/books\/1\/relationships\/author","related":"http:\/\/example.com\/books\/1\/author"}},"co-author":{"links":{"self":"http:\/\/example.com\/books\/1\/relationships\/co-author","related":"http:\/\/example.com\/books\/1\/co-author"}},"author-with-meta":{"links":{"self":"http:\/\/example.com\/books\/1\/relationships\/author-with-meta","related":"http:\/\/example.com\/books\/1\/author-with-meta"}}}}}'; |
1210
|
|
|
$this->assertSame($expectedJson, $scope->toJson()); |
1211
|
|
|
} |
1212
|
|
|
|
1213
|
|
|
public function testSerializingCollectionResourceWithSelfLink() : void |
1214
|
|
|
{ |
1215
|
|
|
$baseUrl = 'http://example.com'; |
1216
|
|
|
$this->manager->setSerializer(new JsonApiSerializer($baseUrl)); |
1217
|
|
|
|
1218
|
|
|
$booksData = [ |
1219
|
|
|
[ |
1220
|
|
|
'id' => 1, |
1221
|
|
|
'title' => 'Foo', |
1222
|
|
|
'year' => '1991', |
1223
|
|
|
'_author' => [ |
1224
|
|
|
'id' => 1, |
1225
|
|
|
'name' => 'Dave', |
1226
|
|
|
], |
1227
|
|
|
], |
1228
|
|
|
[ |
1229
|
|
|
'id' => 2, |
1230
|
|
|
'title' => 'Bar', |
1231
|
|
|
'year' => '1997', |
1232
|
|
|
'_author' => [ |
1233
|
|
|
'id' => 2, |
1234
|
|
|
'name' => 'Bob', |
1235
|
|
|
], |
1236
|
|
|
], |
1237
|
|
|
]; |
1238
|
|
|
|
1239
|
|
|
$resource = new Collection($booksData, new JsonApiBookTransformer(), 'books'); |
1240
|
|
|
$scope = new Scope($this->manager, $resource); |
1241
|
|
|
|
1242
|
|
|
$expected = [ |
1243
|
|
|
'data' => [ |
1244
|
|
|
[ |
1245
|
|
|
'type' => 'books', |
1246
|
|
|
'id' => '1', |
1247
|
|
|
'attributes' => [ |
1248
|
|
|
'title' => 'Foo', |
1249
|
|
|
'year' => 1991, |
1250
|
|
|
], |
1251
|
|
|
'links' => [ |
1252
|
|
|
'self' => 'http://example.com/books/1', |
1253
|
|
|
], |
1254
|
|
|
'relationships' => [ |
1255
|
|
|
'author' => [ |
1256
|
|
|
'links' => [ |
1257
|
|
|
'self' => 'http://example.com/books/1/relationships/author', |
1258
|
|
|
'related' => 'http://example.com/books/1/author', |
1259
|
|
|
], |
1260
|
|
|
], |
1261
|
|
|
'co-author' => [ |
1262
|
|
|
'links' => [ |
1263
|
|
|
'self' => 'http://example.com/books/1/relationships/co-author', |
1264
|
|
|
'related' => 'http://example.com/books/1/co-author', |
1265
|
|
|
], |
1266
|
|
|
], |
1267
|
|
|
'author-with-meta' => [ |
1268
|
|
|
'links' => [ |
1269
|
|
|
'self' => 'http://example.com/books/1/relationships/author-with-meta', |
1270
|
|
|
'related' => 'http://example.com/books/1/author-with-meta', |
1271
|
|
|
], |
1272
|
|
|
], |
1273
|
|
|
], |
1274
|
|
|
], |
1275
|
|
|
[ |
1276
|
|
|
'type' => 'books', |
1277
|
|
|
'id' => '2', |
1278
|
|
|
'attributes' => [ |
1279
|
|
|
'title' => 'Bar', |
1280
|
|
|
'year' => 1997, |
1281
|
|
|
], |
1282
|
|
|
'links' => [ |
1283
|
|
|
'self' => 'http://example.com/books/2', |
1284
|
|
|
], |
1285
|
|
|
'relationships' => [ |
1286
|
|
|
'author' => [ |
1287
|
|
|
'links' => [ |
1288
|
|
|
'self' => 'http://example.com/books/2/relationships/author', |
1289
|
|
|
'related' => 'http://example.com/books/2/author' |
1290
|
|
|
], |
1291
|
|
|
], |
1292
|
|
|
'co-author' => [ |
1293
|
|
|
'links' => [ |
1294
|
|
|
'self' => 'http://example.com/books/2/relationships/co-author', |
1295
|
|
|
'related' => 'http://example.com/books/2/co-author' |
1296
|
|
|
], |
1297
|
|
|
], |
1298
|
|
|
'author-with-meta' => [ |
1299
|
|
|
'links' => [ |
1300
|
|
|
'self' => 'http://example.com/books/2/relationships/author-with-meta', |
1301
|
|
|
'related' => 'http://example.com/books/2/author-with-meta', |
1302
|
|
|
], |
1303
|
|
|
], |
1304
|
|
|
], |
1305
|
|
|
], |
1306
|
|
|
], |
1307
|
|
|
]; |
1308
|
|
|
|
1309
|
|
|
$this->assertSame($expected, $scope->toArray()); |
1310
|
|
|
|
1311
|
|
|
$expectedJson = '{"data":[{"type":"books","id":"1","attributes":{"title":"Foo","year":1991},"links":{"self":"http:\/\/example.com\/books\/1"},"relationships":{"author":{"links":{"self":"http:\/\/example.com\/books\/1\/relationships\/author","related":"http:\/\/example.com\/books\/1\/author"}},"co-author":{"links":{"self":"http:\/\/example.com\/books\/1\/relationships\/co-author","related":"http:\/\/example.com\/books\/1\/co-author"}},"author-with-meta":{"links":{"self":"http:\/\/example.com\/books\/1\/relationships\/author-with-meta","related":"http:\/\/example.com\/books\/1\/author-with-meta"}}}},{"type":"books","id":"2","attributes":{"title":"Bar","year":1997},"links":{"self":"http:\/\/example.com\/books\/2"},"relationships":{"author":{"links":{"self":"http:\/\/example.com\/books\/2\/relationships\/author","related":"http:\/\/example.com\/books\/2\/author"}},"co-author":{"links":{"self":"http:\/\/example.com\/books\/2\/relationships\/co-author","related":"http:\/\/example.com\/books\/2\/co-author"}},"author-with-meta":{"links":{"self":"http:\/\/example.com\/books\/2\/relationships\/author-with-meta","related":"http:\/\/example.com\/books\/2\/author-with-meta"}}}}]}'; |
1312
|
|
|
$this->assertSame($expectedJson, $scope->toJson()); |
1313
|
|
|
} |
1314
|
|
|
|
1315
|
|
|
public function testSerializingItemResourceWithLinksForHasOneRelationship() |
1316
|
|
|
{ |
1317
|
|
|
$baseUrl = 'http://example.com'; |
1318
|
|
|
$this->manager->setSerializer(new JsonApiSerializer($baseUrl)); |
1319
|
|
|
$this->manager->parseIncludes('author'); |
1320
|
|
|
|
1321
|
|
|
$bookData = [ |
1322
|
|
|
'id' => 1, |
1323
|
|
|
'title' => 'Foo', |
1324
|
|
|
'year' => '1991', |
1325
|
|
|
'_author' => [ |
1326
|
|
|
'id' => 1, |
1327
|
|
|
'name' => 'Dave', |
1328
|
|
|
], |
1329
|
|
|
]; |
1330
|
|
|
|
1331
|
|
|
$resource = new Item($bookData, new JsonApiBookTransformer(), 'books'); |
1332
|
|
|
|
1333
|
|
|
$scope = new Scope($this->manager, $resource); |
1334
|
|
|
|
1335
|
|
|
$expected = [ |
1336
|
|
|
'data' => [ |
1337
|
|
|
'type' => 'books', |
1338
|
|
|
'id' => '1', |
1339
|
|
|
'attributes' => [ |
1340
|
|
|
'title' => 'Foo', |
1341
|
|
|
'year' => 1991, |
1342
|
|
|
], |
1343
|
|
|
'relationships' => [ |
1344
|
|
|
'author' => [ |
1345
|
|
|
'links' => [ |
1346
|
|
|
'self' => 'http://example.com/books/1/relationships/author', |
1347
|
|
|
'related' => 'http://example.com/books/1/author', |
1348
|
|
|
], |
1349
|
|
|
'data' => [ |
1350
|
|
|
'type' => 'people', |
1351
|
|
|
'id' => '1', |
1352
|
|
|
], |
1353
|
|
|
], |
1354
|
|
|
'co-author' => [ |
1355
|
|
|
'links' => [ |
1356
|
|
|
'self' => 'http://example.com/books/1/relationships/co-author', |
1357
|
|
|
'related' => 'http://example.com/books/1/co-author' |
1358
|
|
|
], |
1359
|
|
|
], |
1360
|
|
|
'author-with-meta' => [ |
1361
|
|
|
'links' => [ |
1362
|
|
|
'self' => 'http://example.com/books/1/relationships/author-with-meta', |
1363
|
|
|
'related' => 'http://example.com/books/1/author-with-meta' |
1364
|
|
|
], |
1365
|
|
|
], |
1366
|
|
|
], |
1367
|
|
|
'links' => [ |
1368
|
|
|
'self' => 'http://example.com/books/1', |
1369
|
|
|
], |
1370
|
|
|
], |
1371
|
|
|
'included' => [ |
1372
|
|
|
[ |
1373
|
|
|
'type' => 'people', |
1374
|
|
|
'id' => '1', |
1375
|
|
|
'attributes' => [ |
1376
|
|
|
'name' => 'Dave', |
1377
|
|
|
], |
1378
|
|
|
'relationships' => [ |
1379
|
|
|
'published' => [ |
1380
|
|
|
'links' => [ |
1381
|
|
|
'self' => 'http://example.com/people/1/relationships/published', |
1382
|
|
|
'related' => 'http://example.com/people/1/published', |
1383
|
|
|
], |
1384
|
|
|
], |
1385
|
|
|
], |
1386
|
|
|
'links' => [ |
1387
|
|
|
'self' => 'http://example.com/people/1', |
1388
|
|
|
], |
1389
|
|
|
], |
1390
|
|
|
], |
1391
|
|
|
]; |
1392
|
|
|
|
1393
|
|
|
$this->assertEquals($expected, $scope->toArray()); |
1394
|
|
|
|
1395
|
|
|
$expectedJson = '{"data":{"type":"books","id":"1","attributes":{"title":"Foo","year":1991},"links":{"self":"http:\/\/example.com\/books\/1"},"relationships":{"author":{"links":{"self":"http:\/\/example.com\/books\/1\/relationships\/author","related":"http:\/\/example.com\/books\/1\/author"},"data":{"type":"people","id":"1"}},"co-author":{"links":{"self":"http:\/\/example.com\/books\/1\/relationships\/co-author","related":"http:\/\/example.com\/books\/1\/co-author"}},"author-with-meta":{"links":{"self":"http:\/\/example.com\/books\/1\/relationships\/author-with-meta","related":"http:\/\/example.com\/books\/1\/author-with-meta"}}}},"included":[{"type":"people","id":"1","attributes":{"name":"Dave"},"links":{"self":"http:\/\/example.com\/people\/1"},"relationships":{"published":{"links":{"self":"http:\/\/example.com\/people\/1\/relationships\/published","related":"http:\/\/example.com\/people\/1\/published"}}}}]}'; |
1396
|
|
|
$this->assertSame($expectedJson, $scope->toJson()); |
1397
|
|
|
} |
1398
|
|
|
|
1399
|
|
|
public function testSerializingItemResourceWithLinksForHasManyRelationship() : void |
1400
|
|
|
{ |
1401
|
|
|
$baseUrl = 'http://example.com'; |
1402
|
|
|
$this->manager->setSerializer(new JsonApiSerializer($baseUrl)); |
1403
|
|
|
$this->manager->parseIncludes('published'); |
1404
|
|
|
|
1405
|
|
|
$authorData = [ |
1406
|
|
|
'id' => 1, |
1407
|
|
|
'name' => 'Dave', |
1408
|
|
|
'_published' => [ |
1409
|
|
|
[ |
1410
|
|
|
'id' => 1, |
1411
|
|
|
'title' => 'Foo', |
1412
|
|
|
'year' => '1991', |
1413
|
|
|
], |
1414
|
|
|
[ |
1415
|
|
|
'id' => 2, |
1416
|
|
|
'title' => 'Bar', |
1417
|
|
|
'year' => '2015', |
1418
|
|
|
], |
1419
|
|
|
], |
1420
|
|
|
]; |
1421
|
|
|
|
1422
|
|
|
$resource = new Item($authorData, new JsonApiAuthorTransformer(), 'people'); |
1423
|
|
|
|
1424
|
|
|
$scope = new Scope($this->manager, $resource); |
1425
|
|
|
|
1426
|
|
|
$expected = [ |
1427
|
|
|
'data' => [ |
1428
|
|
|
'type' => 'people', |
1429
|
|
|
'id' => '1', |
1430
|
|
|
'attributes' => [ |
1431
|
|
|
'name' => 'Dave', |
1432
|
|
|
], |
1433
|
|
|
'relationships' => [ |
1434
|
|
|
'published' => [ |
1435
|
|
|
'links' => [ |
1436
|
|
|
'self' => 'http://example.com/people/1/relationships/published', |
1437
|
|
|
'related' => 'http://example.com/people/1/published', |
1438
|
|
|
], |
1439
|
|
|
'data' => [ |
1440
|
|
|
[ |
1441
|
|
|
'type' => 'books', |
1442
|
|
|
'id' => 1, |
1443
|
|
|
], |
1444
|
|
|
[ |
1445
|
|
|
'type' => 'books', |
1446
|
|
|
'id' => 2, |
1447
|
|
|
], |
1448
|
|
|
], |
1449
|
|
|
], |
1450
|
|
|
], |
1451
|
|
|
'links' => [ |
1452
|
|
|
'self' => 'http://example.com/people/1', |
1453
|
|
|
], |
1454
|
|
|
], |
1455
|
|
|
'included' => [ |
1456
|
|
|
[ |
1457
|
|
|
'type' => 'books', |
1458
|
|
|
'id' => '1', |
1459
|
|
|
'attributes' => [ |
1460
|
|
|
'title' => 'Foo', |
1461
|
|
|
'year' => 1991, |
1462
|
|
|
], |
1463
|
|
|
'links' => [ |
1464
|
|
|
'self' => 'http://example.com/books/1', |
1465
|
|
|
], |
1466
|
|
|
'relationships' => [ |
1467
|
|
|
'author' => [ |
1468
|
|
|
'links' => [ |
1469
|
|
|
'self' => 'http://example.com/books/1/relationships/author', |
1470
|
|
|
'related' => 'http://example.com/books/1/author' |
1471
|
|
|
], |
1472
|
|
|
], |
1473
|
|
|
'co-author' => [ |
1474
|
|
|
'links' => [ |
1475
|
|
|
'self' => 'http://example.com/books/1/relationships/co-author', |
1476
|
|
|
'related' => 'http://example.com/books/1/co-author' |
1477
|
|
|
], |
1478
|
|
|
], |
1479
|
|
|
'author-with-meta' => [ |
1480
|
|
|
'links' => [ |
1481
|
|
|
'self' => 'http://example.com/books/1/relationships/author-with-meta', |
1482
|
|
|
'related' => 'http://example.com/books/1/author-with-meta' |
1483
|
|
|
], |
1484
|
|
|
], |
1485
|
|
|
], |
1486
|
|
|
], |
1487
|
|
|
[ |
1488
|
|
|
'type' => 'books', |
1489
|
|
|
'id' => '2', |
1490
|
|
|
'attributes' => [ |
1491
|
|
|
'title' => 'Bar', |
1492
|
|
|
'year' => 2015, |
1493
|
|
|
], |
1494
|
|
|
'links' => [ |
1495
|
|
|
'self' => 'http://example.com/books/2', |
1496
|
|
|
], |
1497
|
|
|
'relationships' => [ |
1498
|
|
|
'author' => [ |
1499
|
|
|
'links' => [ |
1500
|
|
|
'self' => 'http://example.com/books/2/relationships/author', |
1501
|
|
|
'related' => 'http://example.com/books/2/author' |
1502
|
|
|
], |
1503
|
|
|
], |
1504
|
|
|
'co-author' => [ |
1505
|
|
|
'links' => [ |
1506
|
|
|
'self' => 'http://example.com/books/2/relationships/co-author', |
1507
|
|
|
'related' => 'http://example.com/books/2/co-author' |
1508
|
|
|
], |
1509
|
|
|
], |
1510
|
|
|
'author-with-meta' => [ |
1511
|
|
|
'links' => [ |
1512
|
|
|
'self' => 'http://example.com/books/2/relationships/author-with-meta', |
1513
|
|
|
'related' => 'http://example.com/books/2/author-with-meta' |
1514
|
|
|
], |
1515
|
|
|
], |
1516
|
|
|
], |
1517
|
|
|
], |
1518
|
|
|
], |
1519
|
|
|
]; |
1520
|
|
|
|
1521
|
|
|
$this->assertEquals($expected, $scope->toArray()); |
1522
|
|
|
|
1523
|
|
|
$expectedJson = '{"data":{"type":"people","id":"1","attributes":{"name":"Dave"},"links":{"self":"http:\/\/example.com\/people\/1"},"relationships":{"published":{"links":{"self":"http:\/\/example.com\/people\/1\/relationships\/published","related":"http:\/\/example.com\/people\/1\/published"},"data":[{"type":"books","id":"1"},{"type":"books","id":"2"}]}}},"included":[{"type":"books","id":"1","attributes":{"title":"Foo","year":1991},"links":{"self":"http:\/\/example.com\/books\/1"},"relationships":{"author":{"links":{"self":"http:\/\/example.com\/books\/1\/relationships\/author","related":"http:\/\/example.com\/books\/1\/author"}},"co-author":{"links":{"self":"http:\/\/example.com\/books\/1\/relationships\/co-author","related":"http:\/\/example.com\/books\/1\/co-author"}},"author-with-meta":{"links":{"self":"http:\/\/example.com\/books\/1\/relationships\/author-with-meta","related":"http:\/\/example.com\/books\/1\/author-with-meta"}}}},{"type":"books","id":"2","attributes":{"title":"Bar","year":2015},"links":{"self":"http:\/\/example.com\/books\/2"},"relationships":{"author":{"links":{"self":"http:\/\/example.com\/books\/2\/relationships\/author","related":"http:\/\/example.com\/books\/2\/author"}},"co-author":{"links":{"self":"http:\/\/example.com\/books\/2\/relationships\/co-author","related":"http:\/\/example.com\/books\/2\/co-author"}},"author-with-meta":{"links":{"self":"http:\/\/example.com\/books\/2\/relationships\/author-with-meta","related":"http:\/\/example.com\/books\/2\/author-with-meta"}}}}]}'; |
1524
|
|
|
$this->assertSame($expectedJson, $scope->toJson()); |
1525
|
|
|
} |
1526
|
|
|
|
1527
|
|
|
public function testSerializingCollectionResourceWithLinksForHasOneRelationship() : void |
1528
|
|
|
{ |
1529
|
|
|
$baseUrl = 'http://example.com'; |
1530
|
|
|
$this->manager->setSerializer(new JsonApiSerializer($baseUrl)); |
1531
|
|
|
$this->manager->parseIncludes('author'); |
1532
|
|
|
|
1533
|
|
|
$bookData = [ |
1534
|
|
|
[ |
1535
|
|
|
'id' => 1, |
1536
|
|
|
'title' => 'Foo', |
1537
|
|
|
'year' => '1991', |
1538
|
|
|
'_author' => [ |
1539
|
|
|
'id' => 1, |
1540
|
|
|
'name' => 'Dave', |
1541
|
|
|
], |
1542
|
|
|
], |
1543
|
|
|
[ |
1544
|
|
|
'id' => 2, |
1545
|
|
|
'title' => 'Bar', |
1546
|
|
|
'year' => '1991', |
1547
|
|
|
'_author' => [ |
1548
|
|
|
'id' => 1, |
1549
|
|
|
'name' => 'Dave', |
1550
|
|
|
], |
1551
|
|
|
], |
1552
|
|
|
]; |
1553
|
|
|
|
1554
|
|
|
$resource = new Collection($bookData, new JsonApiBookTransformer(), 'books'); |
1555
|
|
|
|
1556
|
|
|
$scope = new Scope($this->manager, $resource); |
1557
|
|
|
|
1558
|
|
|
$expected = [ |
1559
|
|
|
'data' => [ |
1560
|
|
|
[ |
1561
|
|
|
'type' => 'books', |
1562
|
|
|
'id' => '1', |
1563
|
|
|
'attributes' => [ |
1564
|
|
|
'title' => 'Foo', |
1565
|
|
|
'year' => 1991, |
1566
|
|
|
], |
1567
|
|
|
'relationships' => [ |
1568
|
|
|
'author' => [ |
1569
|
|
|
'links' => [ |
1570
|
|
|
'self' => 'http://example.com/books/1/relationships/author', |
1571
|
|
|
'related' => 'http://example.com/books/1/author', |
1572
|
|
|
], |
1573
|
|
|
'data' => [ |
1574
|
|
|
'type' => 'people', |
1575
|
|
|
'id' => '1', |
1576
|
|
|
], |
1577
|
|
|
], |
1578
|
|
|
'co-author' => [ |
1579
|
|
|
'links' => [ |
1580
|
|
|
'self' => 'http://example.com/books/1/relationships/co-author', |
1581
|
|
|
'related' => 'http://example.com/books/1/co-author' |
1582
|
|
|
], |
1583
|
|
|
], |
1584
|
|
|
'author-with-meta' => [ |
1585
|
|
|
'links' => [ |
1586
|
|
|
'self' => 'http://example.com/books/1/relationships/author-with-meta', |
1587
|
|
|
'related' => 'http://example.com/books/1/author-with-meta' |
1588
|
|
|
], |
1589
|
|
|
], |
1590
|
|
|
], |
1591
|
|
|
'links' => [ |
1592
|
|
|
'self' => 'http://example.com/books/1', |
1593
|
|
|
], |
1594
|
|
|
], |
1595
|
|
|
[ |
1596
|
|
|
'type' => 'books', |
1597
|
|
|
'id' => '2', |
1598
|
|
|
'attributes' => [ |
1599
|
|
|
'title' => 'Bar', |
1600
|
|
|
'year' => 1991, |
1601
|
|
|
], |
1602
|
|
|
'relationships' => [ |
1603
|
|
|
'author' => [ |
1604
|
|
|
'links' => [ |
1605
|
|
|
'self' => 'http://example.com/books/2/relationships/author', |
1606
|
|
|
'related' => 'http://example.com/books/2/author', |
1607
|
|
|
], |
1608
|
|
|
'data' => [ |
1609
|
|
|
'type' => 'people', |
1610
|
|
|
'id' => '1', |
1611
|
|
|
], |
1612
|
|
|
], |
1613
|
|
|
'co-author' => [ |
1614
|
|
|
'links' => [ |
1615
|
|
|
'self' => 'http://example.com/books/2/relationships/co-author', |
1616
|
|
|
'related' => 'http://example.com/books/2/co-author' |
1617
|
|
|
], |
1618
|
|
|
], |
1619
|
|
|
'author-with-meta' => [ |
1620
|
|
|
'links' => [ |
1621
|
|
|
'self' => 'http://example.com/books/2/relationships/author-with-meta', |
1622
|
|
|
'related' => 'http://example.com/books/2/author-with-meta' |
1623
|
|
|
], |
1624
|
|
|
], |
1625
|
|
|
], |
1626
|
|
|
'links' => [ |
1627
|
|
|
'self' => 'http://example.com/books/2', |
1628
|
|
|
], |
1629
|
|
|
], |
1630
|
|
|
], |
1631
|
|
|
'included' => [ |
1632
|
|
|
[ |
1633
|
|
|
'type' => 'people', |
1634
|
|
|
'id' => '1', |
1635
|
|
|
'attributes' => [ |
1636
|
|
|
'name' => 'Dave', |
1637
|
|
|
], |
1638
|
|
|
'links' => [ |
1639
|
|
|
'self' => 'http://example.com/people/1', |
1640
|
|
|
], |
1641
|
|
|
'relationships' => [ |
1642
|
|
|
'published' => [ |
1643
|
|
|
'links' => [ |
1644
|
|
|
'self' => 'http://example.com/people/1/relationships/published', |
1645
|
|
|
'related' => 'http://example.com/people/1/published', |
1646
|
|
|
], |
1647
|
|
|
], |
1648
|
|
|
], |
1649
|
|
|
], |
1650
|
|
|
], |
1651
|
|
|
]; |
1652
|
|
|
|
1653
|
|
|
$this->assertEquals($expected, $scope->toArray()); |
1654
|
|
|
|
1655
|
|
|
$expectedJson = '{"data":[{"type":"books","id":"1","attributes":{"title":"Foo","year":1991},"links":{"self":"http:\/\/example.com\/books\/1"},"relationships":{"author":{"links":{"self":"http:\/\/example.com\/books\/1\/relationships\/author","related":"http:\/\/example.com\/books\/1\/author"},"data":{"type":"people","id":"1"}},"co-author":{"links":{"self":"http:\/\/example.com\/books\/1\/relationships\/co-author","related":"http:\/\/example.com\/books\/1\/co-author"}},"author-with-meta":{"links":{"self":"http:\/\/example.com\/books\/1\/relationships\/author-with-meta","related":"http:\/\/example.com\/books\/1\/author-with-meta"}}}},{"type":"books","id":"2","attributes":{"title":"Bar","year":1991},"links":{"self":"http:\/\/example.com\/books\/2"},"relationships":{"author":{"links":{"self":"http:\/\/example.com\/books\/2\/relationships\/author","related":"http:\/\/example.com\/books\/2\/author"},"data":{"type":"people","id":"1"}},"co-author":{"links":{"self":"http:\/\/example.com\/books\/2\/relationships\/co-author","related":"http:\/\/example.com\/books\/2\/co-author"}},"author-with-meta":{"links":{"self":"http:\/\/example.com\/books\/2\/relationships\/author-with-meta","related":"http:\/\/example.com\/books\/2\/author-with-meta"}}}}],"included":[{"type":"people","id":"1","attributes":{"name":"Dave"},"links":{"self":"http:\/\/example.com\/people\/1"},"relationships":{"published":{"links":{"self":"http:\/\/example.com\/people\/1\/relationships\/published","related":"http:\/\/example.com\/people\/1\/published"}}}}]}'; |
1656
|
|
|
$this->assertSame($expectedJson, $scope->toJson()); |
1657
|
|
|
} |
1658
|
|
|
|
1659
|
|
|
public function testSerializingCollectionResourceWithLinksForHasManyRelationship() : void |
1660
|
|
|
{ |
1661
|
|
|
$baseUrl = 'http://example.com'; |
1662
|
|
|
$this->manager->setSerializer(new JsonApiSerializer($baseUrl)); |
1663
|
|
|
$this->manager->parseIncludes('published'); |
1664
|
|
|
|
1665
|
|
|
$authorData = [ |
1666
|
|
|
[ |
1667
|
|
|
'id' => 1, |
1668
|
|
|
'name' => 'Dave', |
1669
|
|
|
'_published' => [ |
1670
|
|
|
[ |
1671
|
|
|
'id' => 1, |
1672
|
|
|
'title' => 'Foo', |
1673
|
|
|
'year' => '1991', |
1674
|
|
|
], |
1675
|
|
|
[ |
1676
|
|
|
'id' => 2, |
1677
|
|
|
'title' => 'Bar', |
1678
|
|
|
'year' => '2015', |
1679
|
|
|
], |
1680
|
|
|
], |
1681
|
|
|
], |
1682
|
|
|
[ |
1683
|
|
|
'id' => 2, |
1684
|
|
|
'name' => 'Bill', |
1685
|
|
|
'_published' => [ |
1686
|
|
|
[ |
1687
|
|
|
'id' => 1, |
1688
|
|
|
'title' => 'Foo', |
1689
|
|
|
'year' => '1991', |
1690
|
|
|
], |
1691
|
|
|
[ |
1692
|
|
|
'id' => 2, |
1693
|
|
|
'title' => 'Bar', |
1694
|
|
|
'year' => '2015', |
1695
|
|
|
], |
1696
|
|
|
], |
1697
|
|
|
], |
1698
|
|
|
]; |
1699
|
|
|
|
1700
|
|
|
$resource = new Collection($authorData, new JsonApiAuthorTransformer(), 'people'); |
1701
|
|
|
|
1702
|
|
|
$scope = new Scope($this->manager, $resource); |
1703
|
|
|
|
1704
|
|
|
$expected = [ |
1705
|
|
|
'data' => [ |
1706
|
|
|
[ |
1707
|
|
|
'type' => 'people', |
1708
|
|
|
'id' => '1', |
1709
|
|
|
'attributes' => [ |
1710
|
|
|
'name' => 'Dave', |
1711
|
|
|
], |
1712
|
|
|
'relationships' => [ |
1713
|
|
|
'published' => [ |
1714
|
|
|
'links' => [ |
1715
|
|
|
'self' => 'http://example.com/people/1/relationships/published', |
1716
|
|
|
'related' => 'http://example.com/people/1/published', |
1717
|
|
|
], |
1718
|
|
|
'data' => [ |
1719
|
|
|
[ |
1720
|
|
|
'type' => 'books', |
1721
|
|
|
'id' => 1, |
1722
|
|
|
], |
1723
|
|
|
[ |
1724
|
|
|
'type' => 'books', |
1725
|
|
|
'id' => 2, |
1726
|
|
|
], |
1727
|
|
|
], |
1728
|
|
|
], |
1729
|
|
|
], |
1730
|
|
|
'links' => [ |
1731
|
|
|
'self' => 'http://example.com/people/1', |
1732
|
|
|
], |
1733
|
|
|
], |
1734
|
|
|
[ |
1735
|
|
|
'type' => 'people', |
1736
|
|
|
'id' => '2', |
1737
|
|
|
'attributes' => [ |
1738
|
|
|
'name' => 'Bill', |
1739
|
|
|
], |
1740
|
|
|
'relationships' => [ |
1741
|
|
|
'published' => [ |
1742
|
|
|
'links' => [ |
1743
|
|
|
'self' => 'http://example.com/people/2/relationships/published', |
1744
|
|
|
'related' => 'http://example.com/people/2/published', |
1745
|
|
|
], |
1746
|
|
|
'data' => [ |
1747
|
|
|
[ |
1748
|
|
|
'type' => 'books', |
1749
|
|
|
'id' => 1, |
1750
|
|
|
], |
1751
|
|
|
[ |
1752
|
|
|
'type' => 'books', |
1753
|
|
|
'id' => 2, |
1754
|
|
|
], |
1755
|
|
|
], |
1756
|
|
|
], |
1757
|
|
|
], |
1758
|
|
|
'links' => [ |
1759
|
|
|
'self' => 'http://example.com/people/2', |
1760
|
|
|
], |
1761
|
|
|
], |
1762
|
|
|
], |
1763
|
|
|
'included' => [ |
1764
|
|
|
[ |
1765
|
|
|
'type' => 'books', |
1766
|
|
|
'id' => '1', |
1767
|
|
|
'attributes' => [ |
1768
|
|
|
'title' => 'Foo', |
1769
|
|
|
'year' => 1991, |
1770
|
|
|
], |
1771
|
|
|
'links' => [ |
1772
|
|
|
'self' => 'http://example.com/books/1', |
1773
|
|
|
], |
1774
|
|
|
'relationships' => [ |
1775
|
|
|
'author' => [ |
1776
|
|
|
'links' => [ |
1777
|
|
|
'self' => 'http://example.com/books/1/relationships/author', |
1778
|
|
|
'related' => 'http://example.com/books/1/author', |
1779
|
|
|
], |
1780
|
|
|
], |
1781
|
|
|
'co-author' => [ |
1782
|
|
|
'links' => [ |
1783
|
|
|
'self' => 'http://example.com/books/1/relationships/co-author', |
1784
|
|
|
'related' => 'http://example.com/books/1/co-author' |
1785
|
|
|
], |
1786
|
|
|
], |
1787
|
|
|
'author-with-meta' => [ |
1788
|
|
|
'links' => [ |
1789
|
|
|
'self' => 'http://example.com/books/1/relationships/author-with-meta', |
1790
|
|
|
'related' => 'http://example.com/books/1/author-with-meta' |
1791
|
|
|
], |
1792
|
|
|
], |
1793
|
|
|
], |
1794
|
|
|
], |
1795
|
|
|
[ |
1796
|
|
|
'type' => 'books', |
1797
|
|
|
'id' => '2', |
1798
|
|
|
'attributes' => [ |
1799
|
|
|
'title' => 'Bar', |
1800
|
|
|
'year' => 2015, |
1801
|
|
|
], |
1802
|
|
|
'links' => [ |
1803
|
|
|
'self' => 'http://example.com/books/2', |
1804
|
|
|
], |
1805
|
|
|
'relationships' => [ |
1806
|
|
|
'author' => [ |
1807
|
|
|
'links' => [ |
1808
|
|
|
'self' => 'http://example.com/books/2/relationships/author', |
1809
|
|
|
'related' => 'http://example.com/books/2/author', |
1810
|
|
|
], |
1811
|
|
|
], |
1812
|
|
|
'co-author' => [ |
1813
|
|
|
'links' => [ |
1814
|
|
|
'self' => 'http://example.com/books/2/relationships/co-author', |
1815
|
|
|
'related' => 'http://example.com/books/2/co-author' |
1816
|
|
|
], |
1817
|
|
|
], |
1818
|
|
|
'author-with-meta' => [ |
1819
|
|
|
'links' => [ |
1820
|
|
|
'self' => 'http://example.com/books/2/relationships/author-with-meta', |
1821
|
|
|
'related' => 'http://example.com/books/2/author-with-meta' |
1822
|
|
|
], |
1823
|
|
|
], |
1824
|
|
|
], |
1825
|
|
|
], |
1826
|
|
|
], |
1827
|
|
|
]; |
1828
|
|
|
|
1829
|
|
|
$this->assertEquals($expected, $scope->toArray()); |
1830
|
|
|
|
1831
|
|
|
$expectedJson = '{"data":[{"type":"people","id":"1","attributes":{"name":"Dave"},"links":{"self":"http:\/\/example.com\/people\/1"},"relationships":{"published":{"links":{"self":"http:\/\/example.com\/people\/1\/relationships\/published","related":"http:\/\/example.com\/people\/1\/published"},"data":[{"type":"books","id":"1"},{"type":"books","id":"2"}]}}},{"type":"people","id":"2","attributes":{"name":"Bill"},"links":{"self":"http:\/\/example.com\/people\/2"},"relationships":{"published":{"links":{"self":"http:\/\/example.com\/people\/2\/relationships\/published","related":"http:\/\/example.com\/people\/2\/published"},"data":[{"type":"books","id":"1"},{"type":"books","id":"2"}]}}}],"included":[{"type":"books","id":"1","attributes":{"title":"Foo","year":1991},"links":{"self":"http:\/\/example.com\/books\/1"},"relationships":{"author":{"links":{"self":"http:\/\/example.com\/books\/1\/relationships\/author","related":"http:\/\/example.com\/books\/1\/author"}},"co-author":{"links":{"self":"http:\/\/example.com\/books\/1\/relationships\/co-author","related":"http:\/\/example.com\/books\/1\/co-author"}},"author-with-meta":{"links":{"self":"http:\/\/example.com\/books\/1\/relationships\/author-with-meta","related":"http:\/\/example.com\/books\/1\/author-with-meta"}}}},{"type":"books","id":"2","attributes":{"title":"Bar","year":2015},"links":{"self":"http:\/\/example.com\/books\/2"},"relationships":{"author":{"links":{"self":"http:\/\/example.com\/books\/2\/relationships\/author","related":"http:\/\/example.com\/books\/2\/author"}},"co-author":{"links":{"self":"http:\/\/example.com\/books\/2\/relationships\/co-author","related":"http:\/\/example.com\/books\/2\/co-author"}},"author-with-meta":{"links":{"self":"http:\/\/example.com\/books\/2\/relationships\/author-with-meta","related":"http:\/\/example.com\/books\/2\/author-with-meta"}}}}]}'; |
1832
|
|
|
$this->assertSame($expectedJson, $scope->toJson()); |
1833
|
|
|
} |
1834
|
|
|
|
1835
|
|
|
public function testExceptionThrownIfResourceHasNoId() : void |
1836
|
|
|
{ |
1837
|
|
|
$this->expectException(InvalidArgumentException::class, 'JSON API resource objects MUST have a valid id'); |
1838
|
|
|
|
1839
|
|
|
$bookData = [ |
1840
|
|
|
'title' => 'Foo', |
1841
|
|
|
'year' => '1991', |
1842
|
|
|
]; |
1843
|
|
|
|
1844
|
|
|
$resource = new Item($bookData, new JsonApiBookTransformer(), 'books'); |
1845
|
|
|
|
1846
|
|
|
$scope = new Scope($this->manager, $resource); |
1847
|
|
|
$scope->toArray(); |
1848
|
|
|
} |
1849
|
|
|
|
1850
|
|
|
public function testSerializingItemWithReferenceToRootObject() |
1851
|
|
|
{ |
1852
|
|
|
$this->manager->parseIncludes('published.author'); |
1853
|
|
|
|
1854
|
|
|
$authorData = [ |
1855
|
|
|
'id' => 1, |
1856
|
|
|
'name' => 'Dave', |
1857
|
|
|
'_published' => [ |
1858
|
|
|
[ |
1859
|
|
|
'id' => 1, |
1860
|
|
|
'title' => 'Foo', |
1861
|
|
|
'year' => 1991, |
1862
|
|
|
'_author' => ['id' => 1] |
1863
|
|
|
], |
1864
|
|
|
[ |
1865
|
|
|
'id' => 2, |
1866
|
|
|
'title' => 'Bar', |
1867
|
|
|
'year' => 2015, |
1868
|
|
|
'_author' => ['id' => 1] |
1869
|
|
|
], |
1870
|
|
|
], |
1871
|
|
|
]; |
1872
|
|
|
|
1873
|
|
|
$resource = new Item($authorData, new JsonApiAuthorTransformer(), 'people'); |
1874
|
|
|
|
1875
|
|
|
$scope = new Scope($this->manager, $resource); |
1876
|
|
|
|
1877
|
|
|
$expected = [ |
1878
|
|
|
'data' => [ |
1879
|
|
|
'type' => 'people', |
1880
|
|
|
'id' => '1', |
1881
|
|
|
'attributes' => [ |
1882
|
|
|
'name' => 'Dave', |
1883
|
|
|
], |
1884
|
|
|
'relationships' => [ |
1885
|
|
|
'published' => [ |
1886
|
|
|
'data' => [ |
1887
|
|
|
['type' => 'books', 'id' => '1'], |
1888
|
|
|
['type' => 'books', 'id' => '2'], |
1889
|
|
|
], |
1890
|
|
|
], |
1891
|
|
|
], |
1892
|
|
|
], |
1893
|
|
|
'included' => [ |
1894
|
|
|
[ |
1895
|
|
|
'type' => 'books', |
1896
|
|
|
'id' => '1', |
1897
|
|
|
'attributes' => [ |
1898
|
|
|
'title' => 'Foo', |
1899
|
|
|
'year' => 1991, |
1900
|
|
|
], |
1901
|
|
|
'relationships' => [ |
1902
|
|
|
'author' => [ |
1903
|
|
|
'data' => ['type' => 'people', 'id' => '1'], |
1904
|
|
|
], |
1905
|
|
|
], |
1906
|
|
|
], |
1907
|
|
|
[ |
1908
|
|
|
'type' => 'books', |
1909
|
|
|
'id' => '2', |
1910
|
|
|
'attributes' => [ |
1911
|
|
|
'title' => 'Bar', |
1912
|
|
|
'year' => 2015, |
1913
|
|
|
], |
1914
|
|
|
'relationships' => [ |
1915
|
|
|
'author' => [ |
1916
|
|
|
'data' => ['type' => 'people', 'id' => '1'], |
1917
|
|
|
], |
1918
|
|
|
], |
1919
|
|
|
], |
1920
|
|
|
], |
1921
|
|
|
]; |
1922
|
|
|
|
1923
|
|
|
$this->assertSame($expected, $scope->toArray()); |
1924
|
|
|
|
1925
|
|
|
$expectedJson = '{"data":{"type":"people","id":"1","attributes":{"name":"Dave"},"relationships":{"published":{"data":[{"type":"books","id":"1"},{"type":"books","id":"2"}]}}},"included":[{"type":"books","id":"1","attributes":{"title":"Foo","year":1991},"relationships":{"author":{"data":{"type":"people","id":"1"}}}},{"type":"books","id":"2","attributes":{"title":"Bar","year":2015},"relationships":{"author":{"data":{"type":"people","id":"1"}}}}]}'; |
1926
|
|
|
$this->assertSame($expectedJson, $scope->toJson()); |
1927
|
|
|
} |
1928
|
|
|
|
1929
|
|
|
public function testSerializingCollectionWithReferenceToRootObjects() : void |
1930
|
|
|
{ |
1931
|
|
|
$this->manager->parseIncludes('author.published'); |
1932
|
|
|
|
1933
|
|
|
$booksData = [ |
1934
|
|
|
[ |
1935
|
|
|
'id' => 1, |
1936
|
|
|
'title' => 'Foo', |
1937
|
|
|
'year' => 1991, |
1938
|
|
|
'_author' => [ |
1939
|
|
|
'id' => 1, |
1940
|
|
|
'name' => 'Dave', |
1941
|
|
|
'_published' => [ |
1942
|
|
|
[ |
1943
|
|
|
'id' => 1, |
1944
|
|
|
'title' => 'Foo', |
1945
|
|
|
'year' => 1991, |
1946
|
|
|
], |
1947
|
|
|
[ |
1948
|
|
|
'id' => 2, |
1949
|
|
|
'title' => 'Bar', |
1950
|
|
|
'year' => 2015, |
1951
|
|
|
], |
1952
|
|
|
], |
1953
|
|
|
], |
1954
|
|
|
], |
1955
|
|
|
[ |
1956
|
|
|
'id' => 2, |
1957
|
|
|
'title' => 'Bar', |
1958
|
|
|
'year' => 2015, |
1959
|
|
|
'_author' => [ |
1960
|
|
|
'id' => 1, |
1961
|
|
|
'_published' => [ |
1962
|
|
|
[ |
1963
|
|
|
'id' => 1, |
1964
|
|
|
'title' => 'Foo', |
1965
|
|
|
'year' => 1991, |
1966
|
|
|
], |
1967
|
|
|
[ |
1968
|
|
|
'id' => 2, |
1969
|
|
|
'title' => 'Bar', |
1970
|
|
|
'year' => 2015, |
1971
|
|
|
], |
1972
|
|
|
], |
1973
|
|
|
], |
1974
|
|
|
], |
1975
|
|
|
]; |
1976
|
|
|
|
1977
|
|
|
$resource = new Collection($booksData, new JsonApiBookTransformer(), 'books'); |
1978
|
|
|
|
1979
|
|
|
$scope = new Scope($this->manager, $resource); |
1980
|
|
|
|
1981
|
|
|
$expected = [ |
1982
|
|
|
'data' => [ |
1983
|
|
|
[ |
1984
|
|
|
'type' => 'books', |
1985
|
|
|
'id' => '1', |
1986
|
|
|
'attributes' => [ |
1987
|
|
|
'title' => 'Foo', |
1988
|
|
|
'year' => 1991, |
1989
|
|
|
], |
1990
|
|
|
'relationships' => [ |
1991
|
|
|
'author' => [ |
1992
|
|
|
'data' => [ |
1993
|
|
|
'type' => 'people', |
1994
|
|
|
'id' => '1', |
1995
|
|
|
], |
1996
|
|
|
], |
1997
|
|
|
], |
1998
|
|
|
], |
1999
|
|
|
[ |
2000
|
|
|
'type' => 'books', |
2001
|
|
|
'id' => '2', |
2002
|
|
|
'attributes' => [ |
2003
|
|
|
'title' => 'Bar', |
2004
|
|
|
'year' => 2015, |
2005
|
|
|
], |
2006
|
|
|
'relationships' => [ |
2007
|
|
|
'author' => [ |
2008
|
|
|
'data' => [ |
2009
|
|
|
'type' => 'people', |
2010
|
|
|
'id' => '1', |
2011
|
|
|
], |
2012
|
|
|
], |
2013
|
|
|
], |
2014
|
|
|
], |
2015
|
|
|
], |
2016
|
|
|
'included' => [ |
2017
|
|
|
[ |
2018
|
|
|
'type' => 'people', |
2019
|
|
|
'id' => '1', |
2020
|
|
|
'attributes' => [ |
2021
|
|
|
'name' => 'Dave', |
2022
|
|
|
], |
2023
|
|
|
'relationships' => [ |
2024
|
|
|
'published' => [ |
2025
|
|
|
'data' => [ |
2026
|
|
|
['type' => 'books', 'id' => '1'], |
2027
|
|
|
['type' => 'books', 'id' => '2'], |
2028
|
|
|
], |
2029
|
|
|
], |
2030
|
|
|
], |
2031
|
|
|
], |
2032
|
|
|
], |
2033
|
|
|
]; |
2034
|
|
|
|
2035
|
|
|
$this->assertSame($expected, $scope->toArray()); |
2036
|
|
|
|
2037
|
|
|
$expectedJson = '{"data":[{"type":"books","id":"1","attributes":{"title":"Foo","year":1991},"relationships":{"author":{"data":{"type":"people","id":"1"}}}},{"type":"books","id":"2","attributes":{"title":"Bar","year":2015},"relationships":{"author":{"data":{"type":"people","id":"1"}}}}],"included":[{"type":"people","id":"1","attributes":{"name":"Dave"},"relationships":{"published":{"data":[{"type":"books","id":"1"},{"type":"books","id":"2"}]}}}]}'; |
2038
|
|
|
$this->assertSame($expectedJson, $scope->toJson()); |
2039
|
|
|
} |
2040
|
|
|
|
2041
|
|
|
public function testSerializingCollectionResourceWithPaginator() : void |
2042
|
|
|
{ |
2043
|
|
|
$baseUrl = 'http://example.com'; |
2044
|
|
|
|
2045
|
|
|
$this->manager->setSerializer(new JsonApiSerializer($baseUrl)); |
2046
|
|
|
|
2047
|
|
|
$total = 10; |
2048
|
|
|
$count = 2; |
2049
|
|
|
$perPage = 2; |
2050
|
|
|
$currentPage = 2; |
2051
|
|
|
$lastPage = 5; |
2052
|
|
|
$previousUrl = 'http://example.com/books/?page=1'; |
2053
|
|
|
$currentUrl = 'http://example.com/books/?page=2'; |
2054
|
|
|
$nextUrl = 'http://example.com/books/?page=3'; |
2055
|
|
|
$lastUrl = 'http://example.com/books/?page=5'; |
2056
|
|
|
|
2057
|
|
|
$paginator = Mockery::mock('League\Fractal\Pagination\PaginatorInterface'); |
2058
|
|
|
$paginator->shouldReceive('getCurrentPage')->andReturn($currentPage); |
2059
|
|
|
$paginator->shouldReceive('getLastPage')->andReturn($lastPage); |
2060
|
|
|
$paginator->shouldReceive('getTotal')->andReturn($total); |
2061
|
|
|
$paginator->shouldReceive('getCount')->andReturn($count); |
2062
|
|
|
$paginator->shouldReceive('getPerPage')->andReturn($perPage); |
2063
|
|
|
$paginator->shouldReceive('getUrl')->with(1)->andReturn($previousUrl); |
2064
|
|
|
$paginator->shouldReceive('getUrl')->with(2)->andReturn($currentUrl); |
2065
|
|
|
$paginator->shouldReceive('getUrl')->with(3)->andReturn($nextUrl); |
2066
|
|
|
$paginator->shouldReceive('getUrl')->with(5)->andReturn($lastUrl); |
2067
|
|
|
|
2068
|
|
|
$booksData = [ |
2069
|
|
|
[ |
2070
|
|
|
'id' => 1, |
2071
|
|
|
'title' => 'Foo', |
2072
|
|
|
'year' => '1991', |
2073
|
|
|
'_author' => [ |
2074
|
|
|
'id' => 1, |
2075
|
|
|
'name' => 'Dave', |
2076
|
|
|
], |
2077
|
|
|
], |
2078
|
|
|
[ |
2079
|
|
|
'id' => 2, |
2080
|
|
|
'title' => 'Bar', |
2081
|
|
|
'year' => '1997', |
2082
|
|
|
'_author' => [ |
2083
|
|
|
'id' => 2, |
2084
|
|
|
'name' => 'Bob', |
2085
|
|
|
], |
2086
|
|
|
], |
2087
|
|
|
]; |
2088
|
|
|
|
2089
|
|
|
$resource = new Collection($booksData, new JsonApiBookTransformer(), 'books'); |
2090
|
|
|
$resource->setPaginator($paginator); |
2091
|
|
|
$scope = new Scope($this->manager, $resource); |
2092
|
|
|
|
2093
|
|
|
$expected = [ |
2094
|
|
|
'data' => [ |
2095
|
|
|
[ |
2096
|
|
|
'type' => 'books', |
2097
|
|
|
'id' => '1', |
2098
|
|
|
'attributes' => [ |
2099
|
|
|
'title' => 'Foo', |
2100
|
|
|
'year' => 1991, |
2101
|
|
|
], |
2102
|
|
|
'links' => [ |
2103
|
|
|
'self' => 'http://example.com/books/1', |
2104
|
|
|
], |
2105
|
|
|
'relationships' => [ |
2106
|
|
|
'author' => [ |
2107
|
|
|
'links' => [ |
2108
|
|
|
'self' => 'http://example.com/books/1/relationships/author', |
2109
|
|
|
'related' => 'http://example.com/books/1/author', |
2110
|
|
|
], |
2111
|
|
|
], |
2112
|
|
|
'co-author' => [ |
2113
|
|
|
'links' => [ |
2114
|
|
|
'self' => 'http://example.com/books/1/relationships/co-author', |
2115
|
|
|
'related' => 'http://example.com/books/1/co-author' |
2116
|
|
|
], |
2117
|
|
|
], |
2118
|
|
|
'author-with-meta' => [ |
2119
|
|
|
'links' => [ |
2120
|
|
|
'self' => 'http://example.com/books/1/relationships/author-with-meta', |
2121
|
|
|
'related' => 'http://example.com/books/1/author-with-meta' |
2122
|
|
|
], |
2123
|
|
|
], |
2124
|
|
|
], |
2125
|
|
|
], |
2126
|
|
|
[ |
2127
|
|
|
'type' => 'books', |
2128
|
|
|
'id' => '2', |
2129
|
|
|
'attributes' => [ |
2130
|
|
|
'title' => 'Bar', |
2131
|
|
|
'year' => 1997, |
2132
|
|
|
], |
2133
|
|
|
'links' => [ |
2134
|
|
|
'self' => 'http://example.com/books/2', |
2135
|
|
|
], |
2136
|
|
|
'relationships' => [ |
2137
|
|
|
'author' => [ |
2138
|
|
|
'links' => [ |
2139
|
|
|
'self' => 'http://example.com/books/2/relationships/author', |
2140
|
|
|
'related' => 'http://example.com/books/2/author', |
2141
|
|
|
], |
2142
|
|
|
], |
2143
|
|
|
'co-author' => [ |
2144
|
|
|
'links' => [ |
2145
|
|
|
'self' => 'http://example.com/books/2/relationships/co-author', |
2146
|
|
|
'related' => 'http://example.com/books/2/co-author' |
2147
|
|
|
], |
2148
|
|
|
], |
2149
|
|
|
'author-with-meta' => [ |
2150
|
|
|
'links' => [ |
2151
|
|
|
'self' => 'http://example.com/books/2/relationships/author-with-meta', |
2152
|
|
|
'related' => 'http://example.com/books/2/author-with-meta' |
2153
|
|
|
], |
2154
|
|
|
], |
2155
|
|
|
], |
2156
|
|
|
], |
2157
|
|
|
], |
2158
|
|
|
'meta' => [ |
2159
|
|
|
'pagination' => [ |
2160
|
|
|
'total' => 10, |
2161
|
|
|
'count' => 2, |
2162
|
|
|
'per_page' => 2, |
2163
|
|
|
'current_page' => 2, |
2164
|
|
|
'total_pages' => 5 |
2165
|
|
|
] |
2166
|
|
|
], |
2167
|
|
|
'links' => [ |
2168
|
|
|
'self' => 'http://example.com/books/?page=2', |
2169
|
|
|
'first' => 'http://example.com/books/?page=1', |
2170
|
|
|
'prev' => 'http://example.com/books/?page=1', |
2171
|
|
|
'next' => 'http://example.com/books/?page=3', |
2172
|
|
|
'last' => 'http://example.com/books/?page=5' |
2173
|
|
|
] |
2174
|
|
|
]; |
2175
|
|
|
|
2176
|
|
|
$this->assertSame($expected, $scope->toArray()); |
2177
|
|
|
|
2178
|
|
|
$expectedJson = '{"data":[{"type":"books","id":"1","attributes":{"title":"Foo","year":1991},"links":{"self":"http:\/\/example.com\/books\/1"},"relationships":{"author":{"links":{"self":"http:\/\/example.com\/books\/1\/relationships\/author","related":"http:\/\/example.com\/books\/1\/author"}},"co-author":{"links":{"self":"http:\/\/example.com\/books\/1\/relationships\/co-author","related":"http:\/\/example.com\/books\/1\/co-author"}},"author-with-meta":{"links":{"self":"http:\/\/example.com\/books\/1\/relationships\/author-with-meta","related":"http:\/\/example.com\/books\/1\/author-with-meta"}}}},{"type":"books","id":"2","attributes":{"title":"Bar","year":1997},"links":{"self":"http:\/\/example.com\/books\/2"},"relationships":{"author":{"links":{"self":"http:\/\/example.com\/books\/2\/relationships\/author","related":"http:\/\/example.com\/books\/2\/author"}},"co-author":{"links":{"self":"http:\/\/example.com\/books\/2\/relationships\/co-author","related":"http:\/\/example.com\/books\/2\/co-author"}},"author-with-meta":{"links":{"self":"http:\/\/example.com\/books\/2\/relationships\/author-with-meta","related":"http:\/\/example.com\/books\/2\/author-with-meta"}}}}],"meta":{"pagination":{"total":10,"count":2,"per_page":2,"current_page":2,"total_pages":5}},"links":{"self":"http:\/\/example.com\/books\/?page=2","first":"http:\/\/example.com\/books\/?page=1","prev":"http:\/\/example.com\/books\/?page=1","next":"http:\/\/example.com\/books\/?page=3","last":"http:\/\/example.com\/books\/?page=5"}}'; |
2179
|
|
|
$this->assertSame($expectedJson, $scope->toJson()); |
2180
|
|
|
} |
2181
|
|
|
|
2182
|
|
|
public function testSerializingCollectionResourceWithPaginatorWithOmittedUnavailablePreviousLink() |
2183
|
|
|
{ |
2184
|
|
|
$baseUrl = 'http://example.com'; |
2185
|
|
|
|
2186
|
|
|
$this->manager->setSerializer(new JsonApiSerializer($baseUrl)); |
2187
|
|
|
|
2188
|
|
|
$total = 10; |
2189
|
|
|
$count = 2; |
2190
|
|
|
$perPage = 2; |
2191
|
|
|
$currentPage = 1; |
2192
|
|
|
$lastPage = 5; |
2193
|
|
|
$currentUrl = 'http://example.com/books/?page=1'; |
2194
|
|
|
$nextUrl = 'http://example.com/books/?page=2'; |
2195
|
|
|
$lastUrl = 'http://example.com/books/?page=5'; |
2196
|
|
|
|
2197
|
|
|
$paginator = Mockery::mock('League\Fractal\Pagination\PaginatorInterface'); |
2198
|
|
|
$paginator->shouldReceive('getCurrentPage')->andReturn($currentPage); |
2199
|
|
|
$paginator->shouldReceive('getLastPage')->andReturn($lastPage); |
2200
|
|
|
$paginator->shouldReceive('getTotal')->andReturn($total); |
2201
|
|
|
$paginator->shouldReceive('getCount')->andReturn($count); |
2202
|
|
|
$paginator->shouldReceive('getPerPage')->andReturn($perPage); |
2203
|
|
|
$paginator->shouldReceive('getUrl')->with(1)->andReturn($currentUrl); |
2204
|
|
|
$paginator->shouldReceive('getUrl')->with(2)->andReturn($nextUrl); |
2205
|
|
|
$paginator->shouldReceive('getUrl')->with(5)->andReturn($lastUrl); |
2206
|
|
|
|
2207
|
|
|
$booksData = [ |
2208
|
|
|
[ |
2209
|
|
|
'id' => 1, |
2210
|
|
|
'title' => 'Foo', |
2211
|
|
|
'year' => '1991', |
2212
|
|
|
'_author' => [ |
2213
|
|
|
'id' => 1, |
2214
|
|
|
'name' => 'Dave', |
2215
|
|
|
], |
2216
|
|
|
], |
2217
|
|
|
[ |
2218
|
|
|
'id' => 2, |
2219
|
|
|
'title' => 'Bar', |
2220
|
|
|
'year' => '1997', |
2221
|
|
|
'_author' => [ |
2222
|
|
|
'id' => 2, |
2223
|
|
|
'name' => 'Bob', |
2224
|
|
|
], |
2225
|
|
|
], |
2226
|
|
|
]; |
2227
|
|
|
|
2228
|
|
|
$resource = new Collection($booksData, new JsonApiBookTransformer(), 'books'); |
2229
|
|
|
$resource->setPaginator($paginator); |
2230
|
|
|
$scope = new Scope($this->manager, $resource); |
2231
|
|
|
|
2232
|
|
|
$expected = [ |
2233
|
|
|
'data' => [ |
2234
|
|
|
[ |
2235
|
|
|
'type' => 'books', |
2236
|
|
|
'id' => '1', |
2237
|
|
|
'attributes' => [ |
2238
|
|
|
'title' => 'Foo', |
2239
|
|
|
'year' => 1991, |
2240
|
|
|
], |
2241
|
|
|
'links' => [ |
2242
|
|
|
'self' => 'http://example.com/books/1', |
2243
|
|
|
], |
2244
|
|
|
'relationships' => [ |
2245
|
|
|
'author' => [ |
2246
|
|
|
'links' => [ |
2247
|
|
|
'self' => 'http://example.com/books/1/relationships/author', |
2248
|
|
|
'related' => 'http://example.com/books/1/author', |
2249
|
|
|
], |
2250
|
|
|
], |
2251
|
|
|
'co-author' => [ |
2252
|
|
|
'links' => [ |
2253
|
|
|
'self' => 'http://example.com/books/1/relationships/co-author', |
2254
|
|
|
'related' => 'http://example.com/books/1/co-author' |
2255
|
|
|
], |
2256
|
|
|
], |
2257
|
|
|
'author-with-meta' => [ |
2258
|
|
|
'links' => [ |
2259
|
|
|
'self' => 'http://example.com/books/1/relationships/author-with-meta', |
2260
|
|
|
'related' => 'http://example.com/books/1/author-with-meta' |
2261
|
|
|
], |
2262
|
|
|
], |
2263
|
|
|
], |
2264
|
|
|
], |
2265
|
|
|
[ |
2266
|
|
|
'type' => 'books', |
2267
|
|
|
'id' => '2', |
2268
|
|
|
'attributes' => [ |
2269
|
|
|
'title' => 'Bar', |
2270
|
|
|
'year' => 1997, |
2271
|
|
|
], |
2272
|
|
|
'links' => [ |
2273
|
|
|
'self' => 'http://example.com/books/2', |
2274
|
|
|
], |
2275
|
|
|
'relationships' => [ |
2276
|
|
|
'author' => [ |
2277
|
|
|
'links' => [ |
2278
|
|
|
'self' => 'http://example.com/books/2/relationships/author', |
2279
|
|
|
'related' => 'http://example.com/books/2/author', |
2280
|
|
|
], |
2281
|
|
|
], |
2282
|
|
|
'co-author' => [ |
2283
|
|
|
'links' => [ |
2284
|
|
|
'self' => 'http://example.com/books/2/relationships/co-author', |
2285
|
|
|
'related' => 'http://example.com/books/2/co-author' |
2286
|
|
|
], |
2287
|
|
|
], |
2288
|
|
|
'author-with-meta' => [ |
2289
|
|
|
'links' => [ |
2290
|
|
|
'self' => 'http://example.com/books/2/relationships/author-with-meta', |
2291
|
|
|
'related' => 'http://example.com/books/2/author-with-meta' |
2292
|
|
|
], |
2293
|
|
|
], |
2294
|
|
|
], |
2295
|
|
|
], |
2296
|
|
|
], |
2297
|
|
|
'meta' => [ |
2298
|
|
|
'pagination' => [ |
2299
|
|
|
'total' => 10, |
2300
|
|
|
'count' => 2, |
2301
|
|
|
'per_page' => 2, |
2302
|
|
|
'current_page' => 1, |
2303
|
|
|
'total_pages' => 5 |
2304
|
|
|
] |
2305
|
|
|
], |
2306
|
|
|
'links' => [ |
2307
|
|
|
'self' => 'http://example.com/books/?page=1', |
2308
|
|
|
'first' => 'http://example.com/books/?page=1', |
2309
|
|
|
'next' => 'http://example.com/books/?page=2', |
2310
|
|
|
'last' => 'http://example.com/books/?page=5' |
2311
|
|
|
] |
2312
|
|
|
]; |
2313
|
|
|
|
2314
|
|
|
$this->assertSame($expected, $scope->toArray()); |
2315
|
|
|
|
2316
|
|
|
$expectedJson = '{"data":[{"type":"books","id":"1","attributes":{"title":"Foo","year":1991},"links":{"self":"http:\/\/example.com\/books\/1"},"relationships":{"author":{"links":{"self":"http:\/\/example.com\/books\/1\/relationships\/author","related":"http:\/\/example.com\/books\/1\/author"}},"co-author":{"links":{"self":"http:\/\/example.com\/books\/1\/relationships\/co-author","related":"http:\/\/example.com\/books\/1\/co-author"}},"author-with-meta":{"links":{"self":"http:\/\/example.com\/books\/1\/relationships\/author-with-meta","related":"http:\/\/example.com\/books\/1\/author-with-meta"}}}},{"type":"books","id":"2","attributes":{"title":"Bar","year":1997},"links":{"self":"http:\/\/example.com\/books\/2"},"relationships":{"author":{"links":{"self":"http:\/\/example.com\/books\/2\/relationships\/author","related":"http:\/\/example.com\/books\/2\/author"}},"co-author":{"links":{"self":"http:\/\/example.com\/books\/2\/relationships\/co-author","related":"http:\/\/example.com\/books\/2\/co-author"}},"author-with-meta":{"links":{"self":"http:\/\/example.com\/books\/2\/relationships\/author-with-meta","related":"http:\/\/example.com\/books\/2\/author-with-meta"}}}}],"meta":{"pagination":{"total":10,"count":2,"per_page":2,"current_page":1,"total_pages":5}},"links":{"self":"http:\/\/example.com\/books\/?page=1","first":"http:\/\/example.com\/books\/?page=1","next":"http:\/\/example.com\/books\/?page=2","last":"http:\/\/example.com\/books\/?page=5"}}'; |
2317
|
|
|
$this->assertSame($expectedJson, $scope->toJson()); |
2318
|
|
|
} |
2319
|
|
|
|
2320
|
|
|
public function testSerializingCollectionResourceWithPaginatorWithOmittedUnavailableNextLink() : void |
2321
|
|
|
{ |
2322
|
|
|
$baseUrl = 'http://example.com'; |
2323
|
|
|
|
2324
|
|
|
$this->manager->setSerializer(new JsonApiSerializer($baseUrl)); |
2325
|
|
|
|
2326
|
|
|
$total = 10; |
2327
|
|
|
$count = 2; |
2328
|
|
|
$perPage = 2; |
2329
|
|
|
$currentPage = 5; |
2330
|
|
|
$lastPage = 5; |
2331
|
|
|
$firstUrl = 'http://example.com/books/?page=1'; |
2332
|
|
|
$previousUrl = 'http://example.com/books/?page=4'; |
2333
|
|
|
$lastUrl = 'http://example.com/books/?page=5'; |
2334
|
|
|
|
2335
|
|
|
$paginator = Mockery::mock('League\Fractal\Pagination\PaginatorInterface'); |
2336
|
|
|
$paginator->shouldReceive('getCurrentPage')->andReturn($currentPage); |
2337
|
|
|
$paginator->shouldReceive('getLastPage')->andReturn($lastPage); |
2338
|
|
|
$paginator->shouldReceive('getTotal')->andReturn($total); |
2339
|
|
|
$paginator->shouldReceive('getCount')->andReturn($count); |
2340
|
|
|
$paginator->shouldReceive('getPerPage')->andReturn($perPage); |
2341
|
|
|
$paginator->shouldReceive('getUrl')->with(1)->andReturn($firstUrl); |
2342
|
|
|
$paginator->shouldReceive('getUrl')->with(4)->andReturn($previousUrl); |
2343
|
|
|
$paginator->shouldReceive('getUrl')->with(5)->andReturn($lastUrl); |
2344
|
|
|
|
2345
|
|
|
$booksData = [ |
2346
|
|
|
[ |
2347
|
|
|
'id' => 1, |
2348
|
|
|
'title' => 'Foo', |
2349
|
|
|
'year' => '1991', |
2350
|
|
|
'_author' => [ |
2351
|
|
|
'id' => 1, |
2352
|
|
|
'name' => 'Dave', |
2353
|
|
|
], |
2354
|
|
|
], |
2355
|
|
|
[ |
2356
|
|
|
'id' => 2, |
2357
|
|
|
'title' => 'Bar', |
2358
|
|
|
'year' => '1997', |
2359
|
|
|
'_author' => [ |
2360
|
|
|
'id' => 2, |
2361
|
|
|
'name' => 'Bob', |
2362
|
|
|
], |
2363
|
|
|
], |
2364
|
|
|
]; |
2365
|
|
|
|
2366
|
|
|
$resource = new Collection($booksData, new JsonApiBookTransformer(), 'books'); |
2367
|
|
|
$resource->setPaginator($paginator); |
2368
|
|
|
$scope = new Scope($this->manager, $resource); |
2369
|
|
|
|
2370
|
|
|
$expected = [ |
2371
|
|
|
'data' => [ |
2372
|
|
|
[ |
2373
|
|
|
'type' => 'books', |
2374
|
|
|
'id' => '1', |
2375
|
|
|
'attributes' => [ |
2376
|
|
|
'title' => 'Foo', |
2377
|
|
|
'year' => 1991, |
2378
|
|
|
], |
2379
|
|
|
'links' => [ |
2380
|
|
|
'self' => 'http://example.com/books/1', |
2381
|
|
|
], |
2382
|
|
|
'relationships' => [ |
2383
|
|
|
'author' => [ |
2384
|
|
|
'links' => [ |
2385
|
|
|
'self' => 'http://example.com/books/1/relationships/author', |
2386
|
|
|
'related' => 'http://example.com/books/1/author', |
2387
|
|
|
], |
2388
|
|
|
], |
2389
|
|
|
'co-author' => [ |
2390
|
|
|
'links' => [ |
2391
|
|
|
'self' => 'http://example.com/books/1/relationships/co-author', |
2392
|
|
|
'related' => 'http://example.com/books/1/co-author' |
2393
|
|
|
], |
2394
|
|
|
], |
2395
|
|
|
'author-with-meta' => [ |
2396
|
|
|
'links' => [ |
2397
|
|
|
'self' => 'http://example.com/books/1/relationships/author-with-meta', |
2398
|
|
|
'related' => 'http://example.com/books/1/author-with-meta' |
2399
|
|
|
], |
2400
|
|
|
], |
2401
|
|
|
], |
2402
|
|
|
], |
2403
|
|
|
[ |
2404
|
|
|
'type' => 'books', |
2405
|
|
|
'id' => '2', |
2406
|
|
|
'attributes' => [ |
2407
|
|
|
'title' => 'Bar', |
2408
|
|
|
'year' => 1997, |
2409
|
|
|
], |
2410
|
|
|
'links' => [ |
2411
|
|
|
'self' => 'http://example.com/books/2', |
2412
|
|
|
], |
2413
|
|
|
'relationships' => [ |
2414
|
|
|
'author' => [ |
2415
|
|
|
'links' => [ |
2416
|
|
|
'self' => 'http://example.com/books/2/relationships/author', |
2417
|
|
|
'related' => 'http://example.com/books/2/author', |
2418
|
|
|
], |
2419
|
|
|
], |
2420
|
|
|
'co-author' => [ |
2421
|
|
|
'links' => [ |
2422
|
|
|
'self' => 'http://example.com/books/2/relationships/co-author', |
2423
|
|
|
'related' => 'http://example.com/books/2/co-author' |
2424
|
|
|
], |
2425
|
|
|
], |
2426
|
|
|
'author-with-meta' => [ |
2427
|
|
|
'links' => [ |
2428
|
|
|
'self' => 'http://example.com/books/2/relationships/author-with-meta', |
2429
|
|
|
'related' => 'http://example.com/books/2/author-with-meta' |
2430
|
|
|
], |
2431
|
|
|
], |
2432
|
|
|
], |
2433
|
|
|
], |
2434
|
|
|
], |
2435
|
|
|
'meta' => [ |
2436
|
|
|
'pagination' => [ |
2437
|
|
|
'total' => 10, |
2438
|
|
|
'count' => 2, |
2439
|
|
|
'per_page' => 2, |
2440
|
|
|
'current_page' => 5, |
2441
|
|
|
'total_pages' => 5 |
2442
|
|
|
] |
2443
|
|
|
], |
2444
|
|
|
'links' => [ |
2445
|
|
|
'self' => 'http://example.com/books/?page=5', |
2446
|
|
|
'first' => 'http://example.com/books/?page=1', |
2447
|
|
|
'prev' => 'http://example.com/books/?page=4', |
2448
|
|
|
'last' => 'http://example.com/books/?page=5' |
2449
|
|
|
] |
2450
|
|
|
]; |
2451
|
|
|
|
2452
|
|
|
$this->assertSame($expected, $scope->toArray()); |
2453
|
|
|
|
2454
|
|
|
$expectedJson = '{"data":[{"type":"books","id":"1","attributes":{"title":"Foo","year":1991},"links":{"self":"http:\/\/example.com\/books\/1"},"relationships":{"author":{"links":{"self":"http:\/\/example.com\/books\/1\/relationships\/author","related":"http:\/\/example.com\/books\/1\/author"}},"co-author":{"links":{"self":"http:\/\/example.com\/books\/1\/relationships\/co-author","related":"http:\/\/example.com\/books\/1\/co-author"}},"author-with-meta":{"links":{"self":"http:\/\/example.com\/books\/1\/relationships\/author-with-meta","related":"http:\/\/example.com\/books\/1\/author-with-meta"}}}},{"type":"books","id":"2","attributes":{"title":"Bar","year":1997},"links":{"self":"http:\/\/example.com\/books\/2"},"relationships":{"author":{"links":{"self":"http:\/\/example.com\/books\/2\/relationships\/author","related":"http:\/\/example.com\/books\/2\/author"}},"co-author":{"links":{"self":"http:\/\/example.com\/books\/2\/relationships\/co-author","related":"http:\/\/example.com\/books\/2\/co-author"}},"author-with-meta":{"links":{"self":"http:\/\/example.com\/books\/2\/relationships\/author-with-meta","related":"http:\/\/example.com\/books\/2\/author-with-meta"}}}}],"meta":{"pagination":{"total":10,"count":2,"per_page":2,"current_page":5,"total_pages":5}},"links":{"self":"http:\/\/example.com\/books\/?page=5","first":"http:\/\/example.com\/books\/?page=1","prev":"http:\/\/example.com\/books\/?page=4","last":"http:\/\/example.com\/books\/?page=5"}}'; |
2455
|
|
|
$this->assertSame($expectedJson, $scope->toJson()); |
2456
|
|
|
} |
2457
|
|
|
|
2458
|
|
|
public function testCustomLinkMerge() : void |
2459
|
|
|
{ |
2460
|
|
|
$manager = new Manager(); |
2461
|
|
|
$manager->setSerializer(new JsonApiSerializer('http://test.de')); |
2462
|
|
|
|
2463
|
|
|
$bookData = [ |
2464
|
|
|
'id' => 1, |
2465
|
|
|
'title' => 'Foo', |
2466
|
|
|
'year' => '1991', |
2467
|
|
|
'_author' => [ |
2468
|
|
|
'id' => 1, |
2469
|
|
|
'name' => 'Dave', |
2470
|
|
|
], |
2471
|
|
|
'links' => [ |
2472
|
|
|
'custom_link' => '/custom/link', |
2473
|
|
|
], |
2474
|
|
|
]; |
2475
|
|
|
|
2476
|
|
|
$resource = new Item($bookData, new JsonApiBookTransformer(), 'books'); |
2477
|
|
|
|
2478
|
|
|
$scope = new Scope($manager, $resource); |
2479
|
|
|
|
2480
|
|
|
$expected = [ |
2481
|
|
|
'data' => [ |
2482
|
|
|
'type' => 'books', |
2483
|
|
|
'id' => '1', |
2484
|
|
|
'attributes' => [ |
2485
|
|
|
'title' => 'Foo', |
2486
|
|
|
'year' => 1991, |
2487
|
|
|
], |
2488
|
|
|
'links' => [ |
2489
|
|
|
'self' => 'http://test.de/books/1', |
2490
|
|
|
'custom_link' => '/custom/link', |
2491
|
|
|
], |
2492
|
|
|
'relationships' => [ |
2493
|
|
|
'author' => [ |
2494
|
|
|
'links' => [ |
2495
|
|
|
'self' => 'http://test.de/books/1/relationships/author', |
2496
|
|
|
'related' => 'http://test.de/books/1/author', |
2497
|
|
|
], |
2498
|
|
|
], |
2499
|
|
|
'co-author' => [ |
2500
|
|
|
'links' => [ |
2501
|
|
|
'self' => 'http://test.de/books/1/relationships/co-author', |
2502
|
|
|
'related' => 'http://test.de/books/1/co-author' |
2503
|
|
|
], |
2504
|
|
|
], |
2505
|
|
|
'author-with-meta' => [ |
2506
|
|
|
'links' => [ |
2507
|
|
|
'self' => 'http://test.de/books/1/relationships/author-with-meta', |
2508
|
|
|
'related' => 'http://test.de/books/1/author-with-meta' |
2509
|
|
|
], |
2510
|
|
|
], |
2511
|
|
|
], |
2512
|
|
|
], |
2513
|
|
|
]; |
2514
|
|
|
|
2515
|
|
|
$this->assertSame(json_encode($expected), $scope->toJson()); |
2516
|
|
|
} |
2517
|
|
|
|
2518
|
|
|
public function testCustomLinkMergeNoLink() : void |
2519
|
|
|
{ |
2520
|
|
|
$manager = new Manager(); |
2521
|
|
|
$manager->setSerializer(new JsonApiSerializer('http://test.de')); |
2522
|
|
|
|
2523
|
|
|
$bookData = [ |
2524
|
|
|
'id' => 1, |
2525
|
|
|
'title' => 'Foo', |
2526
|
|
|
'year' => '1991', |
2527
|
|
|
'_author' => [ |
2528
|
|
|
'id' => 1, |
2529
|
|
|
'name' => 'Dave', |
2530
|
|
|
], |
2531
|
|
|
]; |
2532
|
|
|
|
2533
|
|
|
$resource = new Item($bookData, new JsonApiBookTransformer(), 'books'); |
2534
|
|
|
|
2535
|
|
|
$scope = new Scope($manager, $resource); |
2536
|
|
|
|
2537
|
|
|
$expected = [ |
2538
|
|
|
'data' => [ |
2539
|
|
|
'type' => 'books', |
2540
|
|
|
'id' => '1', |
2541
|
|
|
'attributes' => [ |
2542
|
|
|
'title' => 'Foo', |
2543
|
|
|
'year' => 1991, |
2544
|
|
|
], |
2545
|
|
|
'links' => [ |
2546
|
|
|
'self' => 'http://test.de/books/1', |
2547
|
|
|
], |
2548
|
|
|
'relationships' => [ |
2549
|
|
|
'author' => [ |
2550
|
|
|
'links' => [ |
2551
|
|
|
'self' => 'http://test.de/books/1/relationships/author', |
2552
|
|
|
'related' => 'http://test.de/books/1/author', |
2553
|
|
|
], |
2554
|
|
|
], |
2555
|
|
|
'co-author' => [ |
2556
|
|
|
'links' => [ |
2557
|
|
|
'self' => 'http://test.de/books/1/relationships/co-author', |
2558
|
|
|
'related' => 'http://test.de/books/1/co-author' |
2559
|
|
|
], |
2560
|
|
|
], |
2561
|
|
|
'author-with-meta' => [ |
2562
|
|
|
'links' => [ |
2563
|
|
|
'self' => 'http://test.de/books/1/relationships/author-with-meta', |
2564
|
|
|
'related' => 'http://test.de/books/1/author-with-meta' |
2565
|
|
|
], |
2566
|
|
|
], |
2567
|
|
|
], |
2568
|
|
|
], |
2569
|
|
|
]; |
2570
|
|
|
|
2571
|
|
|
$this->assertSame(json_encode($expected), $scope->toJson()); |
2572
|
|
|
} |
2573
|
|
|
|
2574
|
|
|
|
2575
|
|
|
|
2576
|
|
|
public function testEmptyAttributesIsObject() |
2577
|
|
|
{ |
2578
|
|
|
$manager = new Manager(); |
2579
|
|
|
$manager->setSerializer(new JsonApiSerializer()); |
2580
|
|
|
|
2581
|
|
|
$data = ['id' => 1]; |
2582
|
|
|
|
2583
|
|
|
$resource = new Item($data, new JsonApiEmptyTransformer(), 'resources'); |
2584
|
|
|
|
2585
|
|
|
$scope = new Scope($manager, $resource); |
2586
|
|
|
|
2587
|
|
|
$expectedJson = '{"data":{"type":"resources","id":"1","attributes":{}}}'; |
2588
|
|
|
|
2589
|
|
|
$this->assertSame($expectedJson, $scope->toJson()); |
2590
|
|
|
} |
2591
|
|
|
|
2592
|
|
|
/** |
2593
|
|
|
* @dataProvider serializingWithFieldsetsProvider |
2594
|
|
|
*/ |
2595
|
|
|
public function testSerializingWithFieldsets($fieldsetsToParse, $expected) : void |
2596
|
|
|
{ |
2597
|
|
|
$this->manager->parseIncludes(['author', 'author.published']); |
2598
|
|
|
|
2599
|
|
|
$bookData = [ |
2600
|
|
|
'id' => 1, |
2601
|
|
|
'title' => 'Foo', |
2602
|
|
|
'year' => '1991', |
2603
|
|
|
'_author' => [ |
2604
|
|
|
'id' => 1, |
2605
|
|
|
'name' => 'Dave', |
2606
|
|
|
'_published' => [ |
2607
|
|
|
[ |
2608
|
|
|
'id' => 1, |
2609
|
|
|
'title' => 'Foo', |
2610
|
|
|
'year' => '1991', |
2611
|
|
|
], |
2612
|
|
|
[ |
2613
|
|
|
'id' => 2, |
2614
|
|
|
'title' => 'Bar', |
2615
|
|
|
'year' => '2015', |
2616
|
|
|
], |
2617
|
|
|
], |
2618
|
|
|
], |
2619
|
|
|
]; |
2620
|
|
|
|
2621
|
|
|
$resource = new Item($bookData, new JsonApiBookTransformer(), 'books'); |
2622
|
|
|
|
2623
|
|
|
$scope = new Scope($this->manager, $resource); |
2624
|
|
|
|
2625
|
|
|
$this->manager->parseFieldsets($fieldsetsToParse); |
2626
|
|
|
$this->assertSame($expected, $scope->toArray()); |
2627
|
|
|
} |
2628
|
|
|
|
2629
|
|
|
public function serializingWithFieldsetsProvider() : array |
2630
|
|
|
{ |
2631
|
|
|
return [ |
2632
|
|
|
[ |
2633
|
|
|
//Single field |
2634
|
|
|
['books' => 'title'], |
2635
|
|
|
[ |
2636
|
|
|
'data' => [ |
2637
|
|
|
'type' => 'books', |
2638
|
|
|
'id' => '1', |
2639
|
|
|
'attributes' => [ |
2640
|
|
|
'title' => 'Foo' |
2641
|
|
|
] |
2642
|
|
|
] |
2643
|
|
|
] |
2644
|
|
|
], |
2645
|
|
|
[ |
2646
|
|
|
//Multiple fields |
2647
|
|
|
['books' => 'title,year'], |
2648
|
|
|
[ |
2649
|
|
|
'data' => [ |
2650
|
|
|
'type' => 'books', |
2651
|
|
|
'id' => '1', |
2652
|
|
|
'attributes' => [ |
2653
|
|
|
'title' => 'Foo', |
2654
|
|
|
'year' => 1991 |
2655
|
|
|
] |
2656
|
|
|
] |
2657
|
|
|
] |
2658
|
|
|
], |
2659
|
|
|
[ |
2660
|
|
|
//Include 1st level relationship |
2661
|
|
|
['books' => 'title,author', 'people' => 'name'], |
2662
|
|
|
[ |
2663
|
|
|
'data' => [ |
2664
|
|
|
'type' => 'books', |
2665
|
|
|
'id' => '1', |
2666
|
|
|
'attributes' => [ |
2667
|
|
|
'title' => 'Foo' |
2668
|
|
|
], |
2669
|
|
|
'relationships' => [ |
2670
|
|
|
'author' => [ |
2671
|
|
|
'data' => [ |
2672
|
|
|
'type' => 'people', |
2673
|
|
|
'id' => '1' |
2674
|
|
|
] |
2675
|
|
|
] |
2676
|
|
|
] |
2677
|
|
|
], |
2678
|
|
|
'included' => [ |
2679
|
|
|
[ |
2680
|
|
|
'type' => 'people', |
2681
|
|
|
'id' => '1', |
2682
|
|
|
'attributes' => [ |
2683
|
|
|
'name' => 'Dave' |
2684
|
|
|
] |
2685
|
|
|
] |
2686
|
|
|
] |
2687
|
|
|
] |
2688
|
|
|
], |
2689
|
|
|
[ |
2690
|
|
|
//Include 2nd level relationship |
2691
|
|
|
['books' => 'title,author', 'people' => 'name,published'], |
2692
|
|
|
[ |
2693
|
|
|
'data' => [ |
2694
|
|
|
'type' => 'books', |
2695
|
|
|
'id' => '1', |
2696
|
|
|
'attributes' => [ |
2697
|
|
|
'title' => 'Foo' |
2698
|
|
|
], |
2699
|
|
|
'relationships' => [ |
2700
|
|
|
'author' => [ |
2701
|
|
|
'data' => [ |
2702
|
|
|
'type' => 'people', |
2703
|
|
|
'id' => '1' |
2704
|
|
|
] |
2705
|
|
|
] |
2706
|
|
|
] |
2707
|
|
|
], |
2708
|
|
|
'included' => [ |
2709
|
|
|
[ |
2710
|
|
|
'type' => 'books', |
2711
|
|
|
'id' => '2', |
2712
|
|
|
'attributes' => [ |
2713
|
|
|
'title' => 'Bar' |
2714
|
|
|
] |
2715
|
|
|
], |
2716
|
|
|
[ |
2717
|
|
|
'type' => 'people', |
2718
|
|
|
'id' => '1', |
2719
|
|
|
'attributes' => [ |
2720
|
|
|
'name' => 'Dave' |
2721
|
|
|
], |
2722
|
|
|
'relationships' => [ |
2723
|
|
|
'published' => [ |
2724
|
|
|
'data' => [ |
2725
|
|
|
[ |
2726
|
|
|
'type' => 'books', |
2727
|
|
|
'id' => '1' |
2728
|
|
|
], |
2729
|
|
|
[ |
2730
|
|
|
'type' => 'books', |
2731
|
|
|
'id' => '2' |
2732
|
|
|
] |
2733
|
|
|
] |
2734
|
|
|
] |
2735
|
|
|
] |
2736
|
|
|
] |
2737
|
|
|
] |
2738
|
|
|
] |
2739
|
|
|
] |
2740
|
|
|
]; |
2741
|
|
|
} |
2742
|
|
|
} |
2743
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.