Issues (19)

Security Analysis    no request data  

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

test/JsonApiSerializerTest.php (9 issues)

Upgrade to new PHP Analysis Engine

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

1
<?php
2
3
namespace League\Fractal\Test;
4
5
use InvalidArgumentException;
6
use League\Fractal\Manager;
7
use League\Fractal\Resource\Collection;
8
use League\Fractal\Resource\Item;
9
use League\Fractal\Scope;
10
use League\Fractal\Serializer\JsonApiSerializer;
11
use League\Fractal\Test\Stub\Transformer\JsonApiAuthorTransformer;
12
use League\Fractal\Test\Stub\Transformer\JsonApiBookTransformer;
13
use League\Fractal\Test\Stub\Transformer\JsonApiEmptyTransformer;
14
use Mockery;
15
16
class JsonApiSerializerTest extends TestCase
17
{
18
    private $manager;
19
20
    public function setUp(): void
21
    {
22
        $this->manager = new Manager();
23
        $this->manager->setSerializer(new JsonApiSerializer());
24
    }
25
26
    public function testSerializeCollectionWithExtraMeta()
27
    {
28
        $booksData = [
29
            [
30
                'id' => 1,
31
                'title' => 'Foo',
32
                'year' => '1991',
33
                '_author' => [
34
                    'id' => 1,
35
                    'name' => 'Dave',
36
                ],
37
                'meta' => [
38
                    'foo' => 'bar'
39
                ]
40
            ],
41
            [
42
                'id' => 2,
43
                'title' => 'Bar',
44
                'year' => '1997',
45
                '_author' => [
46
                    'id' => 2,
47
                    'name' => 'Bob',
48
                ],
49
                'meta' => [
50
                    'bar' => 'baz'
51
                ]
52
            ],
53
        ];
54
55
        $resource = new Collection($booksData, new JsonApiBookTransformer(), 'books');
56
        $scope = new Scope($this->manager, $resource);
57
58
        $expected = [
59
            'data' => [
60
                [
61
                    'type' => 'books',
62
                    'id' => '1',
63
                    'attributes' => [
64
                        'title' => 'Foo',
65
                        'year' => 1991,
66
                    ],
67
                    'meta' => [
68
                        'foo' => 'bar'
69
                    ]
70
                ],
71
                [
72
                    'type' => 'books',
73
                    'id' => '2',
74
                    'attributes' => [
75
                        'title' => 'Bar',
76
                        'year' => 1997,
77
                    ],
78
                    'meta' => [
79
                        'bar' => 'baz'
80
                    ]
81
                ],
82
            ],
83
        ];
84
85
        $this->assertSame($expected, $scope->toArray());
86
87
        $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"}}]}';
88
        $this->assertSame($expectedJson, $scope->toJson());
89
    }
90
91
    public function testSerializingItemResourceWithHasOneInclude()
92
    {
93
        $this->manager->parseIncludes('author');
94
95
        $bookData = [
96
            'id' => 1,
97
            'title' => 'Foo',
98
            'year' => '1991',
99
            '_author' => [
100
                'id' => 1,
101
                'name' => 'Dave',
102
            ],
103
        ];
104
105
        $resource = new Item($bookData, new JsonApiBookTransformer(), 'books');
106
107
        $scope = new Scope($this->manager, $resource);
108
109
        $expected = [
110
            'data' => [
111
                'type' => 'books',
112
                'id' => '1',
113
                'attributes' => [
114
                    'title' => 'Foo',
115
                    'year' => 1991,
116
                ],
117
                'relationships' => [
118
                    'author' => [
119
                        'data' => [
120
                            'type' => 'people',
121
                            'id' => '1',
122
                        ],
123
                    ],
124
                ],
125
            ],
126
            'included' => [
127
                [
128
                    'type' => 'people',
129
                    'id' => '1',
130
                    'attributes' => [
131
                        'name' => 'Dave',
132
                    ],
133
                ],
134
            ],
135
        ];
136
137
        $this->assertSame($expected, $scope->toArray());
138
139
        $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"}}]}';
140
        $this->assertSame($expectedJson, $scope->toJson());
141
    }
142
143
    public function testSerializingItemResourceWithMetaOnRelationship()
144
    {
145
        $this->manager->parseIncludes('author-with-meta');
146
147
        $bookData = [
148
            'id' => 1,
149
            'title' => 'Foo',
150
            'year' => '1991',
151
            '_author' => [
152
                'id' => 1,
153
                'name' => 'Dave',
154
            ],
155
        ];
156
157
        $resource = new Item($bookData, new JsonApiBookTransformer(), 'books');
158
159
        $scope = new Scope($this->manager, $resource);
160
161
        $expected = [
162
            'data' => [
163
                'type' => 'books',
164
                'id' => '1',
165
                'attributes' => [
166
                    'title' => 'Foo',
167
                    'year' => 1991,
168
                ],
169
                'relationships' => [
170
                    'author-with-meta' => [
171
                        'data' => [
172
                            'type' => 'people',
173
                            'id' => '1',
174
                        ],
175
                        'meta' => [ 'foo' => 'bar' ],
176
                    ],
177
                ],
178
            ],
179
            'included' => [
180
                [
181
                    'type' => 'people',
182
                    'id' => '1',
183
                    'attributes' => [
184
                        'name' => 'Dave',
185
                    ],
186
                ],
187
            ],
188
        ];
189
190
        $this->assertSame($expected, $scope->toArray());
191
192
        $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"}}]}';
193
        $this->assertSame($expectedJson, $scope->toJson());
194
    }
195
196 View Code Duplication
    public function testSerializingItemResourceWithHasOneDasherizedInclude()
0 ignored issues
show
This method seems to be duplicated in your project.

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

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

Loading history...
197
    {
198
        $this->manager->parseIncludes('co-author');
199
200
        $bookData = [
201
            'id' => 1,
202
            'title' => 'Foo',
203
            'year' => '1991',
204
            '_author' => [
205
                'id' => 1,
206
                'name' => 'Dave',
207
            ],
208
            '_co_author' => [
209
                'id' => 2,
210
                'name' => 'Jim',
211
            ],
212
        ];
213
214
        $resource = new Item($bookData, new JsonApiBookTransformer(), 'books');
215
216
        $scope = new Scope($this->manager, $resource);
217
218
        $expected = [
219
            'data' => [
220
                'type' => 'books',
221
                'id' => '1',
222
                'attributes' => [
223
                    'title' => 'Foo',
224
                    'year' => 1991,
225
                ],
226
                'relationships' => [
227
                    'co-author' => [
228
                        'data' => [
229
                            'type' => 'people',
230
                            'id' => '2',
231
                        ],
232
                    ],
233
                ],
234
            ],
235
            'included' => [
236
                [
237
                    'type' => 'people',
238
                    'id' => '2',
239
                    'attributes' => [
240
                        'name' => 'Jim',
241
                    ],
242
                ],
243
            ],
244
        ];
245
246
        $this->assertSame($expected, $scope->toArray());
247
248
        $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"}}]}';
249
        $this->assertSame($expectedJson, $scope->toJson());
250
    }
251
252
    public function testSerializingItemResourceWithEmptyHasOneInclude()
253
    {
254
        $this->manager->parseIncludes('author');
255
256
        $bookData = [
257
            'id' => 1,
258
            'title' => 'Foo',
259
            'year' => '1991',
260
            '_author' => null,
261
        ];
262
263
        $resource = new Item($bookData, new JsonApiBookTransformer(), 'books');
264
265
        $scope = new Scope($this->manager, $resource);
266
267
        $expected = [
268
            'data' => [
269
                'type' => 'books',
270
                'id' => '1',
271
                'attributes' => [
272
                    'title' => 'Foo',
273
                    'year' => 1991,
274
                ],
275
                'relationships' => [
276
                    'author' => [
277
                        'data' => null,
278
                    ],
279
                ],
280
            ],
281
        ];
282
283
        $this->assertSame($expected, $scope->toArray());
284
285
        $expectedJson = '{"data":{"type":"books","id":"1","attributes":{"title":"Foo","year":1991},"relationships":{"author":{"data":null}}}}';
286
        $this->assertSame($expectedJson, $scope->toJson());
287
    }
288
289
    public function testSerializingItemResourceWithHasManyInclude()
290
    {
291
        $this->manager->parseIncludes('published');
292
293
        $authorData = [
294
            'id' => 1,
295
            'name' => 'Dave',
296
            '_published' => [
297
                [
298
                    'id' => 1,
299
                    'title' => 'Foo',
300
                    'year' => '1991',
301
                ],
302
                [
303
                    'id' => 2,
304
                    'title' => 'Bar',
305
                    'year' => '2015',
306
                ],
307
            ],
308
        ];
309
310
        $resource = new Item($authorData, new JsonApiAuthorTransformer(), 'people');
311
312
        $scope = new Scope($this->manager, $resource);
313
314
        $expected = [
315
            'data' => [
316
                'type' => 'people',
317
                'id' => '1',
318
                'attributes' => [
319
                    'name' => 'Dave',
320
                ],
321
                'relationships' => [
322
                    'published' => [
323
                        'data' => [
324
                            [
325
                                'type' => 'books',
326
                                'id' => 1,
327
                            ],
328
                            [
329
                                'type' => 'books',
330
                                'id' => 2,
331
                            ],
332
                        ],
333
                    ],
334
                ],
335
            ],
336
            'included' => [
337
                [
338
                    'type' => 'books',
339
                    'id' => '1',
340
                    'attributes' => [
341
                        'title' => 'Foo',
342
                        'year' => 1991,
343
                    ],
344
                ],
345
                [
346
                    'type' => 'books',
347
                    'id' => '2',
348
                    'attributes' => [
349
                        'title' => 'Bar',
350
                        'year' => 2015,
351
                    ],
352
                ],
353
            ],
354
        ];
355
356
        $this->assertEquals($expected, $scope->toArray());
357
358
        $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}}]}';
