Completed
Pull Request — master (#7)
by Korvin
06:53
created

JsonApiSerializerTest::open()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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

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...
1839
1840
        $bookData = [
1841
            'title' => 'Foo',
1842
            'year' => '1991',
1843
        ];
1844
1845
        $resource = new Item($bookData, new JsonApiBookTransformer(), 'books');
1846
1847
        $scope = new Scope($this->manager, $resource);
1848
        $scope->toArray();
1849
    }
1850
1851
    public function testSerializingItemWithReferenceToRootObject()
1852
    {
1853
        $this->manager->parseIncludes('published.author');
1854
1855
        $authorData = [
1856
            'id' => 1,
1857
            'name' => 'Dave',
1858
            '_published' => [
1859
                [
1860
                    'id' => 1,
1861
                    'title' => 'Foo',
1862
                    'year' => 1991,
1863
                    '_author' => ['id' => 1]
1864
                ],
1865
                [
1866
                    'id' => 2,
1867
                    'title' => 'Bar',
1868
                    'year' => 2015,
1869
                    '_author' => ['id' => 1]
1870
                ],
1871
            ],
1872
        ];
1873
1874
        $resource = new Item($authorData, new JsonApiAuthorTransformer(), 'people');
1875
1876
        $scope = new Scope($this->manager, $resource);
1877
1878
        $expected = [
1879
            'data' => [
1880
                'type' => 'people',
1881
                'id' => '1',
1882
                'attributes' => [
1883
                    'name' => 'Dave',
1884
                ],
1885
                'relationships' => [
1886
                    'published' => [
1887
                        'data' => [
1888
                            ['type' => 'books', 'id' => '1'],
1889
                            ['type' => 'books', 'id' => '2'],
1890
                        ],
1891
                    ],
1892
                ],
1893
            ],
1894
            'included' => [
1895
                [
1896
                    'type' => 'books',
1897
                    'id' => '1',
1898
                    'attributes' => [
1899
                        'title' => 'Foo',
1900
                        'year' => 1991,
1901
                    ],
1902
                    'relationships' => [
1903
                        'author' => [
1904
                            'data' => ['type' => 'people', 'id' => '1'],
1905
                        ],
1906
                    ],
1907
                ],
1908
                [
1909
                    'type' => 'books',
1910
                    'id' => '2',
1911
                    'attributes' => [
1912
                        'title' => 'Bar',
1913
                        'year' => 2015,
1914
                    ],
1915
                    'relationships' => [
1916
                        'author' => [
1917
                            'data' => ['type' => 'people', 'id' => '1'],
1918
                        ],
1919
                    ],
1920
                ],
1921
            ],
1922
        ];
1923
1924
        $this->assertSame($expected, $scope->toArray());
1925
1926
        $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"}}}}]}';
1927
        $this->assertSame($expectedJson, $scope->toJson());
1928
    }
1929
1930
    public function testSerializingCollectionWithReferenceToRootObjects()
