1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace ArpTest\LaminasDoctrine\Service\Configuration; |
6
|
|
|
|
7
|
|
|
use Arp\LaminasDoctrine\Config\DoctrineConfig; |
8
|
|
|
use Arp\LaminasDoctrine\Service\Configuration\ConfigurationFactoryInterface; |
9
|
|
|
use Arp\LaminasDoctrine\Service\Configuration\ConfigurationManager; |
10
|
|
|
use Arp\LaminasDoctrine\Service\Configuration\ConfigurationManagerInterface; |
11
|
|
|
use Arp\LaminasDoctrine\Service\Configuration\Exception\ConfigurationFactoryException; |
12
|
|
|
use Arp\LaminasDoctrine\Service\Configuration\Exception\ConfigurationManagerException; |
13
|
|
|
use Doctrine\ORM\Configuration; |
14
|
|
|
use PHPUnit\Framework\MockObject\MockObject; |
15
|
|
|
use PHPUnit\Framework\TestCase; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @covers \Arp\LaminasDoctrine\Service\Configuration\ConfigurationManager |
19
|
|
|
* |
20
|
|
|
* @author Alex Patterson <[email protected]> |
21
|
|
|
* @package ArpTest\LaminasDoctrine\Service\Configuration |
22
|
|
|
*/ |
23
|
|
|
final class ConfigurationManagerTest extends TestCase |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var ConfigurationFactoryInterface&MockObject |
27
|
|
|
*/ |
28
|
|
|
private $configurationFactory; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var DoctrineConfig&MockObject |
32
|
|
|
*/ |
33
|
|
|
private $doctrineConfig; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Prepare the test case dependencies |
37
|
|
|
*/ |
38
|
|
|
public function setUp(): void |
39
|
|
|
{ |
40
|
|
|
$this->configurationFactory = $this->createMock(ConfigurationFactoryInterface::class); |
41
|
|
|
|
42
|
|
|
$this->doctrineConfig = $this->createMock(DoctrineConfig::class); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Assert that the configuration manager implements ConfigurationManagerInterface |
47
|
|
|
*/ |
48
|
|
|
public function testImplementsConfigurationManagerInterface(): void |
49
|
|
|
{ |
50
|
|
|
$manager = new ConfigurationManager($this->configurationFactory, $this->doctrineConfig); |
|
|
|
|
51
|
|
|
|
52
|
|
|
$this->assertInstanceOf(ConfigurationManagerInterface::class, $manager); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Assert calls to addConfigurationConfig() will proxy to the internal DoctrineConfig instance |
57
|
|
|
*/ |
58
|
|
|
public function testAddConfigurationConfig(): void |
59
|
|
|
{ |
60
|
|
|
$manager = new ConfigurationManager($this->configurationFactory, $this->doctrineConfig); |
|
|
|
|
61
|
|
|
|
62
|
|
|
$name = 'FooTestConfiguration'; |
63
|
|
|
$config = [ |
64
|
|
|
'foo' => 123, |
65
|
|
|
]; |
66
|
|
|
|
67
|
|
|
$this->doctrineConfig->expects($this->once()) |
68
|
|
|
->method('setConfigurationConfig') |
69
|
|
|
->with($name, $config); |
70
|
|
|
|
71
|
|
|
$manager->addConfigurationConfig($name, $config); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Assert that a ConfigurationManagerException is thrown when calling getConfiguration() with a |
76
|
|
|
* unknown configuration $name |
77
|
|
|
* |
78
|
|
|
* @throws ConfigurationManagerException |
79
|
|
|
*/ |
80
|
|
|
public function testGetConfigurationWillThrowConfigurationManagerExceptionForUnknownConnection(): void |
81
|
|
|
{ |
82
|
|
|
$manager = new ConfigurationManager($this->configurationFactory, $this->doctrineConfig); |
|
|
|
|
83
|
|
|
|
84
|
|
|
$name = 'Fred'; |
85
|
|
|
|
86
|
|
|
$this->doctrineConfig->expects($this->once()) |
87
|
|
|
->method('hasConfigurationConfig') |
88
|
|
|
->with($name) |
89
|
|
|
->willReturn(false); |
90
|
|
|
|
91
|
|
|
$this->expectException(ConfigurationManagerException::class); |
92
|
|
|
$this->expectExceptionMessage( |
93
|
|
|
sprintf('Unable to find Doctrine Configuration registered with name \'%s\'', $name) |
94
|
|
|
); |
95
|
|
|
|
96
|
|
|
$manager->getConfiguration($name); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Assert a ConfigurationFactoryException is thrown if getConnection() is unable to create a lazy loaded |
101
|
|
|
* connection instance from matched configuration |
102
|
|
|
* |
103
|
|
|
* @throws ConfigurationManagerException |
104
|
|
|
*/ |
105
|
|
|
public function testGetConfigurationWillThrowConfigurationManagerExceptionIfUnableToLazyLoadConfiguration(): void |
106
|
|
|
{ |
107
|
|
|
$manager = new ConfigurationManager($this->configurationFactory, $this->doctrineConfig); |
|
|
|
|
108
|
|
|
|
109
|
|
|
$name = 'Fred'; |
110
|
|
|
$config = [ |
111
|
|
|
'foo' => 123, |
112
|
|
|
]; |
113
|
|
|
|
114
|
|
|
$this->doctrineConfig->expects($this->once()) |
115
|
|
|
->method('hasConfigurationConfig') |
116
|
|
|
->with($name) |
117
|
|
|
->willReturn(true); |
118
|
|
|
|
119
|
|
|
$this->doctrineConfig->expects($this->once()) |
120
|
|
|
->method('getConfigurationConfig') |
121
|
|
|
->with($name) |
122
|
|
|
->willReturn($config); |
123
|
|
|
|
124
|
|
|
$exceptionCode = 1234; |
125
|
|
|
$exceptionMessage = 'This is a test exception message for ' . __FUNCTION__; |
126
|
|
|
$exception = new ConfigurationFactoryException($exceptionMessage, $exceptionCode); |
|
|
|
|
127
|
|
|
|
128
|
|
|
$this->configurationFactory->expects($this->once()) |
129
|
|
|
->method('create') |
130
|
|
|
->with($config) |
131
|
|
|
->willThrowException($exception); |
132
|
|
|
|
133
|
|
|
$this->expectException(ConfigurationManagerException::class); |
134
|
|
|
$this->expectExceptionCode($exceptionCode); |
135
|
|
|
$this->expectExceptionMessage( |
136
|
|
|
sprintf('Failed to create doctrine configuration \'%s\': %s', $name, $exceptionMessage), |
137
|
|
|
); |
138
|
|
|
|
139
|
|
|
$manager->getConfiguration($name); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Assert that getConfiguration() will return a lazy loaded Configuration instance if the provided $name is |
144
|
|
|
* able to be created |
145
|
|
|
* |
146
|
|
|
* @throws ConfigurationManagerException |
147
|
|
|
*/ |
148
|
|
|
public function testGetConfigurationWillLazyLoadAndReturnConfigurationFromDoctrineConfig(): void |
149
|
|
|
{ |
150
|
|
|
$manager = new ConfigurationManager($this->configurationFactory, $this->doctrineConfig); |
|
|
|
|
151
|
|
|
|
152
|
|
|
$name = 'TestConnectionName'; |
153
|
|
|
|
154
|
|
|
/** @var Configuration&MockObject $createdConfiguration */ |
155
|
|
|
$createdConfiguration = $this->createMock(Configuration::class); |
156
|
|
|
|
157
|
|
|
$this->doctrineConfig->expects($this->once()) |
158
|
|
|
->method('hasConfigurationConfig') |
159
|
|
|
->with($name) |
160
|
|
|
->willReturn(true); |
161
|
|
|
|
162
|
|
|
$configurationConfig = [ |
163
|
|
|
'foo' => 123, |
164
|
|
|
]; |
165
|
|
|
|
166
|
|
|
$this->doctrineConfig->expects($this->once()) |
167
|
|
|
->method('getConfigurationConfig') |
168
|
|
|
->willReturn($configurationConfig); |
169
|
|
|
|
170
|
|
|
$this->configurationFactory->expects($this->once()) |
171
|
|
|
->method('create') |
172
|
|
|
->with($configurationConfig) |
173
|
|
|
->willReturn($createdConfiguration); |
174
|
|
|
|
175
|
|
|
$this->assertSame($createdConfiguration, $manager->getConfiguration($name)); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Assert that an array of configuration can be set when calling setConfiguration() and retrieved by $name |
180
|
|
|
* when calling getConfiguration($name) |
181
|
|
|
* |
182
|
|
|
* @throws ConfigurationManagerException |
183
|
|
|
*/ |
184
|
|
|
public function testSetAndGetConfigurationObjects(): void |
185
|
|
|
{ |
186
|
|
|
$manager = new ConfigurationManager($this->configurationFactory, $this->doctrineConfig); |
|
|
|
|
187
|
|
|
|
188
|
|
|
$configs = [ |
189
|
|
|
'fred' => $this->createMock(Configuration::class), |
190
|
|
|
'bob' => $this->createMock(Configuration::class), |
191
|
|
|
'dick' => $this->createMock(Configuration::class), |
192
|
|
|
'harry' => $this->createMock(Configuration::class), |
193
|
|
|
]; |
194
|
|
|
|
195
|
|
|
$manager->setConfigurations($configs); |
196
|
|
|
|
197
|
|
|
foreach ($configs as $name => $config) { |
198
|
|
|
$this->assertSame($config, $manager->getConfiguration($name)); |
199
|
|
|
} |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Assert that setConfigurations() accepts Connection configuration which is added to the internal DoctrineConfig |
204
|
|
|
*/ |
205
|
|
|
public function testSetAndGetConfigurationArray(): void |
206
|
|
|
{ |
207
|
|
|
$manager = new ConfigurationManager($this->configurationFactory, $this->doctrineConfig); |
|
|
|
|
208
|
|
|
|
209
|
|
|
$configs = [ |
210
|
|
|
'fred' => [ |
211
|
|
|
'test' => 123, |
212
|
|
|
], |
213
|
|
|
'bob' => [ |
214
|
|
|
'test' => 456, |
215
|
|
|
], |
216
|
|
|
'jennifer' => [ |
217
|
|
|
'test' => 456, |
218
|
|
|
], |
219
|
|
|
]; |
220
|
|
|
|
221
|
|
|
$setArgs = []; |
222
|
|
|
foreach ($configs as $name => $config) { |
223
|
|
|
$setArgs[] = [$name, $config]; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
$this->doctrineConfig->expects($this->exactly(count($setArgs))) |
227
|
|
|
->method('setConfigurationConfig') |
228
|
|
|
->withConsecutive(...$setArgs); |
229
|
|
|
|
230
|
|
|
$manager->setConfigurations($configs); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* Assert has will return $expected bool value if the connection is set in $config or the $doctrineConfig |
235
|
|
|
* |
236
|
|
|
* @param bool $expected |
237
|
|
|
* @param string $name |
238
|
|
|
* @param array<mixed> $configs |
239
|
|
|
* |
240
|
|
|
* @dataProvider getHasConfigurationData |
241
|
|
|
*/ |
242
|
|
|
public function testHasConfigurationObject(bool $expected, string $name, array $configs): void |
243
|
|
|
{ |
244
|
|
|
$manager = new ConfigurationManager($this->configurationFactory, $this->doctrineConfig); |
|
|
|
|
245
|
|
|
|
246
|
|
|
if (!empty($configs)) { |
247
|
|
|
$manager->setConfigurations($configs); |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
if (!isset($configs[$name])) { |
251
|
|
|
$this->doctrineConfig->expects($this->once()) |
252
|
|
|
->method('hasConfigurationConfig') |
253
|
|
|
->with($name) |
254
|
|
|
->willReturn($expected); |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
$this->assertSame($expected, $manager->hasConfiguration($name)); |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* @return array<mixed> |
262
|
|
|
*/ |
263
|
|
|
public function getHasConfigurationData(): array |
264
|
|
|
{ |
265
|
|
|
return [ |
266
|
|
|
// Found match in config |
267
|
|
|
[ |
268
|
|
|
true, |
269
|
|
|
'fred', |
270
|
|
|
[ |
271
|
|
|
'fred' => $this->createMock(Configuration::class), |
272
|
|
|
], |
273
|
|
|
], |
274
|
|
|
|
275
|
|
|
// Missing from both |
276
|
|
|
[ |
277
|
|
|
false, |
278
|
|
|
'Kitty', |
279
|
|
|
[ |
280
|
|
|
'fred' => $this->createMock(Configuration::class), |
281
|
|
|
], |
282
|
|
|
], |
283
|
|
|
|
284
|
|
|
// In Doctrine Config |
285
|
|
|
[ |
286
|
|
|
true, |
287
|
|
|
'Barney', |
288
|
|
|
[ |
289
|
|
|
'fred' => $this->createMock(Configuration::class), |
290
|
|
|
'bob' => $this->createMock(Configuration::class), |
291
|
|
|
], |
292
|
|
|
], |
293
|
|
|
]; |
294
|
|
|
} |
295
|
|
|
} |
296
|
|
|
|