359
        $this->assertSame($expectedJson, $scope->toJson());
360
    }
361
362
    public function testSerializingItemResourceWithEmptyHasManyInclude()
363
    {
364
        $this->manager->parseIncludes('published');
365
366
        $authorData = [
367
            'id' => 1,
368
            'name' => 'Dave',
369
            '_published' => [],
370
        ];
371
372
        $resource = new Item($authorData, new JsonApiAuthorTransformer(), 'people');
373
374
        $scope = new Scope($this->manager, $resource);
375
376
        $expected = [
377
            'data' => [
378
                'type' => 'people',
379
                'id' => '1',
380
                'attributes' => [
381
                    'name' => 'Dave',
382
                ],
383
                'relationships' => [
384
                    'published' => [
385
                        'data' => [],
386
                    ],
387
                ],
388
            ],
389
        ];
390
391
        $this->assertSame($expected, $scope->toArray());
392
393
        $expectedJson = '{"data":{"type":"people","id":"1","attributes":{"name":"Dave"},"relationships":{"published":{"data":[]}}}}';
394
        $this->assertSame($expectedJson, $scope->toJson());
395
    }
396
397
    public function testSerializingItemResourceWithoutIncludes()
398
    {
399
        $bookData = [
400
            'id' => 1,
401
            'title' => 'Foo',
402
            'year' => '1991',
403
            '_author' => [
404
                'id' => 1,
405
                'name' => 'Dave',
406
            ],
407
        ];
408
409
        $resource = new Item($bookData, new JsonApiBookTransformer(), 'books');
410
411
        $scope = new Scope($this->manager, $resource);
412
413
        $expected = [
414
            'data' => [
415
                'type' => 'books',
416
                'id' => '1',
417
                'attributes' => [
418
                    'title' => 'Foo',
419
                    'year' => 1991,
420
                ],
421
            ],
422
        ];
423
424
        $this->assertSame($expected, $scope->toArray());
425
426
        $expectedJson = '{"data":{"type":"books","id":"1","attributes":{"title":"Foo","year":1991}}}';
427
        $this->assertSame($expectedJson, $scope->toJson());
428
    }
429
430
    public function testSerializingItemResourceWithMeta()
431
    {
432
        $bookData = [
433
            'id' => 1,
434
            'title' => 'Foo',
435
            'year' => '1991',
436
            '_author' => [
437
                'id' => 1,
438
                'name' => 'Dave',
439
            ],
440
        ];
441
442
        $resource = new Item($bookData, new JsonApiBookTransformer(), 'books');
443
        $resource->setMetaValue('foo', 'bar');
444
445
        $scope = new Scope($this->manager, $resource);
446
447
        $expected = [
448
            'data' => [
449
                'type' => 'books',
450
                'id' => '1',
451
                'attributes' => [
452
                    'title' => 'Foo',
453
                    'year' => 1991,
454
                ],
455
            ],
456
            'meta' => [
457
                'foo' => 'bar',
458
            ],
459
        ];
460
461
        $this->assertSame($expected, $scope->toArray());
462
463
        $expectedJson = '{"data":{"type":"books","id":"1","attributes":{"title":"Foo","year":1991}},"meta":{"foo":"bar"}}';
464
        $this->assertSame($expectedJson, $scope->toJson());
465
    }
466
467
    public function testSerializingItemResourceWithMetaInBody()