1931
    {
1932
        $this->manager->parseIncludes('author.published');
1933
1934
        $booksData = [
1935
            [
1936
                'id' => 1,
1937
                'title' => 'Foo',
1938
                'year' => 1991,
1939
                '_author' => [
1940
                    'id' => 1,
1941
                    'name' => 'Dave',
1942
                    '_published' => [
1943
                        [
1944
                            'id' => 1,
1945
                            'title' => 'Foo',
1946
                            'year' => 1991,
1947
                        ],
1948
                        [
1949
                            'id' => 2,
1950
                            'title' => 'Bar',
1951
                            'year' => 2015,
1952
                        ],
1953
                    ],
1954
                ],
1955
            ],
1956
            [
1957
                'id' => 2,
1958
                'title' => 'Bar',
1959
                'year' => 2015,
1960
                '_author' => [
1961
                    'id' => 1,
1962
                    '_published' => [
1963
                        [
1964
                            'id' => 1,
1965
                            'title' => 'Foo',
1966
                            'year' => 1991,
1967
                        ],
1968
                        [
1969
                            'id' => 2,
1970
                            'title' => 'Bar',
1971
                            'year' => 2015,
1972
                        ],
1973
                    ],
1974
                ],
1975
            ],
1976
        ];
1977
1978
        $resource = new Collection($booksData, new JsonApiBookTransformer(), 'books');
1979
1980
        $scope = new Scope($this->manager, $resource);
1981
1982
        $expected = [
1983
            'data' => [
1984
                [
1985
                    'type' => 'books',
1986
                    'id' => '1',
1987
                    'attributes' => [
1988
                        'title' => 'Foo',
1989
                        'year' => 1991,
1990
                    ],
1991
                    'relationships' => [
1992
                        'author' => [
1993
                            'data' => [
1994
                                'type' => 'people',
1995
                                'id' => '1',
1996
                            ],
1997
                        ],
1998
                    ],
1999
                ],
2000
                [
2001
                    'type' => 'books',
2002
                    'id' => '2',
2003
                    'attributes' => [
2004
                        'title' => 'Bar',
2005
                        'year' => 2015,
2006
                    ],
2007
                    'relationships' => [
2008
                        'author' => [
2009
                            'data' => [
2010
                                'type' => 'people',
2011
                                'id' => '1',
2012
                            ],
2013
                        ],
2014
                    ],
2015
                ],
2016
            ],
2017
            'included' => [
2018
                [
2019
                    'type' => 'people',
2020
                    'id' => '1',
2021
                    'attributes' => [
2022
                        'name' => 'Dave',
2023
                    ],
2024
                    'relationships' => [
2025
                        'published' => [
2026
                            'data' => [
2027
                                ['type' => 'books', 'id' => '1'],
2028
                                ['type' => 'books', 'id' => '2'],
2029
                            ],
2030
                        ],
2031
                    ],
2032
                ],
2033
            ],
2034
        ];
2035
2036
        $this->assertSame($expected, $scope->toArray());
2037
2038
        $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"}]}}}]}';
2039
        $this->assertSame($expectedJson, $scope->toJson());
2040
    }
2041
2042
    public function testSerializingCollectionResourceWithPaginator()
