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