468
    {
469
        $bookData = [
470
            'id' => 1,
471
            'title' => 'Foo',
472
            'year' => '1991',
473
            '_author' => [
474
                'id' => 1,
475
                'name' => 'Dave',
476
            ],
477
            'meta' => [
478
                'something' => 'something'
479
            ]
480
        ];
481
482
        $resource = new Item($bookData, new JsonApiBookTransformer(), 'books');
483
        $resource->setMetaValue('foo', 'bar');
484
485
        $scope = new Scope($this->manager, $resource);
486
487
        $expected = [
488
            'data' => [
489
                'type' => 'books',
490
                'id' => '1',
491
                'attributes' => [
492
                    'title' => 'Foo',
493
                    'year' => 1991,
494
                ],
495
                'meta' => [
496
                    'something' => 'something'
497
                ]
498
            ],
499
            'meta' => [
500
                'foo' => 'bar'
501
            ],
502
        ];
503
504
        $this->assertSame($expected, $scope->toArray());
505
506
        $expectedJson = '{"data":{"type":"books","id":"1","attributes":{"title":"Foo","year":1991},"meta":{"something":"something"}},"meta":{"foo":"bar"}}';
507
        $this->assertSame($expectedJson, $scope->toJson());
508
    }
509
510 View Code Duplication
    public function testSerializingCollectionResourceWithoutIncludes()
0 ignored issues
show
This method seems to be duplicated in your project.

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

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

Loading history...
511
    {
512
        $booksData = [
513
            [
514
                'id' => 1,
515
                'title' => 'Foo',
516
                'year' => '1991',
517
                '_author' => [
518
                    'id' => 1,
519
                    'name' => 'Dave',
520
                ],
521
            ],
522
            [
523
                'id' => 2,
524
                'title' => 'Bar',
525
                'year' => '1997',
526
                '_author' => [
527
                    'id' => 2,
528
                    'name' => 'Bob',
529
                ],
530
            ],
531
        ];
532
533
        $resource = new Collection($booksData, new JsonApiBookTransformer(), 'books');
534
        $scope = new Scope($this->manager, $resource);
535
536
        $expected = [
537
            'data' => [
538
                [
539
                    'type' => 'books',
540
                    'id' => '1',
541
                    'attributes' => [
542
                        'title' => 'Foo',
543
                        'year' => 1991,
544
                    ],
545
                ],
546
                [
547
                    'type' => 'books',
548
                    'id' => '2',
549
                    'attributes' => [
550
                        'title' => 'Bar',
551
                        'year' => 1997,
552
                    ],
553
                ],
554
            ],
555
        ];
556
557
        $this->assertSame($expected, $scope->toArray());
558
559
        $expectedJson = '{"data":[{"type":"books","id":"1","attributes":{"title":"Foo","year":1991}},{"type":"books","id":"2","attributes":{"title":"Bar","year":1997}}]}';
560
        $this->assertSame($expectedJson, $scope->toJson());
561
    }
562
563 View Code Duplication
    public function testSerializingCollectionResourceWithHasOneInclude()
0 ignored issues
show
This method seems to be duplicated in your project.

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

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

Loading history...
564
    {
565
        $this->manager->parseIncludes('author');
566
567
        $booksData = [
568
            [
569
                'id' => 1,
570
                'title' => 'Foo',
571
                'year' => '1991',
572
                '_author' => [
573
                    'id' => 1,
574
                    'name' => 'Dave',
575
                ],
576
            ],
577
            [
578
                'id' => 2,
579
                'title' => 'Bar',
580
                'year' => '1997',
581
                '_author' => [
582
                    'id' => 2,
583
                    'name' => 'Bob',
584
                ],
585
            ],
586
        ];
587
588
        $resource = new Collection($booksData, new JsonApiBookTransformer(), 'books');
589
        $scope = new Scope($this->manager, $resource);
590
591
        $expected = [
592
            'data' => [
593
                [
594
                    'type' => 'books',
595
                    'id' => '1',
596
                    'attributes' => [
597
                        'title' => 'Foo',
598
                        'year' => 1991,
599
                    ],
600
                    'relationships' => [
601
                        'author' => [
602
                            'data' => [
603
                                'type' => 'people',
604
                                'id' => '1',
605
                            ],
606
                        ],
607
                    ],
608
                ],
609
                [
610
                    'type' => 'books',
611
                    'id' => '2',
612
                    'attributes' => [
613
                        'title' => 'Bar',
614
                        'year' => 1997,
615
                    ],
616
                    'relationships' => [
617
                        'author' => [
618
                            'data' => [
619
                                'type' => 'people',
620
                                'id' => '2',
621
                            ],
622
                        ],
623
                    ],
624
                ],
625
            ],
626
            'included' => [
627
                [
628
                    'type' => 'people',
629
                    'id' => '1',
630
                    'attributes' => [
631
                        'name' => 'Dave',
632
                    ],
633
                ],
634
                [
635
                    'type' => 'people',
636
                    'id' => '2',
637
                    'attributes' => [
638
                        'name' => 'Bob',
639
                    ],
640
                ],
641
            ],
642
        ];
643
644
        $this->assertSame($expected, $scope->toArray());
645
646
        $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"}}]}';
647
        $this->assertSame($expectedJson, $scope->toJson());
648
    }
649
650
    public function testSerializingCollectionResourceWithEmptyHasOneInclude()
651
    {
652
        $this->manager->parseIncludes('author');
653
654
        $booksData = [
655
            [
656
                'id' => 1,
657
                'title' => 'Foo',
658
                'year' => '1991',
659
                '_author' => null,
660
            ],
661
            [
662
                'id' => 2,
663
                'title' => 'Bar',
664
                'year' => '1997',
665
                '_author' => [
666
                    'id' => 2,
667
                    'name' => 'Bob',
668
                ],
669
            ],
670
        ];
671
672
        $resource = new Collection($booksData, new JsonApiBookTransformer(), 'books');
673
        $scope = new Scope($this->manager, $resource);
674
675
        $expected = [
676
            'data' => [
677
                [
678
                    'type' => 'books',
679
                    'id' => '1',
680
                    'attributes' => [
681
                        'title' => 'Foo',
682
                        'year' => 1991,
683
                    ],
684
                    'relationships' => [
685
                        'author' => [
686
                            'data' => null,
687
                        ],
688
                    ],
689
                ],
690
                [
691
                    'type' => 'books',
692
                    'id' => '2',
693
                    'attributes' => [
694
                        'title' => 'Bar',
695
                        'year' => 1997,
696
                    ],
697
                    'relationships' => [
698
                        'author' => [
699
                            'data' => [
700
                                'type' => 'people',
701
                                'id' => '2',
702
                            ],
703
                        ],
704
                    ],
705
                ],
706
            ],
707
            'included' => [
708
                [
709
                    'type' => 'people',
710
                    'id' => '2',
711
                    'attributes' => [
712
                        'name' => 'Bob',
713
                    ],
714
                ],
715
            ],
716
        ];
717
718
        $this->assertSame($expected, $scope->toArray());
719
720
        $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"}}]}';
721
        $this->assertSame($expectedJson, $scope->toJson());
722
    }
