1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use League\Fractal\Manager; |
4
|
|
|
use League\Fractal\Resource\Collection; |
5
|
|
|
use League\Fractal\Resource\Item; |
6
|
|
|
use League\Fractal\Scope; |
7
|
|
|
use League\Fractal\Serializer\JsonApiSerializer; |
8
|
|
|
use League\Fractal\Test\Stub\Transformer\JsonApiAuthorTransformer; |
9
|
|
|
use League\Fractal\Test\Stub\Transformer\JsonApiBookTransformer; |
10
|
|
|
|
11
|
|
|
class JsonApiSerializerTest extends PHPUnit_Framework_TestCase |
12
|
|
|
{ |
13
|
|
|
private $manager; |
14
|
|
|
|
15
|
|
|
public function setUp() |
16
|
|
|
{ |
17
|
|
|
$this->manager = new Manager(); |
18
|
|
|
$this->manager->setSerializer(new JsonApiSerializer()); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
public function testSerializingItemResourceWithHasOneInclude() |
22
|
|
|
{ |
23
|
|
|
$this->manager->parseIncludes('author'); |
24
|
|
|
|
25
|
|
|
$bookData = [ |
26
|
|
|
'id' => 1, |
27
|
|
|
'title' => 'Foo', |
28
|
|
|
'year' => '1991', |
29
|
|
|
'_author' => [ |
30
|
|
|
'id' => 1, |
31
|
|
|
'name' => 'Dave', |
32
|
|
|
], |
33
|
|
|
]; |
34
|
|
|
|
35
|
|
|
$resource = new Item($bookData, new JsonApiBookTransformer(), 'books'); |
36
|
|
|
|
37
|
|
|
$scope = new Scope($this->manager, $resource); |
38
|
|
|
|
39
|
|
|
$expected = [ |
40
|
|
|
'data' => [ |
41
|
|
|
'type' => 'books', |
42
|
|
|
'id' => '1', |
43
|
|
|
'attributes' => [ |
44
|
|
|
'title' => 'Foo', |
45
|
|
|
'year' => 1991, |
46
|
|
|
], |
47
|
|
|
'relationships' => [ |
48
|
|
|
'author' => [ |
49
|
|
|
'data' => [ |
50
|
|
|
'type' => 'people', |
51
|
|
|
'id' => '1', |
52
|
|
|
], |
53
|
|
|
], |
54
|
|
|
], |
55
|
|
|
], |
56
|
|
|
'included' => [ |
57
|
|
|
[ |
58
|
|
|
'type' => 'people', |
59
|
|
|
'id' => '1', |
60
|
|
|
'attributes' => [ |
61
|
|
|
'name' => 'Dave', |
62
|
|
|
], |
63
|
|
|
], |
64
|
|
|
], |
65
|
|
|
]; |
66
|
|
|
|
67
|
|
|
$this->assertSame($expected, $scope->toArray()); |
68
|
|
|
|
69
|
|
|
$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"}}]}'; |
70
|
|
|
$this->assertSame($expectedJson, $scope->toJson()); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
View Code Duplication |
public function testSerializingItemResourceWithHasOneDasherizedInclude() |
74
|
|
|
{ |
75
|
|
|
$this->manager->parseIncludes('co-author'); |
76
|
|
|
|
77
|
|
|
$bookData = [ |
78
|
|
|
'id' => 1, |
79
|
|
|
'title' => 'Foo', |
80
|
|
|
'year' => '1991', |
81
|
|
|
'_author' => [ |
82
|
|
|
'id' => 1, |
83
|
|
|
'name' => 'Dave', |
84
|
|
|
], |
85
|
|
|
'_co_author' => [ |
86
|
|
|
'id' => 2, |
87
|
|
|
'name' => 'Jim', |
88
|
|
|
], |
89
|
|
|
]; |
90
|
|
|
|
91
|
|
|
$resource = new Item($bookData, new JsonApiBookTransformer(), 'books'); |
92
|
|
|
|
93
|
|
|
$scope = new Scope($this->manager, $resource); |
94
|
|
|
|
95
|
|
|
$expected = [ |
96
|
|
|
'data' => [ |
97
|
|
|
'type' => 'books', |
98
|
|
|
'id' => '1', |
99
|
|
|
'attributes' => [ |
100
|
|
|
'title' => 'Foo', |
101
|
|
|
'year' => 1991, |
102
|
|
|
], |
103
|
|
|
'relationships' => [ |
104
|
|
|
'co-author' => [ |
105
|
|
|
'data' => [ |
106
|
|
|
'type' => 'people', |
107
|
|
|
'id' => '2', |
108
|
|
|
], |
109
|
|
|
], |
110
|
|
|
], |
111
|
|
|
], |
112
|
|
|
'included' => [ |
113
|
|
|
[ |
114
|
|
|
'type' => 'people', |
115
|
|
|
'id' => '2', |
116
|
|
|
'attributes' => [ |
117
|
|
|
'name' => 'Jim', |
118
|
|
|
], |
119
|
|
|
], |
120
|
|
|
], |
121
|
|
|
]; |
122
|
|
|
|
123
|
|
|
$this->assertSame($expected, $scope->toArray()); |
124
|
|
|
|
125
|
|
|
$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"}}]}'; |
126
|
|
|
$this->assertSame($expectedJson, $scope->toJson()); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
public function testSerializingItemResourceWithEmptyHasOneInclude() |
130
|
|
|
{ |
131
|
|
|
$this->manager->parseIncludes('author'); |
132
|
|
|
|
133
|
|
|
$bookData = [ |
134
|
|
|
'id' => 1, |
135
|
|
|
'title' => 'Foo', |
136
|
|
|
'year' => '1991', |
137
|
|
|
'_author' => null, |
138
|
|
|
]; |
139
|
|
|
|
140
|
|
|
$resource = new Item($bookData, new JsonApiBookTransformer(), 'books'); |
141
|
|
|
|
142
|
|
|
$scope = new Scope($this->manager, $resource); |
143
|
|
|
|
144
|
|
|
$expected = [ |
145
|
|
|
'data' => [ |
146
|
|
|
'type' => 'books', |
147
|
|
|
'id' => '1', |
148
|
|
|
'attributes' => [ |
149
|
|
|
'title' => 'Foo', |
150
|
|
|
'year' => 1991, |
151
|
|
|
], |
152
|
|
|
'relationships' => [ |
153
|
|
|
'author' => [ |
154
|
|
|
'data' => null, |
155
|
|
|
], |
156
|
|
|
], |
157
|
|
|
], |
158
|
|
|
]; |
159
|
|
|
|
160
|
|
|
$this->assertSame($expected, $scope->toArray()); |
161
|
|
|
|
162
|
|
|
$expectedJson = '{"data":{"type":"books","id":"1","attributes":{"title":"Foo","year":1991},"relationships":{"author":{"data":null}}}}'; |
163
|
|
|
$this->assertSame($expectedJson, $scope->toJson()); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
public function testSerializingItemResourceWithHasManyInclude() |
167
|
|
|
{ |
168
|
|
|
$this->manager->parseIncludes('published'); |
169
|
|
|
|
170
|
|
|
$authorData = [ |
171
|
|
|
'id' => 1, |
172
|
|
|
'name' => 'Dave', |
173
|
|
|
'_published' => [ |
174
|
|
|
[ |
175
|
|
|
'id' => 1, |
176
|
|
|
'title' => 'Foo', |
177
|
|
|
'year' => '1991', |
178
|
|
|
], |
179
|
|
|
[ |
180
|
|
|
'id' => 2, |
181
|
|
|
'title' => 'Bar', |
182
|
|
|
'year' => '2015', |
183
|
|
|
], |
184
|
|
|
], |
185
|
|
|
]; |
186
|
|
|
|
187
|
|
|
$resource = new Item($authorData, new JsonApiAuthorTransformer(), 'people'); |
188
|
|
|
|
189
|
|
|
$scope = new Scope($this->manager, $resource); |
190
|
|
|
|
191
|
|
|
$expected = [ |
192
|
|
|
'data' => [ |
193
|
|
|
'type' => 'people', |
194
|
|
|
'id' => '1', |
195
|
|
|
'attributes' => [ |
196
|
|
|
'name' => 'Dave', |
197
|
|
|
], |
198
|
|
|
'relationships' => [ |
199
|
|
|
'published' => [ |
200
|
|
|
'data' => [ |
201
|
|
|
[ |
202
|
|
|
'type' => 'books', |
203
|
|
|
'id' => 1, |
204
|
|
|
], |
205
|
|
|
[ |
206
|
|
|
'type' => 'books', |
207
|
|
|
'id' => 2, |
208
|
|
|
], |
209
|
|
|
], |
210
|
|
|
], |
211
|
|
|
], |
212
|
|
|
], |
213
|
|
|
'included' => [ |
214
|
|
|
[ |
215
|
|
|
'type' => 'books', |
216
|
|
|
'id' => '1', |
217
|
|
|
'attributes' => [ |
218
|
|
|
'title' => 'Foo', |
219
|
|
|
'year' => 1991, |
220
|
|
|
], |
221
|
|
|
], |
222
|
|
|
[ |
223
|
|
|
'type' => 'books', |
224
|
|
|
'id' => '2', |
225
|
|
|
'attributes' => [ |
226
|
|
|
'title' => 'Bar', |
227
|
|
|
'year' => 2015, |
228
|
|
|
], |
229
|
|
|
], |
230
|
|
|
], |
231
|
|
|
]; |
232
|
|
|
|
233
|
|
|
$this->assertEquals($expected, $scope->toArray()); |
234
|
|
|
|
235
|
|
|
$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}}]}'; |
236
|
|
|
$this->assertSame($expectedJson, $scope->toJson()); |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
public function testSerializingItemResourceWithEmptyHasManyInclude() |
240
|
|
|
{ |
241
|
|
|
$this->manager->parseIncludes('published'); |
242
|
|
|
|
243
|
|
|
$authorData = [ |
244
|
|
|
'id' => 1, |
245
|
|
|
'name' => 'Dave', |
246
|
|
|
'_published' => [], |
247
|
|
|
]; |
248
|
|
|
|
249
|
|
|
$resource = new Item($authorData, new JsonApiAuthorTransformer(), 'people'); |
250
|
|
|
|
251
|
|
|
$scope = new Scope($this->manager, $resource); |
252
|
|
|
|
253
|
|
|
$expected = [ |
254
|
|
|
'data' => [ |
255
|
|
|
'type' => 'people', |
256
|
|
|
'id' => '1', |
257
|
|
|
'attributes' => [ |
258
|
|
|
'name' => 'Dave', |
259
|
|
|
], |
260
|
|
|
'relationships' => [ |
261
|
|
|
'published' => [ |
262
|
|
|
'data' => [], |
263
|
|
|
], |
264
|
|
|
], |
265
|
|
|
], |
266
|
|
|
]; |
267
|
|
|
|
268
|
|
|
$this->assertSame($expected, $scope->toArray()); |
269
|
|
|
|
270
|
|
|
$expectedJson = '{"data":{"type":"people","id":"1","attributes":{"name":"Dave"},"relationships":{"published":{"data":[]}}}}'; |
271
|
|
|
$this->assertSame($expectedJson, $scope->toJson()); |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
public function testSerializingItemResourceWithoutIncludes() |
275
|
|
|
{ |
276
|
|
|
$bookData = [ |
277
|
|
|
'id' => 1, |
278
|
|
|
'title' => 'Foo', |
279
|
|
|
'year' => '1991', |
280
|
|
|
'_author' => [ |
281
|
|
|
'id' => 1, |
282
|
|
|
'name' => 'Dave', |
283
|
|
|
], |
284
|
|
|
]; |
285
|
|
|
|
286
|
|
|
$resource = new Item($bookData, new JsonApiBookTransformer(), 'books'); |
287
|
|
|
|
288
|
|
|
$scope = new Scope($this->manager, $resource); |
289
|
|
|
|
290
|
|
|
$expected = [ |
291
|
|
|
'data' => [ |
292
|
|
|
'type' => 'books', |
293
|
|
|
'id' => '1', |
294
|
|
|
'attributes' => [ |
295
|
|
|
'title' => 'Foo', |
296
|
|
|
'year' => 1991, |
297
|
|
|
], |
298
|
|
|
], |
299
|
|
|
]; |
300
|
|
|
|
301
|
|
|
$this->assertSame($expected, $scope->toArray()); |
302
|
|
|
|
303
|
|
|
$expectedJson = '{"data":{"type":"books","id":"1","attributes":{"title":"Foo","year":1991}}}'; |
304
|
|
|
$this->assertSame($expectedJson, $scope->toJson()); |
305
|
|
|
} |
306
|
|
|
|
307
|
|
View Code Duplication |
public function testSerializingItemResourceWithMeta() |
308
|
|
|
{ |
309
|
|
|
$bookData = [ |
310
|
|
|
'id' => 1, |
311
|
|
|
'title' => 'Foo', |
312
|
|
|
'year' => '1991', |
313
|
|
|
'_author' => [ |
314
|
|
|
'id' => 1, |
315
|
|
|
'name' => 'Dave', |
316
|
|
|
], |
317
|
|
|
]; |
318
|
|
|
|
319
|
|
|
$resource = new Item($bookData, new JsonApiBookTransformer(), 'books'); |
320
|
|
|
$resource->setMetaValue('foo', 'bar'); |
321
|
|
|
|
322
|
|
|
$scope = new Scope($this->manager, $resource); |
323
|
|
|
|
324
|
|
|
$expected = [ |
325
|
|
|
'data' => [ |
326
|
|
|
'type' => 'books', |
327
|
|
|
'id' => '1', |
328
|
|
|
'attributes' => [ |
329
|
|
|
'title' => 'Foo', |
330
|
|
|
'year' => 1991, |
331
|
|
|
], |
332
|
|
|
], |
333
|
|
|
'meta' => [ |
334
|
|
|
'foo' => 'bar', |
335
|
|
|
], |
336
|
|
|
]; |
337
|
|
|
|
338
|
|
|
$this->assertSame($expected, $scope->toArray()); |
339
|
|
|
|
340
|
|
|
$expectedJson = '{"data":{"type":"books","id":"1","attributes":{"title":"Foo","year":1991}},"meta":{"foo":"bar"}}'; |
341
|
|
|
$this->assertSame($expectedJson, $scope->toJson()); |
342
|
|
|
} |
343
|
|
|
|
344
|
|
View Code Duplication |
public function testSerializingCollectionResourceWithoutIncludes() |
345
|
|
|
{ |
346
|
|
|
$booksData = [ |
347
|
|
|
[ |
348
|
|
|
'id' => 1, |
349
|
|
|
'title' => 'Foo', |
350
|
|
|
'year' => '1991', |
351
|
|
|
'_author' => [ |
352
|
|
|
'id' => 1, |
353
|
|
|
'name' => 'Dave', |
354
|
|
|
], |
355
|
|
|
], |
356
|
|
|
[ |
357
|
|
|
'id' => 2, |
358
|
|
|
'title' => 'Bar', |
359
|
|
|
'year' => '1997', |
360
|
|
|
'_author' => [ |
361
|
|
|
'id' => 2, |
362
|
|
|
'name' => 'Bob', |
363
|
|
|
], |
364
|
|
|
], |
365
|
|
|
]; |
366
|
|
|
|
367
|
|
|
$resource = new Collection($booksData, new JsonApiBookTransformer(), 'books'); |
368
|
|
|
$scope = new Scope($this->manager, $resource); |
369
|
|
|
|
370
|
|
|
$expected = [ |
371
|
|
|
'data' => [ |
372
|
|
|
[ |
373
|
|
|
'type' => 'books', |
374
|
|
|
'id' => '1', |
375
|
|
|
'attributes' => [ |
376
|
|
|
'title' => 'Foo', |
377
|
|
|
'year' => 1991, |
378
|
|
|
], |
379
|
|
|
], |
380
|
|
|
[ |
381
|
|
|
'type' => 'books', |
382
|
|
|
'id' => '2', |
383
|
|
|
'attributes' => [ |
384
|
|
|
'title' => 'Bar', |
385
|
|
|
'year' => 1997, |
386
|
|
|
], |
387
|
|
|
], |
388
|
|
|
], |
389
|
|
|
]; |
390
|
|
|
|
391
|
|
|
$this->assertSame($expected, $scope->toArray()); |
392
|
|
|
|
393
|
|
|
$expectedJson = '{"data":[{"type":"books","id":"1","attributes":{"title":"Foo","year":1991}},{"type":"books","id":"2","attributes":{"title":"Bar","year":1997}}]}'; |
394
|
|
|
$this->assertSame($expectedJson, $scope->toJson()); |
395
|
|
|
} |
396
|
|
|
|
397
|
|
View Code Duplication |
public function testSerializingCollectionResourceWithHasOneInclude() |
398
|
|
|
{ |
399
|
|
|
$this->manager->parseIncludes('author'); |
400
|
|
|
|
401
|
|
|
$booksData = [ |
402
|
|
|
[ |
403
|
|
|
'id' => 1, |
404
|
|
|
'title' => 'Foo', |
405
|
|
|
'year' => '1991', |
406
|
|
|
'_author' => [ |
407
|
|
|
'id' => 1, |
408
|
|
|
'name' => 'Dave', |
409
|
|
|
], |
410
|
|
|
], |
411
|
|
|
[ |
412
|
|
|
'id' => 2, |
413
|
|
|
'title' => 'Bar', |
414
|
|
|
'year' => '1997', |
415
|
|
|
'_author' => [ |
416
|
|
|
'id' => 2, |
417
|
|
|
'name' => 'Bob', |
418
|
|
|
], |
419
|
|
|
], |
420
|
|
|
]; |
421
|
|
|
|
422
|
|
|
$resource = new Collection($booksData, new JsonApiBookTransformer(), 'books'); |
423
|
|
|
$scope = new Scope($this->manager, $resource); |
424
|
|
|
|
425
|
|
|
$expected = [ |
426
|
|
|
'data' => [ |
427
|
|
|
[ |
428
|
|
|
'type' => 'books', |
429
|
|
|
'id' => '1', |
430
|
|
|
'attributes' => [ |
431
|
|
|
'title' => 'Foo', |
432
|
|
|
'year' => 1991, |
433
|
|
|
], |
434
|
|
|
'relationships' => [ |
435
|
|
|
'author' => [ |
436
|
|
|
'data' => [ |
437
|
|
|
'type' => 'people', |
438
|
|
|
'id' => '1', |
439
|
|
|
], |
440
|
|
|
], |
441
|
|
|
], |
442
|
|
|
], |
443
|
|
|
[ |
444
|
|
|
'type' => 'books', |
445
|
|
|
'id' => '2', |
446
|
|
|
'attributes' => [ |
447
|
|
|
'title' => 'Bar', |
448
|
|
|
'year' => 1997, |
449
|
|
|
], |
450
|
|
|
'relationships' => [ |
451
|
|
|
'author' => [ |
452
|
|
|
'data' => [ |
453
|
|
|
'type' => 'people', |
454
|
|
|
'id' => '2', |
455
|
|
|
], |
456
|
|
|
], |
457
|
|
|
], |
458
|
|
|
], |
459
|
|
|
], |
460
|
|
|
'included' => [ |
461
|
|
|
[ |
462
|
|
|
'type' => 'people', |
463
|
|
|
'id' => '1', |
464
|
|
|
'attributes' => [ |
465
|
|
|
'name' => 'Dave', |
466
|
|
|
], |
467
|
|
|
], |
468
|
|
|
[ |
469
|
|
|
'type' => 'people', |
470
|
|
|
'id' => '2', |
471
|
|
|
'attributes' => [ |
472
|
|
|
'name' => 'Bob', |
473
|
|
|
], |
474
|
|
|
], |
475
|
|
|
], |
476
|
|
|
]; |
477
|
|
|
|
478
|
|
|
$this->assertSame($expected, $scope->toArray()); |
479
|
|
|
|
480
|
|
|
$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"}}]}'; |
481
|
|
|
$this->assertSame($expectedJson, $scope->toJson()); |
482
|
|
|
} |
483
|
|
|
|
484
|
|
|
public function testSerializingCollectionResourceWithEmptyHasOneInclude() |
485
|
|
|
{ |
486
|
|
|
$this->manager->parseIncludes('author'); |
487
|
|
|
|
488
|
|
|
$booksData = [ |
489
|
|
|
[ |
490
|
|
|
'id' => 1, |
491
|
|
|
'title' => 'Foo', |
492
|
|
|
'year' => '1991', |
493
|
|
|
'_author' => null, |
494
|
|
|
], |
495
|
|
|
[ |
496
|
|
|
'id' => 2, |
497
|
|
|
'title' => 'Bar', |
498
|
|
|
'year' => '1997', |
499
|
|
|
'_author' => [ |
500
|
|
|
'id' => 2, |
501
|
|
|
'name' => 'Bob', |
502
|
|
|
], |
503
|
|
|
], |
504
|
|
|
]; |
505
|
|
|
|
506
|
|
|
$resource = new Collection($booksData, new JsonApiBookTransformer(), 'books'); |
507
|
|
|
$scope = new Scope($this->manager, $resource); |
508
|
|
|
|
509
|
|
|
$expected = [ |
510
|
|
|
'data' => [ |
511
|
|
|
[ |
512
|
|
|
'type' => 'books', |
513
|
|
|
'id' => '1', |
514
|
|
|
'attributes' => [ |
515
|
|
|
'title' => 'Foo', |
516
|
|
|
'year' => 1991, |
517
|
|
|
], |
518
|
|
|
'relationships' => [ |
519
|
|
|
'author' => [ |
520
|
|
|
'data' => null, |
521
|
|
|
], |
522
|
|
|
], |
523
|
|
|
], |
524
|
|
|
[ |
525
|
|
|
'type' => 'books', |
526
|
|
|
'id' => '2', |
527
|
|
|
'attributes' => [ |
528
|
|
|
'title' => 'Bar', |
529
|
|
|
'year' => 1997, |
530
|
|
|
], |
531
|
|
|
'relationships' => [ |
532
|
|
|
'author' => [ |
533
|
|
|
'data' => [ |
534
|
|
|
'type' => 'people', |
535
|
|
|
'id' => '2', |
536
|
|
|
], |
537
|
|
|
], |
538
|
|
|
], |
539
|
|
|
], |
540
|
|
|
], |
541
|
|
|
'included' => [ |
542
|
|
|
[ |
543
|
|
|
'type' => 'people', |
544
|
|
|
'id' => '2', |
545
|
|
|
'attributes' => [ |
546
|
|
|
'name' => 'Bob', |
547
|
|
|
], |
548
|
|
|
], |
549
|
|
|
], |
550
|
|
|
]; |
551
|
|
|
|
552
|
|
|
$this->assertSame($expected, $scope->toArray()); |
553
|
|
|
|
554
|
|
|
$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"}}]}'; |
555
|
|
|
$this->assertSame($expectedJson, $scope->toJson()); |
556
|
|
|
} |
557
|
|
|
|
558
|
|
|
public function testSerializingCollectionResourceWithHasManyInclude() |
559
|
|
|
{ |
560
|
|
|
$this->manager->parseIncludes('published'); |
561
|
|
|
|
562
|
|
|
$authorsData = [ |
563
|
|
|
[ |
564
|
|
|
'id' => 1, |
565
|
|
|
'name' => 'Dave', |
566
|
|
|
'_published' => [ |
567
|
|
|
[ |
568
|
|
|
'id' => 1, |
569
|
|
|
'title' => 'Foo', |
570
|
|
|
'year' => '1991', |
571
|
|
|
], |
572
|
|
|
[ |
573
|
|
|
'id' => 2, |
574
|
|
|
'title' => 'Bar', |
575
|
|
|
'year' => '2015', |
576
|
|
|
], |
577
|
|
|
], |
578
|
|
|
], |
579
|
|
|
[ |
580
|
|
|
'id' => 2, |
581
|
|
|
'name' => 'Bob', |
582
|
|
|
'_published' => [ |
583
|
|
|
[ |
584
|
|
|
'id' => 3, |
585
|
|
|
'title' => 'Baz', |
586
|
|
|
'year' => '1995', |
587
|
|
|
], |
588
|
|
|
[ |
589
|
|
|
'id' => 4, |
590
|
|
|
'title' => 'Quux', |
591
|
|
|
'year' => '2000', |
592
|
|
|
], |
593
|
|
|
], |
594
|
|
|
], |
595
|
|
|
]; |
596
|
|
|
|
597
|
|
|
$resource = new Collection($authorsData, new JsonApiAuthorTransformer(), 'people'); |
598
|
|
|
$scope = new Scope($this->manager, $resource); |
599
|
|
|
|
600
|
|
|
$expected = [ |
601
|
|
|
'data' => [ |
602
|
|
|
[ |
603
|
|
|
'type' => 'people', |
604
|
|
|
'id' => '1', |
605
|
|
|
'attributes' => [ |
606
|
|
|
'name' => 'Dave', |
607
|
|
|
], |
608
|
|
|
'relationships' => [ |
609
|
|
|
'published' => [ |
610
|
|
|
'data' => [ |
611
|
|
|
[ |
612
|
|
|
'type' => 'books', |
613
|
|
|
'id' => 1, |
614
|
|
|
], |
615
|
|
|
[ |
616
|
|
|
'type' => 'books', |
617
|
|
|
'id' => 2, |
618
|
|
|
], |
619
|
|
|
], |
620
|
|
|
], |
621
|
|
|
], |
622
|
|
|
], |
623
|
|
|
[ |
624
|
|
|
'type' => 'people', |
625
|
|
|
'id' => '2', |
626
|
|
|
'attributes' => [ |
627
|
|
|
'name' => 'Bob', |
628
|
|
|
], |
629
|
|
|
'relationships' => [ |
630
|
|
|
'published' => [ |
631
|
|
|
'data' => [ |
632
|
|
|
[ |
633
|
|
|
'type' => 'books', |
634
|
|
|
'id' => 3, |
635
|
|
|
], |
636
|
|
|
[ |
637
|
|
|
'type' => 'books', |
638
|
|
|
'id' => 4, |
639
|
|
|
], |
640
|
|
|
], |
641
|
|
|
], |
642
|
|
|
], |
643
|
|
|
], |
644
|
|
|
], |
645
|
|
|
'included' => [ |
646
|
|
|
[ |
647
|
|
|
'type' => 'books', |
648
|
|
|
'id' => '1', |
649
|
|
|
'attributes' => [ |
650
|
|
|
'title' => 'Foo', |
651
|
|
|
'year' => 1991, |
652
|
|
|
], |
653
|
|
|
], |
654
|
|
|
[ |
655
|
|
|
'type' => 'books', |
656
|
|
|
'id' => '2', |
657
|
|
|
'attributes' => [ |
658
|
|
|
'title' => 'Bar', |
659
|
|
|
'year' => 2015, |
660
|
|
|
], |
661
|
|
|
], |
662
|
|
|
[ |
663
|
|
|
'type' => 'books', |
664
|
|
|
'id' => '3', |
665
|
|
|
'attributes' => [ |
666
|
|
|
'title' => 'Baz', |
667
|
|
|
'year' => 1995, |
668
|
|
|
], |
669
|
|
|
], |
670
|
|
|
[ |
671
|
|
|
'type' => 'books', |
672
|
|
|
'id' => '4', |
673
|
|
|
'attributes' => [ |
674
|
|
|
'title' => 'Quux', |
675
|
|
|
'year' => 2000, |
676
|
|
|
], |
677
|
|
|
], |
678
|
|
|
], |
679
|
|
|
]; |
680
|
|
|
|
681
|
|
|
$this->assertEquals($expected, $scope->toArray()); |
682
|
|
|
|
683
|
|
|
$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}}]}'; |
684
|
|
|
$this->assertSame($expectedJson, $scope->toJson()); |
685
|
|
|
} |
686
|
|
|
|
687
|
|
|
public function testSerializingCollectionResourceWithEmptyHasManyInclude() |
688
|
|
|
{ |
689
|
|
|
$this->manager->parseIncludes('published'); |
690
|
|
|
|
691
|
|
|
$authorsData = [ |
692
|
|
|
[ |
693
|
|
|
'id' => 1, |
694
|
|
|
'name' => 'Dave', |
695
|
|
|
'_published' => [], |
696
|
|
|
], |
697
|
|
|
[ |
698
|
|
|
'id' => 2, |
699
|
|
|
'name' => 'Bob', |
700
|
|
|
'_published' => [ |
701
|
|
|
[ |
702
|
|
|
'id' => 3, |
703
|
|
|
'title' => 'Baz', |
704
|
|
|
'year' => '1995', |
705
|
|
|
], |
706
|
|
|
], |
707
|
|
|
], |
708
|
|
|
]; |
709
|
|
|
|
710
|
|
|
$resource = new Collection($authorsData, new JsonApiAuthorTransformer(), 'people'); |
711
|
|
|
$scope = new Scope($this->manager, $resource); |
712
|
|
|
|
713
|
|
|
$expected = [ |
714
|
|
|
'data' => [ |
715
|
|
|
[ |
716
|
|
|
'type' => 'people', |
717
|
|
|
'id' => '1', |
718
|
|
|
'attributes' => [ |
719
|
|
|
'name' => 'Dave', |
720
|
|
|
], |
721
|
|
|
'relationships' => [ |
722
|
|
|
'published' => [ |
723
|
|
|
'data' => [], |
724
|
|
|
], |
725
|
|
|
], |
726
|
|
|
], |
727
|
|
|
[ |
728
|
|
|
'type' => 'people', |
729
|
|
|
'id' => '2', |
730
|
|
|
'attributes' => [ |
731
|
|
|
'name' => 'Bob', |
732
|
|
|
], |
733
|
|
|
'relationships' => [ |
734
|
|
|
'published' => [ |
735
|
|
|
'data' => [ |
736
|
|
|
[ |
737
|
|
|
'type' => 'books', |
738
|
|
|
'id' => 3, |
739
|
|
|
], |
740
|
|
|
], |
741
|
|
|
], |
742
|
|
|
], |
743
|
|
|
], |
744
|
|
|
], |
745
|
|
|
'included' => [ |
746
|
|
|
[ |
747
|
|
|
'type' => 'books', |
748
|
|
|
'id' => '3', |
749
|
|
|
'attributes' => [ |
750
|
|
|
'title' => 'Baz', |
751
|
|
|
'year' => 1995, |
752
|
|
|
], |
753
|
|
|
], |
754
|
|
|
], |
755
|
|
|
]; |
756
|
|
|
|
757
|
|
|
$this->assertEquals($expected, $scope->toArray()); |
758
|
|
|
|
759
|
|
|
$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}}]}'; |
760
|
|
|
$this->assertSame($expectedJson, $scope->toJson()); |
761
|
|
|
} |
762
|
|
|
|
763
|
|
View Code Duplication |
public function testSerializingCollectionResourceWithMeta() |
764
|
|
|
{ |
765
|
|
|
$booksData = [ |
766
|
|
|
[ |
767
|
|
|
'id' => 1, |
768
|
|
|
'title' => 'Foo', |
769
|
|
|
'year' => '1991', |
770
|
|
|
'_author' => [ |
771
|
|
|
'name' => 'Dave', |
772
|
|
|
], |
773
|
|
|
], |
774
|
|
|
[ |
775
|
|
|
'id' => 2, |
776
|
|
|
'title' => 'Bar', |
777
|
|
|
'year' => '1997', |
778
|
|
|
'_author' => [ |
779
|
|
|
'name' => 'Bob', |
780
|
|
|
], |
781
|
|
|
], |
782
|
|
|
]; |
783
|
|
|
|
784
|
|
|
$resource = new Collection($booksData, new JsonApiBookTransformer(), 'books'); |
785
|
|
|
$resource->setMetaValue('foo', 'bar'); |
786
|
|
|
|
787
|
|
|
$scope = new Scope($this->manager, $resource); |
788
|
|
|
|
789
|
|
|
$expected = [ |
790
|
|
|
'data' => [ |
791
|
|
|
[ |
792
|
|
|
'type' => 'books', |
793
|
|
|
'id' => '1', |
794
|
|
|
'attributes' => [ |
795
|
|
|
'title' => 'Foo', |
796
|
|
|
'year' => 1991, |
797
|
|
|
], |
798
|
|
|
], |
799
|
|
|
[ |
800
|
|
|
'type' => 'books', |
801
|
|
|
'id' => '2', |
802
|
|
|
'attributes' => [ |
803
|
|
|
'title' => 'Bar', |
804
|
|
|
'year' => 1997, |
805
|
|
|
], |
806
|
|
|
], |
807
|
|
|
], |
808
|
|
|
'meta' => [ |
809
|
|
|
'foo' => 'bar', |
810
|
|
|
], |
811
|
|
|
]; |
812
|
|
|
|
813
|
|
|
$this->assertSame($expected, $scope->toArray()); |
814
|
|
|
|
815
|
|
|
$expectedJson = '{"data":[{"type":"books","id":"1","attributes":{"title":"Foo","year":1991}},{"type":"books","id":"2","attributes":{"title":"Bar","year":1997}}],"meta":{"foo":"bar"}}'; |
816
|
|
|
$this->assertSame($expectedJson, $scope->toJson()); |
817
|
|
|
} |
818
|
|
|
|
819
|
|
|
public function testSerializingCollectionResourceWithDuplicatedIncludeData() |
820
|
|
|
{ |
821
|
|
|
$this->manager->parseIncludes('author'); |
822
|
|
|
|
823
|
|
|
$booksData = [ |
824
|
|
|
[ |
825
|
|
|
'id' => 1, |
826
|
|
|
'title' => 'Foo', |
827
|
|
|
'year' => '1991', |
828
|
|
|
'_author' => [ |
829
|
|
|
'id' => 1, |
830
|
|
|
'name' => 'Dave', |
831
|
|
|
], |
832
|
|
|
], |
833
|
|
|
[ |
834
|
|
|
'id' => 2, |
835
|
|
|
'title' => 'Bar', |
836
|
|
|
'year' => '1997', |
837
|
|
|
'_author' => [ |
838
|
|
|
'id' => 1, |
839
|
|
|
'name' => 'Dave', |
840
|
|
|
], |
841
|
|
|
], |
842
|
|
|
]; |
843
|
|
|
|
844
|
|
|
$resource = new Collection($booksData, new JsonApiBookTransformer(), 'books'); |
845
|
|
|
$scope = new Scope($this->manager, $resource); |
846
|
|
|
|
847
|
|
|
$expected = [ |
848
|
|
|
'data' => [ |
849
|
|
|
[ |
850
|
|
|
'type' => 'books', |
851
|
|
|
'id' => '1', |
852
|
|
|
'attributes' => [ |
853
|
|
|
'title' => 'Foo', |
854
|
|
|
'year' => 1991, |
855
|
|
|
], |
856
|
|
|
'relationships' => [ |
857
|
|
|
'author' => [ |
858
|
|
|
'data' => [ |
859
|
|
|
'type' => 'people', |
860
|
|
|
'id' => '1', |
861
|
|
|
], |
862
|
|
|
], |
863
|
|
|
], |
864
|
|
|
], |
865
|
|
|
[ |
866
|
|
|
'type' => 'books', |
867
|
|
|
'id' => '2', |
868
|
|
|
'attributes' => [ |
869
|
|
|
'title' => 'Bar', |
870
|
|
|
'year' => 1997, |
871
|
|
|
], |
872
|
|
|
'relationships' => [ |
873
|
|
|
'author' => [ |
874
|
|
|
'data' => [ |
875
|
|
|
'type' => 'people', |
876
|
|
|
'id' => '1', |
877
|
|
|
], |
878
|
|
|
], |
879
|
|
|
], |
880
|
|
|
], |
881
|
|
|
], |
882
|
|
|
'included' => [ |
883
|
|
|
[ |
884
|
|
|
'type' => 'people', |
885
|
|
|
'id' => '1', |
886
|
|
|
'attributes' => [ |
887
|
|
|
'name' => 'Dave', |
888
|
|
|
], |
889
|
|
|
], |
890
|
|
|
], |
891
|
|
|
]; |
892
|
|
|
|
893
|
|
|
$this->assertSame($expected, $scope->toArray()); |
894
|
|
|
|
895
|
|
|
$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"}}]}'; |
896
|
|
|
$this->assertSame($expectedJson, $scope->toJson()); |
897
|
|
|
} |
898
|
|
|
|
899
|
|
View Code Duplication |
public function testSerializingItemResourceWithNestedIncludes() |
900
|
|
|
{ |
901
|
|
|
$this->manager->parseIncludes(['author', 'author.published']); |
902
|
|
|
|
903
|
|
|
$bookData = [ |
904
|
|
|
'id' => 1, |
905
|
|
|
'title' => 'Foo', |
906
|
|
|
'year' => '1991', |
907
|
|
|
'_author' => [ |
908
|
|
|
'id' => 1, |
909
|
|
|
'name' => 'Dave', |
910
|
|
|
'_published' => [ |
911
|
|
|
[ |
912
|
|
|
'id' => 1, |
913
|
|
|
'title' => 'Foo', |
914
|
|
|
'year' => '1991', |
915
|
|
|
], |
916
|
|
|
[ |
917
|
|
|
'id' => 2, |
918
|
|
|
'title' => 'Bar', |
919
|
|
|
'year' => '2015', |
920
|
|
|
], |
921
|
|
|
], |
922
|
|
|
], |
923
|
|
|
]; |
924
|
|
|
|
925
|
|
|
$resource = new Item($bookData, new JsonApiBookTransformer(), 'books'); |
926
|
|
|
|
927
|
|
|
$scope = new Scope($this->manager, $resource); |
928
|
|
|
|
929
|
|
|
$expected = [ |
930
|
|
|
'data' => [ |
931
|
|
|
'type' => 'books', |
932
|
|
|
'id' => '1', |
933
|
|
|
'attributes' => [ |
934
|
|
|
'title' => 'Foo', |
935
|
|
|
'year' => 1991, |
936
|
|
|
], |
937
|
|
|
'relationships' => [ |
938
|
|
|
'author' => [ |
939
|
|
|
'data' => [ |
940
|
|
|
'type' => 'people', |
941
|
|
|
'id' => '1', |
942
|
|
|
], |
943
|
|
|
], |
944
|
|
|
], |
945
|
|
|
], |
946
|
|
|
'included' => [ |
947
|
|
|
[ |
948
|
|
|
'type' => 'books', |
949
|
|
|
'id' => '2', |
950
|
|
|
'attributes' => [ |
951
|
|
|
'title' => 'Bar', |
952
|
|
|
'year' => 2015, |
953
|
|
|
], |
954
|
|
|
], |
955
|
|
|
[ |
956
|
|
|
'type' => 'people', |
957
|
|
|
'id' => '1', |
958
|
|
|
'attributes' => [ |
959
|
|
|
'name' => 'Dave', |
960
|
|
|
], |
961
|
|
|
'relationships' => [ |
962
|
|
|
'published' => [ |
963
|
|
|
'data' => [ |
964
|
|
|
[ |
965
|
|
|
'type' => 'books', |
966
|
|
|
'id' => '1', |
967
|
|
|
], |
968
|
|
|
[ |
969
|
|
|
'type' => 'books', |
970
|
|
|
'id' => '2', |
971
|
|
|
], |
972
|
|
|
], |
973
|
|
|
], |
974
|
|
|
], |
975
|
|
|
], |
976
|
|
|
], |
977
|
|
|
]; |
978
|
|
|
|
979
|
|
|
$this->assertSame($expected, $scope->toArray()); |
980
|
|
|
|
981
|
|
|
$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"}]}}}]}'; |
982
|
|
|
$this->assertSame($expectedJson, $scope->toJson()); |
983
|
|
|
} |
984
|
|
|
|
985
|
|
View Code Duplication |
public function testSerializingItemResourceWithSelfLink() |
986
|
|
|
{ |
987
|
|
|
$baseUrl = 'http://example.com'; |
988
|
|
|
$this->manager->setSerializer(new JsonApiSerializer($baseUrl)); |
989
|
|
|
|
990
|
|
|
$bookData = [ |
991
|
|
|
'id' => 1, |
992
|
|
|
'title' => 'Foo', |
993
|
|
|
'year' => '1991', |
994
|
|
|
'_author' => [ |
995
|
|
|
'id' => 1, |
996
|
|
|
'name' => 'Dave', |
997
|
|
|
], |
998
|
|
|
]; |
999
|
|
|
|
1000
|
|
|
$resource = new Item($bookData, new JsonApiBookTransformer(), 'books'); |
1001
|
|
|
|
1002
|
|
|
$scope = new Scope($this->manager, $resource); |
1003
|
|
|
|
1004
|
|
|
$expected = [ |
1005
|
|
|
'data' => [ |
1006
|
|
|
'type' => 'books', |
1007
|
|
|
'id' => '1', |
1008
|
|
|
'attributes' => [ |
1009
|
|
|
'title' => 'Foo', |
1010
|
|
|
'year' => 1991, |
1011
|
|
|
], |
1012
|
|
|
'links' => [ |
1013
|
|
|
'self' => 'http://example.com/books/1', |
1014
|
|
|
], |
1015
|
|
|
], |
1016
|
|
|
]; |
1017
|
|
|
|
1018
|
|
|
$this->assertSame($expected, $scope->toArray()); |
1019
|
|
|
|
1020
|
|
|
$expectedJson = '{"data":{"type":"books","id":"1","attributes":{"title":"Foo","year":1991},"links":{"self":"http:\/\/example.com\/books\/1"}}}'; |
1021
|
|
|
$this->assertSame($expectedJson, $scope->toJson()); |
1022
|
|
|
} |
1023
|
|
|
|
1024
|
|
View Code Duplication |
public function testSerializingCollectionResourceWithSelfLink() |
1025
|
|
|
{ |
1026
|
|
|
$baseUrl = 'http://example.com'; |
1027
|
|
|
$this->manager->setSerializer(new JsonApiSerializer($baseUrl)); |
1028
|
|
|
|
1029
|
|
|
$booksData = [ |
1030
|
|
|
[ |
1031
|
|
|
'id' => 1, |
1032
|
|
|
'title' => 'Foo', |
1033
|
|
|
'year' => '1991', |
1034
|
|
|
'_author' => [ |
1035
|
|
|
'id' => 1, |
1036
|
|
|
'name' => 'Dave', |
1037
|
|
|
], |
1038
|
|
|
], |
1039
|
|
|
[ |
1040
|
|
|
'id' => 2, |
1041
|
|
|
'title' => 'Bar', |
1042
|
|
|
'year' => '1997', |
1043
|
|
|
'_author' => [ |
1044
|
|
|
'id' => 2, |
1045
|
|
|
'name' => 'Bob', |
1046
|
|
|
], |
1047
|
|
|
], |
1048
|
|
|
]; |
1049
|
|
|
|
1050
|
|
|
$resource = new Collection($booksData, new JsonApiBookTransformer(), 'books'); |
1051
|
|
|
$scope = new Scope($this->manager, $resource); |
1052
|
|
|
|
1053
|
|
|
$expected = [ |
1054
|
|
|
'data' => [ |
1055
|
|
|
[ |
1056
|
|
|
'type' => 'books', |
1057
|
|
|
'id' => '1', |
1058
|
|
|
'attributes' => [ |
1059
|
|
|
'title' => 'Foo', |
1060
|
|
|
'year' => 1991, |
1061
|
|
|
], |
1062
|
|
|
'links' => [ |
1063
|
|
|
'self' => 'http://example.com/books/1', |
1064
|
|
|
], |
1065
|
|
|
], |
1066
|
|
|
[ |
1067
|
|
|
'type' => 'books', |
1068
|
|
|
'id' => '2', |
1069
|
|
|
'attributes' => [ |
1070
|
|
|
'title' => 'Bar', |
1071
|
|
|
'year' => 1997, |
1072
|
|
|
], |
1073
|
|
|
'links' => [ |
1074
|
|
|
'self' => 'http://example.com/books/2', |
1075
|
|
|
], |
1076
|
|
|
], |
1077
|
|
|
], |
1078
|
|
|
]; |
1079
|
|
|
|
1080
|
|
|
$this->assertSame($expected, $scope->toArray()); |
1081
|
|
|
|
1082
|
|
|
$expectedJson = '{"data":[{"type":"books","id":"1","attributes":{"title":"Foo","year":1991},"links":{"self":"http:\/\/example.com\/books\/1"}},{"type":"books","id":"2","attributes":{"title":"Bar","year":1997},"links":{"self":"http:\/\/example.com\/books\/2"}}]}'; |
1083
|
|
|
$this->assertSame($expectedJson, $scope->toJson()); |
1084
|
|
|
} |
1085
|
|
|
|
1086
|
|
View Code Duplication |
public function testSerializingItemResourceWithLinksForHasOneRelationship() |
1087
|
|
|
{ |
1088
|
|
|
$baseUrl = 'http://example.com'; |
1089
|
|
|
$this->manager->setSerializer(new JsonApiSerializer($baseUrl)); |
1090
|
|
|
$this->manager->parseIncludes('author'); |
1091
|
|
|
|
1092
|
|
|
$bookData = [ |
1093
|
|
|
'id' => 1, |
1094
|
|
|
'title' => 'Foo', |
1095
|
|
|
'year' => '1991', |
1096
|
|
|
'_author' => [ |
1097
|
|
|
'id' => 1, |
1098
|
|
|
'name' => 'Dave', |
1099
|
|
|
], |
1100
|
|
|
]; |
1101
|
|
|
|
1102
|
|
|
$resource = new Item($bookData, new JsonApiBookTransformer(), 'books'); |
1103
|
|
|
|
1104
|
|
|
$scope = new Scope($this->manager, $resource); |
1105
|
|
|
|
1106
|
|
|
$expected = [ |
1107
|
|
|
'data' => [ |
1108
|
|
|
'type' => 'books', |
1109
|
|
|
'id' => '1', |
1110
|
|
|
'attributes' => [ |
1111
|
|
|
'title' => 'Foo', |
1112
|
|
|
'year' => 1991, |
1113
|
|
|
], |
1114
|
|
|
'relationships' => [ |
1115
|
|
|
'author' => [ |
1116
|
|
|
'links' => [ |
1117
|
|
|
'self' => 'http://example.com/books/1/relationships/author', |
1118
|
|
|
'related' => 'http://example.com/books/1/author', |
1119
|
|
|
], |
1120
|
|
|
'data' => [ |
1121
|
|
|
'type' => 'people', |
1122
|
|
|
'id' => '1', |
1123
|
|
|
], |
1124
|
|
|
], |
1125
|
|
|
], |
1126
|
|
|
'links' => [ |
1127
|
|
|
'self' => 'http://example.com/books/1', |
1128
|
|
|
], |
1129
|
|
|
], |
1130
|
|
|
'included' => [ |
1131
|
|
|
[ |
1132
|
|
|
'type' => 'people', |
1133
|
|
|
'id' => '1', |
1134
|
|
|
'attributes' => [ |
1135
|
|
|
'name' => 'Dave', |
1136
|
|
|
], |
1137
|
|
|
'links' => [ |
1138
|
|
|
'self' => 'http://example.com/people/1', |
1139
|
|
|
], |
1140
|
|
|
], |
1141
|
|
|
], |
1142
|
|
|
]; |
1143
|
|
|
|
1144
|
|
|
$this->assertEquals($expected, $scope->toArray()); |
1145
|
|
|
|
1146
|
|
|
$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"}}}},"included":[{"type":"people","id":"1","attributes":{"name":"Dave"},"links":{"self":"http:\/\/example.com\/people\/1"}}]}'; |
1147
|
|
|
$this->assertSame($expectedJson, $scope->toJson()); |
1148
|
|
|
} |
1149
|
|
|
|
1150
|
|
|
public function testSerializingItemResourceWithLinksForHasManyRelationship() |
1151
|
|
|
{ |
1152
|
|
|
$baseUrl = 'http://example.com'; |
1153
|
|
|
$this->manager->setSerializer(new JsonApiSerializer($baseUrl)); |
1154
|
|
|
$this->manager->parseIncludes('published'); |
1155
|
|
|
|
1156
|
|
|
$authorData = [ |
1157
|
|
|
'id' => 1, |
1158
|
|
|
'name' => 'Dave', |
1159
|
|
|
'_published' => [ |
1160
|
|
|
[ |
1161
|
|
|
'id' => 1, |
1162
|
|
|
'title' => 'Foo', |
1163
|
|
|
'year' => '1991', |
1164
|
|
|
], |
1165
|
|
|
[ |
1166
|
|
|
'id' => 2, |
1167
|
|
|
'title' => 'Bar', |
1168
|
|
|
'year' => '2015', |
1169
|
|
|
], |
1170
|
|
|
], |
1171
|
|
|
]; |
1172
|
|
|
|
1173
|
|
|
$resource = new Item($authorData, new JsonApiAuthorTransformer(), 'people'); |
1174
|
|
|
|
1175
|
|
|
$scope = new Scope($this->manager, $resource); |
1176
|
|
|
|
1177
|
|
|
$expected = [ |
1178
|
|
|
'data' => [ |
1179
|
|
|
'type' => 'people', |
1180
|
|
|
'id' => '1', |
1181
|
|
|
'attributes' => [ |
1182
|
|
|
'name' => 'Dave', |
1183
|
|
|
], |
1184
|
|
|
'relationships' => [ |
1185
|
|
|
'published' => [ |
1186
|
|
|
'links' => [ |
1187
|
|
|
'self' => 'http://example.com/people/1/relationships/published', |
1188
|
|
|
'related' => 'http://example.com/people/1/published', |
1189
|
|
|
], |
1190
|
|
|
'data' => [ |
1191
|
|
|
[ |
1192
|
|
|
'type' => 'books', |
1193
|
|
|
'id' => 1, |
1194
|
|
|
], |
1195
|
|
|
[ |
1196
|
|
|
'type' => 'books', |
1197
|
|
|
'id' => 2, |
1198
|
|
|
], |
1199
|
|
|
], |
1200
|
|
|
], |
1201
|
|
|
], |
1202
|
|
|
'links' => [ |
1203
|
|
|
'self' => 'http://example.com/people/1', |
1204
|
|
|
], |
1205
|
|
|
], |
1206
|
|
|
'included' => [ |
1207
|
|
|
[ |
1208
|
|
|
'type' => 'books', |
1209
|
|
|
'id' => '1', |
1210
|
|
|
'attributes' => [ |
1211
|
|
|
'title' => 'Foo', |
1212
|
|
|
'year' => 1991, |
1213
|
|
|
], |
1214
|
|
|
'links' => [ |
1215
|
|
|
'self' => 'http://example.com/books/1', |
1216
|
|
|
], |
1217
|
|
|
], |
1218
|
|
|
[ |
1219
|
|
|
'type' => 'books', |
1220
|
|
|
'id' => '2', |
1221
|
|
|
'attributes' => [ |
1222
|
|
|
'title' => 'Bar', |
1223
|
|
|
'year' => 2015, |
1224
|
|
|
], |
1225
|
|
|
'links' => [ |
1226
|
|
|
'self' => 'http://example.com/books/2', |
1227
|
|
|
], |
1228
|
|
|
], |
1229
|
|
|
], |
1230
|
|
|
]; |
1231
|
|
|
|
1232
|
|
|
$this->assertEquals($expected, $scope->toArray()); |
1233
|
|
|
|
1234
|
|
|
$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"}},{"type":"books","id":"2","attributes":{"title":"Bar","year":2015},"links":{"self":"http:\/\/example.com\/books\/2"}}]}'; |
1235
|
|
|
$this->assertSame($expectedJson, $scope->toJson()); |
1236
|
|
|
} |
1237
|
|
|
|
1238
|
|
|
public function testSerializingCollectionResourceWithLinksForHasOneRelationship() |
1239
|
|
|
{ |
1240
|
|
|
$baseUrl = 'http://example.com'; |
1241
|
|
|
$this->manager->setSerializer(new JsonApiSerializer($baseUrl)); |
1242
|
|
|
$this->manager->parseIncludes('author'); |
1243
|
|
|
|
1244
|
|
|
$bookData = [ |
1245
|
|
|
[ |
1246
|
|
|
'id' => 1, |
1247
|
|
|
'title' => 'Foo', |
1248
|
|
|
'year' => '1991', |
1249
|
|
|
'_author' => [ |
1250
|
|
|
'id' => 1, |
1251
|
|
|
'name' => 'Dave', |
1252
|
|
|
], |
1253
|
|
|
], |
1254
|
|
|
[ |
1255
|
|
|
'id' => 2, |
1256
|
|
|
'title' => 'Bar', |
1257
|
|
|
'year' => '1991', |
1258
|
|
|
'_author' => [ |
1259
|
|
|
'id' => 1, |
1260
|
|
|
'name' => 'Dave', |
1261
|
|
|
], |
1262
|
|
|
], |
1263
|
|
|
]; |
1264
|
|
|
|
1265
|
|
|
$resource = new Collection($bookData, new JsonApiBookTransformer(), 'books'); |
1266
|
|
|
|
1267
|
|
|
$scope = new Scope($this->manager, $resource); |
1268
|
|
|
|
1269
|
|
|
$expected = [ |
1270
|
|
|
'data' => [ |
1271
|
|
|
[ |
1272
|
|
|
'type' => 'books', |
1273
|
|
|
'id' => '1', |
1274
|
|
|
'attributes' => [ |
1275
|
|
|
'title' => 'Foo', |
1276
|
|
|
'year' => 1991, |
1277
|
|
|
], |
1278
|
|
|
'relationships' => [ |
1279
|
|
|
'author' => [ |
1280
|
|
|
'links' => [ |
1281
|
|
|
'self' => 'http://example.com/books/1/relationships/author', |
1282
|
|
|
'related' => 'http://example.com/books/1/author', |
1283
|
|
|
], |
1284
|
|
|
'data' => [ |
1285
|
|
|
'type' => 'people', |
1286
|
|
|
'id' => '1', |
1287
|
|
|
], |
1288
|
|
|
], |
1289
|
|
|
], |
1290
|
|
|
'links' => [ |
1291
|
|
|
'self' => 'http://example.com/books/1', |
1292
|
|
|
], |
1293
|
|
|
], |
1294
|
|
|
[ |
1295
|
|
|
'type' => 'books', |
1296
|
|
|
'id' => '2', |
1297
|
|
|
'attributes' => [ |
1298
|
|
|
'title' => 'Bar', |
1299
|
|
|
'year' => 1991, |
1300
|
|
|
], |
1301
|
|
|
'relationships' => [ |
1302
|
|
|
'author' => [ |
1303
|
|
|
'links' => [ |
1304
|
|
|
'self' => 'http://example.com/books/2/relationships/author', |
1305
|
|
|
'related' => 'http://example.com/books/2/author', |
1306
|
|
|
], |
1307
|
|
|
'data' => [ |
1308
|
|
|
'type' => 'people', |
1309
|
|
|
'id' => '1', |
1310
|
|
|
], |
1311
|
|
|
], |
1312
|
|
|
], |
1313
|
|
|
'links' => [ |
1314
|
|
|
'self' => 'http://example.com/books/2', |
1315
|
|
|
], |
1316
|
|
|
], |
1317
|
|
|
], |
1318
|
|
|
'included' => [ |
1319
|
|
|
[ |
1320
|
|
|
'type' => 'people', |
1321
|
|
|
'id' => '1', |
1322
|
|
|
'attributes' => [ |
1323
|
|
|
'name' => 'Dave', |
1324
|
|
|
], |
1325
|
|
|
'links' => [ |
1326
|
|
|
'self' => 'http://example.com/people/1', |
1327
|
|
|
], |
1328
|
|
|
], |
1329
|
|
|
], |
1330
|
|
|
]; |
1331
|
|
|
|
1332
|
|
|
$this->assertEquals($expected, $scope->toArray()); |
1333
|
|
|
|
1334
|
|
|
$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"}}}},{"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"}}}}],"included":[{"type":"people","id":"1","attributes":{"name":"Dave"},"links":{"self":"http:\/\/example.com\/people\/1"}}]}'; |
1335
|
|
|
$this->assertSame($expectedJson, $scope->toJson()); |
1336
|
|
|
} |
1337
|
|
|
|
1338
|
|
|
public function testSerializingCollectionResourceWithLinksForHasManyRelationship() |
1339
|
|
|
{ |
1340
|
|
|
$baseUrl = 'http://example.com'; |
1341
|
|
|
$this->manager->setSerializer(new JsonApiSerializer($baseUrl)); |
1342
|
|
|
$this->manager->parseIncludes('published'); |
1343
|
|
|
|
1344
|
|
|
$authorData = [ |
1345
|
|
|
[ |
1346
|
|
|
'id' => 1, |
1347
|
|
|
'name' => 'Dave', |
1348
|
|
|
'_published' => [ |
1349
|
|
|
[ |
1350
|
|
|
'id' => 1, |
1351
|
|
|
'title' => 'Foo', |
1352
|
|
|
'year' => '1991', |
1353
|
|
|
], |
1354
|
|
|
[ |
1355
|
|
|
'id' => 2, |
1356
|
|
|
'title' => 'Bar', |
1357
|
|
|
'year' => '2015', |
1358
|
|
|
], |
1359
|
|
|
], |
1360
|
|
|
], |
1361
|
|
|
[ |
1362
|
|
|
'id' => 2, |
1363
|
|
|
'name' => 'Bill', |
1364
|
|
|
'_published' => [ |
1365
|
|
|
[ |
1366
|
|
|
'id' => 1, |
1367
|
|
|
'title' => 'Foo', |
1368
|
|
|
'year' => '1991', |
1369
|
|
|
], |
1370
|
|
|
[ |
1371
|
|
|
'id' => 2, |
1372
|
|
|
'title' => 'Bar', |
1373
|
|
|
'year' => '2015', |
1374
|
|
|
], |
1375
|
|
|
], |
1376
|
|
|
], |
1377
|
|
|
]; |
1378
|
|
|
|
1379
|
|
|
$resource = new Collection($authorData, new JsonApiAuthorTransformer(), 'people'); |
1380
|
|
|
|
1381
|
|
|
$scope = new Scope($this->manager, $resource); |
1382
|
|
|
|
1383
|
|
|
$expected = [ |
1384
|
|
|
'data' => [ |
1385
|
|
|
[ |
1386
|
|
|
'type' => 'people', |
1387
|
|
|
'id' => '1', |
1388
|
|
|
'attributes' => [ |
1389
|
|
|
'name' => 'Dave', |
1390
|
|
|
], |
1391
|
|
|
'relationships' => [ |
1392
|
|
|
'published' => [ |
1393
|
|
|
'links' => [ |
1394
|
|
|
'self' => 'http://example.com/people/1/relationships/published', |
1395
|
|
|
'related' => 'http://example.com/people/1/published', |
1396
|
|
|
], |
1397
|
|
|
'data' => [ |
1398
|
|
|
[ |
1399
|
|
|
'type' => 'books', |
1400
|
|
|
'id' => 1, |
1401
|
|
|
], |
1402
|
|
|
[ |
1403
|
|
|
'type' => 'books', |
1404
|
|
|
'id' => 2, |
1405
|
|
|
], |
1406
|
|
|
], |
1407
|
|
|
], |
1408
|
|
|
], |
1409
|
|
|
'links' => [ |
1410
|
|
|
'self' => 'http://example.com/people/1', |
1411
|
|
|
], |
1412
|
|
|
], |
1413
|
|
|
[ |
1414
|
|
|
'type' => 'people', |
1415
|
|
|
'id' => '2', |
1416
|
|
|
'attributes' => [ |
1417
|
|
|
'name' => 'Bill', |
1418
|
|
|
], |
1419
|
|
|
'relationships' => [ |
1420
|
|
|
'published' => [ |
1421
|
|
|
'links' => [ |
1422
|
|
|
'self' => 'http://example.com/people/2/relationships/published', |
1423
|
|
|
'related' => 'http://example.com/people/2/published', |
1424
|
|
|
], |
1425
|
|
|
'data' => [ |
1426
|
|
|
[ |
1427
|
|
|
'type' => 'books', |
1428
|
|
|
'id' => 1, |
1429
|
|
|
], |
1430
|
|
|
[ |
1431
|
|
|
'type' => 'books', |
1432
|
|
|
'id' => 2, |
1433
|
|
|
], |
1434
|
|
|
], |
1435
|
|
|
], |
1436
|
|
|
], |
1437
|
|
|
'links' => [ |
1438
|
|
|
'self' => 'http://example.com/people/2', |
1439
|
|
|
], |
1440
|
|
|
], |
1441
|
|
|
], |
1442
|
|
|
'included' => [ |
1443
|
|
|
[ |
1444
|
|
|
'type' => 'books', |
1445
|
|
|
'id' => '1', |
1446
|
|
|
'attributes' => [ |
1447
|
|
|
'title' => 'Foo', |
1448
|
|
|
'year' => 1991, |
1449
|
|
|
], |
1450
|
|
|
'links' => [ |
1451
|
|
|
'self' => 'http://example.com/books/1', |
1452
|
|
|
], |
1453
|
|
|
], |
1454
|
|
|
[ |
1455
|
|
|
'type' => 'books', |
1456
|
|
|
'id' => '2', |
1457
|
|
|
'attributes' => [ |
1458
|
|
|
'title' => 'Bar', |
1459
|
|
|
'year' => 2015, |
1460
|
|
|
], |
1461
|
|
|
'links' => [ |
1462
|
|
|
'self' => 'http://example.com/books/2', |
1463
|
|
|
], |
1464
|
|
|
], |
1465
|
|
|
], |
1466
|
|
|
]; |
1467
|
|
|
|
1468
|
|
|
$this->assertEquals($expected, $scope->toArray()); |
1469
|
|
|
|
1470
|
|
|
$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"}},{"type":"books","id":"2","attributes":{"title":"Bar","year":2015},"links":{"self":"http:\/\/example.com\/books\/2"}}]}'; |
1471
|
|
|
$this->assertSame($expectedJson, $scope->toJson()); |
1472
|
|
|
} |
1473
|
|
|
|
1474
|
|
|
/** |
1475
|
|
|
* @expectedException InvalidArgumentException |
1476
|
|
|
* @expectedExceptionMessage JSON API resource objects MUST have a valid id |
1477
|
|
|
*/ |
1478
|
|
|
public function testExceptionThrownIfResourceHasNoId() |
1479
|
|
|
{ |
1480
|
|
|
$bookData = [ |
1481
|
|
|
'title' => 'Foo', |
1482
|
|
|
'year' => '1991', |
1483
|
|
|
]; |
1484
|
|
|
|
1485
|
|
|
$resource = new Item($bookData, new JsonApiBookTransformer(), 'books'); |
1486
|
|
|
|
1487
|
|
|
$scope = new Scope($this->manager, $resource); |
1488
|
|
|
$scope->toArray(); |
1489
|
|
|
} |
1490
|
|
|
|
1491
|
|
|
public function testSerializingItemWithReferenceToRootObject() |
1492
|
|
|
{ |
1493
|
|
|
$this->manager->parseIncludes('published.author'); |
1494
|
|
|
|
1495
|
|
|
$authorData = [ |
1496
|
|
|
'id' => 1, |
1497
|
|
|
'name' => 'Dave', |
1498
|
|
|
'_published' => [ |
1499
|
|
|
[ |
1500
|
|
|
'id' => 1, |
1501
|
|
|
'title' => 'Foo', |
1502
|
|
|
'year' => 1991, |
1503
|
|
|
'_author' => ['id' => 1] |
1504
|
|
|
], |
1505
|
|
|
[ |
1506
|
|
|
'id' => 2, |
1507
|
|
|
'title' => 'Bar', |
1508
|
|
|
'year' => 2015, |
1509
|
|
|
'_author' => ['id' => 1] |
1510
|
|
|
], |
1511
|
|
|
], |
1512
|
|
|
]; |
1513
|
|
|
|
1514
|
|
|
$resource = new Item($authorData, new JsonApiAuthorTransformer(), 'people'); |
1515
|
|
|
|
1516
|
|
|
$scope = new Scope($this->manager, $resource); |
1517
|
|
|
|
1518
|
|
|
$expected = [ |
1519
|
|
|
'data' => [ |
1520
|
|
|
'type' => 'people', |
1521
|
|
|
'id' => '1', |
1522
|
|
|
'attributes' => [ |
1523
|
|
|
'name' => 'Dave', |
1524
|
|
|
], |
1525
|
|
|
'relationships' => [ |
1526
|
|
|
'published' => [ |
1527
|
|
|
'data' => [ |
1528
|
|
|
['type' => 'books', 'id' => '1'], |
1529
|
|
|
['type' => 'books', 'id' => '2'], |
1530
|
|
|
], |
1531
|
|
|
], |
1532
|
|
|
], |
1533
|
|
|
], |
1534
|
|
|
'included' => [ |
1535
|
|
|
[ |
1536
|
|
|
'type' => 'books', |
1537
|
|
|
'id' => '1', |
1538
|
|
|
'attributes' => [ |
1539
|
|
|
'title' => 'Foo', |
1540
|
|
|
'year' => 1991, |
1541
|
|
|
], |
1542
|
|
|
'relationships' => [ |
1543
|
|
|
'author' => [ |
1544
|
|
|
'data' => ['type' => 'people', 'id' => '1'], |
1545
|
|
|
], |
1546
|
|
|
], |
1547
|
|
|
], |
1548
|
|
|
[ |
1549
|
|
|
'type' => 'books', |
1550
|
|
|
'id' => '2', |
1551
|
|
|
'attributes' => [ |
1552
|
|
|
'title' => 'Bar', |
1553
|
|
|
'year' => 2015, |
1554
|
|
|
], |
1555
|
|
|
'relationships' => [ |
1556
|
|
|
'author' => [ |
1557
|
|
|
'data' => ['type' => 'people', 'id' => '1'], |
1558
|
|
|
], |
1559
|
|
|
], |
1560
|
|
|
], |
1561
|
|
|
], |
1562
|
|
|
]; |
1563
|
|
|
|
1564
|
|
|
$this->assertSame($expected, $scope->toArray()); |
1565
|
|
|
|
1566
|
|
|
$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"}}}}]}'; |
1567
|
|
|
$this->assertSame($expectedJson, $scope->toJson()); |
1568
|
|
|
} |
1569
|
|
|
|
1570
|
|
|
public function testSerializingCollectionWithReferenceToRootObjects() |
1571
|
|
|
{ |
1572
|
|
|
$this->manager->parseIncludes('author.published'); |
1573
|
|
|
|
1574
|
|
|
$booksData = [ |
1575
|
|
|
[ |
1576
|
|
|
'id' => 1, |
1577
|
|
|
'title' => 'Foo', |
1578
|
|
|
'year' => 1991, |
1579
|
|
|
'_author' => [ |
1580
|
|
|
'id' => 1, |
1581
|
|
|
'name' => 'Dave', |
1582
|
|
|
'_published' => [ |
1583
|
|
|
[ |
1584
|
|
|
'id' => 1, |
1585
|
|
|
'title' => 'Foo', |
1586
|
|
|
'year' => 1991, |
1587
|
|
|
], |
1588
|
|
|
[ |
1589
|
|
|
'id' => 2, |
1590
|
|
|
'title' => 'Bar', |
1591
|
|
|
'year' => 2015, |
1592
|
|
|
], |
1593
|
|
|
], |
1594
|
|
|
], |
1595
|
|
|
], |
1596
|
|
|
[ |
1597
|
|
|
'id' => 2, |
1598
|
|
|
'title' => 'Bar', |
1599
|
|
|
'year' => 2015, |
1600
|
|
|
'_author' => [ |
1601
|
|
|
'id' => 1, |
1602
|
|
|
'_published' => [ |
1603
|
|
|
[ |
1604
|
|
|
'id' => 1, |
1605
|
|
|
'title' => 'Foo', |
1606
|
|
|
'year' => 1991, |
1607
|
|
|
], |
1608
|
|
|
[ |
1609
|
|
|
'id' => 2, |
1610
|
|
|
'title' => 'Bar', |
1611
|
|
|
'year' => 2015, |
1612
|
|
|
], |
1613
|
|
|
], |
1614
|
|
|
], |
1615
|
|
|
], |
1616
|
|
|
]; |
1617
|
|
|
|
1618
|
|
|
$resource = new Collection($booksData, new JsonApiBookTransformer(), 'books'); |
1619
|
|
|
|
1620
|
|
|
$scope = new Scope($this->manager, $resource); |
1621
|
|
|
|
1622
|
|
|
$expected = [ |
1623
|
|
|
'data' => [ |
1624
|
|
|
[ |
1625
|
|
|
'type' => 'books', |
1626
|
|
|
'id' => '1', |
1627
|
|
|
'attributes' => [ |
1628
|
|
|
'title' => 'Foo', |
1629
|
|
|
'year' => 1991, |
1630
|
|
|
], |
1631
|
|
|
'relationships' => [ |
1632
|
|
|
'author' => [ |
1633
|
|
|
'data' => [ |
1634
|
|
|
'type' => 'people', |
1635
|
|
|
'id' => '1', |
1636
|
|
|
], |
1637
|
|
|
], |
1638
|
|
|
], |
1639
|
|
|
], |
1640
|
|
|
[ |
1641
|
|
|
'type' => 'books', |
1642
|
|
|
'id' => '2', |
1643
|
|
|
'attributes' => [ |
1644
|
|
|
'title' => 'Bar', |
1645
|
|
|
'year' => 2015, |
1646
|
|
|
], |
1647
|
|
|
'relationships' => [ |
1648
|
|
|
'author' => [ |
1649
|
|
|
'data' => [ |
1650
|
|
|
'type' => 'people', |
1651
|
|
|
'id' => '1', |
1652
|
|
|
], |
1653
|
|
|
], |
1654
|
|
|
], |
1655
|
|
|
], |
1656
|
|
|
], |
1657
|
|
|
'included' => [ |
1658
|
|
|
[ |
1659
|
|
|
'type' => 'people', |
1660
|
|
|
'id' => '1', |
1661
|
|
|
'attributes' => [ |
1662
|
|
|
'name' => 'Dave', |
1663
|
|
|
], |
1664
|
|
|
'relationships' => [ |
1665
|
|
|
'published' => [ |
1666
|
|
|
'data' => [ |
1667
|
|
|
['type' => 'books', 'id' => '1'], |
1668
|
|
|
['type' => 'books', 'id' => '2'], |
1669
|
|
|
], |
1670
|
|
|
], |
1671
|
|
|
], |
1672
|
|
|
], |
1673
|
|
|
], |
1674
|
|
|
]; |
1675
|
|
|
|
1676
|
|
|
$this->assertSame($expected, $scope->toArray()); |
1677
|
|
|
|
1678
|
|
|
$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"}]}}}]}'; |
1679
|
|
|
$this->assertSame($expectedJson, $scope->toJson()); |
1680
|
|
|
} |
1681
|
|
|
|
1682
|
|
|
public function testSerializingCollectionResourceWithPaginator() |
1683
|
|
|
{ |
1684
|
|
|
$baseUrl = 'http://example.com'; |
1685
|
|
|
|
1686
|
|
|
$this->manager->setSerializer(new JsonApiSerializer($baseUrl)); |
1687
|
|
|
|
1688
|
|
|
$total = 10; |
1689
|
|
|
$count = 2; |
1690
|
|
|
$perPage = 2; |
1691
|
|
|
$currentPage = 2; |
1692
|
|
|
$lastPage = 5; |
1693
|
|
|
$previousUrl = 'http://example.com/books/?page=1'; |
1694
|
|
|
$currentUrl = 'http://example.com/books/?page=2'; |
1695
|
|
|
$nextUrl = 'http://example.com/books/?page=3'; |
1696
|
|
|
$lastUrl = 'http://example.com/books/?page=5'; |
1697
|
|
|
|
1698
|
|
|
$paginator = Mockery::mock('League\Fractal\Pagination\PaginatorInterface'); |
1699
|
|
|
$paginator->shouldReceive('getCurrentPage')->andReturn($currentPage); |
1700
|
|
|
$paginator->shouldReceive('getLastPage')->andReturn($lastPage); |
1701
|
|
|
$paginator->shouldReceive('getTotal')->andReturn($total); |
1702
|
|
|
$paginator->shouldReceive('getCount')->andReturn($count); |
1703
|
|
|
$paginator->shouldReceive('getPerPage')->andReturn($perPage); |
1704
|
|
|
$paginator->shouldReceive('getUrl')->with(1)->andReturn($previousUrl); |
1705
|
|
|
$paginator->shouldReceive('getUrl')->with(2)->andReturn($currentUrl); |
1706
|
|
|
$paginator->shouldReceive('getUrl')->with(3)->andReturn($nextUrl); |
1707
|
|
|
$paginator->shouldReceive('getUrl')->with(5)->andReturn($lastUrl); |
1708
|
|
|
|
1709
|
|
|
$booksData = [ |
1710
|
|
|
[ |
1711
|
|
|
'id' => 1, |
1712
|
|
|
'title' => 'Foo', |
1713
|
|
|
'year' => '1991', |
1714
|
|
|
'_author' => [ |
1715
|
|
|
'id' => 1, |
1716
|
|
|
'name' => 'Dave', |
1717
|
|
|
], |
1718
|
|
|
], |
1719
|
|
|
[ |
1720
|
|
|
'id' => 2, |
1721
|
|
|
'title' => 'Bar', |
1722
|
|
|
'year' => '1997', |
1723
|
|
|
'_author' => [ |
1724
|
|
|
'id' => 2, |
1725
|
|
|
'name' => 'Bob', |
1726
|
|
|
], |
1727
|
|
|
], |
1728
|
|
|
]; |
1729
|
|
|
|
1730
|
|
|
$resource = new Collection($booksData, new JsonApiBookTransformer(), 'books'); |
1731
|
|
|
$resource->setPaginator($paginator); |
1732
|
|
|
$scope = new Scope($this->manager, $resource); |
1733
|
|
|
|
1734
|
|
|
$expected = [ |
1735
|
|
|
'data' => [ |
1736
|
|
|
[ |
1737
|
|
|
'type' => 'books', |
1738
|
|
|
'id' => '1', |
1739
|
|
|
'attributes' => [ |
1740
|
|
|
'title' => 'Foo', |
1741
|
|
|
'year' => 1991, |
1742
|
|
|
], |
1743
|
|
|
'links' => [ |
1744
|
|
|
'self' => 'http://example.com/books/1', |
1745
|
|
|
], |
1746
|
|
|
], |
1747
|
|
|
[ |
1748
|
|
|
'type' => 'books', |
1749
|
|
|
'id' => '2', |
1750
|
|
|
'attributes' => [ |
1751
|
|
|
'title' => 'Bar', |
1752
|
|
|
'year' => 1997, |
1753
|
|
|
], |
1754
|
|
|
'links' => [ |
1755
|
|
|
'self' => 'http://example.com/books/2', |
1756
|
|
|
], |
1757
|
|
|
], |
1758
|
|
|
], |
1759
|
|
|
'meta' => [ |
1760
|
|
|
'pagination' => [ |
1761
|
|
|
'total' => 10, |
1762
|
|
|
'count' => 2, |
1763
|
|
|
'per_page' => 2, |
1764
|
|
|
'current_page' => 2, |
1765
|
|
|
'total_pages' => 5 |
1766
|
|
|
] |
1767
|
|
|
], |
1768
|
|
|
'links' => [ |
1769
|
|
|
'self' => 'http://example.com/books/?page=2', |
1770
|
|
|
'first' => 'http://example.com/books/?page=1', |
1771
|
|
|
'prev' => 'http://example.com/books/?page=1', |
1772
|
|
|
'next' => 'http://example.com/books/?page=3', |
1773
|
|
|
'last' => 'http://example.com/books/?page=5' |
1774
|
|
|
] |
1775
|
|
|
]; |
1776
|
|
|
|
1777
|
|
|
$this->assertSame($expected, $scope->toArray()); |
1778
|
|
|
|
1779
|
|
|
$expectedJson = '{"data":[{"type":"books","id":"1","attributes":{"title":"Foo","year":1991},"links":{"self":"http:\/\/example.com\/books\/1"}},{"type":"books","id":"2","attributes":{"title":"Bar","year":1997},"links":{"self":"http:\/\/example.com\/books\/2"}}],"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"}}'; |
1780
|
|
|
$this->assertSame($expectedJson, $scope->toJson()); |
1781
|
|
|
} |
1782
|
|
|
|
1783
|
|
View Code Duplication |
public function testSerializingCollectionResourceWithPaginatorWithOmittedUnavailablePreviousLink() |
1784
|
|
|
{ |
1785
|
|
|
$baseUrl = 'http://example.com'; |
1786
|
|
|
|
1787
|
|
|
$this->manager->setSerializer(new JsonApiSerializer($baseUrl)); |
1788
|
|
|
|
1789
|
|
|
$total = 10; |
1790
|
|
|
$count = 2; |
1791
|
|
|
$perPage = 2; |
1792
|
|
|
$currentPage = 1; |
1793
|
|
|
$lastPage = 5; |
1794
|
|
|
$currentUrl = 'http://example.com/books/?page=1'; |
1795
|
|
|
$nextUrl = 'http://example.com/books/?page=2'; |
1796
|
|
|
$lastUrl = 'http://example.com/books/?page=5'; |
1797
|
|
|
|
1798
|
|
|
$paginator = Mockery::mock('League\Fractal\Pagination\PaginatorInterface'); |
1799
|
|
|
$paginator->shouldReceive('getCurrentPage')->andReturn($currentPage); |
1800
|
|
|
$paginator->shouldReceive('getLastPage')->andReturn($lastPage); |
1801
|
|
|
$paginator->shouldReceive('getTotal')->andReturn($total); |
1802
|
|
|
$paginator->shouldReceive('getCount')->andReturn($count); |
1803
|
|
|
$paginator->shouldReceive('getPerPage')->andReturn($perPage); |
1804
|
|
|
$paginator->shouldReceive('getUrl')->with(1)->andReturn($currentUrl); |
1805
|
|
|
$paginator->shouldReceive('getUrl')->with(2)->andReturn($nextUrl); |
1806
|
|
|
$paginator->shouldReceive('getUrl')->with(5)->andReturn($lastUrl); |
1807
|
|
|
|
1808
|
|
|
$booksData = [ |
1809
|
|
|
[ |
1810
|
|
|
'id' => 1, |
1811
|
|
|
'title' => 'Foo', |
1812
|
|
|
'year' => '1991', |
1813
|
|
|
'_author' => [ |
1814
|
|
|
'id' => 1, |
1815
|
|
|
'name' => 'Dave', |
1816
|
|
|
], |
1817
|
|
|
], |
1818
|
|
|
[ |
1819
|
|
|
'id' => 2, |
1820
|
|
|
'title' => 'Bar', |
1821
|
|
|
'year' => '1997', |
1822
|
|
|
'_author' => [ |
1823
|
|
|
'id' => 2, |
1824
|
|
|
'name' => 'Bob', |
1825
|
|
|
], |
1826
|
|
|
], |
1827
|
|
|
]; |
1828
|
|
|
|
1829
|
|
|
$resource = new Collection($booksData, new JsonApiBookTransformer(), 'books'); |
1830
|
|
|
$resource->setPaginator($paginator); |
1831
|
|
|
$scope = new Scope($this->manager, $resource); |
1832
|
|
|
|
1833
|
|
|
$expected = [ |
1834
|
|
|
'data' => [ |
1835
|
|
|
[ |
1836
|
|
|
'type' => 'books', |
1837
|
|
|
'id' => '1', |
1838
|
|
|
'attributes' => [ |
1839
|
|
|
'title' => 'Foo', |
1840
|
|
|
'year' => 1991, |
1841
|
|
|
], |
1842
|
|
|
'links' => [ |
1843
|
|
|
'self' => 'http://example.com/books/1', |
1844
|
|
|
], |
1845
|
|
|
], |
1846
|
|
|
[ |
1847
|
|
|
'type' => 'books', |
1848
|
|
|
'id' => '2', |
1849
|
|
|
'attributes' => [ |
1850
|
|
|
'title' => 'Bar', |
1851
|
|
|
'year' => 1997, |
1852
|
|
|
], |
1853
|
|
|
'links' => [ |
1854
|
|
|
'self' => 'http://example.com/books/2', |
1855
|
|
|
], |
1856
|
|
|
], |
1857
|
|
|
], |
1858
|
|
|
'meta' => [ |
1859
|
|
|
'pagination' => [ |
1860
|
|
|
'total' => 10, |
1861
|
|
|
'count' => 2, |
1862
|
|
|
'per_page' => 2, |
1863
|
|
|
'current_page' => 1, |
1864
|
|
|
'total_pages' => 5 |
1865
|
|
|
] |
1866
|
|
|
], |
1867
|
|
|
'links' => [ |
1868
|
|
|
'self' => 'http://example.com/books/?page=1', |
1869
|
|
|
'first' => 'http://example.com/books/?page=1', |
1870
|
|
|
'next' => 'http://example.com/books/?page=2', |
1871
|
|
|
'last' => 'http://example.com/books/?page=5' |
1872
|
|
|
] |
1873
|
|
|
]; |
1874
|
|
|
|
1875
|
|
|
$this->assertSame($expected, $scope->toArray()); |
1876
|
|
|
|
1877
|
|
|
$expectedJson = '{"data":[{"type":"books","id":"1","attributes":{"title":"Foo","year":1991},"links":{"self":"http:\/\/example.com\/books\/1"}},{"type":"books","id":"2","attributes":{"title":"Bar","year":1997},"links":{"self":"http:\/\/example.com\/books\/2"}}],"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"}}'; |
1878
|
|
|
$this->assertSame($expectedJson, $scope->toJson()); |
1879
|
|
|
} |
1880
|
|
|
|
1881
|
|
View Code Duplication |
public function testSerializingCollectionResourceWithPaginatorWithOmittedUnavailableNextLink() |
1882
|
|
|
{ |
1883
|
|
|
$baseUrl = 'http://example.com'; |
1884
|
|
|
|
1885
|
|
|
$this->manager->setSerializer(new JsonApiSerializer($baseUrl)); |
1886
|
|
|
|
1887
|
|
|
$total = 10; |
1888
|
|
|
$count = 2; |
1889
|
|
|
$perPage = 2; |
1890
|
|
|
$currentPage = 5; |
1891
|
|
|
$lastPage = 5; |
1892
|
|
|
$firstUrl = 'http://example.com/books/?page=1'; |
1893
|
|
|
$previousUrl = 'http://example.com/books/?page=4'; |
1894
|
|
|
$lastUrl = 'http://example.com/books/?page=5'; |
1895
|
|
|
|
1896
|
|
|
$paginator = Mockery::mock('League\Fractal\Pagination\PaginatorInterface'); |
1897
|
|
|
$paginator->shouldReceive('getCurrentPage')->andReturn($currentPage); |
1898
|
|
|
$paginator->shouldReceive('getLastPage')->andReturn($lastPage); |
1899
|
|
|
$paginator->shouldReceive('getTotal')->andReturn($total); |
1900
|
|
|
$paginator->shouldReceive('getCount')->andReturn($count); |
1901
|
|
|
$paginator->shouldReceive('getPerPage')->andReturn($perPage); |
1902
|
|
|
$paginator->shouldReceive('getUrl')->with(1)->andReturn($firstUrl); |
1903
|
|
|
$paginator->shouldReceive('getUrl')->with(4)->andReturn($previousUrl); |
1904
|
|
|
$paginator->shouldReceive('getUrl')->with(5)->andReturn($lastUrl); |
1905
|
|
|
|
1906
|
|
|
$booksData = [ |
1907
|
|
|
[ |
1908
|
|
|
'id' => 1, |
1909
|
|
|
'title' => 'Foo', |
1910
|
|
|
'year' => '1991', |
1911
|
|
|
'_author' => [ |
1912
|
|
|
'id' => 1, |
1913
|
|
|
'name' => 'Dave', |
1914
|
|
|
], |
1915
|
|
|
], |
1916
|
|
|
[ |
1917
|
|
|
'id' => 2, |
1918
|
|
|
'title' => 'Bar', |
1919
|
|
|
'year' => '1997', |
1920
|
|
|
'_author' => [ |
1921
|
|
|
'id' => 2, |
1922
|
|
|
'name' => 'Bob', |
1923
|
|
|
], |
1924
|
|
|
], |
1925
|
|
|
]; |
1926
|
|
|
|
1927
|
|
|
$resource = new Collection($booksData, new JsonApiBookTransformer(), 'books'); |
1928
|
|
|
$resource->setPaginator($paginator); |
1929
|
|
|
$scope = new Scope($this->manager, $resource); |
1930
|
|
|
|
1931
|
|
|
$expected = [ |
1932
|
|
|
'data' => [ |
1933
|
|
|
[ |
1934
|
|
|
'type' => 'books', |
1935
|
|
|
'id' => '1', |
1936
|
|
|
'attributes' => [ |
1937
|
|
|
'title' => 'Foo', |
1938
|
|
|
'year' => 1991, |
1939
|
|
|
], |
1940
|
|
|
'links' => [ |
1941
|
|
|
'self' => 'http://example.com/books/1', |
1942
|
|
|
], |
1943
|
|
|
], |
1944
|
|
|
[ |
1945
|
|
|
'type' => 'books', |
1946
|
|
|
'id' => '2', |
1947
|
|
|
'attributes' => [ |
1948
|
|
|
'title' => 'Bar', |
1949
|
|
|
'year' => 1997, |
1950
|
|
|
], |
1951
|
|
|
'links' => [ |
1952
|
|
|
'self' => 'http://example.com/books/2', |
1953
|
|
|
], |
1954
|
|
|
], |
1955
|
|
|
], |
1956
|
|
|
'meta' => [ |
1957
|
|
|
'pagination' => [ |
1958
|
|
|
'total' => 10, |
1959
|
|
|
'count' => 2, |
1960
|
|
|
'per_page' => 2, |
1961
|
|
|
'current_page' => 5, |
1962
|
|
|
'total_pages' => 5 |
1963
|
|
|
] |
1964
|
|
|
], |
1965
|
|
|
'links' => [ |
1966
|
|
|
'self' => 'http://example.com/books/?page=5', |
1967
|
|
|
'first' => 'http://example.com/books/?page=1', |
1968
|
|
|
'prev' => 'http://example.com/books/?page=4', |
1969
|
|
|
'last' => 'http://example.com/books/?page=5' |
1970
|
|
|
] |
1971
|
|
|
]; |
1972
|
|
|
|
1973
|
|
|
$this->assertSame($expected, $scope->toArray()); |
1974
|
|
|
|
1975
|
|
|
$expectedJson = '{"data":[{"type":"books","id":"1","attributes":{"title":"Foo","year":1991},"links":{"self":"http:\/\/example.com\/books\/1"}},{"type":"books","id":"2","attributes":{"title":"Bar","year":1997},"links":{"self":"http:\/\/example.com\/books\/2"}}],"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"}}'; |
1976
|
|
|
$this->assertSame($expectedJson, $scope->toJson()); |
1977
|
|
|
} |
1978
|
|
|
|
1979
|
|
|
public function testCustomLinkMerge() |
1980
|
|
|
{ |
1981
|
|
|
$manager = new Manager(); |
1982
|
|
|
$manager->setSerializer(new JsonApiSerializer('http://test.de')); |
1983
|
|
|
|
1984
|
|
|
$bookData = [ |
1985
|
|
|
'id' => 1, |
1986
|
|
|
'title' => 'Foo', |
1987
|
|
|
'year' => '1991', |
1988
|
|
|
'_author' => [ |
1989
|
|
|
'id' => 1, |
1990
|
|
|
'name' => 'Dave', |
1991
|
|
|
], |
1992
|
|
|
'links' => [ |
1993
|
|
|
'custom_link' => '/custom/link', |
1994
|
|
|
], |
1995
|
|
|
]; |
1996
|
|
|
|
1997
|
|
|
$resource = new Item($bookData, new JsonApiBookTransformer('test.de'), 'books'); |
|
|
|
|
1998
|
|
|
|
1999
|
|
|
$scope = new Scope($manager, $resource); |
2000
|
|
|
|
2001
|
|
|
$expected = [ |
2002
|
|
|
'data' => [ |
2003
|
|
|
'type' => 'books', |
2004
|
|
|
'id' => '1', |
2005
|
|
|
'attributes' => [ |
2006
|
|
|
'title' => 'Foo', |
2007
|
|
|
'year' => 1991, |
2008
|
|
|
], |
2009
|
|
|
'links' => [ |
2010
|
|
|
'custom_link' => '/custom/link', |
2011
|
|
|
'self' => 'http://test.de/books/1', |
2012
|
|
|
] |
2013
|
|
|
], |
2014
|
|
|
]; |
2015
|
|
|
|
2016
|
|
|
$this->assertSame(json_encode($expected), $scope->toJson()); |
2017
|
|
|
} |
2018
|
|
|
|
2019
|
|
|
public function testCustomLinkMergeNoLink() |
2020
|
|
|
{ |
2021
|
|
|
$manager = new Manager(); |
2022
|
|
|
$manager->setSerializer(new JsonApiSerializer('http://test.de')); |
2023
|
|
|
|
2024
|
|
|
$bookData = [ |
2025
|
|
|
'id' => 1, |
2026
|
|
|
'title' => 'Foo', |
2027
|
|
|
'year' => '1991', |
2028
|
|
|
'_author' => [ |
2029
|
|
|
'id' => 1, |
2030
|
|
|
'name' => 'Dave', |
2031
|
|
|
], |
2032
|
|
|
]; |
2033
|
|
|
|
2034
|
|
|
$resource = new Item($bookData, new JsonApiBookTransformer(), 'books'); |
2035
|
|
|
|
2036
|
|
|
$scope = new Scope($manager, $resource); |
2037
|
|
|
|
2038
|
|
|
$expected = [ |
2039
|
|
|
'data' => [ |
2040
|
|
|
'type' => 'books', |
2041
|
|
|
'id' => '1', |
2042
|
|
|
'attributes' => [ |
2043
|
|
|
'title' => 'Foo', |
2044
|
|
|
'year' => 1991, |
2045
|
|
|
], |
2046
|
|
|
'links' => [ |
2047
|
|
|
'self' => 'http://test.de/books/1', |
2048
|
|
|
] |
2049
|
|
|
], |
2050
|
|
|
]; |
2051
|
|
|
|
2052
|
|
|
$this->assertSame(json_encode($expected), $scope->toJson()); |
2053
|
|
|
} |
2054
|
|
|
|
2055
|
|
|
public function tearDown() |
2056
|
|
|
{ |
2057
|
|
|
Mockery::close(); |
2058
|
|
|
} |
2059
|
|
|
} |
2060
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.