2043
    {
2044
        $baseUrl = 'http://example.com';
2045
2046
        $this->manager->setSerializer(new JsonApiSerializer($baseUrl));
2047
2048
        $total = 10;
2049
        $count = 2;
2050
        $perPage = 2;
2051
        $currentPage = 2;
2052
        $lastPage = 5;
2053
        $previousUrl = 'http://example.com/books/?page=1';
2054
        $currentUrl = 'http://example.com/books/?page=2';
2055
        $nextUrl = 'http://example.com/books/?page=3';
2056
        $lastUrl = 'http://example.com/books/?page=5';
2057
2058
        $paginator = Mockery::mock('League\Fractal\Pagination\PaginatorInterface');
2059
        $paginator->shouldReceive('getCurrentPage')->andReturn($currentPage);
2060
        $paginator->shouldReceive('getLastPage')->andReturn($lastPage);
2061
        $paginator->shouldReceive('getTotal')->andReturn($total);
2062
        $paginator->shouldReceive('getCount')->andReturn($count);
2063
        $paginator->shouldReceive('getPerPage')->andReturn($perPage);
2064
        $paginator->shouldReceive('getUrl')->with(1)->andReturn($previousUrl);
2065
        $paginator->shouldReceive('getUrl')->with(2)->andReturn($currentUrl);
2066
        $paginator->shouldReceive('getUrl')->with(3)->andReturn($nextUrl);
2067
        $paginator->shouldReceive('getUrl')->with(5)->andReturn($lastUrl);
2068
2069
        $booksData = [
2070
            [
2071
                'id' => 1,
2072
                'title' => 'Foo',
2073
                'year' => '1991',
2074
                '_author' => [
2075
                    'id' => 1,
2076
                    'name' => 'Dave',
2077
                ],
2078
            ],
2079
            [
2080
                'id' => 2,
2081
                'title' => 'Bar',
2082
                'year' => '1997',
2083
                '_author' => [
2084
                    'id' => 2,
2085
                    'name' => 'Bob',
2086
                ],
2087
            ],
2088
        ];
2089
2090
        $resource = new Collection($booksData, new JsonApiBookTransformer(), 'books');
2091
        $resource->setPaginator($paginator);
0 ignored issues
show
Documentation introduced by
$paginator is of type object<Mockery\LegacyMockInterface>, but the function expects a object<League\Fractal\Pa...ion\PaginatorInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
2092
        $scope = new Scope($this->manager, $resource);
2093
2094
        $expected = [
2095
            'data' => [
2096
                [
2097
                    'type' => 'books',
2098
                    'id' => '1',
2099
                    'attributes' => [
2100
                        'title' => 'Foo',
2101
                        'year' => 1991,
2102
                    ],
2103
                    'links' => [
2104
                        'self' => 'http://example.com/books/1',
2105
                    ],
2106
                    'relationships' => [
2107
                        'author' => [
2108
                            'links' => [
2109
                                'self' => 'http://example.com/books/1/relationships/author',
2110
                                'related' => 'http://example.com/books/1/author',
2111
                            ],
2112
                        ],
2113
                        'co-author' => [
2114
                            'links' => [
2115
                                'self' => 'http://example.com/books/1/relationships/co-author',
2116
                                'related' => 'http://example.com/books/1/co-author'
2117
                            ],
2118
                        ],
2119
                        'author-with-meta' => [
2120
                            'links' => [
2121
                                'self' => 'http://example.com/books/1/relationships/author-with-meta',
2122
                                'related' => 'http://example.com/books/1/author-with-meta'
2123
                            ],
2124
                        ],
2125
                    ],
2126
                ],
2127
                [
2128
                    'type' => 'books',
2129
                    'id' => '2',
2130
                    'attributes' => [
2131
                        'title' => 'Bar',
2132
                        'year' => 1997,
2133
                    ],
2134
                    'links' => [
2135
                        'self' => 'http://example.com/books/2',
2136
                    ],
2137
                    'relationships' => [
2138
                        'author' => [
2139
                            'links' => [
2140
                                'self' => 'http://example.com/books/2/relationships/author',
2141
                                'related' => 'http://example.com/books/2/author',
2142
                            ],
2143
                        ],
2144
                        'co-author' => [
2145
                            'links' => [
2146
                                'self' => 'http://example.com/books/2/relationships/co-author',
2147
                                'related' => 'http://example.com/books/2/co-author'
2148
                            ],
2149
                        ],
2150
                        'author-with-meta' => [
2151
                            'links' => [
2152
                                'self' => 'http://example.com/books/2/relationships/author-with-meta',
2153
                                'related' => 'http://example.com/books/2/author-with-meta'
2154
                            ],
2155
                        ],
2156
                    ],
2157
                ],
2158
            ],
2159
            'meta' => [
2160
                'pagination' => [
2161
                    'total' =>  10,
2162
                    'count' => 2,
2163
                    'per_page' => 2,
2164
                    'current_page' => 2,
2165
                    'total_pages' => 5
2166
                ]
2167
            ],
2168
            'links' => [
2169
                'self' => 'http://example.com/books/?page=2',
2170
                'first' => 'http://example.com/books/?page=1',
2171
                'prev' => 'http://example.com/books/?page=1',
2172
                'next' => 'http://example.com/books/?page=3',
2173
                'last' => 'http://example.com/books/?page=5'
2174
            ]
2175
        ];
2176
2177
        $this->assertSame($expected, $scope->toArray());
2178
2179
        $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"}}';
2180
        $this->assertSame($expectedJson, $scope->toJson());
2181
    }
