Completed
Pull Request — master (#331)
by Matt
03:42
created

testSerializingItemResourceWithLinksForHasOneRelationship()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 77
Code Lines 49

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 77
rs 8.9342
cc 1
eloc 49
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 View Code Duplication
    public function testSerializingItemResourceWithHasOneInclude()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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
    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
                'relationships' => [
1016
                    'author' => [
1017
                        'links' => [
1018
                            'self' => 'http://example.com/books/1/relationships/author',
1019
                            'related' => 'http://example.com/books/1/author',
1020
                        ],
1021
                    ],
1022
                    'co-author' => [
1023
                        'links' => [
1024
                            'self' => 'http://example.com/books/1/relationships/co-author',
1025
                            'related' => 'http://example.com/books/1/co-author',
1026
                        ],
1027
                    ],
1028
                ],
1029
            ],
1030
        ];
1031
1032
        $this->assertSame($expected, $scope->toArray());
1033
1034
        $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"}}}}}';
1035
        $this->assertSame($expectedJson, $scope->toJson());
1036
    }
1037
1038
    public function testSerializingCollectionResourceWithSelfLink()
1039
    {
1040
        $baseUrl = 'http://example.com';
1041
        $this->manager->setSerializer(new JsonApiSerializer($baseUrl));
1042
1043
        $booksData = [
1044
            [
1045
                'id' => 1,
1046
                'title' => 'Foo',
1047
                'year' => '1991',
1048
                '_author' => [
1049
                    'id' => 1,
1050
                    'name' => 'Dave',
1051
                ],
1052
            ],
1053
            [
1054
                'id' => 2,
1055
                'title' => 'Bar',
1056
                'year' => '1997',
1057
                '_author' => [
1058
                    'id' => 2,
1059
                    'name' => 'Bob',
1060
                ],
1061
            ],
1062
        ];
1063
1064
        $resource = new Collection($booksData, new JsonApiBookTransformer(), 'books');
1065
        $scope = new Scope($this->manager, $resource);
1066
1067
        $expected = [
1068
            'data' => [
1069
                [
1070
                    'type' => 'books',
1071
                    'id' => '1',
1072
                    'attributes' => [
1073
                        'title' => 'Foo',
1074
                        'year' => 1991,
1075
                    ],
1076
                    'links' => [
1077
                        'self' => 'http://example.com/books/1',
1078
                    ],
1079
                    'relationships' => [
1080
                        'author' => [
1081
                            'links' => [
1082
                                'self' => 'http://example.com/books/1/relationships/author',
1083
                                'related' => 'http://example.com/books/1/author',
1084
                            ],
1085
                        ],
1086
                        'co-author' => [
1087
                            'links' => [
1088
                                'self' => 'http://example.com/books/1/relationships/co-author',
1089
                                'related' => 'http://example.com/books/1/co-author',
1090
                            ],
1091
                        ],
1092
                    ],
1093
                ],
1094
                [
1095
                    'type' => 'books',
1096
                    'id' => '2',
1097
                    'attributes' => [
1098
                        'title' => 'Bar',
1099
                        'year' => 1997,
1100
                    ],
1101
                    'links' => [
1102
                        'self' => 'http://example.com/books/2',
1103
                    ],
1104
                    'relationships' => [
1105
                        'author' => [
1106
                            'links' => [
1107
                                'self' => 'http://example.com/books/2/relationships/author',
1108
                                'related' => 'http://example.com/books/2/author'
1109
                            ],
1110
                        ],
1111
                        'co-author' => [
1112
                            'links' => [
1113
                                'self' => 'http://example.com/books/2/relationships/co-author',
1114
                                'related' => 'http://example.com/books/2/co-author'
1115
                            ],
1116
                        ],
1117
                    ],
1118
                ],
1119
            ],
1120
        ];
1121
1122
        $this->assertSame($expected, $scope->toArray());
1123
1124
        $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"}}}},{"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"}}}}]}';
1125
        $this->assertSame($expectedJson, $scope->toJson());
1126
    }
1127
1128
    public function testSerializingItemResourceWithLinksForHasOneRelationship()
1129
    {
1130
        $baseUrl = 'http://example.com';
1131
        $this->manager->setSerializer(new JsonApiSerializer($baseUrl));
1132
        $this->manager->parseIncludes('author');
1133
1134
        $bookData = [
1135
            'id' => 1,
1136
            'title' => 'Foo',
1137
            'year' => '1991',
1138
            '_author' => [
1139
                'id' => 1,
1140
                'name' => 'Dave',
1141
            ],
1142
        ];
1143
1144
        $resource = new Item($bookData, new JsonApiBookTransformer(), 'books');
1145
1146
        $scope = new Scope($this->manager, $resource);
1147
1148
        $expected = [
1149
            'data' => [
1150
                'type' => 'books',
1151
                'id' => '1',
1152
                'attributes' => [
1153
                    'title' => 'Foo',
1154
                    'year' => 1991,
1155
                ],
1156
                'relationships' => [
1157
                    'author' => [
1158
                        'links' => [
1159
                            'self' => 'http://example.com/books/1/relationships/author',
1160
                            'related' => 'http://example.com/books/1/author',
1161
                        ],
1162
                        'data' => [
1163
                            'type' => 'people',
1164
                            'id' => '1',
1165
                        ],
1166
                    ],
1167
                    'co-author' => [
1168
                        'links' => [
1169
                            'self' => 'http://example.com/books/1/relationships/co-author',
1170
                            'related' => 'http://example.com/books/1/co-author'
1171
                        ],
1172
                    ],
1173
                ],
1174
                'links' => [
1175
                    'self' => 'http://example.com/books/1',
1176
                ],
1177
            ],
1178
            'included' => [
1179
                [
1180
                    'type' => 'people',
1181
                    'id' => '1',
1182
                    'attributes' => [
1183
                        'name' => 'Dave',
1184
                    ],
1185
                    'relationships' => [
1186
                        'published' => [
1187
                            'links' => [
1188
                                'self' => 'http://example.com/people/1/relationships/published',
1189
                                'related' => 'http://example.com/people/1/published',
1190
                            ],
1191
                        ],
1192
                    ],
1193
                    'links' => [
1194
                        'self' => 'http://example.com/people/1',
1195
                    ],
1196
                ],
1197
            ],
1198
        ];
1199
1200
        $this->assertEquals($expected, $scope->toArray());
1201
1202
        $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"}}}},"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"}}}}]}';
1203
        $this->assertSame($expectedJson, $scope->toJson());
1204
    }