723
724
    public function testSerializingCollectionResourceWithHasManyInclude()
725
    {
726
        $this->manager->parseIncludes('published');
727
728
        $authorsData = [
729
            [
730
                'id' => 1,
731
                'name' => 'Dave',
732
                '_published' => [
733
                    [
734
                        'id' => 1,
735
                        'title' => 'Foo',
736
                        'year' => '1991',
737
                    ],
738
                    [
739
                        'id' => 2,
740
                        'title' => 'Bar',
741
                        'year' => '2015',
742
                    ],
743
                ],
744
            ],
745
            [
746
                'id' => 2,
747
                'name' => 'Bob',
748
                '_published' => [
749
                    [
750
                        'id' => 3,
751
                        'title' => 'Baz',
752
                        'year' => '1995',
753
                    ],
754
                    [
755
                        'id' => 4,
756
                        'title' => 'Quux',
757
                        'year' => '2000',
758
                    ],
759
                ],
760
            ],
761
        ];
762
763
        $resource = new Collection($authorsData, new JsonApiAuthorTransformer(), 'people');
764
        $scope = new Scope($this->manager, $resource);
765
766
        $expected = [
767
            'data' => [
768
                [
769
                    'type' => 'people',
770
                    'id' => '1',
771
                    'attributes' => [
772
                        'name' => 'Dave',
773
                    ],
774
                    'relationships' => [
775
                        'published' => [
776
                            'data' => [
777
                                [
778
                                    'type' => 'books',
779
                                    'id' => 1,
780
                                ],
781
                                [
782
                                    'type' => 'books',
783
                                    'id' => 2,
784
                                ],
785
                            ],
786
                        ],
787
                    ],
788
                ],
789
                [
790
                    'type' => 'people',
791
                    'id' => '2',
792
                    'attributes' => [
793
                        'name' => 'Bob',
794
                    ],
795
                    'relationships' => [
796
                        'published' => [
797
                            'data' => [
798
                                [
799
                                    'type' => 'books',
800
                                    'id' => 3,
801
                                ],
802
                                [
803
                                    'type' => 'books',
804
                                    'id' => 4,
805
                                ],
806
                            ],
807
                        ],
808
                    ],
809
                ],
810
            ],
811
            'included' => [
812
                [
813
                    'type' => 'books',
814
                    'id' => '1',
815
                    'attributes' => [
816
                        'title' => 'Foo',
817
                        'year' => 1991,
818
                    ],
819
                ],
820
                [
821
                    'type' => 'books',
822
                    'id' => '2',
823
                    'attributes' => [
824
                        'title' => 'Bar',
825
                        'year' => 2015,
826
                    ],
827
                ],
828
                [
829
                    'type' => 'books',
830
                    'id' => '3',
831
                    'attributes' => [
832
                        'title' => 'Baz',
833
                        'year' => 1995,
834
                    ],
835
                ],
836
                [
837
                    'type' => 'books',
838
                    'id' => '4',
839
                    'attributes' => [
840
                        'title' => 'Quux',
841
                        'year' => 2000,
842
                    ],
843
                ],
844
            ],
845
        ];
846
847
        $this->assertEquals($expected, $scope->toArray());
848
849
        $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}}]}';
850
        $this->assertSame($expectedJson, $scope->toJson());
851
    }
852
853
    public function testSerializingCollectionResourceWithEmptyHasManyInclude()
854
    {
855
        $this->manager->parseIncludes('published');
856
857
        $authorsData = [
858
            [
859
                'id' => 1,
860
                'name' => 'Dave',
861
                '_published' => [],
862
            ],
863
            [
864
                'id' => 2,
865
                'name' => 'Bob',
866
                '_published' => [
867
                    [
868
                        'id' => 3,
869
                        'title' => 'Baz',
870
                        'year' => '1995',
871
                    ],
872
                ],
873
            ],
874
        ];
875
876
        $resource = new Collection($authorsData, new JsonApiAuthorTransformer(), 'people');
877
        $scope = new Scope($this->manager, $resource);
878
879
        $expected = [
880
            'data' => [
881
                [
882
                    'type' => 'people',
883
                    'id' => '1',
884
                    'attributes' => [
885
                        'name' => 'Dave',
886
                    ],
887
                    'relationships' => [
888
                        'published' => [
889
                            'data' => [],
890
                        ],
891
                    ],
892
                ],
893
                [
894
                    'type' => 'people',
895
                    'id' => '2',
896
                    'attributes' => [
897
                        'name' => 'Bob',
898
                    ],
899
                    'relationships' => [
900
                        'published' => [
901
                            'data' => [
902
                                [
903
                                    'type' => 'books',
904
                                    'id' => 3,
905
                                ],
906
                            ],
907
                        ],
908
                    ],
909
                ],
910
            ],
911
            'included' => [
912
                [
913
                    'type' => 'books',
914
                    'id' => '3',
915
                    'attributes' => [
916
                        'title' => 'Baz',
917
                        'year' => 1995,
918
                    ],
919
                ],
920
            ],
921
        ];
922
923
        $this->assertEquals($expected, $scope->toArray());
924
925
        $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}}]}';
926
        $this->assertSame($expectedJson, $scope->toJson());
927
    }
928
929 View Code Duplication
    public function testSerializingCollectionResourceWithMeta()
0 ignored issues
show
This method seems to be duplicated in your project.

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

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

Loading history...
930
    {
931
        $booksData = [
932
            [
933
                'id' => 1,
934
                'title' => 'Foo',
935
                'year' => '1991',
936
                '_author' => [
937
                    'name' => 'Dave',
938
                ],
939
            ],
940
            [
941
                'id' => 2,
942
                'title' => 'Bar',
943
                'year' => '1997',
944
                '_author' => [
945
                    'name' => 'Bob',
946
                ],
947
            ],
948
        ];
949
950
        $resource = new Collection($booksData, new JsonApiBookTransformer(), 'books');
951
        $resource->setMetaValue('foo', 'bar');
952
953
        $scope = new Scope($this->manager, $resource);
954
955
        $expected = [
956
            'data' => [
957
                [
958
                    'type' => 'books',
959
                    'id' => '1',
960
                    'attributes' => [
961
                        'title' => 'Foo',
962
                        'year' => 1991,
963
                    ],
964
                ],
965
                [
966
                    'type' => 'books',
967
                    'id' => '2',
968
                    'attributes' => [
969
                        'title' => 'Bar',
970
                        'year' => 1997,
971
                    ],
972
                ],
973
            ],
974
            'meta' => [
975
                'foo' => 'bar',
976
            ],
977
        ];
978
979
        $this->assertSame($expected, $scope->toArray());
980
981
        $expectedJson = '{"data":[{"type":"books","id":"1","attributes":{"title":"Foo","year":1991}},{"type":"books","id":"2","attributes":{"title":"Bar","year":1997}}],"meta":{"foo":"bar"}}';
982
        $this->assertSame($expectedJson, $scope->toJson());
983
    }
