Completed
Push — master ( e03925...d40975 )
by Matt
17s queued 12s
created

test/Serializer/JsonApiSerializerTest.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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