1205
1206
    public function testSerializingItemResourceWithLinksForHasManyRelationship()
1207
    {
1208
        $baseUrl = 'http://example.com';
1209
        $this->manager->setSerializer(new JsonApiSerializer($baseUrl));
1210
        $this->manager->parseIncludes('published');
1211
1212
        $authorData = [
1213
            'id' => 1,
1214
            'name' => 'Dave',
1215
            '_published' => [
1216
                [
1217
                    'id' => 1,
1218
                    'title' => 'Foo',
1219
                    'year' => '1991',
1220
                ],
1221
                [
1222
                    'id' => 2,
1223
                    'title' => 'Bar',
1224
                    'year' => '2015',
1225
                ],
1226
            ],
1227
        ];
1228
1229
        $resource = new Item($authorData, new JsonApiAuthorTransformer(), 'people');
1230
1231
        $scope = new Scope($this->manager, $resource);
1232
1233
        $expected = [
1234
            'data' => [
1235
                'type' => 'people',
1236
                'id' => '1',
1237
                'attributes' => [
1238
                    'name' => 'Dave',
1239
                ],
1240
                'relationships' => [
1241
                    'published' => [
1242
                        'links' => [
1243
                            'self' => 'http://example.com/people/1/relationships/published',
1244
                            'related' => 'http://example.com/people/1/published',
1245
                        ],
1246
                        'data' => [
1247
                            [
1248
                                'type' => 'books',
1249
                                'id' => 1,
1250
                            ],
1251
                            [
1252
                                'type' => 'books',
1253
                                'id' => 2,
1254
                            ],
1255
                        ],
1256
                    ],
1257
                ],
1258
                'links' => [
1259
                    'self' => 'http://example.com/people/1',
1260
                ],
1261
            ],
1262
            'included' => [
1263
                [
1264
                    'type' => 'books',
1265
                    'id' => '1',
1266
                    'attributes' => [
1267
                        'title' => 'Foo',
1268
                        'year' => 1991,
1269
                    ],
1270
                    'links' => [
1271
                        'self' => 'http://example.com/books/1',
1272
                    ],
1273
                    'relationships' => [
1274
                        'author' => [
1275
                            'links' => [
1276
                                'self' => 'http://example.com/books/1/relationships/author',
1277
                                'related' => 'http://example.com/books/1/author'
1278
                            ],
1279
                        ],
1280
                        'co-author' => [
1281
                            'links' => [
1282
                                'self' => 'http://example.com/books/1/relationships/co-author',
1283
                                'related' => 'http://example.com/books/1/co-author'
1284
                            ],
1285
                        ],
1286
                    ],
1287
                ],
1288
                [
1289
                    'type' => 'books',
1290
                    'id' => '2',
1291
                    'attributes' => [
1292
                        'title' => 'Bar',
1293
                        'year' => 2015,
1294
                    ],
1295
                    'links' => [
1296
                        'self' => 'http://example.com/books/2',
1297
                    ],
1298
                    'relationships' => [
1299
                        'author' => [
1300
                            'links' => [
1301
                                'self' => 'http://example.com/books/2/relationships/author',
1302
                                'related' => 'http://example.com/books/2/author'
1303
                            ],
1304
                        ],
1305
                        'co-author' => [
1306
                            'links' => [
1307
                                'self' => 'http://example.com/books/2/relationships/co-author',
1308
                                'related' => 'http://example.com/books/2/co-author'
1309
                            ],
1310
                        ],
1311
                    ],
1312
                ],
1313
            ],
1314
        ];
1315
1316
        $this->assertEquals($expected, $scope->toArray());
1317
1318
        $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"}}}},{"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"}}}}]}';
1319
        $this->assertSame($expectedJson, $scope->toJson());
1320
    }
1321
1322
    public function testSerializingCollectionResourceWithLinksForHasOneRelationship()
