Completed
Pull Request — master (#325)
by
unknown
03:24
created

UuidStub::__toString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
use League\Fractal\Manager;
4
use League\Fractal\Resource\Collection;
5
use League\Fractal\Resource\Item;
6
use League\Fractal\Scope;
7
use League\Fractal\Serializer\JsonApiSerializer;
8
use League\Fractal\Test\Stub\Transformer\JsonApiAuthorTransformer;
9
use League\Fractal\Test\Stub\Transformer\JsonApiBookTransformer;
10
11
class JsonApiSerializerTest extends PHPUnit_Framework_TestCase
12
{
13
    private $manager;
14
15
    public function setUp()
16
    {
17
        $this->manager = new Manager();
18
        $this->manager->setSerializer(new JsonApiSerializer());
19
    }
20
21
    public function testSerializingItemResourceWithHasOneInclude()
22
    {
23
        $this->manager->parseIncludes('author');
24
25
        $bookData = [
26
            'id' => 1,
27
            'title' => 'Foo',
28
            'year' => '1991',
29
            '_author' => [
30
                'id' => 1,
31
                'name' => 'Dave',
32
            ],
33
        ];
34
35
        $resource = new Item($bookData, new JsonApiBookTransformer(), 'books');
36
37
        $scope = new Scope($this->manager, $resource);
38
39
        $expected = [
40
            'data' => [
41
                'type' => 'books',
42
                'id' => '1',
43
                'attributes' => [
44
                    'title' => 'Foo',
45
                    'year' => 1991,
46
                ],
47
                'relationships' => [
48
                    'author' => [
49
                        'data' => [
50
                            'type' => 'people',
51
                            'id' => '1',
52
                        ],
53
                    ],
54
                ],
55
            ],
56
            'included' => [
57
                [
58
                    'type' => 'people',
59
                    'id' => '1',
60
                    'attributes' => [
61
                        'name' => 'Dave',
62
                    ],
63
                ],
64
            ],
65
        ];
66
67
        $this->assertSame($expected, $scope->toArray());
68
69
        $expectedJson = '{"data":{"type":"books","id":"1","attributes":{"title":"Foo","year":1991},"relationships":{"author":{"data":{"type":"people","id":"1"}}}},"included":[{"type":"people","id":"1","attributes":{"name":"Dave"}}]}';
70
        $this->assertSame($expectedJson, $scope->toJson());
71
    }
72
73 View Code Duplication
    public function testSerializingItemResourceWithHasOneDasherizedInclude()
74
    {
75
        $this->manager->parseIncludes('co-author');
76
77
        $bookData = [
78
            'id' => 1,
79
            'title' => 'Foo',
80
            'year' => '1991',
81
            '_author' => [
82
                'id' => 1,
83
                'name' => 'Dave',
84
            ],
85
            '_co_author' => [
86
                'id' => 2,
87
                'name' => 'Jim',
88
            ],
89
        ];
90
91
        $resource = new Item($bookData, new JsonApiBookTransformer(), 'books');
92
93
        $scope = new Scope($this->manager, $resource);
94
95
        $expected = [
96
            'data' => [
97
                'type' => 'books',
98
                'id' => '1',
99
                'attributes' => [
100
                    'title' => 'Foo',
101
                    'year' => 1991,
102
                ],
103
                'relationships' => [
104
                    'co-author' => [
105
                        'data' => [
106
                            'type' => 'people',
107
                            'id' => '2',
108
                        ],
109
                    ],
110
                ],
111
            ],
112
            'included' => [
113
                [
114
                    'type' => 'people',
115
                    'id' => '2',
116
                    'attributes' => [
117
                        'name' => 'Jim',
118
                    ],
119
                ],
120
            ],
121
        ];
122
123
        $this->assertSame($expected, $scope->toArray());
124
125
        $expectedJson = '{"data":{"type":"books","id":"1","attributes":{"title":"Foo","year":1991},"relationships":{"co-author":{"data":{"type":"people","id":"2"}}}},"included":[{"type":"people","id":"2","attributes":{"name":"Jim"}}]}';
126
        $this->assertSame($expectedJson, $scope->toJson());
127
    }
128
129
    public function testSerializingItemResourceWithEmptyHasOneInclude()
130
    {
131
        $this->manager->parseIncludes('author');
132
133
        $bookData = [
134
            'id' => 1,
135
            'title' => 'Foo',
136
            'year' => '1991',
137
            '_author' => null,
138
        ];
139
140
        $resource = new Item($bookData, new JsonApiBookTransformer(), 'books');
141
142
        $scope = new Scope($this->manager, $resource);
143
144
        $expected = [
145
            'data' => [
146
                'type' => 'books',
147
                'id' => '1',
148
                'attributes' => [
149
                    'title' => 'Foo',
150
                    'year' => 1991,
151
                ],
152
                'relationships' => [
153
                    'author' => [
154
                        'data' => null,
155
                    ],
156
                ],
157
            ],
158
        ];
159
160
        $this->assertSame($expected, $scope->toArray());
161
162
        $expectedJson = '{"data":{"type":"books","id":"1","attributes":{"title":"Foo","year":1991},"relationships":{"author":{"data":null}}}}';
163
        $this->assertSame($expectedJson, $scope->toJson());
164
    }
165
166
    public function testSerializingItemResourceWithHasManyInclude()
167
    {
168
        $this->manager->parseIncludes('published');
169
170
        $authorData = [
171
            'id' => 1,
172
            'name' => 'Dave',
173
            '_published' => [
174
                [
175
                    'id' => 1,
176
                    'title' => 'Foo',
177
                    'year' => '1991',
178
                ],
179
                [
180
                    'id' => 2,
181
                    'title' => 'Bar',
182
                    'year' => '2015',
183
                ],
184
            ],
185
        ];
186
187
        $resource = new Item($authorData, new JsonApiAuthorTransformer(), 'people');
188
189
        $scope = new Scope($this->manager, $resource);
190
191
        $expected = [
192
            'data' => [
193
                'type' => 'people',
194
                'id' => '1',
195
                'attributes' => [
196
                    'name' => 'Dave',
197
                ],
198
                'relationships' => [
199
                    'published' => [
200
                        'data' => [
201
                            [
202
                                'type' => 'books',
203
                                'id' => 1,
204
                            ],
205
                            [
206
                                'type' => 'books',
207
                                'id' => 2,
208
                            ],
209
                        ],
210
                    ],
211
                ],
212
            ],
213
            'included' => [
214
                [
215
                    'type' => 'books',
216
                    'id' => '1',
217
                    'attributes' => [
218
                        'title' => 'Foo',
219
                        'year' => 1991,
220
                    ],
221
                ],
222
                [
223
                    'type' => 'books',
224
                    'id' => '2',
225
                    'attributes' => [
226
                        'title' => 'Bar',
227
                        'year' => 2015,
228
                    ],
229
                ],
230
            ],
231
        ];
232
233
        $this->assertEquals($expected, $scope->toArray());
234
235
        $expectedJson = '{"data":{"type":"people","id":"1","attributes":{"name":"Dave"},"relationships":{"published":{"data":[{"type":"books","id":"1"},{"type":"books","id":"2"}]}}},"included":[{"type":"books","id":"1","attributes":{"title":"Foo","year":1991}},{"type":"books","id":"2","attributes":{"title":"Bar","year":2015}}]}';
236
        $this->assertSame($expectedJson, $scope->toJson());
237
    }
238
239
    public function testSerializingItemResourceWithEmptyHasManyInclude()
240
    {
241
        $this->manager->parseIncludes('published');
242
243
        $authorData = [
244
            'id' => 1,
245
            'name' => 'Dave',
246
            '_published' => [],
247
        ];
248
249
        $resource = new Item($authorData, new JsonApiAuthorTransformer(), 'people');
250
251
        $scope = new Scope($this->manager, $resource);
252
253
        $expected = [
254
            'data' => [
255
                'type' => 'people',
256
                'id' => '1',
257
                'attributes' => [
258
                    'name' => 'Dave',
259
                ],
260
                'relationships' => [
261
                    'published' => [
262
                        'data' => [],
263
                    ],
264
                ],
265
            ],
266
        ];
267
268
        $this->assertSame($expected, $scope->toArray());
269
270
        $expectedJson = '{"data":{"type":"people","id":"1","attributes":{"name":"Dave"},"relationships":{"published":{"data":[]}}}}';
271
        $this->assertSame($expectedJson, $scope->toJson());
272
    }
273
274
    public function testSerializingItemResourceWithoutIncludes()
275
    {
276
        $bookData = [
277
            'id' => 1,
278
            'title' => 'Foo',
279
            'year' => '1991',
280
            '_author' => [
281
                'id' => 1,
282
                'name' => 'Dave',
283
            ],
284
        ];
285
286
        $resource = new Item($bookData, new JsonApiBookTransformer(), 'books');
287
288
        $scope = new Scope($this->manager, $resource);
289
290
        $expected = [
291
            'data' => [
292
                'type' => 'books',
293
                'id' => '1',
294
                'attributes' => [
295
                    'title' => 'Foo',
296
                    'year' => 1991,
297
                ],
298
            ],
299
        ];
300
301
        $this->assertSame($expected, $scope->toArray());
302
303
        $expectedJson = '{"data":{"type":"books","id":"1","attributes":{"title":"Foo","year":1991}}}';
304
        $this->assertSame($expectedJson, $scope->toJson());
305
    }
306
307 View Code Duplication
    public function testSerializingItemResourceWithMeta()
308
    {
309
        $bookData = [
310
            'id' => 1,
311
            'title' => 'Foo',
312
            'year' => '1991',
313
            '_author' => [
314
                'id' => 1,
315
                'name' => 'Dave',
316
            ],
317
        ];
318
319
        $resource = new Item($bookData, new JsonApiBookTransformer(), 'books');
320
        $resource->setMetaValue('foo', 'bar');
321
322
        $scope = new Scope($this->manager, $resource);
323
324
        $expected = [
325
            'data' => [
326
                'type' => 'books',
327
                'id' => '1',
328
                'attributes' => [
329
                    'title' => 'Foo',
330
                    'year' => 1991,
331
                ],
332
            ],
333
            'meta' => [
334
                'foo' => 'bar',
335
            ],
336
        ];
337
338
        $this->assertSame($expected, $scope->toArray());
339
340
        $expectedJson = '{"data":{"type":"books","id":"1","attributes":{"title":"Foo","year":1991}},"meta":{"foo":"bar"}}';
341
        $this->assertSame($expectedJson, $scope->toJson());
342
    }
343
344 View Code Duplication
    public function testSerializingCollectionResourceWithoutIncludes()
345
    {
346
        $booksData = [
347
            [
348
                'id' => 1,
349
                'title' => 'Foo',
350
                'year' => '1991',
351
                '_author' => [
352
                    'id' => 1,
353
                    'name' => 'Dave',
354
                ],
355
            ],
356
            [
357
                'id' => 2,
358
                'title' => 'Bar',
359
                'year' => '1997',
360
                '_author' => [
361
                    'id' => 2,
362
                    'name' => 'Bob',
363
                ],
364
            ],
365
        ];
366
367
        $resource = new Collection($booksData, new JsonApiBookTransformer(), 'books');
368
        $scope = new Scope($this->manager, $resource);
369
370
        $expected = [
371
            'data' => [
372
                [
373
                    'type' => 'books',
374
                    'id' => '1',
375
                    'attributes' => [
376
                        'title' => 'Foo',
377
                        'year' => 1991,
378
                    ],
379
                ],
380
                [
381
                    'type' => 'books',
382
                    'id' => '2',
383
                    'attributes' => [
384
                        'title' => 'Bar',
385
                        'year' => 1997,
386
                    ],
387
                ],
388
            ],
389
        ];
390
391
        $this->assertSame($expected, $scope->toArray());
392
393
        $expectedJson = '{"data":[{"type":"books","id":"1","attributes":{"title":"Foo","year":1991}},{"type":"books","id":"2","attributes":{"title":"Bar","year":1997}}]}';
394
        $this->assertSame($expectedJson, $scope->toJson());
395
    }
396
397 View Code Duplication
    public function testSerializingCollectionResourceWithHasOneInclude()
398
    {
399
        $this->manager->parseIncludes('author');
400
401
        $booksData = [
402
            [
403
                'id' => 1,
404
                'title' => 'Foo',
405
                'year' => '1991',
406
                '_author' => [
407
                    'id' => 1,
408
                    'name' => 'Dave',
409
                ],
410
            ],
411
            [
412
                'id' => 2,
413
                'title' => 'Bar',
414
                'year' => '1997',
415
                '_author' => [
416
                    'id' => 2,
417
                    'name' => 'Bob',
418
                ],
419
            ],
420
        ];
421
422
        $resource = new Collection($booksData, new JsonApiBookTransformer(), 'books');
423
        $scope = new Scope($this->manager, $resource);
424
425
        $expected = [
426
            'data' => [
427
                [
428
                    'type' => 'books',
429
                    'id' => '1',
430
                    'attributes' => [
431
                        'title' => 'Foo',
432
                        'year' => 1991,
433
                    ],
434
                    'relationships' => [
435
                        'author' => [
436
                            'data' => [
437
                                'type' => 'people',
438
                                'id' => '1',
439
                            ],
440
                        ],
441
                    ],
442
                ],
443
                [
444
                    'type' => 'books',
445
                    'id' => '2',
446
                    'attributes' => [
447
                        'title' => 'Bar',
448
                        'year' => 1997,
449
                    ],
450
                    'relationships' => [
451
                        'author' => [
452
                            'data' => [
453
                                'type' => 'people',
454
                                'id' => '2',
455
                            ],
456
                        ],
457
                    ],
458
                ],
459
            ],
460
            'included' => [
461
                [
462
                    'type' => 'people',
463
                    'id' => '1',
464
                    'attributes' => [
465
                        'name' => 'Dave',
466
                    ],
467
                ],
468
                [
469
                    'type' => 'people',
470
                    'id' => '2',
471
                    'attributes' => [
472
                        'name' => 'Bob',
473
                    ],
474
                ],
475
            ],
476
        ];
477
478
        $this->assertSame($expected, $scope->toArray());
479
480
        $expectedJson = '{"data":[{"type":"books","id":"1","attributes":{"title":"Foo","year":1991},"relationships":{"author":{"data":{"type":"people","id":"1"}}}},{"type":"books","id":"2","attributes":{"title":"Bar","year":1997},"relationships":{"author":{"data":{"type":"people","id":"2"}}}}],"included":[{"type":"people","id":"1","attributes":{"name":"Dave"}},{"type":"people","id":"2","attributes":{"name":"Bob"}}]}';
481
        $this->assertSame($expectedJson, $scope->toJson());
482
    }
483
484
    public function testSerializingCollectionResourceWithEmptyHasOneInclude()
485
    {
486
        $this->manager->parseIncludes('author');
487
488
        $booksData = [
489
            [
490
                'id' => 1,
491
                'title' => 'Foo',
492
                'year' => '1991',
493
                '_author' => null,
494
            ],
495
            [
496
                'id' => 2,
497
                'title' => 'Bar',
498
                'year' => '1997',
499
                '_author' => [
500
                    'id' => 2,
501
                    'name' => 'Bob',
502
                ],
503
            ],
504
        ];
505
506
        $resource = new Collection($booksData, new JsonApiBookTransformer(), 'books');
507
        $scope = new Scope($this->manager, $resource);
508
509
        $expected = [
510
            'data' => [
511
                [
512
                    'type' => 'books',
513
                    'id' => '1',
514
                    'attributes' => [
515
                        'title' => 'Foo',
516
                        'year' => 1991,
517
                    ],
518
                    'relationships' => [
519
                        'author' => [
520
                            'data' => null,
521
                        ],
522
                    ],
523
                ],
524
                [
525
                    'type' => 'books',
526
                    'id' => '2',
527
                    'attributes' => [
528
                        'title' => 'Bar',
529
                        'year' => 1997,
530
                    ],
531
                    'relationships' => [
532
                        'author' => [
533
                            'data' => [
534
                                'type' => 'people',
535
                                'id' => '2',
536
                            ],
537
                        ],
538
                    ],
539
                ],
540
            ],
541
            'included' => [
542
                [
543
                    'type' => 'people',
544
                    'id' => '2',
545
                    'attributes' => [
546
                        'name' => 'Bob',
547
                    ],
548
                ],
549
            ],
550
        ];
551
552
        $this->assertSame($expected, $scope->toArray());
553
554
        $expectedJson = '{"data":[{"type":"books","id":"1","attributes":{"title":"Foo","year":1991},"relationships":{"author":{"data":null}}},{"type":"books","id":"2","attributes":{"title":"Bar","year":1997},"relationships":{"author":{"data":{"type":"people","id":"2"}}}}],"included":[{"type":"people","id":"2","attributes":{"name":"Bob"}}]}';
555
        $this->assertSame($expectedJson, $scope->toJson());
556
    }
557
558
    public function testSerializingCollectionResourceWithHasManyInclude()
559
    {
560
        $this->manager->parseIncludes('published');
561
562
        $authorsData = [
563
            [
564
                'id' => 1,
565
                'name' => 'Dave',
566
                '_published' => [
567
                    [
568
                        'id' => 1,
569
                        'title' => 'Foo',
570
                        'year' => '1991',
571
                    ],
572
                    [
573
                        'id' => 2,
574
                        'title' => 'Bar',
575
                        'year' => '2015',
576
                    ],
577
                ],
578
            ],
579
            [
580
                'id' => 2,
581
                'name' => 'Bob',
582
                '_published' => [
583
                    [
584
                        'id' => 3,
585
                        'title' => 'Baz',
586
                        'year' => '1995',
587
                    ],
588
                    [
589
                        'id' => 4,
590
                        'title' => 'Quux',
591
                        'year' => '2000',
592
                    ],
593
                ],
594
            ],
595
        ];
596
597
        $resource = new Collection($authorsData, new JsonApiAuthorTransformer(), 'people');
598
        $scope = new Scope($this->manager, $resource);
599
600
        $expected = [
601
            'data' => [
602
                [
603
                    'type' => 'people',
604
                    'id' => '1',
605
                    'attributes' => [
606
                        'name' => 'Dave',
607
                    ],
608
                    'relationships' => [
609
                        'published' => [
610
                            'data' => [
611
                                [
612
                                    'type' => 'books',
613
                                    'id' => 1,
614
                                ],
615
                                [
616
                                    'type' => 'books',
617
                                    'id' => 2,
618
                                ],
619
                            ],
620
                        ],
621
                    ],
622
                ],
623
                [
624
                    'type' => 'people',
625
                    'id' => '2',
626
                    'attributes' => [
627
                        'name' => 'Bob',
628
                    ],
629
                    'relationships' => [
630
                        'published' => [
631
                            'data' => [
632
                                [
633
                                    'type' => 'books',
634
                                    'id' => 3,
635
                                ],
636
                                [
637
                                    'type' => 'books',
638
                                    'id' => 4,
639
                                ],
640
                            ],
641
                        ],
642
                    ],
643
                ],
644
            ],
645
            'included' => [
646
                [
647
                    'type' => 'books',
648
                    'id' => '1',
649
                    'attributes' => [
650
                        'title' => 'Foo',
651
                        'year' => 1991,
652
                    ],
653
                ],
654
                [
655
                    'type' => 'books',
656
                    'id' => '2',
657
                    'attributes' => [
658
                        'title' => 'Bar',
659
                        'year' => 2015,
660
                    ],
661
                ],
662
                [
663
                    'type' => 'books',
664
                    'id' => '3',
665
                    'attributes' => [
666
                        'title' => 'Baz',
667
                        'year' => 1995,
668
                    ],
669
                ],
670
                [
671
                    'type' => 'books',
672
                    'id' => '4',
673
                    'attributes' => [
674
                        'title' => 'Quux',
675
                        'year' => 2000,
676
                    ],
677
                ],
678
            ],
679
        ];
680
681
        $this->assertEquals($expected, $scope->toArray());
682
683
        $expectedJson = '{"data":[{"type":"people","id":"1","attributes":{"name":"Dave"},"relationships":{"published":{"data":[{"type":"books","id":"1"},{"type":"books","id":"2"}]}}},{"type":"people","id":"2","attributes":{"name":"Bob"},"relationships":{"published":{"data":[{"type":"books","id":"3"},{"type":"books","id":"4"}]}}}],"included":[{"type":"books","id":"1","attributes":{"title":"Foo","year":1991}},{"type":"books","id":"2","attributes":{"title":"Bar","year":2015}},{"type":"books","id":"3","attributes":{"title":"Baz","year":1995}},{"type":"books","id":"4","attributes":{"title":"Quux","year":2000}}]}';
684
        $this->assertSame($expectedJson, $scope->toJson());
685
    }
686
687
    public function testSerializingCollectionResourceWithEmptyHasManyInclude()
688
    {
689
        $this->manager->parseIncludes('published');
690
691
        $authorsData = [
692
            [
693
                'id' => 1,
694
                'name' => 'Dave',
695
                '_published' => [],
696
            ],
697
            [
698
                'id' => 2,
699
                'name' => 'Bob',
700
                '_published' => [
701
                    [
702
                        'id' => 3,
703
                        'title' => 'Baz',
704
                        'year' => '1995',
705
                    ],
706
                ],
707
            ],
708
        ];
709
710
        $resource = new Collection($authorsData, new JsonApiAuthorTransformer(), 'people');
711
        $scope = new Scope($this->manager, $resource);
712
713
        $expected = [
714
            'data' => [
715
                [
716
                    'type' => 'people',
717
                    'id' => '1',
718
                    'attributes' => [
719
                        'name' => 'Dave',
720
                    ],
721
                    'relationships' => [
722
                        'published' => [
723
                            'data' => [],
724
                        ],
725
                    ],
726
                ],
727
                [
728
                    'type' => 'people',
729
                    'id' => '2',
730
                    'attributes' => [
731
                        'name' => 'Bob',
732
                    ],
733
                    'relationships' => [
734
                        'published' => [
735
                            'data' => [
736
                                [
737
                                    'type' => 'books',
738
                                    'id' => 3,
739
                                ],
740
                            ],
741
                        ],
742
                    ],
743
                ],
744
            ],
745
            'included' => [
746
                [
747
                    'type' => 'books',
748
                    'id' => '3',
749
                    'attributes' => [
750
                        'title' => 'Baz',
751
                        'year' => 1995,
752
                    ],
753
                ],
754
            ],
755
        ];
756
757
        $this->assertEquals($expected, $scope->toArray());
758
759
        $expectedJson = '{"data":[{"type":"people","id":"1","attributes":{"name":"Dave"},"relationships":{"published":{"data":[]}}},{"type":"people","id":"2","attributes":{"name":"Bob"},"relationships":{"published":{"data":[{"type":"books","id":"3"}]}}}],"included":[{"type":"books","id":"3","attributes":{"title":"Baz","year":1995}}]}';
760
        $this->assertSame($expectedJson, $scope->toJson());
761
    }
762
763 View Code Duplication
    public function testSerializingCollectionResourceWithMeta()
764
    {
765
        $booksData = [
766
            [
767
                'id' => 1,
768
                'title' => 'Foo',
769
                'year' => '1991',
770
                '_author' => [
771
                    'name' => 'Dave',
772
                ],
773
            ],
774
            [
775
                'id' => 2,
776
                'title' => 'Bar',
777
                'year' => '1997',
778
                '_author' => [
779
                    'name' => 'Bob',
780
                ],
781
            ],
782
        ];
783
784
        $resource = new Collection($booksData, new JsonApiBookTransformer(), 'books');
785
        $resource->setMetaValue('foo', 'bar');
786
787
        $scope = new Scope($this->manager, $resource);
788
789
        $expected = [
790
            'data' => [
791
                [
792
                    'type' => 'books',
793
                    'id' => '1',
794
                    'attributes' => [
795
                        'title' => 'Foo',
796
                        'year' => 1991,
797
                    ],
798
                ],
799
                [
800
                    'type' => 'books',
801
                    'id' => '2',
802
                    'attributes' => [
803
                        'title' => 'Bar',
804
                        'year' => 1997,
805
                    ],
806
                ],
807
            ],
808
            'meta' => [
809
                'foo' => 'bar',
810
            ],
811
        ];
812
813
        $this->assertSame($expected, $scope->toArray());
814
815
        $expectedJson = '{"data":[{"type":"books","id":"1","attributes":{"title":"Foo","year":1991}},{"type":"books","id":"2","attributes":{"title":"Bar","year":1997}}],"meta":{"foo":"bar"}}';
816
        $this->assertSame($expectedJson, $scope->toJson());
817
    }
818
819
    public function testSerializingCollectionResourceWithDuplicatedIncludeData()
820
    {
821
        $this->manager->parseIncludes('author');
822
823
        $booksData = [
824
            [
825
                'id' => 1,
826
                'title' => 'Foo',
827
                'year' => '1991',
828
                '_author' => [
829
                    'id' => 1,
830
                    'name' => 'Dave',
831
                ],
832
            ],
833
            [
834
                'id' => 2,
835
                'title' => 'Bar',
836
                'year' => '1997',
837
                '_author' => [
838
                    'id' => 1,
839
                    'name' => 'Dave',
840
                ],
841
            ],
842
        ];
843
844
        $resource = new Collection($booksData, new JsonApiBookTransformer(), 'books');
845
        $scope = new Scope($this->manager, $resource);
846
847
        $expected = [
848
            'data' => [
849
                [
850
                    'type' => 'books',
851
                    'id' => '1',
852
                    'attributes' => [
853
                        'title' => 'Foo',
854
                        'year' => 1991,
855
                    ],
856
                    'relationships' => [
857
                        'author' => [
858
                            'data' => [
859
                                'type' => 'people',
860
                                'id' => '1',
861
                            ],
862
                        ],
863
                    ],
864
                ],
865
                [
866
                    'type' => 'books',
867
                    'id' => '2',
868
                    'attributes' => [
869
                        'title' => 'Bar',
870
                        'year' => 1997,
871
                    ],
872
                    'relationships' => [
873
                        'author' => [
874
                            'data' => [
875
                                'type' => 'people',
876
                                'id' => '1',
877
                            ],
878
                        ],
879
                    ],
880
                ],
881
            ],
882
            'included' => [
883
                [
884
                    'type' => 'people',
885
                    'id' => '1',
886
                    'attributes' => [
887
                        'name' => 'Dave',
888
                    ],
889
                ],
890
            ],
891
        ];
892
893
        $this->assertSame($expected, $scope->toArray());
894
895
        $expectedJson = '{"data":[{"type":"books","id":"1","attributes":{"title":"Foo","year":1991},"relationships":{"author":{"data":{"type":"people","id":"1"}}}},{"type":"books","id":"2","attributes":{"title":"Bar","year":1997},"relationships":{"author":{"data":{"type":"people","id":"1"}}}}],"included":[{"type":"people","id":"1","attributes":{"name":"Dave"}}]}';
896
        $this->assertSame($expectedJson, $scope->toJson());
897
    }
898
899 View Code Duplication
    public function testSerializingItemResourceWithNestedIncludes()
900
    {
901
        $this->manager->parseIncludes(['author', 'author.published']);
902
903
        $bookData = [
904
            'id' => 1,
905
            'title' => 'Foo',
906
            'year' => '1991',
907
            '_author' => [
908
                'id' => 1,
909
                'name' => 'Dave',
910
                '_published' => [
911
                    [
912
                        'id' => 1,
913
                        'title' => 'Foo',
914
                        'year' => '1991',
915
                    ],
916
                    [
917
                        'id' => 2,
918
                        'title' => 'Bar',
919
                        'year' => '2015',
920
                    ],
921
                ],
922
            ],
923
        ];
924
925
        $resource = new Item($bookData, new JsonApiBookTransformer(), 'books');
926
927
        $scope = new Scope($this->manager, $resource);
928
929
        $expected = [
930
            'data' => [
931
                'type' => 'books',
932
                'id' => '1',
933
                'attributes' => [
934
                    'title' => 'Foo',
935
                    'year' => 1991,
936
                ],
937
                'relationships' => [
938
                    'author' => [
939
                        'data' => [
940
                            'type' => 'people',
941
                            'id' => '1',
942
                        ],
943
                    ],
944
                ],
945
            ],
946
            'included' => [
947
                [
948
                    'type' => 'books',
949
                    'id' => '2',
950
                    'attributes' => [
951
                        'title' => 'Bar',
952
                        'year' => 2015,
953
                    ],
954
                ],
955
                [
956
                    'type' => 'people',
957
                    'id' => '1',
958
                    'attributes' => [
959
                        'name' => 'Dave',
960
                    ],
961
                    'relationships' => [
962
                        'published' => [
963
                            'data' => [
964
                                [
965
                                    'type' => 'books',
966
                                    'id' => '1',
967
                                ],
968
                                [
969
                                    'type' => 'books',
970
                                    'id' => '2',
971
                                ],
972
                            ],
973
                        ],
974
                    ],
975
                ],
976
            ],
977
        ];
978
979
        $this->assertSame($expected, $scope->toArray());
980
981
        $expectedJson = '{"data":{"type":"books","id":"1","attributes":{"title":"Foo","year":1991},"relationships":{"author":{"data":{"type":"people","id":"1"}}}},"included":[{"type":"books","id":"2","attributes":{"title":"Bar","year":2015}},{"type":"people","id":"1","attributes":{"name":"Dave"},"relationships":{"published":{"data":[{"type":"books","id":"1"},{"type":"books","id":"2"}]}}}]}';
982
        $this->assertSame($expectedJson, $scope->toJson());
983
    }
984
985 View Code Duplication
    public function testSerializingItemResourceWithSelfLink()
986
    {
987
        $baseUrl = 'http://example.com';
988
        $this->manager->setSerializer(new JsonApiSerializer($baseUrl));
989
990
        $bookData = [
991
            'id' => 1,
992
            'title' => 'Foo',
993
            'year' => '1991',
994
            '_author' => [
995
                'id' => 1,
996
                'name' => 'Dave',
997
            ],
998
        ];
999
1000
        $resource = new Item($bookData, new JsonApiBookTransformer(), 'books');
1001
1002
        $scope = new Scope($this->manager, $resource);
1003
1004
        $expected = [
1005
            'data' => [
1006
                'type' => 'books',
1007
                'id' => '1',
1008
                'attributes' => [
1009
                    'title' => 'Foo',
1010
                    'year' => 1991,
1011
                ],
1012
                'links' => [
1013
                    'self' => 'http://example.com/books/1',
1014
                ],
1015
            ],
1016
        ];
1017
1018
        $this->assertSame($expected, $scope->toArray());
1019
1020
        $expectedJson = '{"data":{"type":"books","id":"1","attributes":{"title":"Foo","year":1991},"links":{"self":"http:\/\/example.com\/books\/1"}}}';
1021
        $this->assertSame($expectedJson, $scope->toJson());
1022
    }
1023
1024 View Code Duplication
    public function testSerializingCollectionResourceWithSelfLink()
1025
    {
1026
        $baseUrl = 'http://example.com';
1027
        $this->manager->setSerializer(new JsonApiSerializer($baseUrl));
1028
1029
        $booksData = [
1030
            [
1031
                'id' => 1,
1032
                'title' => 'Foo',
1033
                'year' => '1991',
1034
                '_author' => [
1035
                    'id' => 1,
1036
                    'name' => 'Dave',
1037
                ],
1038
            ],
1039
            [
1040
                'id' => 2,
1041
                'title' => 'Bar',
1042
                'year' => '1997',
1043
                '_author' => [
1044
                    'id' => 2,
1045
                    'name' => 'Bob',
1046
                ],
1047
            ],
1048
        ];
1049
1050
        $resource = new Collection($booksData, new JsonApiBookTransformer(), 'books');
1051
        $scope = new Scope($this->manager, $resource);
1052
1053
        $expected = [
1054
            'data' => [
1055
                [
1056
                    'type' => 'books',
1057
                    'id' => '1',
1058
                    'attributes' => [
1059
                        'title' => 'Foo',
1060
                        'year' => 1991,
1061
                    ],
1062
                    'links' => [
1063
                        'self' => 'http://example.com/books/1',
1064
                    ],
1065
                ],
1066
                [
1067
                    'type' => 'books',
1068
                    'id' => '2',
1069
                    'attributes' => [
1070
                        'title' => 'Bar',
1071
                        'year' => 1997,
1072
                    ],
1073
                    'links' => [
1074
                        'self' => 'http://example.com/books/2',
1075
                    ],
1076
                ],
1077
            ],
1078
        ];
1079
1080
        $this->assertSame($expected, $scope->toArray());
1081
1082
        $expectedJson = '{"data":[{"type":"books","id":"1","attributes":{"title":"Foo","year":1991},"links":{"self":"http:\/\/example.com\/books\/1"}},{"type":"books","id":"2","attributes":{"title":"Bar","year":1997},"links":{"self":"http:\/\/example.com\/books\/2"}}]}';
1083
        $this->assertSame($expectedJson, $scope->toJson());
1084
    }
1085
1086 View Code Duplication
    public function testSerializingItemResourceWithLinksForHasOneRelationship()
1087
    {
1088
        $baseUrl = 'http://example.com';
1089
        $this->manager->setSerializer(new JsonApiSerializer($baseUrl));
1090
        $this->manager->parseIncludes('author');
1091
1092
        $bookData = [
1093
            'id' => 1,
1094
            'title' => 'Foo',
1095
            'year' => '1991',
1096
            '_author' => [
1097
                'id' => 1,
1098
                'name' => 'Dave',
1099
            ],
1100
        ];
1101
1102
        $resource = new Item($bookData, new JsonApiBookTransformer(), 'books');
1103
1104
        $scope = new Scope($this->manager, $resource);
1105
1106
        $expected = [
1107
            'data' => [
1108
                'type' => 'books',
1109
                'id' => '1',
1110
                'attributes' => [
1111
                    'title' => 'Foo',
1112
                    'year' => 1991,
1113
                ],
1114
                'relationships' => [
1115
                    'author' => [
1116
                        'links' => [
1117
                            'self' => 'http://example.com/books/1/relationships/author',
1118
                            'related' => 'http://example.com/books/1/author',
1119
                        ],
1120
                        'data' => [
1121
                            'type' => 'people',
1122
                            'id' => '1',
1123
                        ],
1124
                    ],
1125
                ],
1126
                'links' => [
1127
                    'self' => 'http://example.com/books/1',
1128
                ],
1129
            ],
1130
            'included' => [
1131
                [
1132
                    'type' => 'people',
1133
                    'id' => '1',
1134
                    'attributes' => [
1135
                        'name' => 'Dave',
1136
                    ],
1137
                    'links' => [
1138
                        'self' => 'http://example.com/people/1',
1139
                    ],
1140
                ],
1141
            ],
1142
        ];
1143
1144
        $this->assertEquals($expected, $scope->toArray());
1145
1146
        $expectedJson = '{"data":{"type":"books","id":"1","attributes":{"title":"Foo","year":1991},"links":{"self":"http:\/\/example.com\/books\/1"},"relationships":{"author":{"links":{"self":"http:\/\/example.com\/books\/1\/relationships\/author","related":"http:\/\/example.com\/books\/1\/author"},"data":{"type":"people","id":"1"}}}},"included":[{"type":"people","id":"1","attributes":{"name":"Dave"},"links":{"self":"http:\/\/example.com\/people\/1"}}]}';
1147
        $this->assertSame($expectedJson, $scope->toJson());
1148
    }
1149
1150
    public function testSerializingItemResourceWithLinksForHasManyRelationship()
1151
    {
1152
        $baseUrl = 'http://example.com';
1153
        $this->manager->setSerializer(new JsonApiSerializer($baseUrl));
1154
        $this->manager->parseIncludes('published');
1155
1156
        $authorData = [
1157
            'id' => 1,
1158
            'name' => 'Dave',
1159
            '_published' => [
1160
                [
1161
                    'id' => 1,
1162
                    'title' => 'Foo',
1163
                    'year' => '1991',
1164
                ],
1165
                [
1166
                    'id' => 2,
1167
                    'title' => 'Bar',
1168
                    'year' => '2015',
1169
                ],
1170
            ],
1171
        ];
1172
1173
        $resource = new Item($authorData, new JsonApiAuthorTransformer(), 'people');
1174
1175
        $scope = new Scope($this->manager, $resource);
1176
1177
        $expected = [
1178
            'data' => [
1179
                'type' => 'people',
1180
                'id' => '1',
1181
                'attributes' => [
1182
                    'name' => 'Dave',
1183
                ],
1184
                'relationships' => [
1185
                    'published' => [
1186
                        'links' => [
1187
                            'self' => 'http://example.com/people/1/relationships/published',
1188
                            'related' => 'http://example.com/people/1/published',
1189
                        ],
1190
                        'data' => [
1191
                            [
1192
                                'type' => 'books',
1193
                                'id' => 1,
1194
                            ],
1195
                            [
1196
                                'type' => 'books',
1197
                                'id' => 2,
1198
                            ],
1199
                        ],
1200
                    ],
1201
                ],
1202
                'links' => [
1203
                    'self' => 'http://example.com/people/1',
1204
                ],
1205
            ],
1206
            'included' => [
1207
                [
1208
                    'type' => 'books',
1209
                    'id' => '1',
1210
                    'attributes' => [
1211
                        'title' => 'Foo',
1212
                        'year' => 1991,
1213
                    ],
1214
                    'links' => [
1215
                        'self' => 'http://example.com/books/1',
1216
                    ],
1217
                ],
1218
                [
1219
                    'type' => 'books',
1220
                    'id' => '2',
1221
                    'attributes' => [
1222
                        'title' => 'Bar',
1223
                        'year' => 2015,
1224
                    ],
1225
                    'links' => [
1226
                        'self' => 'http://example.com/books/2',
1227
                    ],
1228
                ],
1229
            ],
1230
        ];
1231
1232
        $this->assertEquals($expected, $scope->toArray());
1233
1234
        $expectedJson = '{"data":{"type":"people","id":"1","attributes":{"name":"Dave"},"links":{"self":"http:\/\/example.com\/people\/1"},"relationships":{"published":{"links":{"self":"http:\/\/example.com\/people\/1\/relationships\/published","related":"http:\/\/example.com\/people\/1\/published"},"data":[{"type":"books","id":"1"},{"type":"books","id":"2"}]}}},"included":[{"type":"books","id":"1","attributes":{"title":"Foo","year":1991},"links":{"self":"http:\/\/example.com\/books\/1"}},{"type":"books","id":"2","attributes":{"title":"Bar","year":2015},"links":{"self":"http:\/\/example.com\/books\/2"}}]}';
1235
        $this->assertSame($expectedJson, $scope->toJson());
1236
    }
1237
1238
    public function testSerializingCollectionResourceWithLinksForHasOneRelationship()
1239
    {
1240
        $baseUrl = 'http://example.com';
1241
        $this->manager->setSerializer(new JsonApiSerializer($baseUrl));
1242
        $this->manager->parseIncludes('author');
1243
1244
        $bookData = [
1245
            [
1246
                'id' => 1,
1247
                'title' => 'Foo',
1248
                'year' => '1991',
1249
                '_author' => [
1250
                    'id' => 1,
1251
                    'name' => 'Dave',
1252
                ],
1253
            ],
1254
            [
1255
                'id' => 2,
1256
                'title' => 'Bar',
1257
                'year' => '1991',
1258
                '_author' => [
1259
                    'id' => 1,
1260
                    'name' => 'Dave',
1261
                ],
1262
            ],
1263
        ];
1264
1265
        $resource = new Collection($bookData, new JsonApiBookTransformer(), 'books');
1266
1267
        $scope = new Scope($this->manager, $resource);
1268
1269
        $expected = [
1270
            'data' => [
1271
                [
1272
                    'type' => 'books',
1273
                    'id' => '1',
1274
                    'attributes' => [
1275
                        'title' => 'Foo',
1276
                        'year' => 1991,
1277
                    ],
1278
                    'relationships' => [
1279
                        'author' => [
1280
                            'links' => [
1281
                                'self' => 'http://example.com/books/1/relationships/author',
1282
                                'related' => 'http://example.com/books/1/author',
1283
                            ],
1284
                            'data' => [
1285
                                'type' => 'people',
1286
                                'id' => '1',
1287
                            ],
1288
                        ],
1289
                    ],
1290
                    'links' => [
1291
                        'self' => 'http://example.com/books/1',
1292
                    ],
1293
                ],
1294
                [
1295
                    'type' => 'books',
1296
                    'id' => '2',
1297
                    'attributes' => [
1298
                        'title' => 'Bar',
1299
                        'year' => 1991,
1300
                    ],
1301
                    'relationships' => [
1302
                        'author' => [
1303
                            'links' => [
1304
                                'self' => 'http://example.com/books/2/relationships/author',
1305
                                'related' => 'http://example.com/books/2/author',
1306
                            ],
1307
                            'data' => [
1308
                                'type' => 'people',
1309
                                'id' => '1',
1310
                            ],
1311
                        ],
1312
                    ],
1313
                    'links' => [
1314
                        'self' => 'http://example.com/books/2',
1315
                    ],
1316
                ],
1317
            ],
1318
            'included' => [
1319
                [
1320
                    'type' => 'people',
1321
                    'id' => '1',
1322
                    'attributes' => [
1323
                        'name' => 'Dave',
1324
                    ],
1325
                    'links' => [
1326
                        'self' => 'http://example.com/people/1',
1327
                    ],
1328
                ],
1329
            ],
1330
        ];
1331
1332
        $this->assertEquals($expected, $scope->toArray());
1333
1334
        $expectedJson = '{"data":[{"type":"books","id":"1","attributes":{"title":"Foo","year":1991},"links":{"self":"http:\/\/example.com\/books\/1"},"relationships":{"author":{"links":{"self":"http:\/\/example.com\/books\/1\/relationships\/author","related":"http:\/\/example.com\/books\/1\/author"},"data":{"type":"people","id":"1"}}}},{"type":"books","id":"2","attributes":{"title":"Bar","year":1991},"links":{"self":"http:\/\/example.com\/books\/2"},"relationships":{"author":{"links":{"self":"http:\/\/example.com\/books\/2\/relationships\/author","related":"http:\/\/example.com\/books\/2\/author"},"data":{"type":"people","id":"1"}}}}],"included":[{"type":"people","id":"1","attributes":{"name":"Dave"},"links":{"self":"http:\/\/example.com\/people\/1"}}]}';
1335
        $this->assertSame($expectedJson, $scope->toJson());
1336
    }
1337
1338
    public function testSerializingCollectionResourceWithLinksForHasManyRelationship()
1339
    {
1340
        $baseUrl = 'http://example.com';
1341
        $this->manager->setSerializer(new JsonApiSerializer($baseUrl));
1342
        $this->manager->parseIncludes('published');
1343
1344
        $authorData = [
1345
            [
1346
                'id' => 1,
1347
                'name' => 'Dave',
1348
                '_published' => [
1349
                    [
1350
                        'id' => 1,
1351
                        'title' => 'Foo',
1352
                        'year' => '1991',
1353
                    ],
1354
                    [
1355
                        'id' => 2,
1356
                        'title' => 'Bar',
1357
                        'year' => '2015',
1358
                    ],
1359
                ],
1360
            ],
1361
            [
1362
                'id' => 2,
1363
                'name' => 'Bill',
1364
                '_published' => [
1365
                    [
1366
                        'id' => 1,
1367
                        'title' => 'Foo',
1368
                        'year' => '1991',
1369
                    ],
1370
                    [
1371
                        'id' => 2,
1372
                        'title' => 'Bar',
1373
                        'year' => '2015',
1374
                    ],
1375
                ],
1376
            ],
1377
        ];
1378
1379
        $resource = new Collection($authorData, new JsonApiAuthorTransformer(), 'people');
1380
1381
        $scope = new Scope($this->manager, $resource);
1382
1383
        $expected = [
1384
            'data' => [
1385
                [
1386
                    'type' => 'people',
1387
                    'id' => '1',
1388
                    'attributes' => [
1389
                        'name' => 'Dave',
1390
                    ],
1391
                    'relationships' => [
1392
                        'published' => [
1393
                            'links' => [
1394
                                'self' => 'http://example.com/people/1/relationships/published',
1395
                                'related' => 'http://example.com/people/1/published',
1396
                            ],
1397
                            'data' => [
1398
                                [
1399
                                    'type' => 'books',
1400
                                    'id' => 1,
1401
                                ],
1402
                                [
1403
                                    'type' => 'books',
1404
                                    'id' => 2,
1405
                                ],
1406
                            ],
1407
                        ],
1408
                    ],
1409
                    'links' => [
1410
                        'self' => 'http://example.com/people/1',
1411
                    ],
1412
                ],
1413
                [
1414
                    'type' => 'people',
1415
                    'id' => '2',
1416
                    'attributes' => [
1417
                        'name' => 'Bill',
1418
                    ],
1419
                    'relationships' => [
1420
                        'published' => [
1421
                            'links' => [
1422
                                'self' => 'http://example.com/people/2/relationships/published',
1423
                                'related' => 'http://example.com/people/2/published',
1424
                            ],
1425
                            'data' => [
1426
                                [
1427
                                    'type' => 'books',
1428
                                    'id' => 1,
1429
                                ],
1430
                                [
1431
                                    'type' => 'books',
1432
                                    'id' => 2,
1433
                                ],
1434
                            ],
1435
                        ],
1436
                    ],
1437
                    'links' => [
1438
                        'self' => 'http://example.com/people/2',
1439
                    ],
1440
                ],
1441
            ],
1442
            'included' => [
1443
                [
1444
                    'type' => 'books',
1445
                    'id' => '1',
1446
                    'attributes' => [
1447
                        'title' => 'Foo',
1448
                        'year' => 1991,
1449
                    ],
1450
                    'links' => [
1451
                        'self' => 'http://example.com/books/1',
1452
                    ],
1453
                ],
1454
                [
1455
                    'type' => 'books',
1456
                    'id' => '2',
1457
                    'attributes' => [
1458
                        'title' => 'Bar',
1459
                        'year' => 2015,
1460
                    ],
1461
                    'links' => [
1462
                        'self' => 'http://example.com/books/2',
1463
                    ],
1464
                ],
1465
            ],
1466
        ];
1467
1468
        $this->assertEquals($expected, $scope->toArray());
1469
1470
        $expectedJson = '{"data":[{"type":"people","id":"1","attributes":{"name":"Dave"},"links":{"self":"http:\/\/example.com\/people\/1"},"relationships":{"published":{"links":{"self":"http:\/\/example.com\/people\/1\/relationships\/published","related":"http:\/\/example.com\/people\/1\/published"},"data":[{"type":"books","id":"1"},{"type":"books","id":"2"}]}}},{"type":"people","id":"2","attributes":{"name":"Bill"},"links":{"self":"http:\/\/example.com\/people\/2"},"relationships":{"published":{"links":{"self":"http:\/\/example.com\/people\/2\/relationships\/published","related":"http:\/\/example.com\/people\/2\/published"},"data":[{"type":"books","id":"1"},{"type":"books","id":"2"}]}}}],"included":[{"type":"books","id":"1","attributes":{"title":"Foo","year":1991},"links":{"self":"http:\/\/example.com\/books\/1"}},{"type":"books","id":"2","attributes":{"title":"Bar","year":2015},"links":{"self":"http:\/\/example.com\/books\/2"}}]}';
1471
        $this->assertSame($expectedJson, $scope->toJson());
1472
    }
1473
1474
    /**
1475
     * @expectedException InvalidArgumentException
1476
     * @expectedExceptionMessage JSON API resource objects MUST have a valid id
1477
     */
1478
    public function testExceptionThrownIfResourceHasNoId()
1479
    {
1480
        $bookData = [
1481
            'title' => 'Foo',
1482
            'year' => '1991',
1483
        ];
1484
1485
        $resource = new Item($bookData, new JsonApiBookTransformer(), 'books');
1486
1487
        $scope = new Scope($this->manager, $resource);
1488
        $scope->toArray();
1489
    }
1490
1491
    /**
1492
     * @expectedException InvalidArgumentException
1493
     * @expectedExceptionMessage JSON API resource objects MUST have a valid id
1494
     * @dataProvider provideResourcesWithInvalidIds
1495
     */
1496 View Code Duplication
    public function testExceptionThrownIfResourceHasInvalidId($resourceData)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1497
    {
1498
        $resource = new Item($resourceData, new JsonApiBookTransformer(), 'books');
1499
1500
        $scope = new Scope($this->manager, $resource);
1501
        $scope->toArray();
1502
    }
1503
1504
    public function provideResourcesWithInvalidIds()
1505
    {
1506
        return [
1507
            'array_id' => [[
1508
                'id' => [],
1509
                'title' => 'Foo',
1510
                'year' => '1991',
1511
            ]],
1512
            'object_id' => [[
1513
                'id' => new \StdClass,
1514
                'title' => 'Bar',
1515
                'year' => '1992',
1516
            ]],
1517
        ];
1518
    }
1519
1520
    /**
1521
     * @dataProvider provideResourcesWithValidIds
1522
     */
1523 View Code Duplication
    public function testNoExceptionThrownIfResourceHasAValidId($resourceData)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1524
    {
1525
        $resource = new Item($resourceData, new JsonApiBookTransformer(), 'books');
1526
1527
        $scope = new Scope($this->manager, $resource);
1528
        $scope->toArray();
1529
    }
1530
1531
    public function provideResourcesWithValidIds()
1532
    {
1533
        return [
1534
            'integer' => [[
1535
                'id' => 2,
1536
                'title' => 'Foo',
1537
                'year' => '1991',
1538
            ]],
1539
            'numeric' => [[
1540
                'id' => '1010122',
1541
                'title' => 'Foo',
1542
                'year' => '1991',
1543
            ]],
1544
            'serializable_id_object' => [[
1545
                'id' => new UuidStub(),
1546
                'title' => 'Bar',
1547
                'year' => '1992',
1548
            ]],
1549
            'uuid' => [[
1550
                'id' => 'e11a81b6-b1d6-4bf6-b730-aa8ce49e60d4',
1551
                'title' => 'Tic',
1552
                'year' => '1993',
1553
            ]]
1554
        ];
1555
    }
1556
1557
    public function testSerializingItemWithReferenceToRootObject()
1558
    {
1559
        $this->manager->parseIncludes('published.author');
1560
1561
        $authorData = [
1562
            'id' => 1,
1563
            'name' => 'Dave',
1564
            '_published' => [
1565
                [
1566
                    'id' => 1,
1567
                    'title' => 'Foo',
1568
                    'year' => 1991,
1569
                    '_author' => ['id' => 1]
1570
                ],
1571
                [
1572
                    'id' => 2,
1573
                    'title' => 'Bar',
1574
                    'year' => 2015,
1575
                    '_author' => ['id' => 1]
1576
                ],
1577
            ],
1578
        ];
1579
1580
        $resource = new Item($authorData, new JsonApiAuthorTransformer(), 'people');
1581
1582
        $scope = new Scope($this->manager, $resource);
1583
1584
        $expected = [
1585
            'data' => [
1586
                'type' => 'people',
1587
                'id' => '1',
1588
                'attributes' => [
1589
                    'name' => 'Dave',
1590
                ],
1591
                'relationships' => [
1592
                    'published' => [
1593
                        'data' => [
1594
                            ['type' => 'books', 'id' => '1'],
1595
                            ['type' => 'books', 'id' => '2'],
1596
                        ],
1597
                    ],
1598
                ],
1599
            ],
1600
            'included' => [
1601
                [
1602
                    'type' => 'books',
1603
                    'id' => '1',
1604
                    'attributes' => [
1605
                        'title' => 'Foo',
1606
                        'year' => 1991,
1607
                    ],
1608
                    'relationships' => [
1609
                        'author' => [
1610
                            'data' => ['type' => 'people', 'id' => '1'],
1611
                        ],
1612
                    ],
1613
                ],
1614
                [
1615
                    'type' => 'books',
1616
                    'id' => '2',
1617
                    'attributes' => [
1618
                        'title' => 'Bar',
1619
                        'year' => 2015,
1620
                    ],
1621
                    'relationships' => [
1622
                        'author' => [
1623
                            'data' => ['type' => 'people', 'id' => '1'],
1624
                        ],
1625
                    ],
1626
                ],
1627
            ],
1628
        ];
1629
1630
        $this->assertSame($expected, $scope->toArray());
1631
1632
        $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"}}}}]}';
1633
        $this->assertSame($expectedJson, $scope->toJson());
1634
    }
1635
1636
    public function testSerializingCollectionWithReferenceToRootObjects()
1637
    {
1638
        $this->manager->parseIncludes('author.published');
1639
1640
        $booksData = [
1641
            [
1642
                'id' => 1,
1643
                'title' => 'Foo',
1644
                'year' => 1991,
1645
                '_author' => [
1646
                    'id' => 1,
1647
                    'name' => 'Dave',
1648
                    '_published' => [
1649
                        [
1650
                            'id' => 1,
1651
                            'title' => 'Foo',
1652
                            'year' => 1991,
1653
                        ],
1654
                        [
1655
                            'id' => 2,
1656
                            'title' => 'Bar',
1657
                            'year' => 2015,
1658
                        ],
1659
                    ],
1660
                ],
1661
            ],
1662
            [
1663
                'id' => 2,
1664
                'title' => 'Bar',
1665
                'year' => 2015,
1666
                '_author' => [
1667
                    'id' => 1,
1668
                    '_published' => [
1669
                        [
1670
                            'id' => 1,
1671
                            'title' => 'Foo',
1672
                            'year' => 1991,
1673
                        ],
1674
                        [
1675
                            'id' => 2,
1676
                            'title' => 'Bar',
1677
                            'year' => 2015,
1678
                        ],
1679
                    ],
1680
                ],
1681
            ],
1682
        ];
1683
1684
        $resource = new Collection($booksData, new JsonApiBookTransformer(), 'books');
1685
1686
        $scope = new Scope($this->manager, $resource);
1687
1688
        $expected = [
1689
            'data' => [
1690
                [
1691
                    'type' => 'books',
1692
                    'id' => '1',
1693
                    'attributes' => [
1694
                        'title' => 'Foo',
1695
                        'year' => 1991,
1696
                    ],
1697
                    'relationships' => [
1698
                        'author' => [
1699
                            'data' => [
1700
                                'type' => 'people',
1701
                                'id' => '1',
1702
                            ],
1703
                        ],
1704
                    ],
1705
                ],
1706
                [
1707
                    'type' => 'books',
1708
                    'id' => '2',
1709
                    'attributes' => [
1710
                        'title' => 'Bar',
1711
                        'year' => 2015,
1712
                    ],
1713
                    'relationships' => [
1714
                        'author' => [
1715
                            'data' => [
1716
                                'type' => 'people',
1717
                                'id' => '1',
1718
                            ],
1719
                        ],
1720
                    ],
1721
                ],
1722
            ],
1723
            'included' => [
1724
                [
1725
                    'type' => 'people',
1726
                    'id' => '1',
1727
                    'attributes' => [
1728
                        'name' => 'Dave',
1729
                    ],
1730
                    'relationships' => [
1731
                        'published' => [
1732
                            'data' => [
1733
                                ['type' => 'books', 'id' => '1'],
1734
                                ['type' => 'books', 'id' => '2'],
1735
                            ],
1736
                        ],
1737
                    ],
1738
                ],
1739
            ],
1740
        ];
1741
1742
        $this->assertSame($expected, $scope->toArray());
1743
1744
        $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"}]}}}]}';
1745
        $this->assertSame($expectedJson, $scope->toJson());
1746
    }
1747
1748
    public function testSerializingCollectionResourceWithPaginator()
1749
    {
1750
        $baseUrl = 'http://example.com';
1751
1752
        $this->manager->setSerializer(new JsonApiSerializer($baseUrl));
1753
1754
        $total = 10;
1755
        $count = 2;
1756
        $perPage = 2;
1757
        $currentPage = 2;
1758
        $lastPage = 5;
1759
        $previousUrl = 'http://example.com/books/?page=1';
1760
        $currentUrl = 'http://example.com/books/?page=2';
1761
        $nextUrl = 'http://example.com/books/?page=3';
1762
        $lastUrl = 'http://example.com/books/?page=5';
1763
1764
        $paginator = Mockery::mock('League\Fractal\Pagination\PaginatorInterface');
1765
        $paginator->shouldReceive('getCurrentPage')->andReturn($currentPage);
1766
        $paginator->shouldReceive('getLastPage')->andReturn($lastPage);
1767
        $paginator->shouldReceive('getTotal')->andReturn($total);
1768
        $paginator->shouldReceive('getCount')->andReturn($count);
1769
        $paginator->shouldReceive('getPerPage')->andReturn($perPage);
1770
        $paginator->shouldReceive('getUrl')->with(1)->andReturn($previousUrl);
1771
        $paginator->shouldReceive('getUrl')->with(2)->andReturn($currentUrl);
1772
        $paginator->shouldReceive('getUrl')->with(3)->andReturn($nextUrl);
1773
        $paginator->shouldReceive('getUrl')->with(5)->andReturn($lastUrl);
1774
1775
        $booksData = [
1776
            [
1777
                'id' => 1,
1778
                'title' => 'Foo',
1779
                'year' => '1991',
1780
                '_author' => [
1781
                    'id' => 1,
1782
                    'name' => 'Dave',
1783
                ],
1784
            ],
1785
            [
1786
                'id' => 2,
1787
                'title' => 'Bar',
1788
                'year' => '1997',
1789
                '_author' => [
1790
                    'id' => 2,
1791
                    'name' => 'Bob',
1792
                ],
1793
            ],
1794
        ];
1795
1796
        $resource = new Collection($booksData, new JsonApiBookTransformer(), 'books');
1797
        $resource->setPaginator($paginator);
1798
        $scope = new Scope($this->manager, $resource);
1799
1800
        $expected = [
1801
            'data' => [
1802
                [
1803
                    'type' => 'books',
1804
                    'id' => '1',
1805
                    'attributes' => [
1806
                        'title' => 'Foo',
1807
                        'year' => 1991,
1808
                    ],
1809
                    'links' => [
1810
                        'self' => 'http://example.com/books/1',
1811
                    ],
1812
                ],
1813
                [
1814
                    'type' => 'books',
1815
                    'id' => '2',
1816
                    'attributes' => [
1817
                        'title' => 'Bar',
1818
                        'year' => 1997,
1819
                    ],
1820
                    'links' => [
1821
                        'self' => 'http://example.com/books/2',
1822
                    ],
1823
                ],
1824
            ],
1825
            'meta' => [
1826
                'pagination' => [
1827
                    'total' =>  10,
1828
                    'count' => 2,
1829
                    'per_page' => 2,
1830
                    'current_page' => 2,
1831
                    'total_pages' => 5
1832
                ]
1833
            ],
1834
            'links' => [
1835
                'self' => 'http://example.com/books/?page=2',
1836
                'first' => 'http://example.com/books/?page=1',
1837
                'prev' => 'http://example.com/books/?page=1',
1838
                'next' => 'http://example.com/books/?page=3',
1839
                'last' => 'http://example.com/books/?page=5'
1840
            ]
1841
        ];
1842
1843
        $this->assertSame($expected, $scope->toArray());
1844
1845
        $expectedJson = '{"data":[{"type":"books","id":"1","attributes":{"title":"Foo","year":1991},"links":{"self":"http:\/\/example.com\/books\/1"}},{"type":"books","id":"2","attributes":{"title":"Bar","year":1997},"links":{"self":"http:\/\/example.com\/books\/2"}}],"meta":{"pagination":{"total":10,"count":2,"per_page":2,"current_page":2,"total_pages":5}},"links":{"self":"http:\/\/example.com\/books\/?page=2","first":"http:\/\/example.com\/books\/?page=1","prev":"http:\/\/example.com\/books\/?page=1","next":"http:\/\/example.com\/books\/?page=3","last":"http:\/\/example.com\/books\/?page=5"}}';
1846
        $this->assertSame($expectedJson, $scope->toJson());
1847
    }
1848
1849 View Code Duplication
    public function testSerializingCollectionResourceWithPaginatorWithOmittedUnavailablePreviousLink()
1850
    {
1851
        $baseUrl = 'http://example.com';
1852
1853
        $this->manager->setSerializer(new JsonApiSerializer($baseUrl));
1854
1855
        $total = 10;
1856
        $count = 2;
1857
        $perPage = 2;
1858
        $currentPage = 1;
1859
        $lastPage = 5;
1860
        $currentUrl = 'http://example.com/books/?page=1';
1861
        $nextUrl = 'http://example.com/books/?page=2';
1862
        $lastUrl = 'http://example.com/books/?page=5';
1863
1864
        $paginator = Mockery::mock('League\Fractal\Pagination\PaginatorInterface');
1865
        $paginator->shouldReceive('getCurrentPage')->andReturn($currentPage);
1866
        $paginator->shouldReceive('getLastPage')->andReturn($lastPage);
1867
        $paginator->shouldReceive('getTotal')->andReturn($total);
1868
        $paginator->shouldReceive('getCount')->andReturn($count);
1869
        $paginator->shouldReceive('getPerPage')->andReturn($perPage);
1870
        $paginator->shouldReceive('getUrl')->with(1)->andReturn($currentUrl);
1871
        $paginator->shouldReceive('getUrl')->with(2)->andReturn($nextUrl);
1872
        $paginator->shouldReceive('getUrl')->with(5)->andReturn($lastUrl);
1873
1874
        $booksData = [
1875
            [
1876
                'id' => 1,
1877
                'title' => 'Foo',
1878
                'year' => '1991',
1879
                '_author' => [
1880
                    'id' => 1,
1881
                    'name' => 'Dave',
1882
                ],
1883
            ],
1884
            [
1885
                'id' => 2,
1886
                'title' => 'Bar',
1887
                'year' => '1997',
1888
                '_author' => [
1889
                    'id' => 2,
1890
                    'name' => 'Bob',
1891
                ],
1892
            ],
1893
        ];
1894
1895
        $resource = new Collection($booksData, new JsonApiBookTransformer(), 'books');
1896
        $resource->setPaginator($paginator);
1897
        $scope = new Scope($this->manager, $resource);
1898
1899
        $expected = [
1900
            'data' => [
1901
                [
1902
                    'type' => 'books',
1903
                    'id' => '1',
1904
                    'attributes' => [
1905
                        'title' => 'Foo',
1906
                        'year' => 1991,
1907
                    ],
1908
                    'links' => [
1909
                        'self' => 'http://example.com/books/1',
1910
                    ],
1911
                ],
1912
                [
1913
                    'type' => 'books',
1914
                    'id' => '2',
1915
                    'attributes' => [
1916
                        'title' => 'Bar',
1917
                        'year' => 1997,
1918
                    ],
1919
                    'links' => [
1920
                        'self' => 'http://example.com/books/2',
1921
                    ],
1922
                ],
1923
            ],
1924
            'meta' => [
1925
                'pagination' => [
1926
                    'total' =>  10,
1927
                    'count' => 2,
1928
                    'per_page' => 2,
1929
                    'current_page' => 1,
1930
                    'total_pages' => 5
1931
                ]
1932
            ],
1933
            'links' => [
1934
                'self' => 'http://example.com/books/?page=1',
1935
                'first' => 'http://example.com/books/?page=1',
1936
                'next' => 'http://example.com/books/?page=2',
1937
                'last' => 'http://example.com/books/?page=5'
1938
            ]
1939
        ];
1940
1941
        $this->assertSame($expected, $scope->toArray());
1942
1943
        $expectedJson = '{"data":[{"type":"books","id":"1","attributes":{"title":"Foo","year":1991},"links":{"self":"http:\/\/example.com\/books\/1"}},{"type":"books","id":"2","attributes":{"title":"Bar","year":1997},"links":{"self":"http:\/\/example.com\/books\/2"}}],"meta":{"pagination":{"total":10,"count":2,"per_page":2,"current_page":1,"total_pages":5}},"links":{"self":"http:\/\/example.com\/books\/?page=1","first":"http:\/\/example.com\/books\/?page=1","next":"http:\/\/example.com\/books\/?page=2","last":"http:\/\/example.com\/books\/?page=5"}}';
1944
        $this->assertSame($expectedJson, $scope->toJson());
1945
    }
1946
1947 View Code Duplication
    public function testSerializingCollectionResourceWithPaginatorWithOmittedUnavailableNextLink()
1948
    {
1949
        $baseUrl = 'http://example.com';
1950
1951
        $this->manager->setSerializer(new JsonApiSerializer($baseUrl));
1952
1953
        $total = 10;
1954
        $count = 2;
1955
        $perPage = 2;
1956
        $currentPage = 5;
1957
        $lastPage = 5;
1958
        $firstUrl = 'http://example.com/books/?page=1';
1959
        $previousUrl = 'http://example.com/books/?page=4';
1960
        $lastUrl = 'http://example.com/books/?page=5';
1961
1962
        $paginator = Mockery::mock('League\Fractal\Pagination\PaginatorInterface');
1963
        $paginator->shouldReceive('getCurrentPage')->andReturn($currentPage);
1964
        $paginator->shouldReceive('getLastPage')->andReturn($lastPage);
1965
        $paginator->shouldReceive('getTotal')->andReturn($total);
1966
        $paginator->shouldReceive('getCount')->andReturn($count);
1967
        $paginator->shouldReceive('getPerPage')->andReturn($perPage);
1968
        $paginator->shouldReceive('getUrl')->with(1)->andReturn($firstUrl);
1969
        $paginator->shouldReceive('getUrl')->with(4)->andReturn($previousUrl);
1970
        $paginator->shouldReceive('getUrl')->with(5)->andReturn($lastUrl);
1971
1972
        $booksData = [
1973
            [
1974
                'id' => 1,
1975
                'title' => 'Foo',
1976
                'year' => '1991',
1977
                '_author' => [
1978
                    'id' => 1,
1979
                    'name' => 'Dave',
1980
                ],
1981
            ],
1982
            [
1983
                'id' => 2,
1984
                'title' => 'Bar',
1985
                'year' => '1997',
1986
                '_author' => [
1987
                    'id' => 2,
1988
                    'name' => 'Bob',
1989
                ],
1990
            ],
1991
        ];
1992
1993
        $resource = new Collection($booksData, new JsonApiBookTransformer(), 'books');
1994
        $resource->setPaginator($paginator);
1995
        $scope = new Scope($this->manager, $resource);
1996
1997
        $expected = [
1998
            'data' => [
1999
                [
2000
                    'type' => 'books',
2001
                    'id' => '1',
2002
                    'attributes' => [
2003
                        'title' => 'Foo',
2004
                        'year' => 1991,
2005
                    ],
2006
                    'links' => [
2007
                        'self' => 'http://example.com/books/1',
2008
                    ],
2009
                ],
2010
                [
2011
                    'type' => 'books',
2012
                    'id' => '2',
2013
                    'attributes' => [
2014
                        'title' => 'Bar',
2015
                        'year' => 1997,
2016
                    ],
2017
                    'links' => [
2018
                        'self' => 'http://example.com/books/2',
2019
                    ],
2020
                ],
2021
            ],
2022
            'meta' => [
2023
                'pagination' => [
2024
                    'total' =>  10,
2025
                    'count' => 2,
2026
                    'per_page' => 2,
2027
                    'current_page' => 5,
2028
                    'total_pages' => 5
2029
                ]
2030
            ],
2031
            'links' => [
2032
                'self' => 'http://example.com/books/?page=5',
2033
                'first' => 'http://example.com/books/?page=1',
2034
                'prev' => 'http://example.com/books/?page=4',
2035
                'last' => 'http://example.com/books/?page=5'
2036
            ]
2037
        ];
2038
2039
        $this->assertSame($expected, $scope->toArray());
2040
2041
        $expectedJson = '{"data":[{"type":"books","id":"1","attributes":{"title":"Foo","year":1991},"links":{"self":"http:\/\/example.com\/books\/1"}},{"type":"books","id":"2","attributes":{"title":"Bar","year":1997},"links":{"self":"http:\/\/example.com\/books\/2"}}],"meta":{"pagination":{"total":10,"count":2,"per_page":2,"current_page":5,"total_pages":5}},"links":{"self":"http:\/\/example.com\/books\/?page=5","first":"http:\/\/example.com\/books\/?page=1","prev":"http:\/\/example.com\/books\/?page=4","last":"http:\/\/example.com\/books\/?page=5"}}';
2042
        $this->assertSame($expectedJson, $scope->toJson());
2043
    }
2044
2045
    public function testCustomLinkMerge()
2046
    {
2047
        $manager = new Manager();
2048
        $manager->setSerializer(new JsonApiSerializer('http://test.de'));
2049
2050
        $bookData = [
2051
            'id' => 1,
2052
            'title' => 'Foo',
2053
            'year' => '1991',
2054
            '_author' => [
2055
                'id' => 1,
2056
                'name' => 'Dave',
2057
            ],
2058
            'links' => [
2059
                'custom_link' => '/custom/link',
2060
            ],
2061
        ];
2062
2063
        $resource = new Item($bookData, new JsonApiBookTransformer('test.de'), 'books');
0 ignored issues
show
Unused Code introduced by
The call to JsonApiBookTransformer::__construct() has too many arguments starting with 'test.de'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
2064
2065
        $scope = new Scope($manager, $resource);
2066
2067
        $expected = [
2068
            'data' => [
2069
                'type' => 'books',
2070
                'id' => '1',
2071
                'attributes' => [
2072
                    'title' => 'Foo',
2073
                    'year' => 1991,
2074
                ],
2075
                'links' => [
2076
                    'custom_link' => '/custom/link',
2077
                    'self' => 'http://test.de/books/1',
2078
                ]
2079
            ],
2080
        ];
2081
2082
        $this->assertSame(json_encode($expected), $scope->toJson());
2083
    }
2084
2085
    public function testCustomLinkMergeNoLink()
2086
    {
2087
        $manager = new Manager();
2088
        $manager->setSerializer(new JsonApiSerializer('http://test.de'));
2089
2090
        $bookData = [
2091
            'id' => 1,
2092
            'title' => 'Foo',
2093
            'year' => '1991',
2094
            '_author' => [
2095
                'id' => 1,
2096
                'name' => 'Dave',
2097
            ],
2098
        ];
2099
2100
        $resource = new Item($bookData, new JsonApiBookTransformer(), 'books');
2101
2102
        $scope = new Scope($manager, $resource);
2103
2104
        $expected = [
2105
            'data' => [
2106
                'type' => 'books',
2107
                'id' => '1',
2108
                'attributes' => [
2109
                    'title' => 'Foo',
2110
                    'year' => 1991,
2111
                ],
2112
                'links' => [
2113
                    'self' => 'http://test.de/books/1',
2114
                ]
2115
            ],
2116
        ];
2117
2118
        $this->assertSame(json_encode($expected), $scope->toJson());
2119
    }
2120
2121
    public function tearDown()
2122
    {
2123
        Mockery::close();
2124
    }
2125
}
2126
2127
class UuidStub
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
2128
{
2129
    public function __toString()
2130
    {
2131
        return '841232a2-8afb-4de5-b018-afc90c97f852';
2132
    }
2133
}
2134