|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Lug package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Eric GELOEN <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please read the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Lug\Bundle\RegistryBundle\Tests\Model; |
|
13
|
|
|
|
|
14
|
|
|
use Lug\Bundle\RegistryBundle\Model\LazyRegistry; |
|
15
|
|
|
use Lug\Bundle\RegistryBundle\Model\LazyRegistryInterface; |
|
16
|
|
|
use Lug\Component\Registry\Model\Registry; |
|
17
|
|
|
use Lug\Component\Registry\Model\RegistryInterface; |
|
18
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @author GeLo <[email protected]> |
|
22
|
|
|
*/ |
|
23
|
|
|
class LazyRegistryTest extends \PHPUnit_Framework_TestCase |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* @var LazyRegistry |
|
27
|
|
|
*/ |
|
28
|
|
|
private $lazyRegistry; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var Registry|\PHPUnit_Framework_MockObject_MockObject |
|
32
|
|
|
*/ |
|
33
|
|
|
private $registry; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var ContainerInterface|\PHPUnit_Framework_MockObject_MockObject |
|
37
|
|
|
*/ |
|
38
|
|
|
private $container; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var string[] |
|
42
|
|
|
*/ |
|
43
|
|
|
private $lazyServices; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @var ServiceInterface[] |
|
47
|
|
|
*/ |
|
48
|
|
|
private $containerServices; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @var ServiceInterface[] |
|
52
|
|
|
*/ |
|
53
|
|
|
private $services; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* {@inheritdoc} |
|
57
|
|
|
*/ |
|
58
|
|
|
protected function setUp() |
|
59
|
|
|
{ |
|
60
|
|
|
$this->services = ['foo' => $this->createServiceMock(), 'bar' => $this->createServiceMock()]; |
|
|
|
|
|
|
61
|
|
|
$this->containerServices = ['my.baz' => $this->createServiceMock(), 'my.bat' => $this->createServiceMock()]; |
|
|
|
|
|
|
62
|
|
|
$this->lazyServices = ['baz' => 'my.baz', 'bat' => 'my.bat']; |
|
|
|
|
|
|
63
|
|
|
$this->container = $this->createContainerMock(); |
|
64
|
|
|
$this->registry = new Registry(ServiceInterface::class, $this->services); |
|
65
|
|
|
|
|
66
|
|
|
$this->lazyRegistry = new LazyRegistry( |
|
67
|
|
|
$this->container, |
|
68
|
|
|
$this->registry, |
|
69
|
|
|
$this->lazyServices |
|
70
|
|
|
); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
public function testInheritance() |
|
74
|
|
|
{ |
|
75
|
|
|
$this->assertInstanceOf(RegistryInterface::class, $this->lazyRegistry); |
|
76
|
|
|
$this->assertInstanceOf(LazyRegistryInterface::class, $this->lazyRegistry); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
public function testDefaultState() |
|
80
|
|
|
{ |
|
81
|
|
|
$this->lazyRegistry = new LazyRegistry( |
|
82
|
|
|
$this->container, |
|
83
|
|
|
new Registry(ServiceInterface::class) |
|
84
|
|
|
); |
|
85
|
|
|
|
|
86
|
|
|
$this->assertEmpty(iterator_to_array($this->lazyRegistry)); |
|
87
|
|
|
$this->assertCount(0, $this->lazyRegistry); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
public function testInitialState() |
|
91
|
|
|
{ |
|
92
|
|
|
$services = array_merge( |
|
93
|
|
|
$this->services, |
|
94
|
|
|
array_combine(array_flip($this->lazyServices), $this->containerServices) |
|
95
|
|
|
); |
|
96
|
|
|
|
|
97
|
|
|
$this->assertSame($services, iterator_to_array($this->lazyRegistry)); |
|
98
|
|
|
$this->assertCount(count($services), $this->lazyRegistry); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
public function testHasLazy() |
|
102
|
|
|
{ |
|
103
|
|
|
foreach (array_keys($this->lazyServices) as $type) { |
|
104
|
|
|
$this->assertTrue($this->lazyRegistry->hasLazy($type)); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
foreach (array_keys($this->services) as $type) { |
|
108
|
|
|
$this->assertFalse($this->lazyRegistry->hasLazy($type)); |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
public function testGetLazy() |
|
113
|
|
|
{ |
|
114
|
|
|
foreach ($this->lazyServices as $type => $service) { |
|
115
|
|
|
$this->assertSame($service, $this->lazyRegistry->getLazy($type)); |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* @expectedException \Lug\Bundle\RegistryBundle\Exception\LazyServiceNotFoundException |
|
121
|
|
|
* @expectedExceptionMessage The lazy service "foo" could not be found. |
|
122
|
|
|
*/ |
|
123
|
|
|
public function testGetLazyWithNonExistingLazy() |
|
124
|
|
|
{ |
|
125
|
|
|
$this->lazyRegistry->getLazy('foo'); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
public function testSetLazy() |
|
129
|
|
|
{ |
|
130
|
|
|
$this->containerServices[$containerService = 'my.ban'] = $service = $this->createServiceMock(); |
|
|
|
|
|
|
131
|
|
|
|
|
132
|
|
|
$this->container = $this->createContainerMock(); |
|
133
|
|
|
$this->registry = new Registry(ServiceInterface::class, $this->services); |
|
134
|
|
|
|
|
135
|
|
|
$this->lazyRegistry = new LazyRegistry( |
|
136
|
|
|
$this->container, |
|
137
|
|
|
$this->registry, |
|
138
|
|
|
$this->lazyServices |
|
139
|
|
|
); |
|
140
|
|
|
|
|
141
|
|
|
$this->lazyServices[$type = 'ban'] = $containerService; |
|
142
|
|
|
$this->lazyRegistry->setLazy($type, $containerService); |
|
143
|
|
|
|
|
144
|
|
|
$this->assertTrue($this->lazyRegistry->hasLazy($type)); |
|
145
|
|
|
$this->assertSame($containerService, $this->lazyRegistry->getLazy($type)); |
|
146
|
|
|
|
|
147
|
|
|
$services = array_merge( |
|
148
|
|
|
$this->services, |
|
149
|
|
|
array_combine(array_flip($this->lazyServices), $this->containerServices) |
|
150
|
|
|
); |
|
151
|
|
|
|
|
152
|
|
|
$this->assertSame($services, iterator_to_array($this->lazyRegistry)); |
|
153
|
|
|
$this->assertCount(count($services), $this->lazyRegistry); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* @expectedException \Lug\Bundle\RegistryBundle\Exception\LazyServiceAlreadyExistsException |
|
158
|
|
|
* @expectedExceptionMessage The lazy service "baz" already exists. |
|
159
|
|
|
*/ |
|
160
|
|
|
public function testSetLazyWithExistingLazy() |
|
161
|
|
|
{ |
|
162
|
|
|
$this->lazyRegistry->setLazy('baz', 'my.baz'); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
public function testRemoveLazy() |
|
166
|
|
|
{ |
|
167
|
|
|
$this->testInitialState(); |
|
168
|
|
|
|
|
169
|
|
|
foreach (array_keys($this->lazyServices) as $type) { |
|
170
|
|
|
$this->lazyRegistry->removeLazy($type); |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
$this->assertSame($this->services, iterator_to_array($this->lazyRegistry)); |
|
174
|
|
|
$this->assertCount(count($this->services), $this->lazyRegistry); |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* @expectedException \Lug\Bundle\RegistryBundle\Exception\LazyServiceNotFoundException |
|
179
|
|
|
* @expectedExceptionMessage The lazy service "ban" could not be found. |
|
180
|
|
|
*/ |
|
181
|
|
|
public function testRemoveLazyWithNonExistingLazy() |
|
182
|
|
|
{ |
|
183
|
|
|
$this->lazyRegistry->removeLazy('ban'); |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
public function testOffsetExists() |
|
187
|
|
|
{ |
|
188
|
|
|
foreach (array_keys(array_merge($this->services, $this->lazyServices)) as $type) { |
|
189
|
|
|
$this->assertTrue(isset($this->lazyRegistry[$type])); |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
$this->assertFalse(isset($this->lazyRegistry['ban'])); |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
public function testOffsetGet() |
|
196
|
|
|
{ |
|
197
|
|
|
$services = array_merge( |
|
198
|
|
|
$this->services, |
|
199
|
|
|
array_combine(array_flip($this->lazyServices), $this->containerServices) |
|
200
|
|
|
); |
|
201
|
|
|
|
|
202
|
|
|
foreach ($services as $type => $service) { |
|
203
|
|
|
$this->assertSame($service, $this->lazyRegistry[$type]); |
|
204
|
|
|
} |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
/** |
|
208
|
|
|
* @expectedException \Lug\Bundle\RegistryBundle\Exception\LazyServiceNotFoundException |
|
209
|
|
|
* @expectedExceptionMessage The lazy service "ban" could not be found. |
|
210
|
|
|
*/ |
|
211
|
|
|
public function testOffsetGetWithNonExistingService() |
|
212
|
|
|
{ |
|
213
|
|
|
$this->lazyRegistry['ban']; |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
public function testOffsetSet() |
|
217
|
|
|
{ |
|
218
|
|
|
$this->lazyRegistry[$type = 'ban'] = $service = $this->createServiceMock(); |
|
219
|
|
|
|
|
220
|
|
|
$this->assertSame($service, $this->lazyRegistry[$type]); |
|
221
|
|
|
|
|
222
|
|
|
$services = array_merge( |
|
223
|
|
|
$this->services, |
|
224
|
|
|
[$type => $service], |
|
225
|
|
|
array_combine(array_flip($this->lazyServices), $this->containerServices) |
|
226
|
|
|
); |
|
227
|
|
|
|
|
228
|
|
|
$this->assertSame($services, iterator_to_array($this->lazyRegistry)); |
|
229
|
|
|
$this->assertCount(count($services), $this->lazyRegistry); |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
/** |
|
233
|
|
|
* @expectedException \Lug\Component\Registry\Exception\ServiceAlreadyExistsException |
|
234
|
|
|
* @expectedExceptionMessage The service "baz" already exists. |
|
235
|
|
|
*/ |
|
236
|
|
|
public function testOffsetSetWithExistingService() |
|
237
|
|
|
{ |
|
238
|
|
|
$this->lazyRegistry['baz'] = $this->createServiceMock(); |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
View Code Duplication |
public function testOffsetUnset() |
|
|
|
|
|
|
242
|
|
|
{ |
|
243
|
|
|
foreach (array_keys(array_merge($this->services, $this->lazyServices)) as $type) { |
|
244
|
|
|
unset($this->lazyRegistry[$type]); |
|
245
|
|
|
|
|
246
|
|
|
$this->assertFalse(isset($this->lazyRegistry[$type])); |
|
247
|
|
|
} |
|
248
|
|
|
|
|
249
|
|
|
$this->assertEmpty(iterator_to_array($this->lazyRegistry)); |
|
250
|
|
|
$this->assertCount(0, $this->lazyRegistry); |
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
|
|
/** |
|
254
|
|
|
* @return ContainerInterface|\PHPUnit_Framework_MockObject_MockObject |
|
255
|
|
|
*/ |
|
256
|
|
|
private function createContainerMock() |
|
257
|
|
|
{ |
|
258
|
|
|
$hasMap = []; |
|
259
|
|
|
$getMap = []; |
|
260
|
|
|
|
|
261
|
|
|
foreach ($this->containerServices as $id => $service) { |
|
262
|
|
|
$hasMap[] = [$id, true]; |
|
263
|
|
|
$getMap[] = [$id, ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, $service]; |
|
264
|
|
|
} |
|
265
|
|
|
|
|
266
|
|
|
$container = $this->getMock(ContainerInterface::class); |
|
267
|
|
|
$container |
|
268
|
|
|
->expects($this->any()) |
|
269
|
|
|
->method('has') |
|
270
|
|
|
->will($this->returnValueMap($hasMap)); |
|
271
|
|
|
|
|
272
|
|
|
$container |
|
273
|
|
|
->expects($this->any()) |
|
274
|
|
|
->method('get') |
|
275
|
|
|
->will($this->returnValueMap($getMap)); |
|
276
|
|
|
|
|
277
|
|
|
return $container; |
|
278
|
|
|
} |
|
279
|
|
|
|
|
280
|
|
|
/** |
|
281
|
|
|
* @return ServiceInterface|\PHPUnit_Framework_MockObject_MockObject |
|
282
|
|
|
*/ |
|
283
|
|
|
private function createServiceMock() |
|
284
|
|
|
{ |
|
285
|
|
|
return $this->getMock(ServiceInterface::class); |
|
286
|
|
|
} |
|
287
|
|
|
} |
|
288
|
|
|
|
|
289
|
|
|
/** |
|
290
|
|
|
* @author GeLo <[email protected]> |
|
291
|
|
|
*/ |
|
292
|
|
|
interface ServiceInterface |
|
|
|
|
|
|
293
|
|
|
{ |
|
294
|
|
|
} |
|
295
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..