1323
    {
1324
        $baseUrl = 'http://example.com';
1325
        $this->manager->setSerializer(new JsonApiSerializer($baseUrl));
1326
        $this->manager->parseIncludes('author');
1327
1328
        $bookData = [
1329
            [
1330
                'id' => 1,
1331
                'title' => 'Foo',
1332
                'year' => '1991',
1333
                '_author' => [
1334
                    'id' => 1,
1335
                    'name' => 'Dave',
1336
                ],
1337
            ],
1338
            [
1339
                'id' => 2,
1340
                'title' => 'Bar',
1341
                'year' => '1991',
1342
                '_author' => [
1343
                    'id' => 1,
1344
                    'name' => 'Dave',
1345
                ],
1346
            ],
1347
        ];
1348
1349
        $resource = new Collection($bookData, new JsonApiBookTransformer(), 'books');
1350
1351
        $scope = new Scope($this->manager, $resource);
1352
1353
        $expected = [
1354
            'data' => [
1355
                [
1356
                    'type' => 'books',
1357
                    'id' => '1',
1358
                    'attributes' => [
1359
                        'title' => 'Foo',
1360
                        'year' => 1991,
1361
                    ],
1362
                    'relationships' => [
1363
                        'author' => [
1364
                            'links' => [
1365
                                'self' => 'http://example.com/books/1/relationships/author',
1366
                                'related' => 'http://example.com/books/1/author',
1367
                            ],
1368
                            'data' => [
1369
                                'type' => 'people',
1370
                                'id' => '1',
1371
                            ],
1372
                        ],
1373
                        'co-author' => [
1374
                            'links' => [
1375
                                'self' => 'http://example.com/books/1/relationships/co-author',
1376
                                'related' => 'http://example.com/books/1/co-author'
1377
                            ],
1378
                        ],
1379
                    ],
1380
                    'links' => [
1381
                        'self' => 'http://example.com/books/1',
1382
                    ],
1383
                ],
1384
                [
1385
                    'type' => 'books',
1386
                    'id' => '2',
1387
                    'attributes' => [
1388
                        'title' => 'Bar',
1389
                        'year' => 1991,
1390
                    ],
1391
                    'relationships' => [
1392
                        'author' => [
1393
                            'links' => [
1394
                                'self' => 'http://example.com/books/2/relationships/author',
1395
                                'related' => 'http://example.com/books/2/author',
1396
                            ],
1397
                            'data' => [
1398
                                'type' => 'people',
1399
                                'id' => '1',
1400
                            ],
1401
                        ],
1402
                        'co-author' => [
1403
                            'links' => [
1404
                                'self' => 'http://example.com/books/2/relationships/co-author',
1405
                                'related' => 'http://example.com/books/2/co-author'
1406
                            ],
1407
                        ],
1408
                    ],
1409
                    'links' => [
1410
                        'self' => 'http://example.com/books/2',
1411
                    ],
1412
                ],
1413
            ],
1414
            'included' => [
1415
                [
1416
                    'type' => 'people',
1417
                    'id' => '1',
1418
                    'attributes' => [
1419
                        'name' => 'Dave',
1420
                    ],
1421
                    'links' => [
1422
                        'self' => 'http://example.com/people/1',
1423
                    ],
1424
                    'relationships' => [
1425
                        'published' => [
1426
                            'links' => [
1427
                                'self' => 'http://example.com/people/1/relationships/published',
1428
                                'related' => 'http://example.com/people/1/published',
1429
                            ],
1430
                        ],
1431
                    ],
1432
                ],
1433
            ],
1434
        ];
1435
1436
        $this->assertEquals($expected, $scope->toArray());
1437
1438
        $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"}}}},{"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"}}}}],"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"}}}}]}';
1439
        $this->assertSame($expectedJson, $scope->toJson());
1440
    }
1441
1442
    public function testSerializingCollectionResourceWithLinksForHasManyRelationship()
