Completed
Push — master ( d9ba74...595cfb )
by Stefano
20:57
created

testConstructorNeedsADataMapperManagerConfigIfNotNull()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
rs 9.6666
cc 1
eloc 5
nc 1
nop 0
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\Test\DataMapper\Manager;
9
10
use InvalidArgumentException;
11
use PHPUnit_Framework_TestCase as TestCase;
12
use Thorr\Persistence\DataMapper\DataMapperInterface;
13
use Thorr\Persistence\DataMapper\Manager\DataMapperManager;
14
use Thorr\Persistence\DataMapper\Manager\DataMapperManagerConfig;
15
use Thorr\Persistence\Entity\SluggableInterface;
16
use Thorr\Persistence\Test\Asset;
17
use Zend\ServiceManager\Config;
18
use Zend\ServiceManager\Exception\RuntimeException;
19
use Zend\ServiceManager\ServiceLocatorInterface;
20
21
class DataMapperManagerTest extends TestCase
22
{
23
    /**
24
     * @param mixed $dataMapper
25
     * @param array $expectedException
26
     * @dataProvider validatePluginProvider
27
     */
28
    public function testValidatePlugin($dataMapper, $expectedException)
29
    {
30
        $dataMapperManager = new DataMapperManager();
31
32
        if (is_callable($dataMapper)) {
33
            $dataMapper = $dataMapper();
34
        }
35
36
        if ($expectedException) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $expectedException of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
37
            $this->setExpectedException($expectedException[0], $expectedException[1]);
38
        }
39
40
        $dataMapperManager->validatePlugin($dataMapper);
41
    }
42
43
    public function validatePluginProvider()
44
    {
45
        return [
46
            [
47
                // $dataMapper
48
                new \stdClass(),
49
                // $expectedException
50
                [ InvalidArgumentException::class, 'Invalid data mapper'],
51
            ],
52
            [
53
                // $dataMapper
54
                $this->getMock(DataMapperInterface::class),
55
                // $expectedException
56
                [ InvalidArgumentException::class, 'Invalid entity class' ],
57
            ],
58
            [
59
                // $dataMapper
60
                function () {
61
                    $mock = $this->getMock(DataMapperInterface::class);
62
                    $mock->expects($this->any())->method('getEntityClass')->willReturn(Asset\Entity::class);
63
64
                    return $mock;
65
                },
66
                // $expectedException
67
                null,
68
            ],
69
        ];
70
    }
71
72
    /**
73
     * @param array  $config
74
     * @param string $requestedDataMapperEntity
75
     * @param bool   $expectedException
76
     * @dataProvider configProvider
77
     */
78
    public function testGetDataMapperForEntity($config, $requestedDataMapperEntity, $expectedException)
79
    {
80
        $dataMapperManager = new DataMapperManager(new DataMapperManagerConfig($config));
81
82
        $serviceLocator = $this->getMock(ServiceLocatorInterface::class);
83
        $serviceLocator->expects($this->any())
84
                        ->method('get')
85
                        ->with('config')
86
                        ->willReturn($config);
87
88
        $dataMapperManager->setServiceLocator($serviceLocator);
89
90
        if (is_string($expectedException)) {
91
            $this->setExpectedException($expectedException);
92
        }
93
94
        if (is_array($expectedException)) {
95
            $this->setExpectedException($expectedException[0], $expectedException[1]);
96
        }
97
98
        $dataMapper = $dataMapperManager->getDataMapperForEntity($requestedDataMapperEntity);
99
100
        if ($expectedException) {
101
            return;
102
        }
103
        $this->assertInstanceOf(DataMapperInterface::class, $dataMapper);
104
    }
105
106
    /**
107
     * @see testGetDataMapperForEntity()
108
     *
109
     * @return array
110
     */
111
    public function configProvider()
