Completed
Push — master ( 1cb9cd...ecf884 )
by Korvin
05:21 queued 01:49
created

test/ScopeTest.php (1 issue)

Upgrade to new PHP Analysis Engine

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

1
<?php namespace League\Fractal\Test;
2
3
use League\Fractal\Manager;
4
use League\Fractal\Pagination\Cursor;
5
use League\Fractal\Resource\Collection;
6
use League\Fractal\Resource\Item;
7
use League\Fractal\Resource\NullResource;
8
use League\Fractal\Resource\Primitive;
9
use League\Fractal\Scope;
10
use League\Fractal\Serializer\ArraySerializer;
11
use League\Fractal\Test\Stub\ArraySerializerWithNull;
12
use League\Fractal\Test\Stub\Transformer\DefaultIncludeBookTransformer;
13
use League\Fractal\Test\Stub\Transformer\NullIncludeBookTransformer;
14
use League\Fractal\Test\Stub\Transformer\PrimitiveIncludeBookTransformer;
15
use Mockery;
16
use PHPUnit\Framework\TestCase;
17
18
class ScopeTest extends TestCase
19
{
20
    protected $simpleItem = ['foo' => 'bar'];
21
    protected $simpleCollection = [['foo' => 'bar']];
22
23 View Code Duplication
    public function testEmbedChildScope()
24
    {
25
        $manager = new Manager();
26
27
        $resource = new Item(['foo' => 'bar'], function () {
28
        });
29
30
        $scope = new Scope($manager, $resource, 'book');
31
        $this->assertSame($scope->getScopeIdentifier(), 'book');
32
        $childScope = $scope->embedChildScope('author', $resource);
33
34
        $this->assertInstanceOf('League\Fractal\Scope', $childScope);
35
    }
36
37 View Code Duplication
    public function testGetManager()
38
    {
39
        $resource = new Item(['foo' => 'bar'], function () {
40
        });
41
42
        $scope = new Scope(new Manager(), $resource, 'book');
43
44
        $this->assertInstanceOf('League\Fractal\Manager', $scope->getManager());
45
    }
46
47 View Code Duplication
    public function testGetResource()
48
    {
49
        $resource = new Item(['foo' => 'bar'], function () {
50
        });
51
52
        $scope = new Scope(new Manager(), $resource, 'book');
53
54
        $this->assertInstanceOf('League\Fractal\Resource\ResourceAbstract', $scope->getResource());
55
        $this->assertInstanceOf('League\Fractal\Resource\Item', $scope->getResource());
56
    }
57
58
    /**
59
     * @covers \League\Fractal\Scope::toArray
60
     */
61
    public function testToArray()
62
    {
63
        $manager = new Manager();
64
65
        $resource = new Item(['foo' => 'bar'], function ($data) {
66
            return $data;
67
        });
68
69
        $scope = new Scope($manager, $resource);
70
71
72
        $this->assertSame(['data' => ['foo' => 'bar']], $scope->toArray());
73
    }
74
75 View Code Duplication
    public function testToJson()
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...
76
    {
77
        $data = [
78
            'foo' => 'bar',
79
        ];
80
81
        $manager = new Manager();
82
83
        $resource = new Item($data, function ($data) {
84
            return $data;
85
        });
86
87
        $scope = new Scope($manager, $resource);
88
89
        $expected = json_encode([
90
            'data' => $data,
91
        ]);
92
93
        $this->assertSame($expected, $scope->toJson());
94
    }
95
96 View Code Duplication
    public function testToJsonWithOption()
97
    {
98
        $data = [
99
            'foo' => 'bar',
100
        ];
101
102
        $manager = new Manager();
103
104
        $resource = new Item($data, function ($data) {
105
            return $data;
106
        });
107
108
        $scope = new Scope($manager, $resource);
109
110
        $expected = json_encode([
111
            'data' => $data,
112
        ], JSON_PRETTY_PRINT);
113
114
        $this->assertSame($expected, $scope->toJson(JSON_PRETTY_PRINT));
115
    }
116
117 View Code Duplication
    public function testGetCurrentScope()
118
    {
119
        $manager = new Manager();
120
121
        $resource = new Item(['name' => 'Larry Ullman'], function () {
122
        });
123
124
        $scope = new Scope($manager, $resource, 'book');
125
        $this->assertSame('book', $scope->getScopeIdentifier());
126
127
        $childScope = $scope->embedChildScope('author', $resource);
128
        $this->assertSame('author', $childScope->getScopeIdentifier());
129
130
        $grandChildScope = $childScope->embedChildScope('profile', $resource);
131
        $this->assertSame('profile', $grandChildScope->getScopeIdentifier());
132
    }
133
134 View Code Duplication
    public function testGetIdentifier()
135
    {
136
        $manager = new Manager();
137
138
        $resource = new Item(['name' => 'Larry Ullman'], function () {
139
        });
140
141
        $scope = new Scope($manager, $resource, 'book');
142
        $this->assertSame('book', $scope->getIdentifier());
143
144
        $childScope = $scope->embedChildScope('author', $resource);
145
        $this->assertSame('book.author', $childScope->getIdentifier());
146
147
        $grandChildScope = $childScope->embedChildScope('profile', $resource);
148
        $this->assertSame('book.author.profile', $grandChildScope->getIdentifier());
149
    }
150
151 View Code Duplication
    public function testGetParentScopes()
152
    {
153
        $manager = new Manager();
154
155
        $resource = new Item(['name' => 'Larry Ullman'], function () {
156
        });
157
158
        $scope = new Scope($manager, $resource, 'book');
159
160
        $childScope = $scope->embedChildScope('author', $resource);
161
162
        $this->assertSame(['book'], $childScope->getParentScopes());
163
164
        $grandChildScope = $childScope->embedChildScope('profile', $resource);
165
        $this->assertSame(['book', 'author'], $grandChildScope->getParentScopes());
166
    }
167
168
    public function testIsRequested()
169
    {
170
        $manager = new Manager();
171
        $manager->parseIncludes(['foo', 'bar', 'baz.bart']);
172
173
        $scope = new Scope($manager, Mockery::mock('League\Fractal\Resource\ResourceAbstract'));
174
175
        $this->assertTrue($scope->isRequested('foo'));
176
        $this->assertTrue($scope->isRequested('bar'));
177
        $this->assertTrue($scope->isRequested('baz'));
178
        $this->assertTrue($scope->isRequested('baz.bart'));
179
        $this->assertFalse($scope->isRequested('nope'));
180
181
        $childScope = $scope->embedChildScope('baz', Mockery::mock('League\Fractal\Resource\ResourceAbstract'));
182
        $this->assertTrue($childScope->isRequested('bart'));
183
        $this->assertFalse($childScope->isRequested('foo'));
184
        $this->assertFalse($childScope->isRequested('bar'));
185
        $this->assertFalse($childScope->isRequested('baz'));
186
    }
187
188
    public function testIsExcluded()
189
    {
190
        $manager = new Manager();
191
        $manager->parseIncludes(['foo', 'bar', 'baz.bart']);
192
193
        $scope = new Scope($manager, Mockery::mock('League\Fractal\Resource\ResourceAbstract'));
194
        $childScope = $scope->embedChildScope('baz', Mockery::mock('League\Fractal\Resource\ResourceAbstract'));
195
196
        $manager->parseExcludes('bar');
197
198
        $this->assertFalse($scope->isExcluded('foo'));
199
        $this->assertTrue($scope->isExcluded('bar'));
200
        $this->assertFalse($scope->isExcluded('baz.bart'));
201
202
        $manager->parseExcludes('baz.bart');
203
204
        $this->assertFalse($scope->isExcluded('baz'));
205
        $this->assertTrue($scope->isExcluded('baz.bart'));
206
    }
207
208
    /**
209
     * @expectedException \InvalidArgumentException
210
     */
211 View Code Duplication
    public function testScopeRequiresConcreteImplementation()
212
    {
213
        $manager = new Manager();
214
        $manager->parseIncludes('book');
215
216
        $resource = Mockery::mock('League\Fractal\Resource\ResourceAbstract', [
217
            ['bar' => 'baz'],
218
            function () {},
219
        ])->makePartial();
220
221
        $scope = new Scope($manager, $resource);
222
        $scope->toArray();
223
    }
224
225
    public function testToArrayWithIncludes()
226
    {
227
        $manager = new Manager();
228
        $manager->parseIncludes('book,price');
229
230
        $transformer = Mockery::mock('League\Fractal\TransformerAbstract')->makePartial();
231
        $transformer->shouldReceive('getAvailableIncludes')->twice()->andReturn(['book']);
232
        $transformer->shouldReceive('transform')->once()->andReturnUsing(function (array $data) {
233
            return $data;
234
        });
235
        $transformer
236
            ->shouldReceive('processIncludedResources')
237
            ->once()
238
            ->andReturn(['book' => ['yin' => 'yang'], 'price' => 99]);
239
240
        $resource = new Item(['bar' => 'baz'], $transformer);
241
242
        $scope = new Scope($manager, $resource);
243
244
        $this->assertSame(['data' => ['bar' => 'baz', 'book' => ['yin' => 'yang'], 'price' => 99]], $scope->toArray());
245
    }
246
247
    public function testToArrayWithNumericKeysPreserved()
248
    {
249
        $manager = new Manager();
250
        $manager->setSerializer(new ArraySerializer());
251
252
        $resource = new Item(['1' => 'First', '2' => 'Second'], function ($data) {
253
            return $data;
254
        });
255
256
        $scope = new Scope($manager, $resource);
257
258
        $this->assertSame(['1' => 'First', '2' => 'Second'], $scope->toArray());
259
    }
260
261
    public function testToArrayWithSideloadedIncludes()
262
    {
263
        $serializer = Mockery::mock('League\Fractal\Serializer\ArraySerializer')->makePartial();
264
        $serializer->shouldReceive('sideloadIncludes')->andReturn(true);
265
        $serializer->shouldReceive('item')->andReturnUsing(function ($key, $data) {
266
            return ['data' => $data];
267
        });
268
        $serializer->shouldReceive('includedData')->andReturnUsing(function ($key, $data) {
269
            return ['sideloaded' => array_pop($data)];
270
        });
271
272
        $manager = new Manager();
273
        $manager->parseIncludes('book');
274
        $manager->setSerializer($serializer);
275
276
        $transformer = Mockery::mock('League\Fractal\TransformerAbstract')->makePartial();
277
        $transformer->shouldReceive('getAvailableIncludes')->twice()->andReturn(['book']);
278
        $transformer->shouldReceive('transform')->once()->andReturnUsing(function (array $data) {
279
            return $data;
280
        });
281
        $transformer->shouldReceive('processIncludedResources')->once()->andReturn(['book' => ['yin' => 'yang']]);
282
283
        $resource = new Item(['bar' => 'baz'], $transformer);
284
285
        $scope = new Scope($manager, $resource);
286
287
        $expected = [
288
            'data' => ['bar' => 'baz'],
289
            'sideloaded' => ['book' => ['yin' => 'yang']],
290
        ];
291
292
        $this->assertSame($expected, $scope->toArray());
293
    }
294
295
    public function testPushParentScope()
296
    {
297
        $manager = new Manager();
298
299
        $resource = new Item(['name' => 'Larry Ullman'], function () {
300
        });
301
302
        $scope = new Scope($manager, $resource);
303
304
        $this->assertSame(1, $scope->pushParentScope('book'));
305
        $this->assertSame(2, $scope->pushParentScope('author'));
306
        $this->assertSame(3, $scope->pushParentScope('profile'));
307
308
309
        $this->assertSame(['book', 'author', 'profile'], $scope->getParentScopes());
310
    }
311
312
    public function testRunAppropriateTransformerWithPrimitive()
313
    {
314
        $manager = new Manager();
315
316
        $transformer = Mockery::mock('League\Fractal\TransformerAbstract');
317
        $transformer->shouldReceive('transform')->once()->andReturn('simple string');
318
        $transformer->shouldReceive('setCurrentScope')->once()->andReturn([]);
319
        $transformer->shouldNotReceive('getAvailableIncludes');
320
        $transformer->shouldNotReceive('getDefaultIncludes');
321
322
        $resource = new Primitive('test', $transformer);
323
        $scope = $manager->createData($resource);
324
325
        $this->assertSame('simple string', $scope->transformPrimitiveResource());
326
327
        $resource = new Primitive(10, function ($x) {return $x + 10;});
328
        $scope = $manager->createData($resource);
329
330
        $this->assertSame(20, $scope->transformPrimitiveResource());
331
    }
332
333
    public function testRunAppropriateTransformerWithItem()
334
    {
335
        $manager = new Manager();
336
337
        $transformer = Mockery::mock('League\Fractal\TransformerAbstract');
338
        $transformer->shouldReceive('transform')->once()->andReturn($this->simpleItem);
339
        $transformer->shouldReceive('getAvailableIncludes')->once()->andReturn([]);
340
        $transformer->shouldReceive('getDefaultIncludes')->once()->andReturn([]);
341
        $transformer->shouldReceive('setCurrentScope')->once()->andReturn([]);
342
343
        $resource = new Item($this->simpleItem, $transformer);
344
        $scope = $manager->createData($resource);
345
346
        $this->assertSame(['data' => $this->simpleItem], $scope->toArray());
347
    }
348
349
    public function testRunAppropriateTransformerWithCollection()
350
    {
351
        $manager = new Manager();
352
353
        $transformer = Mockery::mock('League\Fractal\TransformerAbstract');
354
        $transformer->shouldReceive('transform')->once()->andReturn(['foo' => 'bar']);
355
        $transformer->shouldReceive('getAvailableIncludes')->once()->andReturn([]);
356
        $transformer->shouldReceive('getDefaultIncludes')->once()->andReturn([]);
357
        $transformer->shouldReceive('setCurrentScope')->once()->andReturn([]);
358
359
        $resource = new Collection([['foo' => 'bar']], $transformer);
360
        $scope = $manager->createData($resource);
361
362
        $this->assertSame(['data' => [['foo' => 'bar']]], $scope->toArray());
363
364
    }
365
366
    /**
367
     * @covers \League\Fractal\Scope::executeResourceTransformers
368
     * @expectedException \InvalidArgumentException
369
     * @expectedExceptionMessage Argument $resource should be an instance of League\Fractal\Resource\Item or League\Fractal\Resource\Collection
370
     */
371
    public function testCreateDataWithClassFuckKnows()
372
    {
373
        $manager = new Manager();
374
375
        $transformer = Mockery::mock('League\Fractal\TransformerAbstract')->makePartial();
376
377
        $resource = Mockery::mock('League\Fractal\Resource\ResourceAbstract', [$this->simpleItem, $transformer])->makePartial();
378
        $scope = $manager->createData($resource);
379
        $scope->toArray();
380
    }
381
382
    public function testPaginatorOutput()
383
    {
384
        $manager = new Manager();
385
386
        $collection = new Collection([['foo' => 'bar', 'baz' => 'ban']], function (array $data) {
387
            return $data;
388
        });
389
390
        $paginator = Mockery::mock('League\Fractal\Pagination\IlluminatePaginatorAdapter')->makePartial();
391
392
        $total = 100;
393
        $perPage = $count = 5;
394
        $currentPage = 2;
395
        $lastPage = 20;
396
397
        $paginator->shouldReceive('getTotal')->once()->andReturn($total);
398
        $paginator->shouldReceive('getCount')->once()->andReturn($count);
399
        $paginator->shouldReceive('getPerPage')->once()->andReturn($perPage);
400
        $paginator->shouldReceive('getCurrentPage')->once()->andReturn($currentPage);
401
        $paginator->shouldReceive('getLastPage')->once()->andReturn($lastPage);
402
        $paginator->shouldReceive('getUrl')->times(2)->andReturnUsing(function ($page) {
403
            return 'http://example.com/foo?page='.$page;
404
        });
405
406
        $collection->setPaginator($paginator);
407
408
        $rootScope = $manager->createData($collection);
409
410
411
        $expectedOutput = [
412
            'data' => [
413
                [
414
                    'foo' => 'bar',
415
                    'baz' => 'ban',
416
                ],
417
            ],
418
            'meta' => [
419
                'pagination' => [
420
                    'total' => $total,
421
                    'count' => $count,
422
                    'per_page' => $perPage,
423
                    'current_page' => $currentPage,
424
                    'total_pages' => $lastPage,
425
                    'links' => [
426
                        'previous' => 'http://example.com/foo?page=1',
427
                        'next' => 'http://example.com/foo?page=3',
428
429
                    ],
430
                ],
431
            ],
432
        ];
433
434
        $this->assertSame($expectedOutput, $rootScope->toArray());
435
    }
436
437
    public function testCursorOutput()
438
    {
439
        $manager = new Manager();
440
441
        $inputData = [
442
            [
443
                'foo' => 'bar',
444
                'baz' => 'ban',
445
            ],
446
        ];
447
448
        $collection = new Collection($inputData, function (array $data) {
449
            return $data;
450
        });
451
452
        $cursor = new Cursor(0, 'ban', 'ban', 2);
453
454
        $collection->setCursor($cursor);
455
456
        $rootScope = $manager->createData($collection);
457
458
459
        $expectedOutput = [
460
            'data' => $inputData,
461
            'meta' => [
462
                'cursor' => [
463
                    'current' => 0,
464
                    'prev' => 'ban',
465
                    'next' => 'ban',
466
                    'count' => 2,
467
468
                ],
469
            ],
470
        ];
471
472
        $this->assertSame($expectedOutput, $rootScope->toArray());
473
    }
474
475 View Code Duplication
    public function testDefaultIncludeSuccess()
476
    {
477
        $manager = new Manager();
478
        $manager->setSerializer(new ArraySerializer());
479
480
        // Send this stub junk, it has a specific format anyhow
481
        $resource = new Item([], new DefaultIncludeBookTransformer());
482
483
        // Try without metadata
484
        $scope = new Scope($manager, $resource);
485
486
        $expected = [
487
            'a' => 'b',
488
            'author' => [
489
                'c' => 'd',
490
            ],
491
        ];
492
493
        $this->assertSame($expected, $scope->toArray());
494
    }
495
496 View Code Duplication
    public function testPrimitiveResourceIncludeSuccess()
497
    {
498
        $manager = new Manager();
499
        $manager->setSerializer(new ArraySerializer());
500
501
        $resource = new Item(['price' => '49'], new PrimitiveIncludeBookTransformer);
502
503
        $scope = new Scope($manager, $resource);
504
        $expected = [
505
            'a' => 'b',
506
            'price' => 49,
507
        ];
508
509
        $this->assertSame($expected, $scope->toArray());
510
    }
511
512 View Code Duplication
    public function testNullResourceIncludeSuccess()
513
    {
514
        $manager = new Manager();
515
        $manager->setSerializer(new ArraySerializerWithNull);
516
517
        // Send this stub junk, it has a specific format anyhow
518
        $resource = new Item([], new NullIncludeBookTransformer);
519
520
        // Try without metadata
521
        $scope = new Scope($manager, $resource);
522
        $expected = [
523
            'a' => 'b',
524
            'author' => null,
525
        ];
526
527
        $this->assertSame($expected, $scope->toArray());
528
    }
529
530
    /**
531
     * @covers \League\Fractal\Scope::toArray
532
     */
533 View Code Duplication
    public function testNullResourceDataAndJustMeta()
534
    {
535
        $manager = new Manager();
536
        $manager->setSerializer(new ArraySerializerWithNull);
537
538
        $resource = new NullResource();
539
        $resource->setMeta(['foo' => 'bar']);
540
541
        $scope = new Scope($manager, $resource);
542
543
        $this->assertSame(['meta' => ['foo' => 'bar']], $scope->toArray());
544
    }
545
546
    /**
547
     * @covers \League\Fractal\Scope::toArray
548
     * @dataProvider fieldsetsProvider
549
     */
550 View Code Duplication
    public function testToArrayWithFieldsets($fieldsetsToParse, $expected)
551
    {
552
        $manager = new Manager();
553
554
        $resource = new Item(
555
            ['foo' => 'bar', 'baz' => 'qux'],
556
            function ($data) {
557
                return $data;
558
            },
559
            'resourceName'
560
        );
561
562
        $scope = new Scope($manager, $resource);
563
564
        $manager->parseFieldsets($fieldsetsToParse);
565
        $this->assertSame($expected, $scope->toArray());
566
    }
567
568 View Code Duplication
    public function fieldsetsProvider()
569
    {
570
        return [
571
            [
572
                ['resourceName' => 'foo'],
573
                ['data' => ['foo' => 'bar']]
574
            ],
575
            [
576
                ['resourceName' => 'foo,baz'],
577
                ['data' => ['foo' => 'bar', 'baz' => 'qux']]
578
            ],
579
            [
580
                ['resourceName' => 'inexistentField'],
581
                ['data' => []]
582
            ]
583
        ];
584
    }
585
586
    /**
587
     * @covers \League\Fractal\Scope::toArray
588
     * @dataProvider fieldsetsWithMandatorySerializerFieldsProvider
589
     */
590 View Code Duplication
    public function testToArrayWithFieldsetsAndMandatorySerializerFields($fieldsetsToParse, $expected)
591
    {
592
        $serializer = Mockery::mock('League\Fractal\Serializer\DataArraySerializer')->makePartial();
593
        $serializer->shouldReceive('getMandatoryFields')->andReturn(['foo']);
594
595
        $resource = new Item(
596
            ['foo' => 'bar', 'baz' => 'qux'],
597
            function ($data) {
598
                return $data;
599
            },
600
            'resourceName'
601
        );
602
603
        $manager = new Manager();
604
        $manager->setSerializer($serializer);
605
        $scope = new Scope($manager, $resource);
606
607
        $manager->parseFieldsets($fieldsetsToParse);
608
        $this->assertSame($expected, $scope->toArray());
609
    }
610
611 View Code Duplication
    public function fieldsetsWithMandatorySerializerFieldsProvider()
612
    {
613
        return [
614
            //Don't request for mandatory field
615
            [
616
                ['resourceName' => 'baz'],
617
                ['data' => ['foo' => 'bar', 'baz' => 'qux']]
618
            ],
619
            //Request required field anyway
620
            [
621
                ['resourceName' => 'foo,baz'],
622
                ['data' => ['foo' => 'bar', 'baz' => 'qux']]
623
            ]
624
        ];
625
    }
626
627
    /**
628
     * @dataProvider fieldsetsWithIncludesProvider
629
     */
630 View Code Duplication
    public function testToArrayWithIncludesAndFieldsets($fieldsetsToParse, $expected)
631
    {
632
        $transformer = $this->createTransformerWithIncludedResource('book', ['book' => ['yin' => 'yang']]);
633
634
        $resource = new Item(
635
            ['foo' => 'bar', 'baz' => 'qux'],
636
            $transformer,
637
            'resourceName'
638
        );
639
        $manager = new Manager();
640
        $scope = new Scope($manager, $resource);
641
642
        $manager->parseIncludes('book');
643
644
        $manager->parseFieldsets($fieldsetsToParse);
645
        $this->assertSame($expected, $scope->toArray());
646
    }
647
648 View Code Duplication
    public function fieldsetsWithIncludesProvider()
649
    {
650
        return [
651
            //Included relation was not requested
652
            [
653
                ['resourceName' => 'foo'],
654
                ['data' => ['foo' => 'bar']]
655
            ],
656
            //Included relation was requested
657
            [
658
                ['resourceName' => 'foo,book', 'book' => 'yin'],
659
                ['data' => ['foo' => 'bar', 'book' => ['yin' => 'yang']]]
660
            ]
661
        ];
662
    }
663
664
    /**
665
     * @covers \League\Fractal\Scope::toArray
666
     * @dataProvider fieldsetsWithSideLoadIncludesProvider
667
     */
668
    public function testToArrayWithSideloadedIncludesAndFieldsets($fieldsetsToParse, $expected)
669
    {
670
        $serializer = Mockery::mock('League\Fractal\Serializer\DataArraySerializer')->makePartial();
671
        $serializer->shouldReceive('sideloadIncludes')->andReturn(true);
672
        $serializer->shouldReceive('item')->andReturnUsing(
673
            function ($key, $data) {
674
                return ['data' => $data];
675
            }
676
        );
677
        $serializer->shouldReceive('includedData')->andReturnUsing(
678
            function ($key, $data) {
679
                $data = array_pop($data);
680
                return empty($data) ? [] : ['sideloaded' => $data];
681
            }
682
        );
683
684
        $manager = new Manager();
685
        $manager->parseIncludes('book');
686
        $manager->setSerializer($serializer);
687
688
        $transformer = $this->createTransformerWithIncludedResource('book', ['book' => ['yin' => 'yang']]);
689
690
        $resource = new Item(['foo' => 'bar'], $transformer, 'resourceName');
691
        $scope = new Scope($manager, $resource);
692
693
        $manager->parseFieldsets($fieldsetsToParse);
694
        $this->assertSame($expected, $scope->toArray());
695
    }
696
697 View Code Duplication
    public function fieldsetsWithSideLoadIncludesProvider()
698
    {
699
        return [
700
            //Included relation was not requested
701
            [
702
                ['resourceName' => 'foo'],
703
                ['data' => ['foo' => 'bar']]
704
            ],
705
            //Included relation was requested
706
            [
707
                ['resourceName' => 'foo,book', 'book' => 'yin'],
708
                ['data' => ['foo' => 'bar'], 'sideloaded' => ['book' => ['yin' => 'yang']]]
709
            ]
710
        ];
711
    }
712
713
    public function tearDown()
714
    {
715
        Mockery::close();
716
    }
717
718
    protected function createTransformerWithIncludedResource($resourceName, $transformResult)
719
    {
720
        $transformer = Mockery::mock('League\Fractal\TransformerAbstract')->makePartial();
721
        $transformer->shouldReceive('getAvailableIncludes')->twice()->andReturn([$resourceName]);
722
        $transformer->shouldReceive('transform')->once()->andReturnUsing(
723
            function (array $data) {
724
                return $data;
725
            }
726
        );
727
        $transformer->shouldReceive('processIncludedResources')->once()->andReturn($transformResult);
728
        return $transformer;
729
    }
730
}
731