1443
    {
1444
        $baseUrl = 'http://example.com';
1445
        $this->manager->setSerializer(new JsonApiSerializer($baseUrl));
1446
        $this->manager->parseIncludes('published');
1447
1448
        $authorData = [
1449
            [
1450
                'id' => 1,
1451
                'name' => 'Dave',
1452
                '_published' => [
1453
                    [
1454
                        'id' => 1,
1455
                        'title' => 'Foo',
1456
                        'year' => '1991',
1457
                    ],
1458
                    [
1459
                        'id' => 2,
1460
                        'title' => 'Bar',
1461
                        'year' => '2015',
1462
                    ],
1463
                ],
1464
            ],
1465
            [
1466
                'id' => 2,
1467
                'name' => 'Bill',
1468
                '_published' => [
1469
                    [
1470
                        'id' => 1,
1471
                        'title' => 'Foo',
1472
                        'year' => '1991',
1473
                    ],
1474
                    [
1475
                        'id' => 2,
1476
                        'title' => 'Bar',
1477
                        'year' => '2015',
1478
                    ],
1479
                ],
1480
            ],
1481
        ];
1482
1483
        $resource = new Collection($authorData, new JsonApiAuthorTransformer(), 'people');
1484
1485
        $scope = new Scope($this->manager, $resource);
1486
1487
        $expected = [
1488
            'data' => [
1489
                [
1490
                    'type' => 'people',
1491
                    'id' => '1',
1492
                    'attributes' => [
1493
                        'name' => 'Dave',
1494
                    ],
1495
                    'relationships' => [
1496
                        'published' => [
1497
                            'links' => [
1498
                                'self' => 'http://example.com/people/1/relationships/published',
1499
                                'related' => 'http://example.com/people/1/published',
1500
                            ],
1501
                            'data' => [
1502
                                [
1503
                                    'type' => 'books',
1504
                                    'id' => 1,
1505
                                ],
1506
                                [
1507
                                    'type' => 'books',
1508
                                    'id' => 2,
1509
                                ],
1510
                            ],
1511
                        ],
1512
                    ],
1513
                    'links' => [
1514
                        'self' => 'http://example.com/people/1',
1515
                    ],
1516
                ],
1517
                [
1518
                    'type' => 'people',
1519
                    'id' => '2',
1520
                    'attributes' => [
1521
                        'name' => 'Bill',
1522
                    ],
1523
                    'relationships' => [
1524
                        'published' => [
1525
                            'links' => [
1526
                                'self' => 'http://example.com/people/2/relationships/published',
1527
                                'related' => 'http://example.com/people/2/published',
1528
                            ],
1529
                            'data' => [
1530
                                [
1531
                                    'type' => 'books',
1532
                                    'id' => 1,
1533
                                ],
1534
                                [
1535
                                    'type' => 'books',
1536
                                    'id' => 2,
1537
                                ],
1538
                            ],
1539
                        ],
1540
                    ],
1541
                    'links' => [
1542
                        'self' => 'http://example.com/people/2',
1543
                    ],
1544
                ],
1545
            ],
1546
            'included' => [
1547
                [
1548
                    'type' => 'books',
1549
                    'id' => '1',
1550
                    'attributes' => [
1551
                        'title' => 'Foo',
1552
                        'year' => 1991,
1553
                    ],
1554
                    'links' => [
1555
                        'self' => 'http://example.com/books/1',
1556
                    ],
1557
                    'relationships' => [
1558
                        'author' => [
1559
                            'links' => [
1560
                                'self' => 'http://example.com/books/1/relationships/author',
1561
                                'related' => 'http://example.com/books/1/author',
1562
                            ],
1563
                        ],
1564
                        'co-author' => [
1565
                            'links' => [
1566
                                'self' => 'http://example.com/books/1/relationships/co-author',
1567
                                'related' => 'http://example.com/books/1/co-author'
1568
                            ],
1569
                        ],
1570
                    ],
1571
                ],
1572
                [
1573
                    'type' => 'books',
1574
                    'id' => '2',
1575
                    'attributes' => [
1576
                        'title' => 'Bar',
1577
                        'year' => 2015,
1578
                    ],
1579
                    'links' => [
1580
                        'self' => 'http://example.com/books/2',
1581
                    ],
1582
                    'relationships' => [
1583
                        'author' => [
1584
                            'links' => [
1585
                                'self' => 'http://example.com/books/2/relationships/author',
1586
                                'related' => 'http://example.com/books/2/author',
1587
                            ],
1588
                        ],
1589
                        'co-author' => [
1590
                            'links' => [
1591
                                'self' => 'http://example.com/books/2/relationships/co-author',
1592
                                'related' => 'http://example.com/books/2/co-author'
1593
                            ],
1594
                        ],
1595
                    ],
1596
                ],
1597
            ],
1598
        ];
1599
1600
        $this->assertEquals($expected, $scope->toArray());
1601
1602
        $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"}}}},{"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"}}}}]}';
1603
        $this->assertSame($expectedJson, $scope->toJson());
1604
    }
1605
1606
    /**
1607
     * @expectedException InvalidArgumentException
1608
     * @expectedExceptionMessage JSON API resource objects MUST have a valid id
1609
     */
1610
    public function testExceptionThrownIfResourceHasNoId()
1611
    {
1612
        $bookData = [
1613
            'title' => 'Foo',
1614
            'year' => '1991',
1615
        ];
1616
1617
        $resource = new Item($bookData, new JsonApiBookTransformer(), 'books');
1618
1619
        $scope = new Scope($this->manager, $resource);
1620
        $scope->toArray();
1621
    }
1622
1623
    public function testSerializingItemWithReferenceToRootObject()
1624
    {
1625
        $this->manager->parseIncludes('published.author');
1626
1627
        $authorData = [
1628
            'id' => 1,
1629
            'name' => 'Dave',
1630
            '_published' => [
1631
                [
1632
                    'id' => 1,
1633
                    'title' => 'Foo',
1634
                    'year' => 1991,
1635
                    '_author' => ['id' => 1]
1636
                ],
1637
                [
1638
                    'id' => 2,
1639
                    'title' => 'Bar',
1640
                    'year' => 2015,
1641
                    '_author' => ['id' => 1]
1642
                ],
1643
            ],
1644
        ];
1645
1646
        $resource = new Item($authorData, new JsonApiAuthorTransformer(), 'people');
1647
1648
        $scope = new Scope($this->manager, $resource);
1649
1650
        $expected = [
1651
            'data' => [
1652
                'type' => 'people',
1653
                'id' => '1',
1654
                'attributes' => [
1655
                    'name' => 'Dave',
1656
                ],
1657
                'relationships' => [
1658
                    'published' => [
1659
                        'data' => [
1660
                            ['type' => 'books', 'id' => '1'],
1661
                            ['type' => 'books', 'id' => '2'],
1662
                        ],
1663
                    ],
1664
                ],
1665
            ],
1666
            'included' => [
1667
                [
1668
                    'type' => 'books',
1669
                    'id' => '1',
1670
                    'attributes' => [
1671
                        'title' => 'Foo',
1672
                        'year' => 1991,
1673
                    ],
1674
                    'relationships' => [
1675
                        'author' => [
1676
                            'data' => ['type' => 'people', 'id' => '1'],
1677
                        ],
1678
                    ],
1679
                ],
1680
                [
1681
                    'type' => 'books',
1682
                    'id' => '2',
1683
                    'attributes' => [
1684
                        'title' => 'Bar',
1685
                        'year' => 2015,
1686
                    ],
1687
                    'relationships' => [
1688
                        'author' => [
1689
                            'data' => ['type' => 'people', 'id' => '1'],
1690
                        ],
1691
                    ],
1692
                ],
1693
            ],
1694
        ];
1695
1696
        $this->assertSame($expected, $scope->toArray());
1697
1698
        $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"}}}}]}';
1699
        $this->assertSame($expectedJson, $scope->toJson());
1700
    }