2182
2183 View Code Duplication
    public function testSerializingCollectionResourceWithPaginatorWithOmittedUnavailablePreviousLink()
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...
2184
    {
2185
        $baseUrl = 'http://example.com';
2186
2187
        $this->manager->setSerializer(new JsonApiSerializer($baseUrl));
2188
2189
        $total = 10;
2190
        $count = 2;
2191
        $perPage = 2;
2192
        $currentPage = 1;
2193
        $lastPage = 5;
2194
        $currentUrl = 'http://example.com/books/?page=1';
2195
        $nextUrl = 'http://example.com/books/?page=2';
2196
        $lastUrl = 'http://example.com/books/?page=5';
2197
2198
        $paginator = Mockery::mock('League\Fractal\Pagination\PaginatorInterface');
2199
        $paginator->shouldReceive('getCurrentPage')->andReturn($currentPage);
2200
        $paginator->shouldReceive('getLastPage')->andReturn($lastPage);
2201
        $paginator->shouldReceive('getTotal')->andReturn($total);
2202
        $paginator->shouldReceive('getCount')->andReturn($count);
2203
        $paginator->shouldReceive('getPerPage')->andReturn($perPage);
2204
        $paginator->shouldReceive('getUrl')->with(1)->andReturn($currentUrl);
2205
        $paginator->shouldReceive('getUrl')->with(2)->andReturn($nextUrl);
2206
        $paginator->shouldReceive('getUrl')->with(5)->andReturn($lastUrl);
2207
2208
        $booksData = [
2209
            [
2210
                'id' => 1,
2211
                'title' => 'Foo',
2212
                'year' => '1991',
2213
                '_author' => [
2214
                    'id' => 1,
2215
                    'name' => 'Dave',
2216
                ],
2217
            ],
2218
            [
2219
                'id' => 2,
2220
                'title' => 'Bar',
2221
                'year' => '1997',
2222
                '_author' => [
2223
                    'id' => 2,
2224
                    'name' => 'Bob',
2225
                ],
2226
            ],
2227
        ];
2228
2229
        $resource = new Collection($booksData, new JsonApiBookTransformer(), 'books');
2230
        $resource->setPaginator($paginator);
0 ignored issues
show
Documentation introduced by
$paginator is of type object<Mockery\LegacyMockInterface>, but the function expects a object<League\Fractal\Pa...ion\PaginatorInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
2231
        $scope = new Scope($this->manager, $resource);
2232
2233
        $expected = [
2234
            'data' => [
2235
                [
2236
                    'type' => 'books',
2237
                    'id' => '1',
2238
                    'attributes' => [
2239
                        'title' => 'Foo',
2240
                        'year' => 1991,
2241
                    ],
2242
                    'links' => [
2243
                        'self' => 'http://example.com/books/1',
2244
                    ],
2245
                    'relationships' => [
2246
                        'author' => [
2247
                            'links' => [
2248
                                'self' => 'http://example.com/books/1/relationships/author',
2249
                                'related' => 'http://example.com/books/1/author',
2250
                            ],
2251
                        ],
2252
                        'co-author' => [
2253
                            'links' => [
2254
                                'self' => 'http://example.com/books/1/relationships/co-author',
2255
                                'related' => 'http://example.com/books/1/co-author'
2256
                            ],
2257
                        ],
2258
                        'author-with-meta' => [
2259
                            'links' => [
2260
                                'self' => 'http://example.com/books/1/relationships/author-with-meta',
2261
                                'related' => 'http://example.com/books/1/author-with-meta'
2262
                            ],
2263
                        ],
2264
                    ],
2265
                ],
2266
                [
2267
                    'type' => 'books',
2268
                    'id' => '2',
2269
                    'attributes' => [
2270
                        'title' => 'Bar',
2271
                        'year' => 1997,
2272
                    ],
2273
                    'links' => [
2274
                        'self' => 'http://example.com/books/2',
2275
                    ],
2276
                    'relationships' => [
2277
                        'author' => [
2278
                            'links' => [
2279
                                'self' => 'http://example.com/books/2/relationships/author',
2280
                                'related' => 'http://example.com/books/2/author',
2281
                            ],
2282
                        ],
2283
                        'co-author' => [
2284
                            'links' => [
2285
                                'self' => 'http://example.com/books/2/relationships/co-author',
2286
                                'related' => 'http://example.com/books/2/co-author'
2287
                            ],
2288
                        ],
2289
                        'author-with-meta' => [
2290
                            'links' => [
2291
                                'self' => 'http://example.com/books/2/relationships/author-with-meta',
2292
                                'related' => 'http://example.com/books/2/author-with-meta'
2293
                            ],
2294
                        ],
2295
                    ],
2296
                ],
2297
            ],
2298
            'meta' => [
2299
                'pagination' => [
2300
                    'total' =>  10,
2301
                    'count' => 2,
2302
                    'per_page' => 2,
2303
                    'current_page' => 1,
2304
                    'total_pages' => 5
2305
                ]
2306
            ],
2307
            'links' => [
2308
                'self' => 'http://example.com/books/?page=1',
2309
                'first' => 'http://example.com/books/?page=1',
2310
                'next' => 'http://example.com/books/?page=2',
2311
                'last' => 'http://example.com/books/?page=5'
2312
            ]
2313
        ];
2314
2315
        $this->assertSame($expected, $scope->toArray());
2316
2317
        $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"}}';
2318
        $this->assertSame($expectedJson, $scope->toJson());
2319
    }