984
985
    public function testSerializingCollectionResourceWithDuplicatedIncludeData()
986
    {
987
        $this->manager->parseIncludes('author');
988
989
        $booksData = [
990
            [
991
                'id' => 1,
992
                'title' => 'Foo',
993
                'year' => '1991',
994
                '_author' => [
995
                    'id' => 1,
996
                    'name' => 'Dave',
997
                ],
998
            ],
999
            [
1000
                'id' => 2,
1001
                'title' => 'Bar',
1002
                'year' => '1997',
1003
                '_author' => [
1004
                    'id' => 1,
1005
                    'name' => 'Dave',
1006
                ],
1007
            ],
1008
        ];
1009
1010
        $resource = new Collection($booksData, new JsonApiBookTransformer(), 'books');
1011
        $scope = new Scope($this->manager, $resource);
1012
1013
        $expected = [
1014
            'data' => [
1015
                [
1016
                    'type' => 'books',
1017
                    'id' => '1',
1018
                    'attributes' => [
1019
                        'title' => 'Foo',
1020
                        'year' => 1991,
1021
                    ],
1022
                    'relationships' => [
1023
                        'author' => [
1024
                            'data' => [
1025
                                'type' => 'people',
1026
                                'id' => '1',
1027
                            ],
1028
                        ],
1029
                    ],
1030
                ],
1031
                [
1032
                    'type' => 'books',
1033
                    'id' => '2',
1034
                    'attributes' => [
1035
                        'title' => 'Bar',
1036
                        'year' => 1997,
1037
                    ],
1038
                    'relationships' => [
1039
                        'author' => [
1040
                            'data' => [
1041
                                'type' => 'people',
1042
                                'id' => '1',
1043
                            ],
1044
                        ],
1045
                    ],
1046
                ],
1047
            ],
1048
            'included' => [
1049
                [
1050
                    'type' => 'people',
1051
                    'id' => '1',
1052
                    'attributes' => [
1053
                        'name' => 'Dave',
1054
                    ],
1055
                ],
1056
            ],
1057
        ];
1058
1059
        $this->assertSame($expected, $scope->toArray());
1060
1061
        $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"}}]}';
1062
        $this->assertSame($expectedJson, $scope->toJson());
1063
    }
1064
1065 View Code Duplication
    public function testSerializingItemResourceWithNestedIncludes()
0 ignored issues
show
This method seems to be duplicated in your project.

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

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

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

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

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

Loading history...
2180
    {
2181
        $baseUrl = 'http://example.com';
2182
2183
        $this->manager->setSerializer(new JsonApiSerializer($baseUrl));
2184
2185
        $total = 10;
2186
        $count = 2;
2187
        $perPage = 2;
2188
        $currentPage = 1;
2189
        $lastPage = 5;
2190
        $currentUrl = 'http://example.com/books/?page=1';
2191
        $nextUrl = 'http://example.com/books/?page=2';
2192
        $lastUrl = 'http://example.com/books/?page=5';
2193
2194
        $paginator = Mockery::mock('League\Fractal\Pagination\PaginatorInterface');
2195
        $paginator->shouldReceive('getCurrentPage')->andReturn($currentPage);
2196
        $paginator->shouldReceive('getLastPage')->andReturn($lastPage);
2197
        $paginator->shouldReceive('getTotal')->andReturn($total);
2198
        $paginator->shouldReceive('getCount')->andReturn($count);
2199
        $paginator->shouldReceive('getPerPage')->andReturn($perPage);
2200
        $paginator->shouldReceive('getUrl')->with(1)->andReturn($currentUrl);
2201
        $paginator->shouldReceive('getUrl')->with(2)->andReturn($nextUrl);
2202
        $paginator->shouldReceive('getUrl')->with(5)->andReturn($lastUrl);
2203
2204
        $booksData = [
2205
            [
2206
                'id' => 1,
2207
                'title' => 'Foo',
2208
                'year' => '1991',
2209
                '_author' => [
2210
                    'id' => 1,
2211
                    'name' => 'Dave',
2212
                ],
2213
            ],
2214
            [
2215
                'id' => 2,
2216
                'title' => 'Bar',
2217
                'year' => '1997',
2218
                '_author' => [
2219
                    'id' => 2,
2220
                    'name' => 'Bob',
2221
                ],
2222
            ],
2223
        ];
2224
2225
        $resource = new Collection($booksData, new JsonApiBookTransformer(), 'books');
2226
        $resource->setPaginator($paginator);
2227
        $scope = new Scope($this->manager, $resource);
2228
2229
        $expected = [
2230
            'data' => [
2231
                [
2232
                    'type' => 'books',
2233
                    'id' => '1',
2234
                    'attributes' => [
2235
                        'title' => 'Foo',
2236
                        'year' => 1991,
2237
                    ],
2238
                    'links' => [
2239
                        'self' => 'http://example.com/books/1',
2240
                    ],
2241
                    'relationships' => [
2242
                        'author' => [
2243
                            'links' => [
2244
                                'self' => 'http://example.com/books/1/relationships/author',
2245
                                'related' => 'http://example.com/books/1/author',
2246
                            ],
2247
                        ],
2248
                        'co-author' => [
2249
                            'links' => [
2250
                                'self' => 'http://example.com/books/1/relationships/co-author',
2251
                                'related' => 'http://example.com/books/1/co-author'
2252
                            ],
2253
                        ],
2254
                        'author-with-meta' => [
2255
                            'links' => [
2256
                                'self' => 'http://example.com/books/1/relationships/author-with-meta',
2257
                                'related' => 'http://example.com/books/1/author-with-meta'
2258
                            ],
2259
                        ],
2260
                    ],
2261
                ],
2262
                [
2263
                    'type' => 'books',
2264
                    'id' => '2',
2265
                    'attributes' => [
2266
                        'title' => 'Bar',
2267
                        'year' => 1997,
2268
                    ],
2269
                    'links' => [
2270
                        'self' => 'http://example.com/books/2',
2271
                    ],
2272
                    'relationships' => [
2273
                        'author' => [
2274
                            'links' => [
2275
                                'self' => 'http://example.com/books/2/relationships/author',
2276
                                'related' => 'http://example.com/books/2/author',
2277
                            ],
2278
                        ],
2279
                        'co-author' => [
2280
                            'links' => [
2281
                                'self' => 'http://example.com/books/2/relationships/co-author',
2282
                                'related' => 'http://example.com/books/2/co-author'
2283
                            ],
2284
                        ],
2285
                        'author-with-meta' => [
2286
                            'links' => [
2287
                                'self' => 'http://example.com/books/2/relationships/author-with-meta',
2288
                                'related' => 'http://example.com/books/2/author-with-meta'
2289
                            ],
2290
                        ],
2291
                    ],
2292
                ],
2293
            ],
2294
            'meta' => [
2295
                'pagination' => [
2296
                    'total' =>  10,
2297
                    'count' => 2,
2298
                    'per_page' => 2,
2299
                    'current_page' => 1,
2300
                    'total_pages' => 5
2301
                ]
2302
            ],
2303
            'links' => [
2304
                'self' => 'http://example.com/books/?page=1',
2305
                'first' => 'http://example.com/books/?page=1',
2306
                'next' => 'http://example.com/books/?page=2',
2307
                'last' => 'http://example.com/books/?page=5'
2308
            ]
2309
        ];
2310
2311
        $this->assertSame($expected, $scope->toArray());
2312
2313
        $expectedJson = '{"data":[{"type":"books","id":"1","attributes":{"title":"Foo","year":1991},"links":{"self":"http:\/\/example.com\/books\/1"},"relationships":{"author":{"links":{"self":"http:\/\/example.com\/books\/1\/relationships\/author","related":"http:\/\/example.com\/books\/1\/author"}},"co-author":{"links":{"self":"http:\/\/example.com\/books\/1\/relationships\/co-author","related":"http:\/\/example.com\/books\/1\/co-author"}},"author-with-meta":{"links":{"self":"http:\/\/example.com\/books\/1\/relationships\/author-with-meta","related":"http:\/\/example.com\/books\/1\/author-with-meta"}}}},{"type":"books","id":"2","attributes":{"title":"Bar","year":1997},"links":{"self":"http:\/\/example.com\/books\/2"},"relationships":{"author":{"links":{"self":"http:\/\/example.com\/books\/2\/relationships\/author","related":"http:\/\/example.com\/books\/2\/author"}},"co-author":{"links":{"self":"http:\/\/example.com\/books\/2\/relationships\/co-author","related":"http:\/\/example.com\/books\/2\/co-author"}},"author-with-meta":{"links":{"self":"http:\/\/example.com\/books\/2\/relationships\/author-with-meta","related":"http:\/\/example.com\/books\/2\/author-with-meta"}}}}],"meta":{"pagination":{"total":10,"count":2,"per_page":2,"current_page":1,"total_pages":5}},"links":{"self":"http:\/\/example.com\/books\/?page=1","first":"http:\/\/example.com\/books\/?page=1","next":"http:\/\/example.com\/books\/?page=2","last":"http:\/\/example.com\/books\/?page=5"}}';
2314
        $this->assertSame($expectedJson, $scope->toJson());
2315
    }