1701
1702
    public function testSerializingCollectionWithReferenceToRootObjects()
1703
    {
1704
        $this->manager->parseIncludes('author.published');
1705
1706
        $booksData = [
1707
            [
1708
                'id' => 1,
1709
                'title' => 'Foo',
1710
                'year' => 1991,
1711
                '_author' => [
1712
                    'id' => 1,
1713
                    'name' => 'Dave',
1714
                    '_published' => [
1715
                        [
1716
                            'id' => 1,
1717
                            'title' => 'Foo',
1718
                            'year' => 1991,
1719
                        ],
1720
                        [
1721
                            'id' => 2,
1722
                            'title' => 'Bar',
1723
                            'year' => 2015,
1724
                        ],
1725
                    ],
1726
                ],
1727
            ],
1728
            [
1729
                'id' => 2,
1730
                'title' => 'Bar',
1731
                'year' => 2015,
1732
                '_author' => [
1733
                    'id' => 1,
1734
                    '_published' => [
1735
                        [
1736
                            'id' => 1,
1737
                            'title' => 'Foo',
1738
                            'year' => 1991,
1739
                        ],
1740
                        [
1741
                            'id' => 2,
1742
                            'title' => 'Bar',
1743
                            'year' => 2015,
1744
                        ],
1745
                    ],
1746
                ],
1747
            ],
1748
        ];
1749
1750
        $resource = new Collection($booksData, new JsonApiBookTransformer(), 'books');
1751
1752
        $scope = new Scope($this->manager, $resource);
1753
1754
        $expected = [
1755
            'data' => [
1756
                [
1757
                    'type' => 'books',
1758
                    'id' => '1',
1759
                    'attributes' => [
1760
                        'title' => 'Foo',
1761
                        'year' => 1991,
1762
                    ],
1763
                    'relationships' => [
1764
                        'author' => [
1765
                            'data' => [
1766
                                'type' => 'people',
1767
                                'id' => '1',
1768
                            ],
1769
                        ],
1770
                    ],
1771
                ],
1772
                [
1773
                    'type' => 'books',
1774
                    'id' => '2',
1775
                    'attributes' => [
1776
                        'title' => 'Bar',
1777
                        'year' => 2015,
1778
                    ],
1779
                    'relationships' => [
1780
                        'author' => [
1781
                            'data' => [
1782
                                'type' => 'people',
1783
                                'id' => '1',
1784
                            ],
1785
                        ],
1786
                    ],
1787
                ],
1788
            ],
1789
            'included' => [
1790
                [
1791
                    'type' => 'people',
1792
                    'id' => '1',
1793
                    'attributes' => [
1794
                        'name' => 'Dave',
1795
                    ],
1796
                    'relationships' => [
1797
                        'published' => [
1798
                            'data' => [
1799
                                ['type' => 'books', 'id' => '1'],
1800
                                ['type' => 'books', 'id' => '2'],
1801
                            ],
1802
                        ],
1803
                    ],
1804
                ],
1805
            ],
1806
        ];
1807
1808
        $this->assertSame($expected, $scope->toArray());
1809
1810
        $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"}]}}}]}';
1811
        $this->assertSame($expectedJson, $scope->toJson());
1812
    }
1813
1814
    public function testSerializingCollectionResourceWithPaginator()
