GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

TransformerAbstractTest   A
last analyzed

Coupling/Cohesion

Components 3
Dependencies 9

Complexity

Total Complexity 23

Size/Duplication

Total Lines 366
Duplicated Lines 48.63 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 23
c 1
b 1
f 0
lcom 3
cbo 9
dl 178
loc 366
rs 10

23 Methods

Rating   Name   Duplication   Size   Complexity  
A testSetAvailableIncludes() 0 5 1
A testSetDefaultIncludes() 0 5 1
A testSetCurrentScope() 7 7 1
A testGetCurrentScope() 8 8 1
A testProcessEmbeddedResourcesNoAvailableIncludes() 10 10 1
A testProcessEmbeddedResourcesNoDefaultIncludes() 10 10 1
A testItem() 0 7 1
A testCollection() 0 7 1
A tearDown() 0 4 1
A testGetAvailableIncludes() 7 7 1
A testGetDefaultIncludes() 7 7 1
A testProcessEmbeddedResourcesInvalidAvailableEmbed() 0 13 1
A testProcessEmbeddedResourcesInvalidDefaultEmbed() 11 11 1
A testProcessIncludedAvailableResources() 17 17 1
A testProcessExcludedAvailableResources() 23 23 1
A testProcessExcludedDefaultResources() 22 22 1
A testProcessIncludedAvailableResourcesEmptyEmbed() 0 14 1
A testCallEmbedMethodReturnsCrap() 12 12 1
A testProcessEmbeddedDefaultResources() 16 16 1
A testIncludedItem() 17 17 1
A testParamBagIsProvidedForIncludes() 0 15 1
A testIncludedCollection() 0 22 1
A testProcessEmbeddedDefaultResourcesEmptyEmbed() 11 11 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
9
class TransformerAbstractTest extends \PHPUnit_Framework_TestCase
10
{
11
    /**
12
     * @covers League\Fractal\TransformerAbstract::setAvailableIncludes
13
     */
14
    public function testSetAvailableIncludes()
15
    {
16
        $transformer = m::mock('League\Fractal\TransformerAbstract')->makePartial();
17
        $this->assertInstanceOf('League\Fractal\TransformerAbstract', $transformer->setAvailableIncludes(['foo']));
18
    }
19
20
    /**
21
     * @covers League\Fractal\TransformerAbstract::getAvailableIncludes
22
     */
23 View Code Duplication
    public function testGetAvailableIncludes()
24
    {
25
        $transformer = m::mock('League\Fractal\TransformerAbstract')->makePartial();
26
27
        $transformer->setAvailableIncludes(['foo', 'bar']);
28
        $this->assertSame(['foo', 'bar'], $transformer->getAvailableIncludes());
29
    }
30
31
    /**
32
     * @covers League\Fractal\TransformerAbstract::setDefaultIncludes
33
     */
34
    public function testSetDefaultIncludes()
35
    {
36
        $transformer = m::mock('League\Fractal\TransformerAbstract')->makePartial();
37
        $this->assertInstanceOf('League\Fractal\TransformerAbstract', $transformer->setDefaultIncludes(['foo']));
38
    }
39
40
    /**
41
     * @covers League\Fractal\TransformerAbstract::getDefaultIncludes
42
     */
43 View Code Duplication
    public function testGetDefaultIncludes()
44
    {
45
        $transformer = m::mock('League\Fractal\TransformerAbstract')->makePartial();
46
47
        $transformer->setDefaultIncludes(['foo', 'bar']);
48
        $this->assertSame(['foo', 'bar'], $transformer->getDefaultIncludes());
49
    }
50
51
    /**
52
     * @covers League\Fractal\TransformerAbstract::setCurrentScope
53
     */
54 View Code Duplication
    public function testSetCurrentScope()
55
    {
56
        $transformer = $this->getMockForAbstractClass('League\Fractal\TransformerAbstract');
57
        $manager = new Manager();
58
        $scope = new Scope($manager, m::mock('League\Fractal\Resource\ResourceAbstract'));
59
        $this->assertInstanceOf('League\Fractal\TransformerAbstract', $transformer->setCurrentScope($scope));
60
    }
61
62
    /**
63
     * @covers League\Fractal\TransformerAbstract::getCurrentScope
64
     */
65 View Code Duplication
    public function testGetCurrentScope()
66
    {
67
        $transformer = m::mock('League\Fractal\TransformerAbstract')->makePartial();
68
        $manager = new Manager();
69
        $scope = new Scope($manager, m::mock('League\Fractal\Resource\ResourceAbstract'));
70
        $transformer->setCurrentScope($scope);
71
        $this->assertSame($transformer->getCurrentScope(), $scope);
72
    }
73
74 View Code Duplication
    public function testProcessEmbeddedResourcesNoAvailableIncludes()
75
    {
76
        $transformer = m::mock('League\Fractal\TransformerAbstract')->makePartial();
77
78
        $manager = new Manager();
79
        $manager->parseIncludes('foo');
80
81
        $scope = new Scope($manager, m::mock('League\Fractal\Resource\ResourceAbstract'));
82
        $this->assertFalse($transformer->processIncludedResources($scope, ['some' => 'data']));
83
    }
84
85 View Code Duplication
    public function testProcessEmbeddedResourcesNoDefaultIncludes()
86
    {
87
        $transformer = m::mock('League\Fractal\TransformerAbstract')->makePartial();
88
89
        $manager = new Manager();
90
        $manager->parseIncludes('foo');
91
92
        $scope = new Scope($manager, m::mock('League\Fractal\Resource\ResourceAbstract'));
93
        $this->assertFalse($transformer->processIncludedResources($scope, ['some' => 'data']));
94
    }
95
96
    /**
97
     * @covers League\Fractal\TransformerAbstract::processIncludedResources
98
     * @covers League\Fractal\TransformerAbstract::callIncludeMethod
99
     * @expectedException BadMethodCallException
100
     */
101
    public function testProcessEmbeddedResourcesInvalidAvailableEmbed()
102
    {
103
        $transformer = m::mock('League\Fractal\TransformerAbstract')->makePartial();
104
105
        $manager = new Manager();
106
        $manager->parseIncludes('book');
107
108
        $scope = new Scope($manager, m::mock('League\Fractal\Resource\ResourceAbstract'));
109
        $transformer->setCurrentScope($scope);
110
111
        $transformer->setAvailableIncludes(['book']);
112
        $transformer->processIncludedResources($scope, []);
113
    }
114
115
    /**
116
     * @covers League\Fractal\TransformerAbstract::processIncludedResources
117
     * @covers League\Fractal\TransformerAbstract::callIncludeMethod
118
     * @expectedException BadMethodCallException
119
     */
120 View Code Duplication
    public function testProcessEmbeddedResourcesInvalidDefaultEmbed()
121
    {
122
        $transformer = m::mock('League\Fractal\TransformerAbstract')->makePartial();
123
124
        $manager = new Manager();
125
126
        $scope = new Scope($manager, m::mock('League\Fractal\Resource\ResourceAbstract'));
127
128
        $transformer->setDefaultIncludes(['book']);
129
        $transformer->processIncludedResources($scope, []);
130
    }
131
132
    /**
133
     * @covers League\Fractal\TransformerAbstract::processIncludedResources
134
     * @covers League\Fractal\TransformerAbstract::callIncludeMethod
135
     */
136 View Code Duplication
    public function testProcessIncludedAvailableResources()
137
    {
138
        $manager = new Manager();
139
        $manager->parseIncludes('book');
140
        $transformer = m::mock('League\Fractal\TransformerAbstract[transform]');
141
142
        $transformer->shouldReceive('includeBook')->once()->andReturnUsing(function ($data) {
143
            return new Item(['included' => 'thing'], function ($data) {
144
                return $data;
145
            });
146
        });
147
148
        $transformer->setAvailableIncludes(['book', 'publisher']);
149
        $scope = new Scope($manager, new Item([], $transformer));
150
        $included = $transformer->processIncludedResources($scope, ['meh']);
151
        $this->assertSame(['book' => ['data' => ['included' => 'thing']]], $included);
152
    }
153
154
    /**
155
     * @covers League\Fractal\TransformerAbstract::processIncludedResources
156
     * @covers League\Fractal\TransformerAbstract::figureOutWhichIncludes
157
     */
158 View Code Duplication
    public function testProcessExcludedAvailableResources()
0 ignored issues
show
Duplication introduced by Jason Daly
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...
159
    {
160
        $manager = new Manager();
161
        $transformer = m::mock('League\Fractal\TransformerAbstract[transform]');
162
        $scope = new Scope($manager, new Item([], $transformer));
163
164
        $transformer->shouldReceive('includeBook')->never();
165
166
        $transformer->shouldReceive('includePublisher')->once()->andReturnUsing(function ($data) {
0 ignored issues
show
Unused Code introduced by Jason Daly
The parameter $data is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
167
            return new Item(['another' => 'thing'], function ($data) {
168
                return $data;
169
            });
170
        });
171
172
        // available includes that have been requested are excluded
173
        $manager->parseIncludes('book,publisher');
174
        $manager->parseExcludes('book');
175
176
        $transformer->setAvailableIncludes(['book', 'publisher']);
0 ignored issues
show
Bug introduced by Jason Daly
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...
177
178
        $included = $transformer->processIncludedResources($scope, ['meh']);
0 ignored issues
show
Bug introduced by Jason Daly
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...
179
        $this->assertSame(['publisher' => ['data' => ['another' => 'thing']]], $included);
180
    }
181
182
    /**
183
     * @covers League\Fractal\TransformerAbstract::processIncludedResources
184
     * @covers League\Fractal\TransformerAbstract::figureOutWhichIncludes
185
     */
186 View Code Duplication
    public function testProcessExcludedDefaultResources()
0 ignored issues
show
Duplication introduced by Jason Daly
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...
187
    {
188
        $manager = new Manager();
189
        $transformer = m::mock('League\Fractal\TransformerAbstract[transform]');
190
        $scope = new Scope($manager, new Item([], $transformer));
191
192
        $transformer->shouldReceive('includeBook')->never();
193
194
        $transformer->shouldReceive('includePublisher')->once()->andReturnUsing(function ($data) {
0 ignored issues
show
Unused Code introduced by Jason Daly
The parameter $data is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
195
            return new Item(['another' => 'thing'], function ($data) {
196
                return $data;
197
            });
198
        });
199
200
        $manager->parseIncludes('book,publisher');
201
        $manager->parseExcludes('book');
202
203
        $transformer->setDefaultIncludes(['book', 'publisher']);
0 ignored issues
show
Bug introduced by Jason Daly
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...
204
205
        $included = $transformer->processIncludedResources($scope, ['meh']);
0 ignored issues
show
Bug introduced by Jason Daly
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...
206
        $this->assertSame(['publisher' => ['data' => ['another' => 'thing']]], $included);
207
    }
208
209
    /**
210
     * @covers League\Fractal\TransformerAbstract::processIncludedResources
211
     * @covers League\Fractal\TransformerAbstract::callIncludeMethod
212
     */
213
    public function testProcessIncludedAvailableResourcesEmptyEmbed()
214
    {
215
        $manager = new Manager();
216
        $manager->parseIncludes(['book']);
217
        $transformer = m::mock('League\Fractal\TransformerAbstract[transform]');
218
219
        $transformer->shouldReceive('includeBook')->once()->andReturn(null);
220
221
        $transformer->setAvailableIncludes(['book']);
0 ignored issues
show
Bug introduced by Andreas Möller
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...
222
        $scope = new Scope($manager, new Item([], $transformer));
223
        $included = $transformer->processIncludedResources($scope, ['meh']);
0 ignored issues
show
Bug introduced by Andreas Möller
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...
224
225
        $this->assertFalse($included);
226
    }
227
228
    /**
229
     * @covers League\Fractal\TransformerAbstract::callIncludeMethod
230
     * @expectedException Exception
231
     * @expectedExceptionMessage Invalid return value from League\Fractal\TransformerAbstract::includeBook().
232
     */
233 View Code Duplication
    public function testCallEmbedMethodReturnsCrap()
234
    {
235
        $manager = new Manager();
236
        $manager->parseIncludes('book');
237
        $transformer = m::mock('League\Fractal\TransformerAbstract[transform]');
238
239
        $transformer->shouldReceive('includeBook')->once()->andReturn(new \stdClass());
240
241
        $transformer->setAvailableIncludes(['book']);
0 ignored issues
show
Bug introduced by Andreas Möller
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...
242
        $scope = new Scope($manager, new Item([], $transformer));
243
        $transformer->processIncludedResources($scope, ['meh']);
244
    }
245
246
    /**
247
     * @covers League\Fractal\TransformerAbstract::processIncludedResources
248
     * @covers League\Fractal\TransformerAbstract::callIncludeMethod
249
     */
250 View Code Duplication
    public function testProcessEmbeddedDefaultResources()
251
    {
252
        $manager = new Manager();
253
        $transformer = m::mock('League\Fractal\TransformerAbstract[transform]');
254
255
        $transformer->shouldReceive('includeBook')->once()->andReturnUsing(function ($data) {
0 ignored issues
show
Unused Code introduced by Phil Sturgeon
The parameter $data is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
256
            return new Item(['included' => 'thing'], function ($data) {
257
                return $data;
258
            });
259
        });
260
261
        $transformer->setDefaultIncludes(['book']);
0 ignored issues
show
Bug introduced by Andreas Möller
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...
262
        $scope = new Scope($manager, new Item([], $transformer));
263
        $included = $transformer->processIncludedResources($scope, ['meh']);
0 ignored issues
show
Bug introduced by Andreas Möller
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...
264
        $this->assertSame(['book' => ['data' => ['included' => 'thing']]], $included);
265
    }
266
267
    /**
268
     * @covers League\Fractal\TransformerAbstract::processIncludedResources
269
     * @covers League\Fractal\TransformerAbstract::callIncludeMethod
270
     */
271 View Code Duplication
    public function testIncludedItem()
272
    {
273
        $manager = new Manager();
274
        $manager->parseIncludes('book');
275
276
        $transformer = m::mock('League\Fractal\TransformerAbstract[transform]');
277
        $transformer->shouldReceive('includeBook')->once()->andReturnUsing(function ($data) {
0 ignored issues
show
Unused Code introduced by Phil Sturgeon
The parameter $data is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
278
            return new Item(['included' => 'thing'], function ($data) {
279
                return $data;
280
            });
281
        });
282
283
        $transformer->setAvailableIncludes(['book']);
0 ignored issues
show
Bug introduced by Andreas Möller
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...
284
        $scope = new Scope($manager, new Item([], $transformer));
285
        $included = $transformer->processIncludedResources($scope, ['meh']);
0 ignored issues
show
Bug introduced by Andreas Möller
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...
286
        $this->assertSame(['book' => ['data' => ['included' => 'thing']]], $included);
287
    }
288
289
    public function testParamBagIsProvidedForIncludes()
290
    {
291
        $manager = new Manager();
292
        $manager->parseIncludes('book:foo(bar)');
293
294
        $transformer = m::mock('League\Fractal\TransformerAbstract')->makePartial();
295
296
        $transformer->shouldReceive('includeBook')
297
            ->with(m::any(), m::type('\League\Fractal\ParamBag'))
298
            ->once();
299
300
        $transformer->setAvailableIncludes(['book']);
301
        $scope = new Scope($manager, new Item([], $transformer));
302
        $included = $transformer->processIncludedResources($scope, []);
303
    }
304
305
    /**
306
     * @covers League\Fractal\TransformerAbstract::processIncludedResources
307
     * @covers League\Fractal\TransformerAbstract::callIncludeMethod
308
     */
309
    public function testIncludedCollection()
310
    {
311
        $manager = new Manager();
312
        $manager->parseIncludes('book');
313
314
        $collectionData = [
315
            ['included' => 'thing'],
316
            ['another' => 'thing'],
317
        ];
318
319
        $transformer = m::mock('League\Fractal\TransformerAbstract[transform]');
320
        $transformer->shouldReceive('includeBook')->once()->andReturnUsing(function ($data) use ($collectionData) {
321
            return new Collection($collectionData, function ($data) {
322
                return $data;
323
            });
324
        });
325
326
        $transformer->setAvailableIncludes(['book']);
0 ignored issues
show
Bug introduced by Andreas Möller
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...
327
        $scope = new Scope($manager, new Collection([], $transformer));
328
        $included = $transformer->processIncludedResources($scope, ['meh']);
0 ignored issues
show
Bug introduced by Andreas Möller
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...
329
        $this->assertSame(['book' => ['data' => $collectionData]], $included);
330
    }
331
332
    /**
333
     * @covers League\Fractal\TransformerAbstract::processIncludedResources
334
     * @covers League\Fractal\TransformerAbstract::callIncludeMethod
335
     */
336 View Code Duplication
    public function testProcessEmbeddedDefaultResourcesEmptyEmbed()
337
    {
338
        $transformer = m::mock('League\Fractal\TransformerAbstract[transform]');
339
        $transformer->shouldReceive('includeBook')->once()->andReturn(null);
340
341
        $transformer->setDefaultIncludes(['book']);
0 ignored issues
show
Bug introduced by Andreas Möller
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...
342
        $scope = new Scope(new Manager(), new Item([], $transformer));
343
        $included = $transformer->processIncludedResources($scope, ['meh']);
0 ignored issues
show
Bug introduced by Andreas Möller
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...
344
345
        $this->assertFalse($included);
346
    }
347
348
    /**
349
     * @covers League\Fractal\TransformerAbstract::item
350
     */
351
    public function testItem()
352
    {
353
        $mock = m::mock('League\Fractal\TransformerAbstract');
354
        $item = $mock->item([], function () {
355
        });
356
        $this->assertInstanceOf('League\Fractal\Resource\Item', $item);
357
    }
358
359
    /**
360
     * @covers League\Fractal\TransformerAbstract::collection
361
     */
362
    public function testCollection()
363
    {
364
        $mock = m::mock('League\Fractal\TransformerAbstract');
365
        $collection = $mock->collection([], function () {
366
        });
367
        $this->assertInstanceOf('League\Fractal\Resource\Collection', $collection);
368
    }
369
370
    public function tearDown()
371
    {
372
        m::close();
373
    }
374
}
375