2320
2321 View Code Duplication
    public function testSerializingCollectionResourceWithPaginatorWithOmittedUnavailableNextLink()
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...
2322
    {
2323
        $baseUrl = 'http://example.com';
2324
2325
        $this->manager->setSerializer(new JsonApiSerializer($baseUrl));
2326
2327
        $total = 10;
2328
        $count = 2;
2329
        $perPage = 2;
2330
        $currentPage = 5;
2331
        $lastPage = 5;
2332
        $firstUrl = 'http://example.com/books/?page=1';
2333
        $previousUrl = 'http://example.com/books/?page=4';
2334
        $lastUrl = 'http://example.com/books/?page=5';
2335
2336
        $paginator = Mockery::mock('League\Fractal\Pagination\PaginatorInterface');
2337
        $paginator->shouldReceive('getCurrentPage')->andReturn($currentPage);
2338
        $paginator->shouldReceive('getLastPage')->andReturn($lastPage);
2339
        $paginator->shouldReceive('getTotal')->andReturn($total);
2340
        $paginator->shouldReceive('getCount')->andReturn($count);
2341
        $paginator->shouldReceive('getPerPage')->andReturn($perPage);
2342
        $paginator->shouldReceive('getUrl')->with(1)->andReturn($firstUrl);
2343
        $paginator->shouldReceive('getUrl')->with(4)->andReturn($previousUrl);
2344
        $paginator->shouldReceive('getUrl')->with(5)->andReturn($lastUrl);
2345
2346
        $booksData = [
2347
            [
2348
                'id' => 1,
2349
                'title' => 'Foo',
2350
                'year' => '1991',
2351
                '_author' => [
2352
                    'id' => 1,
2353
                    'name' => 'Dave',
2354
                ],
2355
            ],
2356
            [
2357
                'id' => 2,
2358
                'title' => 'Bar',
2359
                'year' => '1997',
2360
                '_author' => [
2361
                    'id' => 2,
2362
                    'name' => 'Bob',
2363
                ],
2364
            ],
2365
        ];
2366
2367
        $resource = new Collection($booksData, new JsonApiBookTransformer(), 'books');
2368
        $resource->setPaginator($paginator);
0 ignored issues
show
Documentation introduced by
$paginator is of type object<Mockery\LegacyMockInterface>, but the function expects a object<League\Fractal\Pa...ion\PaginatorInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
2369
        $scope = new Scope($this->manager, $resource);
2370
2371
        $expected = [
2372
            'data' => [
2373
                [
2374
                    'type' => 'books',
2375
                    'id' => '1',
2376
                    'attributes' => [
2377
                        'title' => 'Foo',
2378
                        'year' => 1991,
2379
                    ],
2380
                    'links' => [
2381
                        'self' => 'http://example.com/books/1',
2382
                    ],
2383
                    'relationships' => [
2384
                        'author' => [
2385
                            'links' => [
2386
                                'self' => 'http://example.com/books/1/relationships/author',
2387
                                'related' => 'http://example.com/books/1/author',
2388
                            ],
2389
                        ],
2390
                        'co-author' => [
2391
                            'links' => [
2392
                                'self' => 'http://example.com/books/1/relationships/co-author',
2393
                                'related' => 'http://example.com/books/1/co-author'
2394
                            ],
2395
                        ],
2396
                        'author-with-meta' => [
2397
                            'links' => [
2398
                                'self' => 'http://example.com/books/1/relationships/author-with-meta',
2399
                                'related' => 'http://example.com/books/1/author-with-meta'
2400
                            ],
2401
                        ],
2402
                    ],
2403
                ],
2404
                [
2405
                    'type' => 'books',
2406
                    'id' => '2',
2407
                    'attributes' => [
2408
                        'title' => 'Bar',
2409
                        'year' => 1997,
2410
                    ],
2411
                    'links' => [
2412
                        'self' => 'http://example.com/books/2',
2413
                    ],
2414
                    'relationships' => [
2415
                        'author' => [
2416
                            'links' => [
2417
                                'self' => 'http://example.com/books/2/relationships/author',
2418
                                'related' => 'http://example.com/books/2/author',
2419
                            ],
2420
                        ],
2421
                        'co-author' => [
2422
                            'links' => [
2423
                                'self' => 'http://example.com/books/2/relationships/co-author',
2424
                                'related' => 'http://example.com/books/2/co-author'
2425
                            ],
2426
                        ],
2427
                        'author-with-meta' => [
2428
                            'links' => [
2429
                                'self' => 'http://example.com/books/2/relationships/author-with-meta',
2430
                                'related' => 'http://example.com/books/2/author-with-meta'
2431
                            ],
2432
                        ],
2433
                    ],
2434
                ],
2435
            ],
2436
            'meta' => [
2437
                'pagination' => [
2438
                    'total' =>  10,
2439
                    'count' => 2,
2440
                    'per_page' => 2,
2441
                    'current_page' => 5,
2442
                    'total_pages' => 5
2443
                ]
2444
            ],
2445
            'links' => [
2446
                'self' => 'http://example.com/books/?page=5',
2447
                'first' => 'http://example.com/books/?page=1',
2448
                'prev' => 'http://example.com/books/?page=4',
2449
                'last' => 'http://example.com/books/?page=5'
2450
            ]
2451
        ];
2452
2453
        $this->assertSame($expected, $scope->toArray());
2454
2455
        $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"}}';
2456
        $this->assertSame($expectedJson, $scope->toJson());
2457
    }
