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\Resource\Collection; |
||
5 | use League\Fractal\Resource\Item; |
||
6 | use League\Fractal\Scope; |
||
7 | use Mockery as m; |
||
8 | use PHPUnit\Framework\TestCase; |
||
9 | |||
10 | class TransformerAbstractTest extends TestCase |
||
11 | { |
||
12 | /** |
||
13 | * @covers \League\Fractal\TransformerAbstract::setAvailableIncludes |
||
14 | */ |
||
15 | public function testSetAvailableIncludes() |
||
16 | { |
||
17 | $transformer = m::mock('League\Fractal\TransformerAbstract')->makePartial(); |
||
18 | $this->assertInstanceOf('League\Fractal\TransformerAbstract', $transformer->setAvailableIncludes(['foo'])); |
||
19 | } |
||
20 | |||
21 | /** |
||
22 | * @covers \League\Fractal\TransformerAbstract::getAvailableIncludes |
||
23 | */ |
||
24 | View Code Duplication | public function testGetAvailableIncludes() |
|
25 | { |
||
26 | $transformer = m::mock('League\Fractal\TransformerAbstract')->makePartial(); |
||
27 | |||
28 | $transformer->setAvailableIncludes(['foo', 'bar']); |
||
29 | $this->assertSame(['foo', 'bar'], $transformer->getAvailableIncludes()); |
||
30 | } |
||
31 | |||
32 | /** |
||
33 | * @covers \League\Fractal\TransformerAbstract::setDefaultIncludes |
||
34 | */ |
||
35 | public function testSetDefaultIncludes() |
||
36 | { |
||
37 | $transformer = m::mock('League\Fractal\TransformerAbstract')->makePartial(); |
||
38 | $this->assertInstanceOf('League\Fractal\TransformerAbstract', $transformer->setDefaultIncludes(['foo'])); |
||
39 | } |
||
40 | |||
41 | /** |
||
42 | * @covers \League\Fractal\TransformerAbstract::getDefaultIncludes |
||
43 | */ |
||
44 | View Code Duplication | public function testGetDefaultIncludes() |
|
45 | { |
||
46 | $transformer = m::mock('League\Fractal\TransformerAbstract')->makePartial(); |
||
47 | |||
48 | $transformer->setDefaultIncludes(['foo', 'bar']); |
||
49 | $this->assertSame(['foo', 'bar'], $transformer->getDefaultIncludes()); |
||
50 | } |
||
51 | |||
52 | /** |
||
53 | * @covers \League\Fractal\TransformerAbstract::setCurrentScope |
||
54 | */ |
||
55 | View Code Duplication | public function testSetCurrentScope() |
|
56 | { |
||
57 | $transformer = $this->getMockForAbstractClass('League\Fractal\TransformerAbstract'); |
||
58 | $manager = new Manager(); |
||
59 | $scope = new Scope($manager, m::mock('League\Fractal\Resource\ResourceAbstract')); |
||
60 | $this->assertInstanceOf('League\Fractal\TransformerAbstract', $transformer->setCurrentScope($scope)); |
||
61 | } |
||
62 | |||
63 | /** |
||
64 | * @covers \League\Fractal\TransformerAbstract::getCurrentScope |
||
65 | */ |
||
66 | View Code Duplication | public function testGetCurrentScope() |
|
67 | { |
||
68 | $transformer = m::mock('League\Fractal\TransformerAbstract')->makePartial(); |
||
69 | $manager = new Manager(); |
||
70 | $scope = new Scope($manager, m::mock('League\Fractal\Resource\ResourceAbstract')); |
||
71 | $transformer->setCurrentScope($scope); |
||
72 | $this->assertSame($transformer->getCurrentScope(), $scope); |
||
73 | } |
||
74 | |||
75 | View Code Duplication | public function testProcessEmbeddedResourcesNoAvailableIncludes() |
|
76 | { |
||
77 | $transformer = m::mock('League\Fractal\TransformerAbstract')->makePartial(); |
||
78 | |||
79 | $manager = new Manager(); |
||
80 | $manager->parseIncludes('foo'); |
||
81 | |||
82 | $scope = new Scope($manager, m::mock('League\Fractal\Resource\ResourceAbstract')); |
||
83 | $this->assertFalse($transformer->processIncludedResources($scope, ['some' => 'data'])); |
||
84 | } |
||
85 | |||
86 | View Code Duplication | public function testProcessEmbeddedResourcesNoDefaultIncludes() |
|
87 | { |
||
88 | $transformer = m::mock('League\Fractal\TransformerAbstract')->makePartial(); |
||
89 | |||
90 | $manager = new Manager(); |
||
91 | $manager->parseIncludes('foo'); |
||
92 | |||
93 | $scope = new Scope($manager, m::mock('League\Fractal\Resource\ResourceAbstract')); |
||
94 | $this->assertFalse($transformer->processIncludedResources($scope, ['some' => 'data'])); |
||
95 | } |
||
96 | |||
97 | /** |
||
98 | * @covers \League\Fractal\TransformerAbstract::processIncludedResources |
||
99 | * @covers \League\Fractal\TransformerAbstract::callIncludeMethod |
||
100 | * @expectedException \BadMethodCallException |
||
101 | */ |
||
102 | public function testProcessEmbeddedResourcesInvalidAvailableEmbed() |
||
103 | { |
||
104 | $transformer = m::mock('League\Fractal\TransformerAbstract')->makePartial(); |
||
105 | |||
106 | $manager = new Manager(); |
||
107 | $manager->parseIncludes('book'); |
||
108 | |||
109 | $scope = new Scope($manager, m::mock('League\Fractal\Resource\ResourceAbstract')); |
||
110 | $transformer->setCurrentScope($scope); |
||
111 | |||
112 | $transformer->setAvailableIncludes(['book']); |
||
113 | $transformer->processIncludedResources($scope, []); |
||
114 | } |
||
115 | |||
116 | /** |
||
117 | * @covers \League\Fractal\TransformerAbstract::processIncludedResources |
||
118 | * @covers \League\Fractal\TransformerAbstract::callIncludeMethod |
||
119 | * @expectedException \BadMethodCallException |
||
120 | */ |
||
121 | View Code Duplication | public function testProcessEmbeddedResourcesInvalidDefaultEmbed() |
|
122 | { |
||
123 | $transformer = m::mock('League\Fractal\TransformerAbstract')->makePartial(); |
||
124 | |||
125 | $manager = new Manager(); |
||
126 | |||
127 | $scope = new Scope($manager, m::mock('League\Fractal\Resource\ResourceAbstract')); |
||
128 | |||
129 | $transformer->setDefaultIncludes(['book']); |
||
130 | $transformer->processIncludedResources($scope, []); |
||
131 | } |
||
132 | |||
133 | /** |
||
134 | * @covers \League\Fractal\TransformerAbstract::processIncludedResources |
||
135 | * @covers \League\Fractal\TransformerAbstract::callIncludeMethod |
||
136 | */ |
||
137 | View Code Duplication | public function testProcessIncludedAvailableResources() |
|
138 | { |
||
139 | $manager = new Manager(); |
||
140 | $manager->parseIncludes('book'); |
||
141 | $transformer = m::mock('League\Fractal\TransformerAbstract[transform]'); |
||
142 | |||
143 | $transformer->shouldReceive('includeBook')->once()->andReturnUsing(function ($data) { |
||
144 | return new Item(['included' => 'thing'], function ($data) { |
||
145 | return $data; |
||
146 | }); |
||
147 | }); |
||
148 | |||
149 | $transformer->setAvailableIncludes(['book', 'publisher']); |
||
150 | $scope = new Scope($manager, new Item([], $transformer)); |
||
151 | $included = $transformer->processIncludedResources($scope, ['meh']); |
||
152 | $this->assertSame(['book' => ['data' => ['included' => 'thing']]], $included); |
||
153 | } |
||
154 | |||
155 | /** |
||
156 | * @covers \League\Fractal\TransformerAbstract::processIncludedResources |
||
157 | * @covers \League\Fractal\TransformerAbstract::figureOutWhichIncludes |
||
158 | */ |
||
159 | View Code Duplication | public function testProcessExcludedAvailableResources() |
|
0 ignored issues
–
show
|
|||
160 | { |
||
161 | $manager = new Manager(); |
||
162 | $transformer = m::mock('League\Fractal\TransformerAbstract[transform]'); |
||
163 | $scope = new Scope($manager, new Item([], $transformer)); |
||
164 | |||
165 | $transformer->shouldReceive('includeBook')->never(); |
||
166 | |||
167 | $transformer->shouldReceive('includePublisher')->once()->andReturnUsing(function ($data) { |
||
0 ignored issues
–
show
|
|||
168 | return new Item(['another' => 'thing'], function ($data) { |
||
169 | return $data; |
||
170 | }); |
||
171 | }); |
||
172 | |||
173 | // available includes that have been requested are excluded |
||
174 | $manager->parseIncludes('book,publisher'); |
||
175 | $manager->parseExcludes('book'); |
||
176 | |||
177 | $transformer->setAvailableIncludes(['book', 'publisher']); |
||
0 ignored issues
–
show
The method
setAvailableIncludes() does not seem to exist on object<Mockery\MockInterface> .
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed.
Loading history...
|
|||
178 | |||
179 | $included = $transformer->processIncludedResources($scope, ['meh']); |
||
0 ignored issues
–
show
The method
processIncludedResources() does not seem to exist on object<Mockery\MockInterface> .
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed.
Loading history...
|
|||
180 | $this->assertSame(['publisher' => ['data' => ['another' => 'thing']]], $included); |
||
181 | } |
||
182 | |||
183 | /** |
||
184 | * @covers \League\Fractal\TransformerAbstract::processIncludedResources |
||
185 | * @covers \League\Fractal\TransformerAbstract::figureOutWhichIncludes |
||
186 | */ |
||
187 | View Code Duplication | public function testProcessExcludedDefaultResources() |
|
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...
|
|||
188 | { |
||
189 | $manager = new Manager(); |
||
190 | $transformer = m::mock('League\Fractal\TransformerAbstract[transform]'); |
||
191 | $scope = new Scope($manager, new Item([], $transformer)); |
||
192 | |||
193 | $transformer->shouldReceive('includeBook')->never(); |
||
194 | |||
195 | $transformer->shouldReceive('includePublisher')->once()->andReturnUsing(function ($data) { |
||
0 ignored issues
–
show
|
|||
196 | return new Item(['another' => 'thing'], function ($data) { |
||
197 | return $data; |
||
198 | }); |
||
199 | }); |
||
200 | |||
201 | $manager->parseIncludes('book,publisher'); |
||
202 | $manager->parseExcludes('book'); |
||
203 | |||
204 | $transformer->setDefaultIncludes(['book', 'publisher']); |
||
0 ignored issues
–
show
The method
setDefaultIncludes() does not seem to exist on object<Mockery\MockInterface> .
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed.
Loading history...
|
|||
205 | |||
206 | $included = $transformer->processIncludedResources($scope, ['meh']); |
||
0 ignored issues
–
show
The method
processIncludedResources() does not seem to exist on object<Mockery\MockInterface> .
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed.
Loading history...
|
|||
207 | $this->assertSame(['publisher' => ['data' => ['another' => 'thing']]], $included); |
||
208 | } |
||
209 | |||
210 | /** |
||
211 | * @covers \League\Fractal\TransformerAbstract::processIncludedResources |
||
212 | * @covers \League\Fractal\TransformerAbstract::callIncludeMethod |
||
213 | */ |
||
214 | public function testProcessIncludedAvailableResourcesEmptyEmbed() |
||
215 | { |
||
216 | $manager = new Manager(); |
||
217 | $manager->parseIncludes(['book']); |
||
218 | $transformer = m::mock('League\Fractal\TransformerAbstract[transform]'); |
||
219 | |||
220 | $transformer->shouldReceive('includeBook')->once()->andReturn(null); |
||
221 | |||
222 | $transformer->setAvailableIncludes(['book']); |
||
223 | $scope = new Scope($manager, new Item([], $transformer)); |
||
224 | $included = $transformer->processIncludedResources($scope, ['meh']); |
||
225 | |||
226 | $this->assertFalse($included); |
||
227 | } |
||
228 | |||
229 | /** |
||
230 | * @covers \League\Fractal\TransformerAbstract::callIncludeMethod |
||
231 | * @expectedException \Exception |
||
232 | * @expectedExceptionMessage Invalid return value from League\Fractal\TransformerAbstract::includeBook(). |
||
233 | */ |
||
234 | View Code Duplication | public function testCallEmbedMethodReturnsCrap() |
|
235 | { |
||
236 | $manager = new Manager(); |
||
237 | $manager->parseIncludes('book'); |
||
238 | $transformer = m::mock('League\Fractal\TransformerAbstract[transform]'); |
||
239 | |||
240 | $transformer->shouldReceive('includeBook')->once()->andReturn(new \stdClass()); |
||
241 | |||
242 | $transformer->setAvailableIncludes(['book']); |
||
243 | $scope = new Scope($manager, new Item([], $transformer)); |
||
244 | $transformer->processIncludedResources($scope, ['meh']); |
||
245 | } |
||
246 | |||
247 | /** |
||
248 | * @covers \League\Fractal\TransformerAbstract::processIncludedResources |
||
249 | * @covers \League\Fractal\TransformerAbstract::callIncludeMethod |
||
250 | */ |
||
251 | View Code Duplication | public function testProcessEmbeddedDefaultResources() |
|
252 | { |
||
253 | $manager = new Manager(); |
||
254 | $transformer = m::mock('League\Fractal\TransformerAbstract[transform]'); |
||
255 | |||
256 | $transformer->shouldReceive('includeBook')->once()->andReturnUsing(function ($data) { |
||
257 | return new Item(['included' => 'thing'], function ($data) { |
||
258 | return $data; |
||
259 | }); |
||
260 | }); |
||
261 | |||
262 | $transformer->setDefaultIncludes(['book']); |
||
263 | $scope = new Scope($manager, new Item([], $transformer)); |
||
264 | $included = $transformer->processIncludedResources($scope, ['meh']); |
||
265 | $this->assertSame(['book' => ['data' => ['included' => 'thing']]], $included); |
||
266 | } |
||
267 | |||
268 | /** |
||
269 | * @covers \League\Fractal\TransformerAbstract::processIncludedResources |
||
270 | * @covers \League\Fractal\TransformerAbstract::callIncludeMethod |
||
271 | */ |
||
272 | View Code Duplication | public function testIncludedItem() |
|
273 | { |
||
274 | $manager = new Manager(); |
||
275 | $manager->parseIncludes('book'); |
||
276 | |||
277 | $transformer = m::mock('League\Fractal\TransformerAbstract[transform]'); |
||
278 | $transformer->shouldReceive('includeBook')->once()->andReturnUsing(function ($data) { |
||
279 | return new Item(['included' => 'thing'], function ($data) { |
||
280 | return $data; |
||
281 | }); |
||
282 | }); |
||
283 | |||
284 | $transformer->setAvailableIncludes(['book']); |
||
285 | $scope = new Scope($manager, new Item([], $transformer)); |
||
286 | $included = $transformer->processIncludedResources($scope, ['meh']); |
||
287 | $this->assertSame(['book' => ['data' => ['included' => 'thing']]], $included); |
||
288 | } |
||
289 | |||
290 | public function testParamBagIsProvidedForIncludes() |
||
291 | { |
||
292 | $manager = new Manager(); |
||
293 | $manager->parseIncludes('book:foo(bar)'); |
||
294 | |||
295 | $transformer = m::mock('League\Fractal\TransformerAbstract')->makePartial(); |
||
296 | |||
297 | $transformer->shouldReceive('includeBook') |
||
298 | ->with(m::any(), m::type('\League\Fractal\ParamBag')) |
||
299 | ->once(); |
||
300 | |||
301 | $transformer->setAvailableIncludes(['book']); |
||
302 | $scope = new Scope($manager, new Item([], $transformer)); |
||
303 | |||
304 | $this->assertFalse($transformer->processIncludedResources($scope, [])); |
||
305 | } |
||
306 | |||
307 | /** |
||
308 | * @covers \League\Fractal\TransformerAbstract::processIncludedResources |
||
309 | * @covers \League\Fractal\TransformerAbstract::callIncludeMethod |
||
310 | */ |
||
311 | public function testIncludedCollection() |
||
312 | { |
||
313 | $manager = new Manager(); |
||
314 | $manager->parseIncludes('book'); |
||
315 | |||
316 | $collectionData = [ |
||
317 | ['included' => 'thing'], |
||
318 | ['another' => 'thing'], |
||
319 | ]; |
||
320 | |||
321 | $transformer = m::mock('League\Fractal\TransformerAbstract[transform]'); |
||
322 | $transformer->shouldReceive('includeBook')->once()->andReturnUsing(function ($data) use ($collectionData) { |
||
323 | return new Collection($collectionData, function ($data) { |
||
324 | return $data; |
||
325 | }); |
||
326 | }); |
||
327 | |||
328 | $transformer->setAvailableIncludes(['book']); |
||
329 | $scope = new Scope($manager, new Collection([], $transformer)); |
||
330 | $included = $transformer->processIncludedResources($scope, ['meh']); |
||
331 | $this->assertSame(['book' => ['data' => $collectionData]], $included); |
||
332 | } |
||
333 | |||
334 | /** |
||
335 | * @covers \League\Fractal\TransformerAbstract::processIncludedResources |
||
336 | * @covers \League\Fractal\TransformerAbstract::callIncludeMethod |
||
337 | */ |
||
338 | View Code Duplication | public function testProcessEmbeddedDefaultResourcesEmptyEmbed() |
|
339 | { |
||
340 | $transformer = m::mock('League\Fractal\TransformerAbstract[transform]'); |
||
341 | $transformer->shouldReceive('includeBook')->once()->andReturn(null); |
||
342 | |||
343 | $transformer->setDefaultIncludes(['book']); |
||
344 | $scope = new Scope(new Manager(), new Item([], $transformer)); |
||
345 | $included = $transformer->processIncludedResources($scope, ['meh']); |
||
346 | |||
347 | $this->assertFalse($included); |
||
348 | } |
||
349 | |||
350 | /** |
||
351 | * @covers \League\Fractal\TransformerAbstract::item |
||
352 | */ |
||
353 | public function testItem() |
||
354 | { |
||
355 | $mock = m::mock('League\Fractal\TransformerAbstract'); |
||
356 | $item = $mock->item([], function () { |
||
357 | }); |
||
358 | $this->assertInstanceOf('League\Fractal\Resource\Item', $item); |
||
359 | } |
||
360 | |||
361 | /** |
||
362 | * @covers \League\Fractal\TransformerAbstract::collection |
||
363 | */ |
||
364 | public function testCollection() |
||
365 | { |
||
366 | $mock = m::mock('League\Fractal\TransformerAbstract'); |
||
367 | $collection = $mock->collection([], function () { |
||
368 | }); |
||
369 | $this->assertInstanceOf('League\Fractal\Resource\Collection', $collection); |
||
370 | } |
||
371 | |||
372 | public function tearDown() |
||
373 | { |
||
374 | m::close(); |
||
375 | } |
||
376 | } |
||
377 |
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.