DoctrineAdapterAbstractFactoryTest   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 311
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 10
Bugs 3 Features 1
Metric Value
wmc 13
c 10
b 3
f 1
lcom 1
cbo 3
dl 0
loc 311
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 5 1
A testCanCreateService() 0 12 1
B canCreateServiceConfigProvider() 0 117 1
B testCreateService() 0 45 6
A createServiceConfigProvider() 0 68 1
B testInvalidObjectManagerClass() 0 36 3
1
<?php
2
/**
3
 * @author Stefano Torresi (http://stefanotorresi.it)
4
 * @license See the file LICENSE.txt for copying permission.
5
 * ************************************************
6
 */
7
8
namespace Thorr\Persistence\Doctrine\Test\DataMapper\Manager;
9
10
use Doctrine\Common\Persistence\ObjectManager;
11
use PHPUnit_Framework_MockObject_MockObject;
12
use PHPUnit_Framework_TestCase as TestCase;
13
use Thorr\Persistence\Doctrine\DataMapper\DoctrineAdapter;
14
use Thorr\Persistence\Doctrine\DataMapper\Manager\DoctrineAdapterAbstractFactory;
15
use Zend\ServiceManager\Exception as SMException;
16
use Zend\ServiceManager\ServiceLocatorInterface;
17
18
/**
19
 * @covers Thorr\Persistence\Doctrine\DataMapper\Manager\DoctrineAdapterAbstractFactory
20
 */