2316
2317 View Code Duplication
    public function testSerializingCollectionResourceWithPaginatorWithOmittedUnavailableNextLink()
0 ignored issues
show
This method seems to be duplicated in your project.

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

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

Loading history...
2318
    {
2319
        $baseUrl = 'http://example.com';
2320
2321
        $this->manager->setSerializer(new JsonApiSerializer($baseUrl));
2322
2323
        $total = 10;
2324
        $count = 2;
2325
        $perPage = 2;
2326
        $currentPage = 5;
2327
        $lastPage = 5;
2328
        $firstUrl = 'http://example.com/books/?page=1';
2329
        $previousUrl = 'http://example.com/books/?page=4';
2330
        $lastUrl = 'http://example.com/books/?page=5';
2331
2332
        $paginator = Mockery::mock('League\Fractal\Pagination\PaginatorInterface');
2333
        $paginator->shouldReceive('getCurrentPage')->andReturn($currentPage);
2334
        $paginator->shouldReceive('getLastPage')->andReturn($lastPage);
2335
        $paginator->shouldReceive('getTotal')->andReturn($total);
2336
        $paginator->shouldReceive('getCount')->andReturn($count);
2337
        $paginator->shouldReceive('getPerPage')->andReturn($perPage);
2338
        $paginator->shouldReceive('getUrl')->with(1)->andReturn($firstUrl);
2339
        $paginator->shouldReceive('getUrl')->with(4)->andReturn($previousUrl);
2340
        $paginator->shouldReceive('getUrl')->with(5)->andReturn($lastUrl);
2341
2342
        $booksData = [
2343
            [
2344
                'id' => 1,
2345
                'title' => 'Foo',
2346
                'year' => '1991',
2347
                '_author' => [
2348
                    'id' => 1,
2349
                    'name' => 'Dave',
2350
                ],
2351
            ],
2352
            [
2353
                'id' => 2,
2354
                'title' => 'Bar',
2355
                'year' => '1997',
2356
                '_author' => [
2357
                    'id' => 2,
2358
                    'name' => 'Bob',
2359
                ],
2360
            ],
2361
        ];
2362
2363
        $resource = new Collection($booksData, new JsonApiBookTransformer(), 'books');
2364
        $resource->setPaginator($paginator);
2365
        $scope = new Scope($this->manager, $resource);
2366
2367
        $expected = [
2368
            'data' => [
2369
                [
2370
                    'type' => 'books',
2371
                    'id' => '1',
2372
                    'attributes' => [
2373
                        'title' => 'Foo',
2374
                        'year' => 1991,
2375
                    ],
2376
                    'links' => [
2377
                        'self' => 'http://example.com/books/1',
2378
                    ],
2379
                    'relationships' => [
2380
                        'author' => [
2381
                            'links' => [
2382
                                'self' => 'http://example.com/books/1/relationships/author',
2383
                                'related' => 'http://example.com/books/1/author',
2384
                            ],
2385
                        ],
2386
                        'co-author' => [
2387
                            'links' => [
2388
                                'self' => 'http://example.com/books/1/relationships/co-author',
2389
                                'related' => 'http://example.com/books/1/co-author'
2390
                            ],
2391
                        ],
2392
                        'author-with-meta' => [
2393
                            'links' => [
2394
                                'self' => 'http://example.com/books/1/relationships/author-with-meta',
2395
                                'related' => 'http://example.com/books/1/author-with-meta'
2396
                            ],
2397
                        ],
2398
                    ],
2399
                ],
2400
                [
2401
                    'type' => 'books',
2402
                    'id' => '2',
2403
                    'attributes' => [
2404
                        'title' => 'Bar',
2405
                        'year' => 1997,
2406
                    ],
2407
                    'links' => [
2408
                        'self' => 'http://example.com/books/2',
2409
                    ],
2410
                    'relationships' => [
2411
                        'author' => [
2412
                            'links' => [
2413
                                'self' => 'http://example.com/books/2/relationships/author',
2414
                                'related' => 'http://example.com/books/2/author',
2415
                            ],
2416
                        ],
2417
                        'co-author' => [
2418
                            'links' => [
2419
                                'self' => 'http://example.com/books/2/relationships/co-author',
2420
                                'related' => 'http://example.com/books/2/co-author'
2421
                            ],
2422
                        ],
2423
                        'author-with-meta' => [
2424
                            'links' => [
2425
                                'self' => 'http://example.com/books/2/relationships/author-with-meta',
2426
                                'related' => 'http://example.com/books/2/author-with-meta'
2427
                            ],
2428
                        ],
2429
                    ],
2430
                ],
2431
            ],
2432
            'meta' => [
2433
                'pagination' => [
2434
                    'total' =>  10,
2435
                    'count' => 2,
2436
                    'per_page' => 2,
2437
                    'current_page' => 5,
2438
                    'total_pages' => 5
2439
                ]
2440
            ],
2441
            'links' => [
2442
                'self' => 'http://example.com/books/?page=5',
2443
                'first' => 'http://example.com/books/?page=1',
2444
                'prev' => 'http://example.com/books/?page=4',
2445
                'last' => 'http://example.com/books/?page=5'
2446
            ]
2447
        ];
2448
2449
        $this->assertSame($expected, $scope->toArray());
2450
2451
        $expectedJson = '{"data":[{"type":"books","id":"1","attributes":{"title":"Foo","year":1991},"links":{"self":"http:\/\/example.com\/books\/1"},"relationships":{"author":{"links":{"self":"http:\/\/example.com\/books\/1\/relationships\/author","related":"http:\/\/example.com\/books\/1\/author"}},"co-author":{"links":{"self":"http:\/\/example.com\/books\/1\/relationships\/co-author","related":"http:\/\/example.com\/books\/1\/co-author"}},"author-with-meta":{"links":{"self":"http:\/\/example.com\/books\/1\/relationships\/author-with-meta","related":"http:\/\/example.com\/books\/1\/author-with-meta"}}}},{"type":"books","id":"2","attributes":{"title":"Bar","year":1997},"links":{"self":"http:\/\/example.com\/books\/2"},"relationships":{"author":{"links":{"self":"http:\/\/example.com\/books\/2\/relationships\/author","related":"http:\/\/example.com\/books\/2\/author"}},"co-author":{"links":{"self":"http:\/\/example.com\/books\/2\/relationships\/co-author","related":"http:\/\/example.com\/books\/2\/co-author"}},"author-with-meta":{"links":{"self":"http:\/\/example.com\/books\/2\/relationships\/author-with-meta","related":"http:\/\/example.com\/books\/2\/author-with-meta"}}}}],"meta":{"pagination":{"total":10,"count":2,"per_page":2,"current_page":5,"total_pages":5}},"links":{"self":"http:\/\/example.com\/books\/?page=5","first":"http:\/\/example.com\/books\/?page=1","prev":"http:\/\/example.com\/books\/?page=4","last":"http:\/\/example.com\/books\/?page=5"}}';
2452
        $this->assertSame($expectedJson, $scope->toJson());
2453
    }