1815
    {
1816
        $baseUrl = 'http://example.com';
1817
1818
        $this->manager->setSerializer(new JsonApiSerializer($baseUrl));
1819
1820
        $total = 10;
1821
        $count = 2;
1822
        $perPage = 2;
1823
        $currentPage = 2;
1824
        $lastPage = 5;
1825
        $previousUrl = 'http://example.com/books/?page=1';
1826
        $currentUrl = 'http://example.com/books/?page=2';
1827
        $nextUrl = 'http://example.com/books/?page=3';
1828
        $lastUrl = 'http://example.com/books/?page=5';
1829
1830
        $paginator = Mockery::mock('League\Fractal\Pagination\PaginatorInterface');
1831
        $paginator->shouldReceive('getCurrentPage')->andReturn($currentPage);
1832
        $paginator->shouldReceive('getLastPage')->andReturn($lastPage);
1833
        $paginator->shouldReceive('getTotal')->andReturn($total);
1834
        $paginator->shouldReceive('getCount')->andReturn($count);
1835
        $paginator->shouldReceive('getPerPage')->andReturn($perPage);
1836
        $paginator->shouldReceive('getUrl')->with(1)->andReturn($previousUrl);
1837
        $paginator->shouldReceive('getUrl')->with(2)->andReturn($currentUrl);
1838
        $paginator->shouldReceive('getUrl')->with(3)->andReturn($nextUrl);
1839
        $paginator->shouldReceive('getUrl')->with(5)->andReturn($lastUrl);
1840
1841
        $booksData = [
1842
            [
1843
                'id' => 1,
1844
                'title' => 'Foo',
1845
                'year' => '1991',
1846
                '_author' => [
1847
                    'id' => 1,
1848
                    'name' => 'Dave',
1849
                ],
1850
            ],
1851
            [
1852
                'id' => 2,
1853
                'title' => 'Bar',
1854
                'year' => '1997',
1855
                '_author' => [
1856
                    'id' => 2,
1857
                    'name' => 'Bob',
1858
                ],
1859
            ],
1860
        ];
1861
1862
        $resource = new Collection($booksData, new JsonApiBookTransformer(), 'books');
1863
        $resource->setPaginator($paginator);
1864
        $scope = new Scope($this->manager, $resource);
1865
1866
        $expected = [
1867
            'data' => [
1868
                [
1869
                    'type' => 'books',
1870
                    'id' => '1',
1871
                    'attributes' => [
1872
                        'title' => 'Foo',
1873
                        'year' => 1991,
1874
                    ],
1875
                    'links' => [
1876
                        'self' => 'http://example.com/books/1',
1877
                    ],
1878
                    'relationships' => [
1879
                        'author' => [
1880
                            'links' => [
1881
                                'self' => 'http://example.com/books/1/relationships/author',
1882
                                'related' => 'http://example.com/books/1/author',
1883
                            ],
1884
                        ],
1885
                        'co-author' => [
1886
                            'links' => [
1887
                                'self' => 'http://example.com/books/1/relationships/co-author',
1888
                                'related' => 'http://example.com/books/1/co-author'
1889
                            ],
1890
                        ],
1891
                    ],
1892
                ],
1893
                [
1894
                    'type' => 'books',
1895
                    'id' => '2',
1896
                    'attributes' => [
1897
                        'title' => 'Bar',
1898
                        'year' => 1997,
1899
                    ],
1900
                    'links' => [
1901
                        'self' => 'http://example.com/books/2',
1902
                    ],
1903
                    'relationships' => [
1904
                        'author' => [
1905
                            'links' => [
1906
                                'self' => 'http://example.com/books/2/relationships/author',
1907
                                'related' => 'http://example.com/books/2/author',
1908
                            ],
1909
                        ],
1910
                        'co-author' => [
1911
                            'links' => [
1912
                                'self' => 'http://example.com/books/2/relationships/co-author',
1913
                                'related' => 'http://example.com/books/2/co-author'
1914
                            ],
1915
                        ],
1916
                    ],
1917
                ],
1918
            ],
1919
            'meta' => [
1920
                'pagination' => [
1921
                    'total' =>  10,
1922
                    'count' => 2,
1923
                    'per_page' => 2,
1924
                    'current_page' => 2,
1925
                    'total_pages' => 5
1926
                ]
1927
            ],
1928
            'links' => [
1929
                'self' => 'http://example.com/books/?page=2',
1930
                'first' => 'http://example.com/books/?page=1',
1931
                'prev' => 'http://example.com/books/?page=1',
1932
                'next' => 'http://example.com/books/?page=3',
1933
                'last' => 'http://example.com/books/?page=5'
1934
            ]
1935
        ];
1936
1937
        $this->assertSame($expected, $scope->toArray());
1938
1939
        $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"}}}},{"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"}}}}],"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"}}';
1940
        $this->assertSame($expectedJson, $scope->toJson());
1941
    }
1942
1943 View Code Duplication
    public function testSerializingCollectionResourceWithPaginatorWithOmittedUnavailablePreviousLink()