112
    {
113
        return [
114
            'empty config' => [
115
                // $config
116
                [],
117
118
                // $requestedDataMapperEntity
119
                'anything',
120
121
                // $expectedException
122
                [ InvalidArgumentException::class, 'Could not find data mapper service name' ],
123
            ],
124
            'no data mappers' => [
125
                // $config
126
                [
127
                    'entity_data_mapper_map' => [],
128
                ],
129
130
                // $requestedDataMapperEntity
131
                'anything',
132
133
                // $expectedException
134
                InvalidArgumentException::class,
135
            ],
136
            'valid data mapper config' => [
137
                // $config
138
                [
139
                    'entity_data_mapper_map' => [
140
                        Asset\Entity::class => 'SomeDataMapperServiceName',
141
                    ],
142
                    'factories' => [
143
                        'SomeDataMapperServiceName' => function () {
144
                            $mock = $this->getMock(DataMapperInterface::class);
145
                            $mock->expects($this->any())->method('getEntityClass')->willReturn(Asset\Entity::class);
146
147
                            return $mock;
148
                        },
149
                    ],
150
                ],
151
152
                // $requestedDataMapperEntity
153
                Asset\Entity::class,
154
155
                // $expectedException
156
                null,
157
            ],
158
            'inexistent service' => [
159
                // $config
160
                [
161
                    'entity_data_mapper_map' => [
162
                        Asset\AnotherEntity::class    => 'SomeDataMapperServiceName',
163
                    ],
164
                ],
165
166
                // $requestedDataMapperEntity
167
                Asset\Entity::class,
168
169
                // $expectedException
170
                [ InvalidArgumentException::class, 'Could not find data mapper service name'],
171
            ],
172
            [
173
                // $config
174
                [
175
                    'entity_data_mapper_map' => [
176
                        Asset\Entity::class           => 'SomeDataMapperServiceName',
177
                        Asset\AnotherEntity::class    => 'SomeDataMapperServiceName',
178
                    ],
179
                    'factories' => [
180
                        'SomeDataMapperServiceName' => function () {
181
                            $mock = $this->getMock(DataMapperInterface::class);
182
                            $mock->expects($this->any())->method('getEntityClass')->willReturn(Asset\AnotherEntity::class);
183
184
                            return $mock;
185
                        },
186
                    ],
187
                ],
188
189
                // $requestedDataMapperEntity
190
                Asset\Entity::class,
191
192
                // $expectedException
193
                [ RuntimeException::class, 'entity class mismatch' ],
194
            ],
195
            [
196
                // $config
197
                [
198
                    'entity_data_mapper_map' => [
199
                        Asset\Entity::class           => 'SomeDataMapperServiceName',
200
                        Asset\AnotherEntity::class    => 'SomeDataMapperServiceName',
201
                    ],
202
                    'factories' => [
203
                        'SomeDataMapperServiceName' => function () {
204
                            $mock = $this->getMock(DataMapperInterface::class);
205
                            $mock->expects($this->any())->method('getEntityClass')->willReturn(Asset\AnotherEntity::class);
206
207
                            return $mock;
208
                        },
209
                    ],
210
                ],
211
212
                // $requestedDataMapperEntity
213
                Asset\AnotherEntity::class,
214
215
                // $expectedException
216
                null,
217
            ],
218
            [
219
                // $config
220
                [
221
                    'entity_data_mapper_map' => [
222
                        SluggableInterface::class       => 'SomeDataMapperServiceName',
223
                    ],
224
                    'factories' => [
225
                        'SomeDataMapperServiceName' => function () {
226
                            $mock = $this->getMock(DataMapperInterface::class);
227
                            $mock->expects($this->any())->method('getEntityClass')->willReturn(Asset\Entity::class);
228
229
                            return $mock;
230
                        },
231
                    ],
232
                ],
233
234
                // $requestedDataMapperEntity
235
                SluggableInterface::class,
236
237
                // $expectedException
238
                null,
239
            ],
240
        ];
241
    }
242
243
    public function testConstructorNeedsADataMapperManagerConfigIfNotNull()
244
    {
245
        $this->setExpectedException(
246
            InvalidArgumentException::class,
247
            'expected to be instanceof of "Thorr\Persistence\DataMapper\Manager\DataMapperManagerConfig"'
248
        );
249
250
        new DataMapperManager(new Config());
251
    }
252
}
253