testSerializingCollectionResourceWithHasOneInclude()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 86

Duplication

Lines 86
Ratio 100 %

Importance

Changes 0
Metric Value
dl 86
loc 86
rs 8.3054
c 0
b 0
f 0
cc 1
nc 1
nop 0

How to fix   Long Method   

Long Method

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

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

Commonly applied refactorings include:

1
<?php namespace League\Fractal\Test\Serializer;
2
3
use League\Fractal\Manager;
4
use League\Fractal\Resource\Collection;
5
use League\Fractal\Resource\Item;
6
use League\Fractal\Scope;
7
use League\Fractal\Serializer\JsonApiSerializer;
8
use League\Fractal\Test\Stub\Transformer\JsonApiAuthorTransformer;
9
use League\Fractal\Test\Stub\Transformer\JsonApiBookTransformer;
10
use League\Fractal\Test\Stub\Transformer\JsonApiEmptyTransformer;
11
use Mockery;
12
use PHPUnit\Framework\TestCase;
13
14
class JsonApiSerializerTest extends TestCase
15
{
16
    private $manager;
17
18
    public function setUp()
19
    {
20
        $this->manager = new Manager();
21
        $this->manager->setSerializer(new JsonApiSerializer());
22
    }
23
24
    public function testSerializeCollectionWithExtraMeta()
25
    {
26
        $booksData = [
27
            [
28
                'id' => 1,
29
                'title' => 'Foo',
30
                'year' => '1991',
31
                '_author' => [
32
                    'id' => 1,
33
                    'name' => 'Dave',
34
                ],
35
                'meta' => [
36
                    'foo' => 'bar'
37
                ]
38
            ],
39
            [
40
                'id' => 2,
41
                'title' => 'Bar',
42
                'year' => '1997',
43
                '_author' => [
44
                    'id' => 2,
45
                    'name' => 'Bob',
46
                ],
47
                'meta' => [
48
                    'bar' => 'baz'
49
                ]
50
            ],
51
        ];
52
53
        $resource = new Collection($booksData, new JsonApiBookTransformer(), 'books');
54
        $scope = new Scope($this->manager, $resource);
55
56
        $expected = [
57
            'data' => [
58
                [
59
                    'type' => 'books',
60
                    'id' => '1',
61
                    'attributes' => [
62
                        'title' => 'Foo',
63
                        'year' => 1991,
64
                    ],
65
                    'meta' => [
66
                        'foo' => 'bar'
67
                    ]
68
                ],
69
                [
70
                    'type' => 'books',
71
                    'id' => '2',
72
                    'attributes' => [
73
                        'title' => 'Bar',
74
                        'year' => 1997,
75
                    ],
76
                    'meta' => [
77
                        'bar' => 'baz'
78
                    ]
79
                ],
80
            ],
81
        ];
82
83
        $this->assertSame($expected, $scope->toArray());
84
85
        $expectedJson = '{"data":[{"type":"books","id":"1","attributes":{"title":"Foo","year":1991},"meta":{"foo":"bar"}},{"type":"books","id":"2","attributes":{"title":"Bar","year":1997},"meta":{"bar":"baz"}}]}';
86
        $this->assertSame($expectedJson, $scope->toJson());
87
    }
88
89
    public function testSerializingItemResourceWithHasOneInclude()
90
    {
91
        $this->manager->parseIncludes('author');
92
93
        $bookData = [
94
            'id' => 1,
95
            'title' => 'Foo',
96
            'year' => '1991',
97
            '_author' => [
98
                'id' => 1,
99
                'name' => 'Dave',
100
            ],
101
        ];
102
103
        $resource = new Item($bookData, new JsonApiBookTransformer(), 'books');
104
105
        $scope = new Scope($this->manager, $resource);
106
107
        $expected = [
108
            'data' => [
109
                'type' => 'books',
110
                'id' => '1',
111
                'attributes' => [
112
                    'title' => 'Foo',
113
                    'year' => 1991,
114
                ],
115
                'relationships' => [
116
                    'author' => [
117
                        'data' => [
118
                            'type' => 'people',
119
                            'id' => '1',
120
                        ],
121
                    ],
122
                ],
123
            ],
124
            'included' => [
125
                [
126
                    'type' => 'people',
127
                    'id' => '1',
128
                    'attributes' => [
129
                        'name' => 'Dave',
130
                    ],
131
                ],
132
            ],
133
        ];
134
135
        $this->assertSame($expected, $scope->toArray());
136
137
        $expectedJson = '{"data":{"type":"books","id":"1","attributes":{"title":"Foo","year":1991},"relationships":{"author":{"data":{"type":"people","id":"1"}}}},"included":[{"type":"people","id":"1","attributes":{"name":"Dave"}}]}';
138
        $this->assertSame($expectedJson, $scope->toJson());
139
    }
140
141
    public function testSerializingItemResourceWithMetaOnRelationship()
142
    {
143
        $this->manager->parseIncludes('author-with-meta');
144
145
        $bookData = [
146
            'id' => 1,
147
            'title' => 'Foo',
148
            'year' => '1991',
149
            '_author' => [
150
                'id' => 1,
151
                'name' => 'Dave',
152
            ],
153
        ];
154
155
        $resource = new Item($bookData, new JsonApiBookTransformer(), 'books');
156
157
        $scope = new Scope($this->manager, $resource);
158
159
        $expected = [
160
            'data' => [
161
                'type' => 'books',
162
                'id' => '1',
163
                'attributes' => [
164
                    'title' => 'Foo',
165
                    'year' => 1991,
166
                ],
167
                'relationships' => [
168
                    'author-with-meta' => [
169
                        'data' => [
170
                            'type' => 'people',
171
                            'id' => '1',
172
                        ],
173
                        'meta' => [ 'foo' => 'bar' ],
174
                    ],
175
                ],
176
            ],
177
            'included' => [
178
                [
179
                    'type' => 'people',
180
                    'id' => '1',
181
                    'attributes' => [
182
                        'name' => 'Dave',
183
                    ],
184
                ],
185
            ],
186
        ];
187
188
        $this->assertSame($expected, $scope->toArray());
189
190
        $expectedJson = '{"data":{"type":"books","id":"1","attributes":{"title":"Foo","year":1991},"relationships":{"author-with-meta":{"data":{"type":"people","id":"1"},"meta":{"foo":"bar"}}}},"included":[{"type":"people","id":"1","attributes":{"name":"Dave"}}]}';
191
        $this->assertSame($expectedJson, $scope->toJson());
192
    }
193
194 View Code Duplication
    public function testSerializingItemResourceWithHasOneDasherizedInclude()
0 ignored issues
show
Duplication introduced by Ryan Tablada
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...
195
    {
196
        $this->manager->parseIncludes('co-author');
197
198
        $bookData = [
199
            'id' => 1,
200
            'title' => 'Foo',
201
            'year' => '1991',
202
            '_author' => [
203
                'id' => 1,
204
                'name' => 'Dave',
205
            ],
206
            '_co_author' => [
207
                'id' => 2,
208
                'name' => 'Jim',
209
            ],
210
        ];
211
212
        $resource = new Item($bookData, new JsonApiBookTransformer(), 'books');
213
214
        $scope = new Scope($this->manager, $resource);
215
216
        $expected = [
217
            'data' => [
218
                'type' => 'books',
219
                'id' => '1',
220
                'attributes' => [
221
                    'title' => 'Foo',
222
                    'year' => 1991,
223
                ],
224
                'relationships' => [
225
                    'co-author' => [
226
                        'data' => [
227
                            'type' => 'people',
228
                            'id' => '2',
229
                        ],
230
                    ],
231
                ],
232
            ],
233
            'included' => [
234
                [
235
                    'type' => 'people',
236
                    'id' => '2',
237
                    'attributes' => [
238
                        'name' => 'Jim',
239
                    ],
240
                ],
241
            ],
242
        ];
243
244
        $this->assertSame($expected, $scope->toArray());
245
246
        $expectedJson = '{"data":{"type":"books","id":"1","attributes":{"title":"Foo","year":1991},"relationships":{"co-author":{"data":{"type":"people","id":"2"}}}},"included":[{"type":"people","id":"2","attributes":{"name":"Jim"}}]}';
247
        $this->assertSame($expectedJson, $scope->toJson());
248
    }
249
250
    public function testSerializingItemResourceWithEmptyHasOneInclude()
251
    {
252
        $this->manager->parseIncludes('author');
253
254
        $bookData = [
255
            'id' => 1,
256
            'title' => 'Foo',
257
            'year' => '1991',
258
            '_author' => null,
259
        ];
260
261
        $resource = new Item($bookData, new JsonApiBookTransformer(), 'books');
262
263
        $scope = new Scope($this->manager, $resource);
264
265
        $expected = [
266
            'data' => [
267
                'type' => 'books',
268
                'id' => '1',
269
                'attributes' => [
270
                    'title' => 'Foo',
271
                    'year' => 1991,
272
                ],
273
                'relationships' => [
274
                    'author' => [
275
                        'data' => null,
276
                    ],
277
                ],
278
            ],
279
        ];
280
281
        $this->assertSame($expected, $scope->toArray());
282
283
        $expectedJson = '{"data":{"type":"books","id":"1","attributes":{"title":"Foo","year":1991},"relationships":{"author":{"data":null}}}}';
284
        $this->assertSame($expectedJson, $scope->toJson());
285
    }
286
287
    public function testSerializingItemResourceWithHasManyInclude()
288
    {
289
        $this->manager->parseIncludes('published');
290
291
        $authorData = [
292
            'id' => 1,
293
            'name' => 'Dave',
294
            '_published' => [
295
                [
296
                    'id' => 1,
297
                    'title' => 'Foo',
298
                    'year' => '1991',
299
                ],
300
                [
301
                    'id' => 2,
302
                    'title' => 'Bar',
303
                    'year' => '2015',
304
                ],
305
            ],
306
        ];
307
308
        $resource = new Item($authorData, new JsonApiAuthorTransformer(), 'people');
309
310
        $scope = new Scope($this->manager, $resource);
311
312
        $expected = [
313
            'data' => [
314
                'type' => 'people',
315
                'id' => '1',
316
                'attributes' => [
317
                    'name' => 'Dave',
318
                ],
319
                'relationships' => [
320
                    'published' => [
321
                        'data' => [
322
                            [
323
                                'type' => 'books',
324
                                'id' => 1,
325
                            ],
326
                            [
327
                                'type' => 'books',
328
                                'id' => 2,
329
                            ],
330
                        ],
331
                    ],
332
                ],
333
            ],
334
            'included' => [
335
                [
336
                    'type' => 'books',
337
                    'id' => '1',
338
                    'attributes' => [
339
                        'title' => 'Foo',
340
                        'year' => 1991,
341
                    ],
342
                ],
343
                [
344
                    'type' => 'books',
345
                    'id' => '2',
346
                    'attributes' => [
347
                        'title' => 'Bar',
348
                        'year' => 2015,
349
                    ],
350
                ],
351
            ],
352
        ];
353
354
        $this->assertEquals($expected, $scope->toArray());
355
356
        $expectedJson = '{"data":{"type":"people","id":"1","attributes":{"name":"Dave"},"relationships":{"published":{"data":[{"type":"books","id":"1"},{"type":"books","id":"2"}]}}},"included":[{"type":"books","id":"1","attributes":{"title":"Foo","year":1991}},{"type":"books","id":"2","attributes":{"title":"Bar","year":2015}}]}';
357
        $this->assertSame($expectedJson, $scope->toJson());
358
    }
359
360
    public function testSerializingItemResourceWithEmptyHasManyInclude()
361
    {
362
        $this->manager->parseIncludes('published');
363
364
        $authorData = [
365
            'id' => 1,
366
            'name' => 'Dave',
367
            '_published' => [],
368
        ];
369
370
        $resource = new Item($authorData, new JsonApiAuthorTransformer(), 'people');
371
372
        $scope = new Scope($this->manager, $resource);
373
374
        $expected = [
375
            'data' => [
376
                'type' => 'people',
377
                'id' => '1',
378
                'attributes' => [
379
                    'name' => 'Dave',
380
                ],
381
                'relationships' => [
382
                    'published' => [
383
                        'data' => [],
384
                    ],
385
                ],
386
            ],
387
        ];
388
389
        $this->assertSame($expected, $scope->toArray());
390
391
        $expectedJson = '{"data":{"type":"people","id":"1","attributes":{"name":"Dave"},"relationships":{"published":{"data":[]}}}}';
392
        $this->assertSame($expectedJson, $scope->toJson());
393
    }
394
395
    public function testSerializingItemResourceWithoutIncludes()
396
    {
397
        $bookData = [
398
            'id' => 1,
399
            'title' => 'Foo',
400
            'year' => '1991',
401
            '_author' => [
402
                'id' => 1,
403
                'name' => 'Dave',
404
            ],
405
        ];
406
407
        $resource = new Item($bookData, new JsonApiBookTransformer(), 'books');
408
409
        $scope = new Scope($this->manager, $resource);
410
411
        $expected = [
412
            'data' => [
413
                'type' => 'books',
414
                'id' => '1',
415
                'attributes' => [
416
                    'title' => 'Foo',
417
                    'year' => 1991,
418
                ],
419
            ],
420
        ];
421
422
        $this->assertSame($expected, $scope->toArray());
423
424
        $expectedJson = '{"data":{"type":"books","id":"1","attributes":{"title":"Foo","year":1991}}}';
425
        $this->assertSame($expectedJson, $scope->toJson());
426
    }
427
428
    public function testSerializingItemResourceWithMeta()
429
    {
430
        $bookData = [
431
            'id' => 1,
432
            'title' => 'Foo',
433
            'year' => '1991',
434
            '_author' => [
435
                'id' => 1,
436
                'name' => 'Dave',
437
            ],
438
        ];
439
440
        $resource = new Item($bookData, new JsonApiBookTransformer(), 'books');
441
        $resource->setMetaValue('foo', 'bar');
442
443
        $scope = new Scope($this->manager, $resource);
444
445
        $expected = [
446
            'data' => [
447
                'type' => 'books',
448
                'id' => '1',
449
                'attributes' => [
450
                    'title' => 'Foo',
451
                    'year' => 1991,
452
                ],
453
            ],
454
            'meta' => [
455
                'foo' => 'bar',
456
            ],
457
        ];
458
459
        $this->assertSame($expected, $scope->toArray());
460
461
        $expectedJson = '{"data":{"type":"books","id":"1","attributes":{"title":"Foo","year":1991}},"meta":{"foo":"bar"}}';
462
        $this->assertSame($expectedJson, $scope->toJson());
463
    }
464
465
    public function testSerializingItemResourceWithMetaInBody()
466
    {
467
        $bookData = [
468
            'id' => 1,
469
            'title' => 'Foo',
470
            'year' => '1991',
471
            '_author' => [
472
                'id' => 1,
473
                'name' => 'Dave',
474
            ],
475
            'meta' => [
476
                'something' => 'something'
477
            ]
478
        ];
479
480
        $resource = new Item($bookData, new JsonApiBookTransformer(), 'books');
481
        $resource->setMetaValue('foo', 'bar');
482
483
        $scope = new Scope($this->manager, $resource);
484
485
        $expected = [
486
            'data' => [
487
                'type' => 'books',
488
                'id' => '1',
489
                'attributes' => [
490
                    'title' => 'Foo',
491
                    'year' => 1991,
492
                ],
493
                'meta' => [
494
                    'something' => 'something'
495
                ]
496
            ],
497
            'meta' => [
498
                'foo' => 'bar'
499
            ],
500
        ];
501
502
        $this->assertSame($expected, $scope->toArray());
503
504
        $expectedJson = '{"data":{"type":"books","id":"1","attributes":{"title":"Foo","year":1991},"meta":{"something":"something"}},"meta":{"foo":"bar"}}';
505
        $this->assertSame($expectedJson, $scope->toJson());
506
    }
507
508 View Code Duplication
    public function testSerializingCollectionResourceWithoutIncludes()
0 ignored issues
show
Duplication introduced by Felix Kiss
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...
509
    {
510
        $booksData = [
511
            [
512
                'id' => 1,
513
                'title' => 'Foo',
514
                'year' => '1991',
515
                '_author' => [
516
                    'id' => 1,
517
                    'name' => 'Dave',
518
                ],
519
            ],
520
            [
521
                'id' => 2,
522
                'title' => 'Bar',
523
                'year' => '1997',
524
                '_author' => [
525
                    'id' => 2,
526
                    'name' => 'Bob',
527
                ],
528
            ],
529
        ];
530
531
        $resource = new Collection($booksData, new JsonApiBookTransformer(), 'books');
532
        $scope = new Scope($this->manager, $resource);
533
534
        $expected = [
535
            'data' => [
536
                [
537
                    'type' => 'books',
538
                    'id' => '1',
539
                    'attributes' => [
540
                        'title' => 'Foo',
541
                        'year' => 1991,
542
                    ],
543
                ],
544
                [
545
                    'type' => 'books',
546
                    'id' => '2',
547
                    'attributes' => [
548
                        'title' => 'Bar',
549
                        'year' => 1997,
550
                    ],
551
                ],
552
            ],
553
        ];
554
555
        $this->assertSame($expected, $scope->toArray());
556
557
        $expectedJson = '{"data":[{"type":"books","id":"1","attributes":{"title":"Foo","year":1991}},{"type":"books","id":"2","attributes":{"title":"Bar","year":1997}}]}';
558
        $this->assertSame($expectedJson, $scope->toJson());
559
    }
560
561 View Code Duplication
    public function testSerializingCollectionResourceWithHasOneInclude()
0 ignored issues
show
Duplication introduced by Felix Kiss
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...
562
    {
563
        $this->manager->parseIncludes('author');
564
565
        $booksData = [
566
            [
567
                'id' => 1,
568
                'title' => 'Foo',
569
                'year' => '1991',
570
                '_author' => [
571
                    'id' => 1,
572
                    'name' => 'Dave',
573
                ],
574
            ],
575
            [
576
                'id' => 2,
577
                'title' => 'Bar',
578
                'year' => '1997',
579
                '_author' => [
580
                    'id' => 2,
581
                    'name' => 'Bob',
582
                ],
583
            ],
584
        ];
585
586
        $resource = new Collection($booksData, new JsonApiBookTransformer(), 'books');
587
        $scope = new Scope($this->manager, $resource);
588
589
        $expected = [
590
            'data' => [
591
                [
592
                    'type' => 'books',
593
                    'id' => '1',
594
                    'attributes' => [
595
                        'title' => 'Foo',
596
                        'year' => 1991,
597
                    ],
598
                    'relationships' => [
599
                        'author' => [
600
                            'data' => [
601
                                'type' => 'people',
602
                                'id' => '1',
603
                            ],
604
                        ],
605
                    ],
606
                ],
607
                [
608
                    'type' => 'books',
609
                    'id' => '2',
610
                    'attributes' => [
611
                        'title' => 'Bar',
612
                        'year' => 1997,
613
                    ],
614
                    'relationships' => [
615
                        'author' => [
616
                            'data' => [
617
                                'type' => 'people',
618
                                'id' => '2',
619
                            ],
620
                        ],
621
                    ],
622
                ],
623
            ],
624
            'included' => [
625
                [
626
                    'type' => 'people',
627
                    'id' => '1',
628
                    'attributes' => [
629
                        'name' => 'Dave',
630
                    ],
631
                ],
632
                [
633
                    'type' => 'people',
634
                    'id' => '2',
635
                    'attributes' => [
636
                        'name' => 'Bob',
637
                    ],
638
                ],
639
            ],
640
        ];
641
642
        $this->assertSame($expected, $scope->toArray());
643
644
        $expectedJson = '{"data":[{"type":"books","id":"1","attributes":{"title":"Foo","year":1991},"relationships":{"author":{"data":{"type":"people","id":"1"}}}},{"type":"books","id":"2","attributes":{"title":"Bar","year":1997},"relationships":{"author":{"data":{"type":"people","id":"2"}}}}],"included":[{"type":"people","id":"1","attributes":{"name":"Dave"}},{"type":"people","id":"2","attributes":{"name":"Bob"}}]}';
645
        $this->assertSame($expectedJson, $scope->toJson());
646
    }
647
648
    public function testSerializingCollectionResourceWithEmptyHasOneInclude()
649
    {
650
        $this->manager->parseIncludes('author');
651
652
        $booksData = [
653
            [
654
                'id' => 1,
655
                'title' => 'Foo',
656
                'year' => '1991',
657
                '_author' => null,
658
            ],
659
            [
660
                'id' => 2,
661
                'title' => 'Bar',
662
                'year' => '1997',
663
                '_author' => [
664
                    'id' => 2,
665
                    'name' => 'Bob',
666
                ],
667
            ],
668
        ];
669
670
        $resource = new Collection($booksData, new JsonApiBookTransformer(), 'books');
671
        $scope = new Scope($this->manager, $resource);
672
673
        $expected = [
674
            'data' => [
675
                [
676
                    'type' => 'books',
677
                    'id' => '1',
678
                    'attributes' => [
679
                        'title' => 'Foo',
680
                        'year' => 1991,
681
                    ],
682
                    'relationships' => [
683
                        'author' => [
684
                            'data' => null,
685
                        ],
686
                    ],
687
                ],
688
                [
689
                    'type' => 'books',
690
                    'id' => '2',
691
                    'attributes' => [
692
                        'title' => 'Bar',
693
                        'year' => 1997,
694
                    ],
695
                    'relationships' => [
696
                        'author' => [
697
                            'data' => [
698
                                'type' => 'people',
699
                                'id' => '2',
700
                            ],
701
                        ],
702
                    ],
703
                ],
704
            ],
705
            'included' => [
706
                [
707
                    'type' => 'people',
708
                    'id' => '2',
709
                    'attributes' => [
710
                        'name' => 'Bob',
711
                    ],
712
                ],
713
            ],
714
        ];
715
716
        $this->assertSame($expected, $scope->toArray());
717
718
        $expectedJson = '{"data":[{"type":"books","id":"1","attributes":{"title":"Foo","year":1991},"relationships":{"author":{"data":null}}},{"type":"books","id":"2","attributes":{"title":"Bar","year":1997},"relationships":{"author":{"data":{"type":"people","id":"2"}}}}],"included":[{"type":"people","id":"2","attributes":{"name":"Bob"}}]}';
719
        $this->assertSame($expectedJson, $scope->toJson());
720
    }
721
722
    public function testSerializingCollectionResourceWithHasManyInclude()
723
    {
724
        $this->manager->parseIncludes('published');
725
726
        $authorsData = [
727
            [
728
                'id' => 1,
729
                'name' => 'Dave',
730
                '_published' => [
731
                    [
732
                        'id' => 1,
733
                        'title' => 'Foo',
734
                        'year' => '1991',
735
                    ],
736
                    [
737
                        'id' => 2,
738
                        'title' => 'Bar',
739
                        'year' => '2015',
740
                    ],
741
                ],
742
            ],
743
            [
744
                'id' => 2,
745
                'name' => 'Bob',
746
                '_published' => [
747
                    [
748
                        'id' => 3,
749
                        'title' => 'Baz',
750
                        'year' => '1995',
751
                    ],
752
                    [
753
                        'id' => 4,
754
                        'title' => 'Quux',
755
                        'year' => '2000',
756
                    ],
757
                ],
758
            ],
759
        ];
760
761
        $resource = new Collection($authorsData, new JsonApiAuthorTransformer(), 'people');
762
        $scope = new Scope($this->manager, $resource);
763
764
        $expected = [
765
            'data' => [
766
                [
767
                    'type' => 'people',
768
                    'id' => '1',
769
                    'attributes' => [
770
                        'name' => 'Dave',
771
                    ],
772
                    'relationships' => [
773
                        'published' => [
774
                            'data' => [
775
                                [
776
                                    'type' => 'books',
777
                                    'id' => 1,
778
                                ],
779
                                [
780
                                    'type' => 'books',
781
                                    'id' => 2,
782
                                ],
783
                            ],
784
                        ],
785
                    ],
786
                ],
787
                [
788
                    'type' => 'people',
789
                    'id' => '2',
790
                    'attributes' => [
791
                        'name' => 'Bob',
792
                    ],
793
                    'relationships' => [
794
                        'published' => [
795
                            'data' => [
796
                                [
797
                                    'type' => 'books',
798
                                    'id' => 3,
799
                                ],
800
                                [
801
                                    'type' => 'books',
802
                                    'id' => 4,
803
                                ],
804
                            ],
805
                        ],
806
                    ],
807
                ],
808
            ],
809
            'included' => [
810
                [
811
                    'type' => 'books',
812
                    'id' => '1',
813
                    'attributes' => [
814
                        'title' => 'Foo',
815
                        'year' => 1991,
816
                    ],
817
                ],
818
                [
819
                    'type' => 'books',
820
                    'id' => '2',
821
                    'attributes' => [
822
                        'title' => 'Bar',
823
                        'year' => 2015,
824
                    ],
825
                ],
826
                [
827
                    'type' => 'books',
828
                    'id' => '3',
829
                    'attributes' => [
830
                        'title' => 'Baz',
831
                        'year' => 1995,
832
                    ],
833
                ],
834
                [
835
                    'type' => 'books',
836
                    'id' => '4',
837
                    'attributes' => [
838
                        'title' => 'Quux',
839
                        'year' => 2000,
840
                    ],
841
                ],
842
            ],
843
        ];
844
845
        $this->assertEquals($expected, $scope->toArray());
846
847
        $expectedJson = '{"data":[{"type":"people","id":"1","attributes":{"name":"Dave"},"relationships":{"published":{"data":[{"type":"books","id":"1"},{"type":"books","id":"2"}]}}},{"type":"people","id":"2","attributes":{"name":"Bob"},"relationships":{"published":{"data":[{"type":"books","id":"3"},{"type":"books","id":"4"}]}}}],"included":[{"type":"books","id":"1","attributes":{"title":"Foo","year":1991}},{"type":"books","id":"2","attributes":{"title":"Bar","year":2015}},{"type":"books","id":"3","attributes":{"title":"Baz","year":1995}},{"type":"books","id":"4","attributes":{"title":"Quux","year":2000}}]}';
848
        $this->assertSame($expectedJson, $scope->toJson());
849
    }
850
851
    public function testSerializingCollectionResourceWithEmptyHasManyInclude()
852
    {
853
        $this->manager->parseIncludes('published');
854
855
        $authorsData = [
856
            [
857
                'id' => 1,
858
                'name' => 'Dave',
859
                '_published' => [],
860
            ],
861
            [
862
                'id' => 2,
863
                'name' => 'Bob',
864
                '_published' => [
865
                    [
866
                        'id' => 3,
867
                        'title' => 'Baz',
868
                        'year' => '1995',
869
                    ],
870
                ],
871
            ],
872
        ];
873
874
        $resource = new Collection($authorsData, new JsonApiAuthorTransformer(), 'people');
875
        $scope = new Scope($this->manager, $resource);
876
877
        $expected = [
878
            'data' => [
879
                [
880
                    'type' => 'people',
881
                    'id' => '1',
882
                    'attributes' => [
883
                        'name' => 'Dave',
884
                    ],
885
                    'relationships' => [
886
                        'published' => [
887
                            'data' => [],
888
                        ],
889
                    ],
890
                ],
891
                [
892
                    'type' => 'people',
893
                    'id' => '2',
894
                    'attributes' => [
895
                        'name' => 'Bob',
896
                    ],
897
                    'relationships' => [
898
                        'published' => [
899
                            'data' => [
900
                                [
901
                                    'type' => 'books',
902
                                    'id' => 3,
903
                                ],
904
                            ],
905
                        ],
906
                    ],
907
                ],
908
            ],
909
            'included' => [
910
                [
911
                    'type' => 'books',
912
                    'id' => '3',
913
                    'attributes' => [
914
                        'title' => 'Baz',
915
                        'year' => 1995,
916
                    ],
917
                ],
918
            ],
919
        ];
920
921
        $this->assertEquals($expected, $scope->toArray());
922
923
        $expectedJson = '{"data":[{"type":"people","id":"1","attributes":{"name":"Dave"},"relationships":{"published":{"data":[]}}},{"type":"people","id":"2","attributes":{"name":"Bob"},"relationships":{"published":{"data":[{"type":"books","id":"3"}]}}}],"included":[{"type":"books","id":"3","attributes":{"title":"Baz","year":1995}}]}';
924
        $this->assertSame($expectedJson, $scope->toJson());
925
    }
926
927 View Code Duplication
    public function testSerializingCollectionResourceWithMeta()
0 ignored issues
show
Duplication introduced by Phil Sturgeon
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...
928
    {
929
        $booksData = [
930
            [
931
                'id' => 1,
932
                'title' => 'Foo',
933
                'year' => '1991',
934
                '_author' => [
935
                    'name' => 'Dave',
936
                ],
937
            ],
938
            [
939
                'id' => 2,
940
                'title' => 'Bar',
941
                'year' => '1997',
942
                '_author' => [
943
                    'name' => 'Bob',
944
                ],
945
            ],
946
        ];
947
948
        $resource = new Collection($booksData, new JsonApiBookTransformer(), 'books');
949
        $resource->setMetaValue('foo', 'bar');
950
951
        $scope = new Scope($this->manager, $resource);
952
953
        $expected = [
954
            'data' => [
955
                [
956
                    'type' => 'books',
957
                    'id' => '1',
958
                    'attributes' => [
959
                        'title' => 'Foo',
960
                        'year' => 1991,
961
                    ],
962
                ],
963
                [
964
                    'type' => 'books',
965
                    'id' => '2',
966
                    'attributes' => [
967
                        'title' => 'Bar',
968
                        'year' => 1997,
969
                    ],
970
                ],
971
            ],
972
            'meta' => [
973
                'foo' => 'bar',
974
            ],
975
        ];
976
977
        $this->assertSame($expected, $scope->toArray());
978
979
        $expectedJson = '{"data":[{"type":"books","id":"1","attributes":{"title":"Foo","year":1991}},{"type":"books","id":"2","attributes":{"title":"Bar","year":1997}}],"meta":{"foo":"bar"}}';
980
        $this->assertSame($expectedJson, $scope->toJson());
981
    }
982
983
    public function testSerializingCollectionResourceWithDuplicatedIncludeData()
984
    {
985
        $this->manager->parseIncludes('author');
986
987
        $booksData = [
988
            [
989
                'id' => 1,
990
                'title' => 'Foo',
991
                'year' => '1991',
992
                '_author' => [
993
                    'id' => 1,
994
                    'name' => 'Dave',
995
                ],
996
            ],
997
            [
998
                'id' => 2,
999
                'title' => 'Bar',
1000
                'year' => '1997',
1001
                '_author' => [
1002
                    'id' => 1,
1003
                    'name' => 'Dave',
1004
                ],
1005
            ],
1006
        ];
1007
1008
        $resource = new Collection($booksData, new JsonApiBookTransformer(), 'books');
1009
        $scope = new Scope($this->manager, $resource);
1010
1011
        $expected = [
1012
            'data' => [
1013
                [
1014
                    'type' => 'books',
1015
                    'id' => '1',
1016
                    'attributes' => [
1017
                        'title' => 'Foo',
1018
                        'year' => 1991,
1019
                    ],
1020
                    'relationships' => [
1021
                        'author' => [
1022
                            'data' => [
1023
                                'type' => 'people',
1024
                                'id' => '1',
1025
                            ],
1026
                        ],
1027
                    ],
1028
                ],
1029
                [
1030
                    'type' => 'books',
1031
                    'id' => '2',
1032
                    'attributes' => [
1033
                        'title' => 'Bar',
1034
                        'year' => 1997,
1035
                    ],
1036
                    'relationships' => [
1037
                        'author' => [
1038
                            'data' => [
1039
                                'type' => 'people',
1040
                                'id' => '1',
1041
                            ],
1042
                        ],
1043
                    ],
1044
                ],
1045
            ],
1046
            'included' => [
1047
                [
1048
                    'type' => 'people',
1049
                    'id' => '1',
1050
                    'attributes' => [
1051
                        'name' => 'Dave',
1052
                    ],
1053
                ],
1054
            ],
1055
        ];
1056
1057
        $this->assertSame($expected, $scope->toArray());
1058
1059
        $expectedJson = '{"data":[{"type":"books","id":"1","attributes":{"title":"Foo","year":1991},"relationships":{"author":{"data":{"type":"people","id":"1"}}}},{"type":"books","id":"2","attributes":{"title":"Bar","year":1997},"relationships":{"author":{"data":{"type":"people","id":"1"}}}}],"included":[{"type":"people","id":"1","attributes":{"name":"Dave"}}]}';
1060
        $this->assertSame($expectedJson, $scope->toJson());
1061
    }
1062
1063 View Code Duplication
    public function testSerializingItemResourceWithNestedIncludes()
0 ignored issues
show
Duplication introduced by Felix Kiss
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...
1064
    {
1065
        $this->manager->parseIncludes(['author', 'author.published']);
1066
1067
        $bookData = [
1068
            'id' => 1,
1069
            'title' => 'Foo',
1070
            'year' => '1991',
1071
            '_author' => [
1072
                'id' => 1,
1073
                'name' => 'Dave',
1074
                '_published' => [
1075
                    [
1076
                        'id' => 1,
1077
                        'title' => 'Foo',
1078
                        'year' => '1991',
1079
                    ],
1080
                    [
1081
                        'id' => 2,
1082
                        'title' => 'Bar',
1083
                        'year' => '2015',
1084
                    ],
1085
                ],
1086
            ],
1087
        ];
1088
1089
        $resource = new Item($bookData, new JsonApiBookTransformer(), 'books');
1090
1091
        $scope = new Scope($this->manager, $resource);
1092
1093
        $expected = [
1094
            'data' => [
1095
                'type' => 'books',
1096
                'id' => '1',
1097
                'attributes' => [
1098
                    'title' => 'Foo',
1099
                    'year' => 1991,
1100
                ],
1101
                'relationships' => [
1102
                    'author' => [
1103
                        'data' => [
1104
                            'type' => 'people',
1105
                            'id' => '1',
1106
                        ],
1107
                    ],
1108
                ],
1109
            ],
1110
            'included' => [
1111
                [
1112
                    'type' => 'books',
1113
                    'id' => '2',
1114
                    'attributes' => [
1115
                        'title' => 'Bar',
1116
                        'year' => 2015,
1117
                    ],
1118
                ],
1119
                [
1120
                    'type' => 'people',
1121
                    'id' => '1',
1122
                    'attributes' => [
1123
                        'name' => 'Dave',
1124
                    ],
1125
                    'relationships' => [
1126
                        'published' => [
1127
                            'data' => [
1128
                                [
1129
                                    'type' => 'books',
1130
                                    'id' => '1',
1131
                                ],
1132
                                [
1133
                                    'type' => 'books',
1134
                                    'id' => '2',
1135
                                ],
1136
                            ],
1137
                        ],
1138
                    ],
1139
                ],
1140
            ],
1141
        ];
1142
1143
        $this->assertSame($expected, $scope->toArray());
1144
1145
        $expectedJson = '{"data":{"type":"books","id":"1","attributes":{"title":"Foo","year":1991},"relationships":{"author":{"data":{"type":"people","id":"1"}}}},"included":[{"type":"books","id":"2","attributes":{"title":"Bar","year":2015}},{"type&q