1944
    {
1945
        $baseUrl = 'http://example.com';
1946
1947
        $this->manager->setSerializer(new JsonApiSerializer($baseUrl));
1948
1949
        $total = 10;
1950
        $count = 2;
1951
        $perPage = 2;
1952
        $currentPage = 1;
1953
        $lastPage = 5;
1954
        $currentUrl = 'http://example.com/books/?page=1';
1955
        $nextUrl = 'http://example.com/books/?page=2';
1956
        $lastUrl = 'http://example.com/books/?page=5';
1957
1958
        $paginator = Mockery::mock('League\Fractal\Pagination\PaginatorInterface');
1959
        $paginator->shouldReceive('getCurrentPage')->andReturn($currentPage);
1960
        $paginator->shouldReceive('getLastPage')->andReturn($lastPage);
1961
        $paginator->shouldReceive('getTotal')->andReturn($total);
1962
        $paginator->shouldReceive('getCount')->andReturn($count);
1963
        $paginator->shouldReceive('getPerPage')->andReturn($perPage);
1964
        $paginator->shouldReceive('getUrl')->with(1)->andReturn($currentUrl);
1965
        $paginator->shouldReceive('getUrl')->with(2)->andReturn($nextUrl);
1966
        $paginator->shouldReceive('getUrl')->with(5)->andReturn($lastUrl);
1967
1968
        $booksData = [
1969
            [
1970
                'id' => 1,
1971
                'title' => 'Foo',
1972
                'year' => '1991',
1973
                '_author' => [
1974
                    'id' => 1,
1975
                    'name' => 'Dave',
1976
                ],
1977
            ],
1978
            [
1979
                'id' => 2,
1980
                'title' => 'Bar',
1981
                'year' => '1997',
1982
                '_author' => [
1983
                    'id' => 2,
1984
                    'name' => 'Bob',
1985
                ],
1986
            ],
1987
        ];
1988
1989
        $resource = new Collection($booksData, new JsonApiBookTransformer(), 'books');
1990
        $resource->setPaginator($paginator);
1991
        $scope = new Scope($this->manager, $resource);
1992
1993
        $expected = [
1994
            'data' => [
1995
                [
1996
                    'type' => 'books',
1997
                    'id' => '1',
1998
                    'attributes' => [
1999
                        'title' => 'Foo',
2000
                        'year' => 1991,
2001
                    ],
2002
                    'links' => [
2003
                        'self' => 'http://example.com/books/1',
2004
                    ],
2005
                    'relationships' => [
2006
                        'author' => [
2007
                            'links' => [
2008
                                'self' => 'http://example.com/books/1/relationships/author',
2009
                                'related' => 'http://example.com/books/1/author',
2010
                            ],
2011
                        ],
2012
                        'co-author' => [
2013
                            'links' => [
2014
                                'self' => 'http://example.com/books/1/relationships/co-author',
2015
                                'related' => 'http://example.com/books/1/co-author'
2016
                            ],
2017
                        ],
2018
                    ],
2019
                ],
2020
                [
2021
                    'type' => 'books',
2022
                    'id' => '2',
2023
                    'attributes' => [
2024
                        'title' => 'Bar',
2025
                        'year' => 1997,
2026
                    ],
2027
                    'links' => [
2028
                        'self' => 'http://example.com/books/2',
2029
                    ],
2030
                    'relationships' => [
2031
                        'author' => [
2032
                            'links' => [
2033
                                'self' => 'http://example.com/books/2/relationships/author',
2034
                                'related' => 'http://example.com/books/2/author',
2035
                            ],
2036
                        ],
2037
                        'co-author' => [
2038
                            'links' => [
2039
                                'self' => 'http://example.com/books/2/relationships/co-author',
2040
                                'related' => 'http://example.com/books/2/co-author'
2041
                            ],
2042
                        ],
2043
                    ],
2044
                ],
2045
            ],
2046
            'meta' => [
2047
                'pagination' => [
2048
                    'total' =>  10,
2049
                    'count' => 2,
2050
                    'per_page' => 2,
2051
                    'current_page' => 1,
2052
                    'total_pages' => 5
2053
                ]
2054
            ],
2055
            'links' => [
2056
                'self' => 'http://example.com/books/?page=1',
2057
                'first' => 'http://example.com/books/?page=1',
2058
                'next' => 'http://example.com/books/?page=2',
2059
                'last' => 'http://example.com/books/?page=5'
2060
            ]
2061
        ];
2062
2063
        $this->assertSame($expected, $scope->toArray());
2064
2065
        $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"}}}},{"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"}}}}],"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"}}';
2066
        $this->assertSame($expectedJson, $scope->toJson());
2067
    }
2068
2069 View Code Duplication
    public function testSerializingCollectionResourceWithPaginatorWithOmittedUnavailableNextLink()
2070
    {
2071
        $baseUrl = 'http://example.com';
2072
2073
        $this->manager->setSerializer(new JsonApiSerializer($baseUrl));
2074
2075
        $total = 10;
2076
        $count = 2;
2077
        $perPage = 2;
2078
        $currentPage = 5;
2079
        $lastPage = 5;
2080
        $firstUrl = 'http://example.com/books/?page=1';
2081
        $previousUrl = 'http://example.com/books/?page=4';
2082
        $lastUrl = 'http://example.com/books/?page=5';
2083
2084
        $paginator = Mockery::mock('League\Fractal\Pagination\PaginatorInterface');
2085
        $paginator->shouldReceive('getCurrentPage')->andReturn($currentPage);
2086
        $paginator->shouldReceive('getLastPage')->andReturn($lastPage);
2087
        $paginator->shouldReceive('getTotal')->andReturn($total);
2088
        $paginator->shouldReceive('getCount')->andReturn($count);
2089
        $paginator->shouldReceive('getPerPage')->andReturn($perPage);
2090
        $paginator->shouldReceive('getUrl')->with(1)->andReturn($firstUrl);
2091
        $paginator->shouldReceive('getUrl')->with(4)->andReturn($previousUrl);
2092
        $paginator->shouldReceive('getUrl')->with(5)->andReturn($lastUrl);
2093
2094
        $booksData = [
2095
            [
2096
                'id' => 1,
2097
                'title' => 'Foo',
2098
                'year' => '1991',
2099
                '_author' => [
2100
                    'id' => 1,
2101
                    'name' => 'Dave',
2102
                ],
2103
            ],
2104
            [
2105
                'id' => 2,
2106
                'title' => 'Bar',
2107
                'year' => '1997',
2108
                '_author' => [
2109
                    'id' => 2,
2110
                    'name' => 'Bob',
2111
                ],
2112
            ],
2113
        ];
2114
2115
        $resource = new Collection($booksData, new JsonApiBookTransformer(), 'books');
2116
        $resource->setPaginator($paginator);
2117
        $scope = new Scope($this->manager, $resource);
2118
2119
        $expected = [
2120
            'data' => [
2121
                [
2122
                    'type' => 'books',
2123
                    'id' => '1',
2124
                    'attributes' => [
2125
                        'title' => 'Foo',
2126
                        'year' => 1991,
2127
                    ],
2128
                    'links' => [
2129
                        'self' => 'http://example.com/books/1',
2130
                    ],
2131
                    'relationships' => [
2132
                        'author' => [
2133
                            'links' => [
2134
                                'self' => 'http://example.com/books/1/relationships/author',
2135
                                'related' => 'http://example.com/books/1/author',
2136
                            ],
2137
                        ],
2138
                        'co-author' => [
2139
                            'links' => [
2140
                                'self' => 'http://example.com/books/1/relationships/co-author',
2141
                                'related' => 'http://example.com/books/1/co-author'
2142
                            ],
2143
                        ],
2144
                    ],
2145
                ],
2146
                [
2147
                    'type' => 'books',
2148
                    'id' => '2',
2149
                    'attributes' => [
2150
                        'title' => 'Bar',
2151
                        'year' => 1997,
2152
                    ],
2153
                    'links' => [
2154
                        'self' => 'http://example.com/books/2',
2155
                    ],
2156
                    'relationships' => [
2157
                        'author' => [
2158
                            'links' => [
2159
                                'self' => 'http://example.com/books/2/relationships/author',
2160
                                'related' => 'http://example.com/books/2/author',
2161
                            ],
2162
                        ],
2163
                        'co-author' => [
2164
                            'links' => [
2165
                                'self' => 'http://example.com/books/2/relationships/co-author',
2166
                                'related' => 'http://example.com/books/2/co-author'
2167
                            ],
2168
                        ],
2169
                    ],
2170
                ],
2171
            ],
2172
            'meta' => [
2173
                'pagination' => [
2174
                    'total' =>  10,
2175
                    'count' => 2,
2176
                    'per_page' => 2,
2177
                    'current_page' => 5,
2178
                    'total_pages' => 5
2179
                ]
2180
            ],
2181
            'links' => [
2182
                'self' => 'http://example.com/books/?page=5',
2183
                'first' => 'http://example.com/books/?page=1',
2184
                'prev' => 'http://example.com/books/?page=4',
2185
                'last' => 'http://example.com/books/?page=5'
2186
            ]
2187
        ];
2188
2189
        $this->assertSame($expected, $scope->toArray());
2190
2191
        $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"}}}},{"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"}}}}],"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"}}';
2192
        $this->assertSame($expectedJson, $scope->toJson());
2193
    }
