Completed
Push — master ( c9f3b4...e7d2cf )
by Bogdan
01:58
created

ContainerHydratorTest   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 300
Duplicated Lines 49 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 16
c 2
b 0
f 0
lcom 1
cbo 5
dl 147
loc 300
rs 10

16 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 4 1
A testEmptyAdding() 12 12 1
A testArrayAccessAdding() 13 13 1
A testAddFromNotArrayNorArrayAcces() 0 8 1
A testAddingScalar() 16 16 1
A testAddingConcrete() 16 16 1
A testSharingConcrete() 18 18 1
A testAddingAlias() 16 16 1
A testAddingShared() 0 19 1
B testAddingAliasWithArgument() 27 27 1
B testAddingAliasWithMethodCalls() 29 29 1
B testAddingAliasWithArgumentsAndMethodCalls() 0 35 1
A testAddDefinition() 0 20 1
A getContainerMock() 0 4 1
A getDefinitionMock() 0 4 1
A getClassDefinitionMock() 0 6 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
2
3
/*
4
  +----------------------------------------------------------------------+
5
  | This file is part of the pinepain/container-extras PHP library.      |
6
  |                                                                      |
7
  | Copyright (c) 2015-2016 Bogdan Padalko <[email protected]>          |
8
  |                                                                      |
9
  | Licensed under the MIT license: http://opensource.org/licenses/MIT   |
10
  |                                                                      |
11
  | For the full copyright and license information, please view the      |
12
  | LICENSE file that was distributed with this source or visit          |
13
  | http://opensource.org/licenses/MIT                                   |
14
  +----------------------------------------------------------------------+
15
*/
16
17
/* Based on League\Container\Test\ContainerTest
18
 * v1.x class (https://github.com/thephpleague/container/blob/1.x/tests/ContainerTest.php)
19
 * which is authored by Phil Bennett (https://github.com/philipobenito)
20
 * and other contributors (https://github.com/thephpleague/container/contributors).
21
 */