2458
2459 View Code Duplication
    public function testCustomLinkMerge()
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...
2460
    {
2461
        $manager = new Manager();
2462
        $manager->setSerializer(new JsonApiSerializer('http://test.de'));
2463
2464
        $bookData = [
2465
            'id' => 1,
2466
            'title' => 'Foo',
2467
            'year' => '1991',
2468
            '_author' => [
2469
                'id' => 1,
2470
                'name' => 'Dave',
2471
            ],
2472
            'links' => [
2473
                'custom_link' => '/custom/link',
2474
            ],
2475
        ];
2476
2477
        $resource = new Item($bookData, new JsonApiBookTransformer(), 'books');
2478
2479
        $scope = new Scope($manager, $resource);
2480
2481
        $expected = [
2482
            'data' => [
2483
                'type' => 'books',
2484
                'id' => '1',
2485
                'attributes' => [
2486
                    'title' => 'Foo',
2487
                    'year' => 1991,
2488
                ],
2489
                'links' => [
2490
                    'self' => 'http://test.de/books/1',
2491
                    'custom_link' => '/custom/link',
2492
                ],
2493
                'relationships' => [
2494
                    'author' => [
2495
                        'links' => [
2496
                            'self' => 'http://test.de/books/1/relationships/author',
2497
                            'related' => 'http://test.de/books/1/author',
2498
                        ],
2499
                    ],
2500
                    'co-author' => [
2501
                        'links' => [
2502
                            'self' => 'http://test.de/books/1/relationships/co-author',
2503
                            'related' => 'http://test.de/books/1/co-author'
2504
                        ],
2505
                    ],
2506
                    'author-with-meta' => [
2507
                        'links' => [
2508
                            'self' => 'http://test.de/books/1/relationships/author-with-meta',
2509
                            'related' => 'http://test.de/books/1/author-with-meta'
2510
                        ],
2511
                    ],
2512
                ],
2513
            ],
2514
        ];
2515
2516
        $this->assertSame(json_encode($expected), $scope->toJson());
2517
    }
2518
2519
    public function testCustomLinkMergeNoLink()