2194
2195
    public function testCustomLinkMerge()
2196
    {
2197
        $manager = new Manager();
2198
        $manager->setSerializer(new JsonApiSerializer('http://test.de'));
2199
2200
        $bookData = [
2201
            'id' => 1,
2202
            'title' => 'Foo',
2203
            'year' => '1991',
2204
            '_author' => [
2205
                'id' => 1,
2206
                'name' => 'Dave',
2207
            ],
2208
            'links' => [
2209
                'custom_link' => '/custom/link',
2210
            ],
2211
        ];
2212
2213
        $resource = new Item($bookData, new JsonApiBookTransformer('test.de'), 'books');
0 ignored issues
show
Unused Code introduced by
The call to JsonApiBookTransformer::__construct() has too many arguments starting with 'test.de'.

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.

Loading history...
2214
2215
        $scope = new Scope($manager, $resource);
2216
2217
        $expected = [
2218
            'data' => [
2219
                'type' => 'books',
2220
                'id' => '1',
2221
                'attributes' => [
2222
                    'title' => 'Foo',
2223
                    'year' => 1991,
2224
                ],
2225
                'links' => [
2226
                    'custom_link' => '/custom/link',
2227
                    'self' => 'http://test.de/books/1',
2228
                ],
2229
                'relationships' => [
2230
                    'author' => [
2231
                        'links' => [
2232
                            'self' => 'http://test.de/books/1/relationships/author',
2233
                            'related' => 'http://test.de/books/1/author',
2234
                        ],
2235
                    ],
2236
                    'co-author' => [
2237
                        'links' => [
2238
                            'self' => 'http://test.de/books/1/relationships/co-author',
2239
                            'related' => 'http://test.de/books/1/co-author'
2240
                        ],
2241
                    ],
2242
                ],
2243
            ],
2244
        ];
2245
2246
        $this->assertSame(json_encode($expected), $scope->toJson());
2247
    }
2248
2249
    public function testCustomLinkMergeNoLink()
2250
    {
2251
        $manager = new Manager();
2252
        $manager->setSerializer(new JsonApiSerializer('http://test.de'));
2253
2254
        $bookData = [
2255
            'id' => 1,
2256
            'title' => 'Foo',
2257
            'year' => '1991',
2258
            '_author' => [
2259
                'id' => 1,
2260
                'name' => 'Dave',
2261
            ],
2262
        ];
2263
2264
        $resource = new Item($bookData, new JsonApiBookTransformer(), 'books');
2265
2266
        $scope = new Scope($manager, $resource);
2267
2268
        $expected = [
2269
            'data' => [
2270
                'type' => 'books',
2271
                'id' => '1',
2272
                'attributes' => [
2273
                    'title' => 'Foo',
2274
                    'year' => 1991,
2275
                ],
2276
                'links' => [
2277
                    'self' => 'http://test.de/books/1',
2278
                ],
2279
                'relationships' => [
2280
                    'author' => [
2281
                        'links' => [
2282
                            'self' => 'http://test.de/books/1/relationships/author',
2283
                            'related' => 'http://test.de/books/1/author',
2284
                        ],
2285
                    ],
2286
                    'co-author' => [
2287
                        'links' => [
2288
                            'self' => 'http://test.de/books/1/relationships/co-author',
2289
                            'related' => 'http://test.de/books/1/co-author'
2290
                        ],
2291
                    ],
2292
                ],
2293
            ],
2294
        ];
2295
2296
        $this->assertSame(json_encode($expected), $scope->toJson());
2297
    }
2298
2299
    public function tearDown()
2300
    {
2301
        Mockery::close();
2302
    }
2303
}
2304