1
|
|
|
<?php |
2
|
|
|
namespace Lebran; |
3
|
|
|
|
4
|
|
|
class ContainerTest extends \PHPUnit_Framework_TestCase |
5
|
|
|
{ |
6
|
|
|
/** |
7
|
|
|
* @param mixed $definition |
8
|
|
|
* |
9
|
|
|
* @dataProvider providerDefinitions |
10
|
|
|
*/ |
11
|
|
|
public function testAllDefinitions($definition) |
12
|
|
|
{ |
13
|
|
|
$di = new Container(); |
14
|
|
|
$di->set('test', $definition); |
15
|
|
|
$this->assertInstanceOf('Lebran\TestService', $di->get('test')); |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @param mixed $definition |
20
|
|
|
* |
21
|
|
|
* @dataProvider providerDefinitions |
22
|
|
|
*/ |
23
|
|
|
public function testInjectable($definition) |
24
|
|
|
{ |
25
|
|
|
$di = new Container(); |
26
|
|
|
$di->set('test', $definition); |
27
|
|
|
$service = $di->get('test'); |
28
|
|
|
$this->assertInstanceOf('Lebran\Container\InjectableInterface', $service); |
29
|
|
|
$this->assertSame($di, $service->getDi()); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param mixed $definition |
34
|
|
|
* |
35
|
|
|
* @dataProvider providerDefinitions |
36
|
|
|
*/ |
37
|
|
|
public function testServicesShouldBeDifferent($definition) |
38
|
|
|
{ |
39
|
|
|
$di = new Container(); |
40
|
|
|
$di->set('test', $definition); |
41
|
|
|
$this->assertNotSame($di->get('test'), $di->get('test')); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param mixed $definition |
46
|
|
|
* |
47
|
|
|
* @dataProvider providerDefinitions |
48
|
|
|
*/ |
49
|
|
|
public function testSharedServices($definition) |
50
|
|
|
{ |
51
|
|
|
$di = new Container(); |
52
|
|
|
$di->shared('test', $definition); |
53
|
|
|
$this->assertSame($di->get('test'), $di->get('test')); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param mixed $definition |
58
|
|
|
* |
59
|
|
|
* @dataProvider providerDefinitions |
60
|
|
|
*/ |
61
|
|
|
public function testIsSharedAndSetShared($definition) |
62
|
|
|
{ |
63
|
|
|
$di = new Container(); |
64
|
|
|
$di->shared('test', $definition); |
65
|
|
|
|
66
|
|
|
$this->assertTrue($di->isShared('test')); |
67
|
|
|
$di->setShared('test', false); |
68
|
|
|
$this->assertFalse($di->isShared('test')); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @expectedException \Lebran\Container\NotFoundException |
73
|
|
|
*/ |
74
|
|
|
public function testSetSharedNotFound() |
75
|
|
|
{ |
76
|
|
|
$di = new Container(); |
77
|
|
|
$di->setShared('test'); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param mixed $definition |
83
|
|
|
* |
84
|
|
|
* @dataProvider providerDefinitions |
85
|
|
|
*/ |
86
|
|
|
public function testHasAndRemove($definition) |
87
|
|
|
{ |
88
|
|
|
$di = new Container(); |
89
|
|
|
$di->set('test', $definition); |
90
|
|
|
|
91
|
|
|
$this->assertTrue($di->has('test')); |
92
|
|
|
$di->remove('test'); |
93
|
|
|
$this->assertFalse($di->has('test')); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param mixed $definition |
98
|
|
|
* |
99
|
|
|
* @dataProvider providerDefinitions |
100
|
|
|
*/ |
101
|
|
|
public function testMagicalSyntax($definition) |
102
|
|
|
{ |
103
|
|
|
$di = new Container(); |
104
|
|
|
$di->test = $definition; |
|
|
|
|
105
|
|
|
$this->assertTrue(isset($di->test)); |
106
|
|
|
$this->assertInstanceOf('Lebran\TestService', $di->test); |
|
|
|
|
107
|
|
|
$this->assertSame($di->test, $di->test); |
|
|
|
|
108
|
|
|
unset($di->test); |
109
|
|
|
$this->assertFalse(isset($di->test)); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param mixed $definition |
114
|
|
|
* |
115
|
|
|
* @dataProvider providerDefinitions |
116
|
|
|
*/ |
117
|
|
|
public function testArrayAccessSyntax($definition) |
118
|
|
|
{ |
119
|
|
|
$di = new Container(); |
120
|
|
|
$di['test'] = $definition; |
121
|
|
|
$this->assertTrue(isset($di['test'])); |
122
|
|
|
$this->assertInstanceOf('Lebran\TestService', $di['test']); |
123
|
|
|
$this->assertNotSame($di['test'], $di['test']); |
124
|
|
|
unset($di['test']); |
125
|
|
|
$this->assertFalse(isset($di['test'])); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public function providerDefinitions() |
129
|
|
|
{ |
130
|
|
|
return [ |
131
|
|
|
['Lebran\TestService'], |
132
|
|
|
[new TestService()], |
133
|
|
|
[function(){ |
134
|
|
|
return new TestService(); |
135
|
|
|
}] |
136
|
|
|
]; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
public function testStaticInstance() |
140
|
|
|
{ |
141
|
|
|
$di = new Container(); |
142
|
|
|
$di1 = new Container(); |
143
|
|
|
$this->assertNotSame($di, Container::instance()); |
144
|
|
|
$this->assertSame($di1, Container::instance()); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @param mixed $definition |
149
|
|
|
* |
150
|
|
|
* @dataProvider providerDefinitions |
151
|
|
|
*/ |
152
|
|
|
public function testNormalize($definition) |
153
|
|
|
{ |
154
|
|
|
$di = new Container(); |
155
|
|
|
$di->set('test', $definition); |
156
|
|
|
$this->assertInstanceOf('Lebran\TestService', $di->get('test')); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
public function providerNormalize() |
160
|
|
|
{ |
161
|
|
|
return [ |
162
|
|
|
['Lebran\TestService'], |
163
|
|
|
['\Lebran\TestService\\'], |
164
|
|
|
[' Lebran\TestService '], |
165
|
|
|
[' \\\\\\Lebran\TestService'], |
166
|
|
|
['Lebran\TestService\\\\\\\ '] |
167
|
|
|
]; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @expectedException \Lebran\ContainerException |
172
|
|
|
*/ |
173
|
|
|
public function testCircle() |
174
|
|
|
{ |
175
|
|
|
$di = new Container(); |
176
|
|
|
$di->set('Lebran\TestService', 'Lebran\TestService') |
177
|
|
|
->get('Lebran\TestService'); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* @expectedException \Lebran\ContainerException |
182
|
|
|
*/ |
183
|
|
|
public function testCircle2() |
184
|
|
|
{ |
185
|
|
|
$di = new Container(); |
186
|
|
|
|
187
|
|
|
$di->set('Lebran\TestService', 'test') |
188
|
|
|
->set('test', 'Lebran\TestService') |
189
|
|
|
->get('Lebran\TestService'); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
|
193
|
|
|
public function testStringDefinition() |
194
|
|
|
{ |
195
|
|
|
$di = new Container(); |
196
|
|
|
$this->assertInstanceOf('Lebran\TestService', $di->get('Lebran\TestService')); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* @param mixed $definition |
201
|
|
|
* |
202
|
|
|
* @dataProvider providerWrongDefinitions |
203
|
|
|
* @expectedException \Lebran\ContainerException |
204
|
|
|
*/ |
205
|
|
|
public function testWrongDefinition($definition) |
206
|
|
|
{ |
207
|
|
|
$di = new Container(); |
208
|
|
|
$di->set('test', $definition) |
209
|
|
|
->get('test'); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
public function providerWrongDefinitions() |
213
|
|
|
{ |
214
|
|
|
return [ |
215
|
|
|
[[]], |
216
|
|
|
[10], |
217
|
|
|
[10.5] |
218
|
|
|
]; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* @expectedException \Lebran\Container\NotFoundException |
223
|
|
|
*/ |
224
|
|
|
public function testNotFoundClass() |
225
|
|
|
{ |
226
|
|
|
$di = new Container(); |
227
|
|
|
$di->get('test'); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
public function testRegisterServiceProvider() |
231
|
|
|
{ |
232
|
|
|
$di = new Container(); |
233
|
|
|
$di->register(new TestServiceProvider()); |
234
|
|
|
$this->assertInstanceOf('Lebran\TestService', $di->get('test')); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
public function testResolveParameters() |
238
|
|
|
{ |
239
|
|
|
$di = new Container(); |
240
|
|
|
$di->set('test', 'Lebran\TestService2'); |
241
|
|
|
$service = $di->get('test', [ |
242
|
|
|
'param3' => 'test', |
243
|
|
|
1 => 'test2' |
244
|
|
|
]); |
245
|
|
|
$this->assertInstanceOf('Lebran\TestService', $service->param); |
246
|
|
|
$this->assertEquals('test2', $service->param1); |
247
|
|
|
$this->assertEquals('test3', $service->param2); |
248
|
|
|
$this->assertEquals('test', $service->param3); |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* @expectedException \Lebran\ContainerException |
253
|
|
|
*/ |
254
|
|
|
public function testParameterNotPassed() |
255
|
|
|
{ |
256
|
|
|
$di = new Container(); |
257
|
|
|
$di->set('test', 'Lebran\TestService2') |
258
|
|
|
->get('test'); |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
public function testClosureThisMustBeContainer() |
262
|
|
|
{ |
263
|
|
|
$di = new Container(); |
264
|
|
|
$di->set('test', function(){ |
265
|
|
|
return $this; |
266
|
|
|
}); |
267
|
|
|
|
268
|
|
|
$this->assertSame($di, $di->get('test')); |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* @param mixed $definition |
273
|
|
|
* |
274
|
|
|
* @dataProvider providerCall |
275
|
|
|
*/ |
276
|
|
|
public function testCall($definition) |
277
|
|
|
{ |
278
|
|
|
$di = new Container(); |
279
|
|
|
$this->assertInstanceOf('Lebran\TestServiceProvider', $di->call($definition)); |
|
|
|
|
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
public function providerCall() |
283
|
|
|
{ |
284
|
|
|
return [ |
285
|
|
|
[['Lebran\TestService','testCall']], |
286
|
|
|
[ |
287
|
|
|
function(TestServiceProvider $test){ |
288
|
|
|
return $test; |
289
|
|
|
} |
290
|
|
|
] |
291
|
|
|
]; |
292
|
|
|
} |
293
|
|
|
} |
Since your code implements the magic setter
_set
, this function will be called for any write access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.