2520
    {
2521
        $manager = new Manager();
2522
        $manager->setSerializer(new JsonApiSerializer('http://test.de'));
2523
2524
        $bookData = [
2525
            'id' => 1,
2526
            'title' => 'Foo',
2527
            'year' => '1991',
2528
            '_author' => [
2529
                'id' => 1,
2530
                'name' => 'Dave',
2531
            ],
2532
        ];
2533
2534
        $resource = new Item($bookData, new JsonApiBookTransformer(), 'books');
2535
2536
        $scope = new Scope($manager, $resource);
2537
2538
        $expected = [
2539
            'data' => [
2540
                'type' => 'books',
2541
                'id' => '1',
2542
                'attributes' => [
2543
                    'title' => 'Foo',
2544
                    'year' => 1991,
2545
                ],
2546
                'links' => [
2547
                    'self' => 'http://test.de/books/1',
2548
                ],
2549
                'relationships' => [
2550
                    'author' => [
2551
                        'links' => [
2552
                            'self' => 'http://test.de/books/1/relationships/author',
2553
                            'related' => 'http://test.de/books/1/author',
2554
                        ],
2555
                    ],
2556
                    'co-author' => [
2557
                        'links' => [
2558
                            'self' => 'http://test.de/books/1/relationships/co-author',
2559
                            'related' => 'http://test.de/books/1/co-author'
2560
                        ],
2561
                    ],
2562
                    'author-with-meta' => [
2563
                        'links' => [
2564
                            'self' => 'http://test.de/books/1/relationships/author-with-meta',
2565
                            'related' => 'http://test.de/books/1/author-with-meta'
2566
                        ],
2567
                    ],
2568
                ],
2569
            ],
2570
        ];
2571
2572
        $this->assertSame(json_encode($expected), $scope->toJson());
2573
    }
2574
2575 View Code Duplication
    public function testCustomSelfLinkMerge()
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...
2576
    {
2577
        $manager = new Manager();
2578
        $manager->setSerializer(new JsonApiSerializer('http://test.de'));
2579
2580
        $bookData = [
2581
            'id' => 1,
2582
            'title' => 'Foo',
2583
            'year' => '1991',
2584
            '_author' => [
2585
                'id' => 1,
2586
                'name' => 'Dave',
2587
            ],
2588
            'links' => [
2589
                'self' => '/custom/link',
2590
            ],
2591
        ];
2592
2593
        $resource = new Item($bookData, new JsonApiBookTransformer(), 'books');
2594
2595
        $scope = new Scope($manager, $resource);
2596
2597
        $expected = [
2598
            'data' => [
2599
                'type' => 'books',
2600
                'id' => '1',
2601
                'attributes' => [
2602
                    'title' => 'Foo',
2603
                    'year' => 1991,
2604
                ],
2605
                'links' => [
2606
                    'self' => '/custom/link',
2607
                ],
2608
                'relationships' => [
2609
                    'author' => [
2610
                        'links' => [
2611
                            'self' => 'http://test.de/books/1/relationships/author',
2612
                            'related' => 'http://test.de/books/1/author',
2613
                        ],
2614
                    ],
2615
                    'co-author' => [
2616
                        'links' => [
2617
                            'self' => 'http://test.de/books/1/relationships/co-author',
2618
                            'related' => 'http://test.de/books/1/co-author'
2619
                        ],
2620
                    ],
2621
                    'author-with-meta' => [
2622
                        'links' => [
2623
                            'self' => 'http://test.de/books/1/relationships/author-with-meta',
2624
                            'related' => 'http://test.de/books/1/author-with-meta'
2625
                        ],
2626
                    ],
2627
                ],
2628
            ],
2629
        ];
2630
2631
        $this->assertSame(json_encode($expected), $scope->toJson());
2632
    }
2633
2634
    public function testEmptyAttributesIsObject()
2635
    {
2636
        $manager = new Manager();
2637
        $manager->setSerializer(new JsonApiSerializer());
2638
2639
        $data = ['id' => 1];
2640
2641
        $resource = new Item($data, new JsonApiEmptyTransformer(), 'resources');
2642
2643
        $scope = new Scope($manager, $resource);
2644
2645
        $expectedJson = '{"data":{"type":"resources","id":"1","attributes":{}}}';
2646
2647
        $this->assertSame($expectedJson, $scope->toJson());
2648
    }
2649
2650
    /**
2651
     * @dataProvider serializingWithFieldsetsProvider
2652
     */
2653
    public function testSerializingWithFieldsets($fieldsetsToParse, $expected)