21
class DoctrineAdapterAbstractFactoryTest extends TestCase
22
{
23
    /**
24
     * @var DoctrineAdapterAbstractFactory
25
     */
26
    protected $abstractFactory;
27
28
    /**
29
     * @var ServiceLocatorInterface|PHPUnit_Framework_MockObject_MockObject
30
     */
31
    protected $serviceLocator;
32
33
    public function setUp()
34
    {
35
        $this->abstractFactory = new DoctrineAdapterAbstractFactory();
36
        $this->serviceLocator  = $this->getMock(ServiceLocatorInterface::class);
37
    }
38
39
    /**
40
     * @param $config
41
     * @param $expectedResult
42
     *
43
     * @dataProvider canCreateServiceConfigProvider()
44
     */
45
    public function testCanCreateService($config, $serviceName, $expectedResult)
46
    {
47
        $this->serviceLocator->expects($this->atLeastOnce())
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Zend\ServiceManager\ServiceLocatorInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
48
                             ->method('get')
49
                             ->with('config')
50
                             ->willReturn($config);
51
52
        $this->assertSame(
53
            $expectedResult,
54
            $this->abstractFactory->canCreateServiceWithName($this->serviceLocator, 'unrelevant', $serviceName)
55
        );
56
    }
57
58
    public function canCreateServiceConfigProvider()
59
    {
60
        return [
61
            [
62
                [],
63
                'anything',
64
                false,
65
            ],
66
            [
67
                [ 'thorr_persistence_dmm' => [] ],
68
                'anything',
69
                false,
70
            ],
71
            [
72
                [
73
                    'thorr_persistence_dmm' => [
74
                        'doctrine' => [],
75
                    ],
76
                ],
77
                'anything',
78
                false,
79
            ],
80
            [
81
                [
82
                    'thorr_persistence_dmm' => [
83
                        'doctrine' => [
84
                            'adapters' => [],
85
                        ],
86
                    ],
87
                ],
88
                'anything',
89
                false,
90
            ],
91
            [
92
                [
93
                    'thorr_persistence_dmm' => [
94
                        'doctrine' => [
95
                            'adapters' => [
96
                                'SomeDataMapperServiceName' => 123,
97
                            ],
98
                        ],
99
                    ],
100
                ],
101
                'SomeDataMapperServiceName',
102
                false,
103
            ],
104
            [
105
                [
106
                    'thorr_persistence_dmm' => [
107
                        'doctrine' => [
108
                            'adapters' => [
109
                                'SomeDataMapperServiceName' => DoctrineAdapter::class,
110
                            ],
111
                        ],
112
                    ],
113
                ],
114
                'SomeDataMapperServiceName',
115
                true,
116
            ],
117
            [
118
                [
119
                    'thorr_persistence_dmm' => [
120
                        'doctrine' => [
121
                            'adapters' => [
122
                                'SomeDataMapperServiceName' => 'not-a-class',
123
                            ],
124
                        ],
125
                    ],
126
                ],
127
                'SomeDataMapperServiceName',
128
                false,
129
            ],
130
            [
131
                [
132
                    'thorr_persistence_dmm' => [
133
                        'doctrine' => [
134
                            'adapters' => [
135
                                'SomeDataMapperServiceName' => [
136
                                    'class' => DoctrineAdapter::class,
137
                                ],
138
                            ],
139
                        ],
140
                    ],
141
                ],
142
                'SomeDataMapperServiceName',
143
                true,
144
            ],
145
            [
146
                [
147
                    'thorr_persistence_dmm' => [
148
                        'doctrine' => [
149
                            'adapters' => [
150
                                'SomeDataMapperServiceName' => [],
151
                            ],
152
                        ],
153
                    ],
154
                ],
155
                'SomeDataMapperServiceName',
156
                false,
157
            ],
158
            [
159
                [
160
                    'thorr_persistence_dmm' => [
161
                        'doctrine' => [
162
                            'adapters' => [
163
                                'SomeDataMapperServiceName' => [
164
                                    'class' => 'not-a-class',
165
                                ],
166
                            ],
167
                        ],
168
                    ],
169
                ],
170
                'SomeDataMapperServiceName',
171
                false,
172
            ],
173
        ];
174
    }
175
176
    /**
177
     * @param $config
178
     * @dataProvider createServiceConfigProvider
179
     */
180
    public function testCreateService($config, $requestedName, $expectedException)
181
    {
182
        $objectManagers = [
183
            'SomeObjectManagerService'    => $this->getMock(ObjectManager::class, [], [], 'SomeObjectManagerService'),
184
            'AnotherObjectManagerService' => $this->getMock(ObjectManager::class, [], [], 'AnotherObjectManagerService'),
185
        ];
186
187
        $this->serviceLocator->expects($this->any())
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Zend\ServiceManager\ServiceLocatorInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
188
            ->method('has')
189
            ->willReturnCallback(function ($name) use ($objectManagers) {
190
                return array_key_exists($name, $objectManagers);
191
            });
192
193
        $this->serviceLocator->expects($this->any())
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Zend\ServiceManager\ServiceLocatorInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
194
            ->method('get')
195
            ->willReturnCallback(function ($name) use ($config, $objectManagers) {
196
                switch ($name) {
197
                    case 'config' :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
198
                        return $config;
199
                    case 'SomeObjectManagerService' :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
200
                    case 'AnotherObjectManagerService' :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
201
                        return $objectManagers[$name];
202
                }
203
            });
204
205
        if ($expectedException) {
206
            $this->setExpectedException($expectedException[0], $expectedException[1]);
207
        }
208
209
        /** @var DoctrineAdapter $instance */
210
        $instance = $this->abstractFactory->createServiceWithName($this->serviceLocator, 'unrelevant', $requestedName);
211
212
        $this->assertInstanceOf(DoctrineAdapter::class, $instance);
213
        $this->assertSame(
214
            $requestedName,
215
            $config['thorr_persistence_dmm']['entity_data_mapper_map'][$instance->getEntityClass()]
216
        );
217
218
        if (isset($config['thorr_persistence_dmm']['doctrine']['adapters'][$requestedName]['object_manager'])) {
219
            $this->assertSame(
220
                $objectManagers[$config['thorr_persistence_dmm']['doctrine']['adapters'][$requestedName]['object_manager']],
221
                $instance->getObjectManager()
222
            );
223
        }
224
    }
225
226
    public function createServiceConfigProvider()
227
    {
228
        return [
229
            [
230
                // $config
231
                [
232
                    'thorr_persistence_dmm' => [
233
                        'doctrine' => [
234
                            'object_manager' => 'SomeObjectManagerService',
235
                            'adapters'       => [
236
                                'SomeDataMapperServiceName' => DoctrineAdapter::class,
237
                            ],
238
                        ],
239
                        'entity_data_mapper_map' => [
240
                            'SomeEntityClass' => 'SomeDataMapperServiceName',
241
                        ],
242
                    ],
243
                ],
244
                // $requestedName
245
                'SomeDataMapperServiceName',
246
                // $expectedException
247
                null,
248
            ],
249
            [
250
                // $config
251
                [
252
                    'thorr_persistence_dmm' => [
253
                        'doctrine' => [
254
                            'object_manager' => 'SomeObjectManagerService',
255
                            'adapters'       => [
256
                                'SomeDataMapperServiceName' => \stdClass::class,
257
                            ],
258
                        ],
259
                        'entity_data_mapper_map' => [
260
                            'SomeEntityClass' => 'SomeDataMapperServiceName',
261
                        ],
262
                    ],
263
                ],
264
                // $requestedName
265
                'SomeDataMapperServiceName',
266
                // $expectedException
267
                [ SMException\ServiceNotCreatedException::class, 'Invalid data mapper type' ],
268
            ],
269
            [
270
                // $config
271
                [
272
                    'thorr_persistence_dmm' => [
273
                        'doctrine' => [
274
                            'object_manager' => 'SomeObjectManagerService',
275
                            'adapters'       => [
276
                                'SomeDataMapperServiceName' => [
277
                                    'class'          => DoctrineAdapter::class,
278
                                    'object_manager' => 'AnotherObjectManagerService',
279
                                ],
280
                            ],
281
                        ],
282
                        'entity_data_mapper_map' => [
283
                            'SomeEntityClass' => 'SomeDataMapperServiceName',
284
                        ],
285
                    ],
286
                ],
287
                // $requestedName
288
                'SomeDataMapperServiceName',
289
                // $expectedException
290
                null,
291
            ],
292
        ];
293
    }
294
295
    public function testInvalidObjectManagerClass()
296
    {
297
        $config = [
298
            'thorr_persistence_dmm' => [
299
                'doctrine' => [
300
                    'object_manager' => 'SomeObjectManagerService',
301
                    'adapters'       => [
302
                        'SomeDataMapperServiceName' => DoctrineAdapter::class,
303
                    ],
304
                ],
305
                'entity_data_mapper_map' => [],
306
            ],
307
        ];
308
309
        $objectManager = $this->getMock(\stdClass::class);
310
311
        $this->serviceLocator->expects($this->atLeastOnce())
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Zend\ServiceManager\ServiceLocatorInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
312
            ->method('has')
313
            ->with($config['thorr_persistence_dmm']['doctrine']['object_manager'])
314
            ->willReturn(true);
315
316
        $this->serviceLocator->expects($this->atLeastOnce())
317
            ->method('get')
318
            ->willReturnCallback(function ($name) use ($config, $objectManager) {
319
                switch ($name) {
320
                    case 'config' :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
321
                        return $config;
322
                    case $config['thorr_persistence_dmm']['doctrine']['object_manager'] :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
323
                        return $objectManager;
324
                }
325
            });
326
327
        $this->setExpectedException(SMException\InvalidServiceNameException::class, 'Invalid object manager type');
328
329
        $this->abstractFactory->createServiceWithName($this->serviceLocator, 'unrelevant', 'SomeDataMapperServiceName');
330
    }
331
}
332