2454
2455 View Code Duplication
    public function testCustomLinkMerge()
0 ignored issues
show
This method seems to be duplicated in your project.

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

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

Loading history...
2456
    {
2457
        $manager = new Manager();
2458
        $manager->setSerializer(new JsonApiSerializer('http://test.de'));
2459
2460
        $bookData = [
2461
            'id' => 1,
2462
            'title' => 'Foo',
2463
            'year' => '1991',
2464
            '_author' => [
2465
                'id' => 1,
2466
                'name' => 'Dave',
2467
            ],
2468
            'links' => [
2469
                'custom_link' => '/custom/link',
2470
            ],
2471
        ];
2472
2473
        $resource = new Item($bookData, new JsonApiBookTransformer(), 'books');
2474
2475
        $scope = new Scope($manager, $resource);
2476
2477
        $expected = [
2478
            'data' => [
2479
                'type' => 'books',
2480
                'id' => '1',
2481
                'attributes' => [
2482
                    'title' => 'Foo',
2483
                    'year' => 1991,
2484
                ],
2485
                'links' => [
2486
                    'self' => 'http://test.de/books/1',
2487
                    'custom_link' => '/custom/link',
2488
                ],
2489
                'relationships' => [
2490
                    'author' => [
2491
                        'links' => [
2492
                            'self' => 'http://test.de/books/1/relationships/author',
2493
                            'related' => 'http://test.de/books/1/author',
2494
                        ],
2495
                    ],
2496
                    'co-author' => [
2497
                        'links' => [
2498
                            'self' => 'http://test.de/books/1/relationships/co-author',
2499
                            'related' => 'http://test.de/books/1/co-author'
2500
                        ],
2501
                    ],
2502
                    'author-with-meta' => [
2503
                        'links' => [
2504
                            'self' => 'http://test.de/books/1/relationships/author-with-meta',
2505
                            'related' => 'http://test.de/books/1/author-with-meta'
2506
                        ],
2507
                    ],
2508
                ],
2509
            ],
2510
        ];
2511
2512
        $this->assertSame(json_encode($expected), $scope->toJson());
2513
    }
2514
2515
    public function testCustomLinkMergeNoLink()
2516
    {
2517
        $manager = new Manager();
2518
        $manager->setSerializer(new JsonApiSerializer('http://test.de'));
2519
2520
        $bookData = [
2521
            'id' => 1,
2522
            'title' => 'Foo',
2523
            'year' => '1991',
2524
            '_author' => [
2525
                'id' => 1,
2526
                'name' => 'Dave',
2527
            ],
2528
        ];
2529
2530
        $resource = new Item($bookData, new JsonApiBookTransformer(), 'books');
2531
2532
        $scope = new Scope($manager, $resource);
2533
2534
        $expected = [
2535
            'data' => [
2536
                'type' => 'books',
2537
                'id' => '1',
2538
                'attributes' => [
2539
                    'title' => 'Foo',
2540
                    'year' => 1991,
2541
                ],
2542
                'links' => [
2543
                    'self' => 'http://test.de/books/1',
2544
                ],
2545
                'relationships' => [
2546
                    'author' => [
2547
                        'links' => [
2548
                            'self' => 'http://test.de/books/1/relationships/author',
2549
                            'related' => 'http://test.de/books/1/author',
2550
                        ],
2551
                    ],
2552
                    'co-author' => [
2553
                        'links' => [
2554
                            'self' => 'http://test.de/books/1/relationships/co-author',
2555
                            'related' => 'http://test.de/books/1/co-author'
2556
                        ],
2557
                    ],
2558
                    'author-with-meta' => [
2559
                        'links' => [
2560
                            'self' => 'http://test.de/books/1/relationships/author-with-meta',
2561
                            'related' => 'http://test.de/books/1/author-with-meta'
2562
                        ],
2563
                    ],
2564
                ],
2565
            ],
2566
        ];
2567
2568
        $this->assertSame(json_encode($expected), $scope->toJson());
2569
    }
2570
2571 View Code Duplication
    public function testCustomSelfLinkMerge()