2654
    {
2655
        $this->manager->parseIncludes(['author', 'author.published']);
2656
2657
        $bookData = [
2658
            'id' => 1,
2659
            'title' => 'Foo',
2660
            'year' => '1991',
2661
            '_author' => [
2662
                'id' => 1,
2663
                'name' => 'Dave',
2664
                '_published' => [
2665
                    [
2666
                        'id' => 1,
2667
                        'title' => 'Foo',
2668
                        'year' => '1991',
2669
                    ],
2670
                    [
2671
                        'id' => 2,
2672
                        'title' => 'Bar',
2673
                        'year' => '2015',
2674
                    ],
2675
                ],
2676
            ],
2677
        ];
2678
2679
        $resource = new Item($bookData, new JsonApiBookTransformer(), 'books');
2680
2681
        $scope = new Scope($this->manager, $resource);
2682
2683
        $this->manager->parseFieldsets($fieldsetsToParse);
2684
        $this->assertSame($expected, $scope->toArray());
2685
    }
2686
2687
    public function serializingWithFieldsetsProvider()
2688
    {
2689
        return [
2690
            [
2691
                //Single field
2692
                ['books' => 'title'],
2693
                [
2694
                    'data' => [
2695
                        'type' => 'books',
2696
                        'id' => '1',
2697
                        'attributes' => [
2698
                            'title' => 'Foo'
2699
                        ]
2700
                    ]
2701
                ]
2702
            ],
2703
            [
2704
                //Multiple fields
2705
                ['books' => 'title,year'],
2706
                [
2707
                    'data' => [
2708
                        'type' => 'books',
2709
                        'id' => '1',
2710
                        'attributes' => [
2711
                            'title' => 'Foo',
2712
                            'year' => 1991
2713
                        ]
2714
                    ]
2715
                ]
2716
            ],
2717
            [
2718
                //Include 1st level relationship
2719
                ['books' => 'title,author', 'people' => 'name'],
2720
                [
2721
                    'data' => [
2722
                        'type' => 'books',
2723
                        'id' => '1',
2724
                        'attributes' => [
2725
                            'title' => 'Foo'
2726
                        ],
2727
                        'relationships' => [
2728
                            'author' => [
2729
                                'data' => [
2730
                                    'type' => 'people',
2731
                                    'id' => '1'
2732
                                ]
2733
                            ]
2734
                        ]
2735
                    ],
2736
                    'included' => [
2737
                        [
2738
                            'type' => 'people',
2739
                            'id' => '1',
2740
                            'attributes' => [
2741
                                'name' => 'Dave'
2742
                            ]
2743
                        ]
2744
                    ]
2745
                ]
2746
            ],
2747
            [
2748
                //Include 2nd level relationship
2749
                ['books' => 'title,author', 'people' => 'name,published'],
2750
                [
2751
                    'data' => [
2752
                        'type' => 'books',
2753
                        'id' => '1',
2754
                        'attributes' => [
2755
                            'title' => 'Foo'
2756
                        ],
2757
                        'relationships' => [
2758
                            'author' => [
2759
                                'data' => [
2760
                                    'type' => 'people',
2761
                                    'id' => '1'
2762
                                ]
2763
                            ]
2764
                        ]
2765
                    ],
2766
                    'included' => [
2767
                        [
2768
                            'type' => 'books',
2769
                            'id' => '2',
2770
                            'attributes' => [
2771
                                'title' => 'Bar'
2772
                            ]
2773
                        ],
2774
                        [
2775
                            'type' => 'people',
2776
                            'id' => '1',
2777
                            'attributes' => [
2778
                                'name' => 'Dave'
2779
                            ],
2780
                            'relationships' => [
2781
                                'published' => [
2782
                                    'data' => [
2783
                                        [
2784
                                            'type' => 'books',
2785
                                            'id' => '1'
2786
                                        ],
2787
                                        [
2788
                                            'type' => 'books',
2789
                                            'id' => '2'
2790
                                        ]
2791
                                    ]
2792
                                ]
2793
                            ]
2794
                        ]
2795
                    ]
2796
                ]
2797
            ]
2798
        ];
2799
    }
2800
}
2801