22
23
24
namespace Pinepain\Container\Extras\Tests;
25
26
27
use League\Container\ContainerInterface;
28
use League\Container\Definition\ClassDefinition;
29
use League\Container\Definition\DefinitionInterface;
30
use Pinepain\Container\Extras\ContainerHydrator;
31
use stdClass;
32
33
34
class ContainerHydratorTest extends \PHPUnit_Framework_TestCase
35
{
36
    protected $configArray = [
37
        'League\Container\Test\Asset\Foo' => [
38
            'class'     => 'League\Container\Test\Asset\Foo',
39
            'arguments' => ['League\Container\Test\Asset\Bar'],
40
            'methods'   => [
41
                'injectBaz' => ['League\Container\Test\Asset\Baz'],
42
            ],
43
        ],
44
        'League\Container\Test\Asset\Bar' => [
45
            'definition' => 'League\Container\Test\Asset\Bar',
46
            'arguments'  => ['League\Container\Test\Asset\Baz'],
47
        ],
48
        'League\Container\Test\Asset\Baz' => 'League\Container\Test\Asset\Baz',
49
    ];
50
51
    /**
52
     * @var ContainerHydrator
53
     */
54
    private $obj;
55
56
    public function setUp()
57
    {
58
        $this->obj = new ContainerHydrator();
59
    }
60
61 View Code Duplication
    public function testEmptyAdding()
1 ignored issue
show
Duplication introduced by
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...
62
    {
63
        $loader = $this->obj;
64
65
        $config = [];
66
67
        $container = $this->getContainerMock();
68
69
        $container->expects($this->never())->method('add');
70
71
        $loader->populate($container, $config);
72
    }
73
74 View Code Duplication
    public function testArrayAccessAdding()
1 ignored issue
show
Duplication introduced by
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...
75
    {
76
        $loader = $this->obj;
77
78
        /** @var \Iterator | \PHPUnit_Framework_MockObject_MockObject $config */
79
        $config = $this->getMockBuilder('\Iterator')->getMockForAbstractClass();
80
81
        $container = $this->getContainerMock();
82
83
        $container->expects($this->never())->method('add');
84
85
        $loader->populate($container, $config);
86
    }
87
88
    /**
89
     * @expectedException \InvalidArgumentException
90
     * @expectedExceptionMessage You can only load definitions from an array or an object that implements Traversable
91
     *                           interface.
92
     */
93
    public function testAddFromNotArrayNorArrayAcces()
94
    {
95
        $loader = $this->obj;
96
97
        $container = $this->getContainerMock();
98
99
        $loader->populate($container, new stdClass());
1 ignored issue
show
Documentation introduced by
new \stdClass() is of type object<stdClass>, but the function expects a array|object<Traversable>.

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...
100
    }
101
102 View Code Duplication
    public function testAddingScalar()
1 ignored issue
show
Duplication introduced by
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...
103
    {
104
        $loader = $this->obj;
105
106
        $config = [
107
            'key' => 'value',
108
        ];
109
110
        $container = $this->getContainerMock();
111
112
        $container->expects($this->once())
113
                  ->method('add')
114
                  ->with('key', 'value', false);
115
116
        $loader->populate($container, $config);
117
    }
118
119 View Code Duplication
    public function testAddingConcrete()
1 ignored issue
show
Duplication introduced by
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...
120
    {
121
        $loader = $this->obj;
122
123
        $config = [
124
            'TestInterface' => null,
125
        ];
126
127
        $container = $this->getContainerMock();
128
129
        $container->expects($this->once())
130
                  ->method('add')
131
                  ->with('TestInterface', null, false);
132
133
        $loader->populate($container, $config);
134
    }
135
136 View Code Duplication
    public function testSharingConcrete()
1 ignored issue
show
Duplication introduced by
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...
137
    {
138
        $loader = $this->obj;
139
140
        $config = [
141
            'TestInterface' => [
142
                'share' => true,
143
            ],
144
        ];
145
146
        $container = $this->getContainerMock();
147
148
        $container->expects($this->once())
149
                  ->method('add')
150
                  ->with('TestInterface', null, true);
151
152
        $loader->populate($container, $config);
153
    }
154
155 View Code Duplication
    public function testAddingAlias()
1 ignored issue
show
Duplication introduced by
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...
156
    {
157
        $loader = $this->obj;
158
159
        $config = [
160
            'TestInterface' => 'Test',
161
        ];
162
163
        $container = $this->getContainerMock();
164
165
        $container->expects($this->once())
166
                  ->method('add')
167
                  ->with('TestInterface', 'Test', false);
168
169
        $loader->populate($container, $config);
170
    }
171
172
    public function testAddingShared()
173
    {
174
        $loader = $this->obj;
175
176
        $config = [
177
            'TestInterface' => [
178
                'class' => 'Test',
179
                'share' => true,
180
            ],
181
        ];
182
183
        $container = $this->getContainerMock();
184
185
        $container->expects($this->once())
186
                  ->method('add')
187
                  ->with('TestInterface', 'Test', true);
188
189
        $loader->populate($container, $config);
190
    }
191
192 View Code Duplication
    public function testAddingAliasWithArgument()
1 ignored issue
show
Duplication introduced by
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...
193
    {
194
        $loader = $this->obj;
195
196
        $config = [
197
            'TestInterface' => [
198
                'class'     => 'TestWithArguments',
199
                'arguments' => ['test', 'arguments'],
200
            ],
201
        ];
202
203
        $container = $this->getContainerMock();
204
205
        $definition = $this->getDefinitionMock();
206
207
        $definition->expects($this->once())
208
                   ->method('withArguments')
209
                   ->with(['test', 'arguments'])
210
                   ->willReturn($definition);
211
212
        $container->expects($this->once())
213
                  ->method('add')
214
                  ->with('TestInterface', 'TestWithArguments', false)
215
                  ->willReturn($definition);
216
217
        $loader->populate($container, $config);
218
    }
219
220
221 View Code Duplication
    public function testAddingAliasWithMethodCalls()
1 ignored issue
show
Duplication introduced by
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...
222
    {
223
        $loader = $this->obj;
224
225
        $config = [
226
            'TestInterface' => [
227
                'class'   => 'TestWithMethodCalls',
228
                'methods' => [
229
                    'setValue' => ['test value'],
230
                ],
231
            ],
232
        ];
233
234
        $container = $this->getContainerMock();
235
236
        $definition = $this->getClassDefinitionMock();
237
238
        $definition->expects($this->once())
1 ignored issue
show
Bug introduced by
The method expects() does not seem to exist on object<League\Container\...nition\ClassDefinition>.

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...
239
                   ->method('withMethodCalls')
240
                   ->with(['setValue' => ['test value']])
241
                   ->willReturn($definition);
242
243
        $container->expects($this->once())
244
                  ->method('add')
245
                  ->with('TestInterface', 'TestWithMethodCalls', false)
246
                  ->willReturn($definition);
247
248
        $loader->populate($container, $config);
249
    }
250
251
    public function testAddingAliasWithArgumentsAndMethodCalls()
252
    {
253
        $loader = $this->obj;
254
255
        $config = [
256
            'TestInterface' => [
257
                'class'     => 'TestWithArgumentsAndMethodCalls',
258
                'arguments' => ['test', 'arguments'],
259
                'methods'   => [
260
                    'setValue' => ['test value'],
261
                ],
262
            ],
263
        ];
264
265
        $container = $this->getContainerMock();
266
267
        $definition = $this->getClassDefinitionMock();
268
269
        $definition->expects($this->once())
1 ignored issue
show
Bug introduced by
The method expects() does not seem to exist on object<League\Container\...nition\ClassDefinition>.

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...
270
                   ->method('withArguments')
271
                   ->with(['test', 'arguments'])
272
                   ->willReturn($definition);
273
274
        $definition->expects($this->once())
1 ignored issue
show
Bug introduced by
The method expects() does not seem to exist on object<League\Container\...nition\ClassDefinition>.

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...
275
                   ->method('withMethodCalls')
276
                   ->with(['setValue' => ['test value']])
277
                   ->willReturn($definition);
278
279
        $container->expects($this->once())
280
                  ->method('add')
281
                  ->with('TestInterface', 'TestWithArgumentsAndMethodCalls', false)
282
                  ->willReturn($definition);
283
284
        $loader->populate($container, $config);
285
    }
286
287
    public function testAddDefinition()
288
    {
289
        $loader = $this->obj;
290
291
        $definition = $this->getClassDefinitionMock();
292
293
        $config = [
294
            'TestInterface' => [
295
                'definition' => $definition,
296
            ],
297
        ];
298
299
        $container = $this->getContainerMock();
300
301
        $container->expects($this->once())
302
                  ->method('add')
303
                  ->with('TestInterface', $definition, false);
304
305
        $loader->populate($container, $config);
306
    }
307
308
    /**
309
     * @return ContainerInterface | \PHPUnit_Framework_MockObject_MockObject
310
     */
311
    public function getContainerMock()
312
    {
313
        return $this->getMockBuilder('\League\Container\ContainerInterface')->getMockForAbstractClass();
314
    }
315
316
    /**
317
     * @return DefinitionInterface | \PHPUnit_Framework_MockObject_MockObject
318
     */
319
    public function getDefinitionMock()
320
    {
321
        return $this->getMockBuilder('\League\Container\Definition\DefinitionInterface')->getMockForAbstractClass();
322
    }
323
324
    /**
325
     * @return ClassDefinition | \PHPUnit_Framework_MockObject_MockObject
326
     */
327
    public function getClassDefinitionMock()
328
    {
329
        return $this->getMockBuilder('\League\Container\Definition\ClassDefinition')
330
                    ->disableOriginalConstructor()
331
                    ->getMock();
332
    }
333
}
334