0 ignored issues
show
This method seems to be duplicated in your project.

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

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

Loading history...
2572
    {
2573
        $manager = new Manager();
2574
        $manager->setSerializer(new JsonApiSerializer('http://test.de'));
2575
2576
        $bookData = [
2577
            'id' => 1,
2578
            'title' => 'Foo',
2579
            'year' => '1991',
2580
            '_author' => [
2581
                'id' => 1,
2582
                'name' => 'Dave',
2583
            ],
2584
            'links' => [
2585
                'self' => '/custom/link',
2586
            ],
2587
        ];
2588
2589
        $resource = new Item($bookData, new JsonApiBookTransformer(), 'books');
2590
2591
        $scope = new Scope($manager, $resource);
2592
2593
        $expected = [
2594
            'data' => [
2595
                'type' => 'books',
2596
                'id' => '1',
2597
                'attributes' => [
2598
                    'title' => 'Foo',
2599
                    'year' => 1991,
2600
                ],
2601
                'links' => [
2602
                    'self' => '/custom/link',
2603
                ],
2604
                'relationships' => [
2605
                    'author' => [
2606
                        'links' => [
2607
                            'self' => 'http://test.de/books/1/relationships/author',
2608
                            'related' => 'http://test.de/books/1/author',
2609
                        ],
2610
                    ],
2611
                    'co-author' => [
2612
                        'links' => [
2613
                            'self' => 'http://test.de/books/1/relationships/co-author',
2614
                            'related' => 'http://test.de/books/1/co-author'
2615
                        ],
2616
                    ],
2617
                    'author-with-meta' => [
2618
                        'links' => [
2619
                            'self' => 'http://test.de/books/1/relationships/author-with-meta',
2620
                            'related' => 'http://test.de/books/1/author-with-meta'
2621
                        ],
2622
                    ],
2623
                ],
2624
            ],
2625
        ];
2626
2627
        $this->assertSame(json_encode($expected), $scope->toJson());
2628
    }
2629
2630
    public function testEmptyAttributesIsObject()
2631
    {
2632
        $manager = new Manager();
2633
        $manager->setSerializer(new JsonApiSerializer());
2634
2635
        $data = ['id' => 1];
2636
2637
        $resource = new Item($data, new JsonApiEmptyTransformer(), 'resources');
2638
2639
        $scope = new Scope($manager, $resource);
2640
2641
        $expectedJson = '{"data":{"type":"resources","id":"1","attributes":{}}}';
2642
2643
        $this->assertSame($expectedJson, $scope->toJson());
2644
    }
2645
2646
    /**
2647
     * @dataProvider serializingWithFieldsetsProvider
2648
     */
2649
    public function testSerializingWithFieldsets($fieldsetsToParse, $expected)
2650
    {
2651
        $this->manager->parseIncludes(['author', 'author.published']);
2652
2653
        $bookData = [
2654
            'id' => 1,
2655
            'title' => 'Foo',
2656
            'year' => '1991',
2657
            '_author' => [
2658
                'id' => 1,
2659
                'name' => 'Dave',
2660
                '_published' => [
2661
                    [
2662
                        'id' => 1,
2663
                        'title' => 'Foo',
2664
                        'year' => '1991',
2665
                    ],
2666
                    [
2667
                        'id' => 2,
2668
                        'title' => 'Bar',
2669
                        'year' => '2015',
2670
                    ],
2671
                ],
2672
            ],
2673
        ];
2674
2675
        $resource = new Item($bookData, new JsonApiBookTransformer(), 'books');
2676
2677
        $scope = new Scope($this->manager, $resource);
2678
2679
        $this->manager->parseFieldsets($fieldsetsToParse);
2680
        $this->assertSame($expected, $scope->toArray());
2681
    }
2682
2683
    public function serializingWithFieldsetsProvider()
2684
    {
2685
        return [
2686
            [
2687
                //Single field
2688
                ['books' => 'title'],
2689
                [
2690
                    'data' => [
2691
                        'type' => 'books',
2692
                        'id' => '1',
2693
                        'attributes' => [
2694
                            'title' => 'Foo'
2695
                        ]
2696
                    ]
2697
                ]
2698
            ],
2699
            [
2700
                //Multiple fields
2701
                ['books' => 'title,year'],
2702
                [
2703
                    'data' => [
2704
                        'type' => 'books',
2705
                        'id' => '1',
2706
                        'attributes' => [
2707
                            'title' => 'Foo',
2708
                            'year' => 1991
2709
                        ]
2710
                    ]
2711
                ]
2712
            ],
2713
            [
2714
                //Include 1st level relationship
2715
                ['books' => 'title,author', 'people' => 'name'],
2716
                [
2717
                    'data' => [
2718
                        'type' => 'books',
2719
                        'id' => '1',
2720
                        'attributes' => [
2721
                            'title' => 'Foo'
2722
                        ],
2723
                        'relationships' => [
2724
                            'author' => [
2725
                                'data' => [
2726
                                    'type' => 'people',
2727
                                    'id' => '1'
2728
                                ]
2729
                            ]
2730
                        ]
2731
                    ],
2732
                    'included' => [
2733
                        [
2734
                            'type' => 'people',
2735
                            'id' => '1',
2736
                            'attributes' => [
2737
                                'name' => 'Dave'
2738
                            ]
2739
                        ]
2740
                    ]
2741
                ]
2742
            ],
2743
            [
2744
                //Include 2nd level relationship
2745
                ['books' => 'title,author', 'people' => 'name,published'],
2746
                [
2747
                    'data' => [
2748
                        'type' => 'books',
2749
                        'id' => '1',
2750
                        'attributes' => [
2751
                            'title' => 'Foo'
2752
                        ],
2753
                        'relationships' => [
2754
                            'author' => [
2755
                                'data' => [
2756
                                    'type' => 'people',
2757
                                    'id' => '1'
2758
                                ]
2759
                            ]
2760
                        ]
2761
                    ],
2762
                    'included' => [
2763
                        [
2764
                            'type' => 'books',
2765
                            'id' => '2',
2766
                            'attributes' => [
2767
                                'title' => 'Bar'
2768
                            ]
2769
                        ],
2770
                        [
2771
                            'type' => 'people',
2772
                            'id' => '1',
2773
                            'attributes' => [
2774
                                'name' => 'Dave'
2775
                            ],
2776
                            'relationships' => [
2777
                                'published' => [
2778
                                    'data' => [
2779
                                        [
2780
                                            'type' => 'books',
2781
                                            'id' => '1'
2782
                                        ],
2783
                                        [
2784
                                            'type' => 'books',
2785
                                            'id' => '2'
2786
                                        ]
2787
                                    ]
2788
                                ]
2789
                            ]
2790
                        ]
2791
                    ]
2792
                ]
2793
            ]
2794
        ];
2795
    }
2796
}
2797