1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
+----------------------------------------------------------------------+ |
5
|
|
|
| This file is part of the pinepain/container-extras PHP library. | |
6
|
|
|
| | |
7
|
|
|
| Copyright (c) 2015-2016 Bogdan Padalko <[email protected]> | |
8
|
|
|
| | |
9
|
|
|
| Licensed under the MIT license: http://opensource.org/licenses/MIT | |
10
|
|
|
| | |
11
|
|
|
| For the full copyright and license information, please view the | |
12
|
|
|
| LICENSE file that was distributed with this source or visit | |
13
|
|
|
| http://opensource.org/licenses/MIT | |
14
|
|
|
+----------------------------------------------------------------------+ |
15
|
|
|
*/ |
16
|
|
|
|
17
|
|
|
/* Based on League\Container\Test\ContainerTest |
18
|
|
|
* v1.x class (https://github.com/thephpleague/container/blob/1.x/tests/ContainerTest.php) |
19
|
|
|
* which is authored by Phil Bennett (https://github.com/philipobenito) |
20
|
|
|
* and other contributors (https://github.com/thephpleague/container/contributors). |
21
|
|
|
*/ |
22
|
|
|
|
23
|
|
|
|
24
|
|
|
namespace Pinepain\Container\Extras\Tests; |
25
|
|
|
|
26
|
|
|
|
27
|
|
|
use League\Container\ContainerInterface; |
28
|
|
|
use League\Container\Definition\ClassDefinition; |
29
|
|
|
use League\Container\Definition\DefinitionInterface; |
30
|
|
|
use Pinepain\Container\Extras\ContainerHydrator; |
31
|
|
|
use stdClass; |
32
|
|
|
|
33
|
|
|
|
34
|
|
|
class ContainerHydratorTest extends \PHPUnit_Framework_TestCase |
35
|
|
|
{ |
36
|
|
|
protected $configArray = [ |
37
|
|
|
'League\Container\Test\Asset\Foo' => [ |
38
|
|
|
'class' => 'League\Container\Test\Asset\Foo', |
39
|
|
|
'arguments' => ['League\Container\Test\Asset\Bar'], |
40
|
|
|
'methods' => [ |
41
|
|
|
'injectBaz' => ['League\Container\Test\Asset\Baz'], |
42
|
|
|
], |
43
|
|
|
], |
44
|
|
|
'League\Container\Test\Asset\Bar' => [ |
45
|
|
|
'definition' => 'League\Container\Test\Asset\Bar', |
46
|
|
|
'arguments' => ['League\Container\Test\Asset\Baz'], |
47
|
|
|
], |
48
|
|
|
'League\Container\Test\Asset\Baz' => 'League\Container\Test\Asset\Baz', |
49
|
|
|
]; |
50
|
|
|
|
51
|
|
|
public function testPopulateWithEmpty() |
52
|
|
|
{ |
53
|
|
|
$loader = new ContainerHydrator(); |
54
|
|
|
|
55
|
|
|
$config = []; |
56
|
|
|
|
57
|
|
|
$container = $this->getContainerMock(); |
58
|
|
|
|
59
|
|
|
$container->expects($this->never())->method('add'); |
60
|
|
|
|
61
|
|
|
$loader->populate($container, $config); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function testPopulateWithTraversable() |
65
|
|
|
{ |
66
|
|
|
$loader = new ContainerHydrator(); |
67
|
|
|
|
68
|
|
|
/** @var \Iterator | \PHPUnit_Framework_MockObject_MockObject $config */ |
69
|
|
|
$config = $this->getMockBuilder('\Iterator')->getMockForAbstractClass(); |
70
|
|
|
|
71
|
|
|
$container = $this->getContainerMock(); |
72
|
|
|
|
73
|
|
|
$container->expects($this->never())->method('add'); |
74
|
|
|
|
75
|
|
|
$loader->populate($container, $config); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @expectedException \InvalidArgumentException |
80
|
|
|
* @expectedExceptionMessage You can only load definitions from an array or an object that implements Traversable |
81
|
|
|
* interface. |
82
|
|
|
*/ |
83
|
|
|
public function testPopulateFromNotArrayNorTraversable() |
84
|
|
|
{ |
85
|
|
|
$loader = new ContainerHydrator(); |
86
|
|
|
|
87
|
|
|
$container = $this->getContainerMock(); |
88
|
|
|
|
89
|
|
|
$loader->populate($container, new stdClass()); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
View Code Duplication |
public function testAddingScalar() |
93
|
|
|
{ |
94
|
|
|
$loader = new ContainerHydrator(); |
95
|
|
|
|
96
|
|
|
$config = [ |
97
|
|
|
'key' => 'value', |
98
|
|
|
]; |
99
|
|
|
|
100
|
|
|
$container = $this->getContainerMock(); |
101
|
|
|
|
102
|
|
|
$container->expects($this->once()) |
103
|
|
|
->method('add') |
104
|
|
|
->with('key', 'value', false); |
105
|
|
|
|
106
|
|
|
$loader->populate($container, $config); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
View Code Duplication |
public function testAddingConcrete() |
110
|
|
|
{ |
111
|
|
|
$loader = new ContainerHydrator(); |
112
|
|
|
|
113
|
|
|
$config = [ |
114
|
|
|
'TestInterface' => null, |
115
|
|
|
]; |
116
|
|
|
|
117
|
|
|
$container = $this->getContainerMock(); |
118
|
|
|
|
119
|
|
|
$container->expects($this->once()) |
120
|
|
|
->method('add') |
121
|
|
|
->with('TestInterface', null, false); |
122
|
|
|
|
123
|
|
|
$loader->populate($container, $config); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
View Code Duplication |
public function testSharingConcrete() |
127
|
|
|
{ |
128
|
|
|
$loader = new ContainerHydrator(); |
129
|
|
|
|
130
|
|
|
$config = [ |
131
|
|
|
'TestInterface' => [ |
132
|
|
|
'share' => true, |
133
|
|
|
], |
134
|
|
|
]; |
135
|
|
|
|
136
|
|
|
$container = $this->getContainerMock(); |
137
|
|
|
|
138
|
|
|
$container->expects($this->once()) |
139
|
|
|
->method('add') |
140
|
|
|
->with('TestInterface', null, true); |
141
|
|
|
|
142
|
|
|
$loader->populate($container, $config); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
View Code Duplication |
public function testAddingAlias() |
146
|
|
|
{ |
147
|
|
|
$loader = new ContainerHydrator(); |
148
|
|
|
|
149
|
|
|
$config = [ |
150
|
|
|
'TestInterface' => 'Test', |
151
|
|
|
]; |
152
|
|
|
|
153
|
|
|
$container = $this->getContainerMock(); |
154
|
|
|
|
155
|
|
|
$container->expects($this->once()) |
156
|
|
|
->method('add') |
157
|
|
|
->with('TestInterface', 'Test', false); |
158
|
|
|
|
159
|
|
|
$loader->populate($container, $config); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
public function testAddingShared() |
163
|
|
|
{ |
164
|
|
|
$loader = new ContainerHydrator(); |
165
|
|
|
|
166
|
|
|
$config = [ |
167
|
|
|
'TestInterface' => [ |
168
|
|
|
'class' => 'Test', |
169
|
|
|
'share' => true, |
170
|
|
|
], |
171
|
|
|
]; |
172
|
|
|
|
173
|
|
|
$container = $this->getContainerMock(); |
174
|
|
|
|
175
|
|
|
$container->expects($this->once()) |
176
|
|
|
->method('add') |
177
|
|
|
->with('TestInterface', 'Test', true); |
178
|
|
|
|
179
|
|
|
$loader->populate($container, $config); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
View Code Duplication |
public function testAddingAliasWithArgument() |
183
|
|
|
{ |
184
|
|
|
$loader = new ContainerHydrator(); |
185
|
|
|
|
186
|
|
|
$config = [ |
187
|
|
|
'TestInterface' => [ |
188
|
|
|
'class' => 'TestWithArguments', |
189
|
|
|
'arguments' => ['test', 'arguments'], |
190
|
|
|
], |
191
|
|
|
]; |
192
|
|
|
|
193
|
|
|
$container = $this->getContainerMock(); |
194
|
|
|
|
195
|
|
|
$definition = $this->getDefinitionMock(); |
196
|
|
|
|
197
|
|
|
$definition->expects($this->once()) |
198
|
|
|
->method('withArguments') |
199
|
|
|
->with(['test', 'arguments']) |
200
|
|
|
->willReturn($definition); |
201
|
|
|
|
202
|
|
|
$container->expects($this->once()) |
203
|
|
|
->method('add') |
204
|
|
|
->with('TestInterface', 'TestWithArguments', false) |
205
|
|
|
->willReturn($definition); |
206
|
|
|
|
207
|
|
|
$loader->populate($container, $config); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
|
211
|
|
View Code Duplication |
public function testAddingAliasWithMethodCalls() |
212
|
|
|
{ |
213
|
|
|
$loader = new ContainerHydrator(); |
214
|
|
|
|
215
|
|
|
$config = [ |
216
|
|
|
'TestInterface' => [ |
217
|
|
|
'class' => 'TestWithMethodCalls', |
218
|
|
|
'methods' => [ |
219
|
|
|
'setValue' => ['test value'], |
220
|
|
|
], |
221
|
|
|
], |
222
|
|
|
]; |
223
|
|
|
|
224
|
|
|
$container = $this->getContainerMock(); |
225
|
|
|
|
226
|
|
|
$definition = $this->getClassDefinitionMock(); |
227
|
|
|
|
228
|
|
|
$definition->expects($this->once()) |
229
|
|
|
->method('withMethodCalls') |
230
|
|
|
->with(['setValue' => ['test value']]) |
231
|
|
|
->willReturn($definition); |
232
|
|
|
|
233
|
|
|
$container->expects($this->once()) |
234
|
|
|
->method('add') |
235
|
|
|
->with('TestInterface', 'TestWithMethodCalls', false) |
236
|
|
|
->willReturn($definition); |
237
|
|
|
|
238
|
|
|
$loader->populate($container, $config); |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
public function testAddingAliasWithArgumentsAndMethodCalls() |
242
|
|
|
{ |
243
|
|
|
$loader = new ContainerHydrator(); |
244
|
|
|
|
245
|
|
|
$config = [ |
246
|
|
|
'TestInterface' => [ |
247
|
|
|
'class' => 'TestWithArgumentsAndMethodCalls', |
248
|
|
|
'arguments' => ['test', 'arguments'], |
249
|
|
|
'methods' => [ |
250
|
|
|
'setValue' => ['test value'], |
251
|
|
|
], |
252
|
|
|
], |
253
|
|
|
]; |
254
|
|
|
|
255
|
|
|
$container = $this->getContainerMock(); |
256
|
|
|
|
257
|
|
|
$definition = $this->getClassDefinitionMock(); |
258
|
|
|
|
259
|
|
|
$definition->expects($this->once()) |
260
|
|
|
->method('withArguments') |
261
|
|
|
->with(['test', 'arguments']) |
262
|
|
|
->willReturn($definition); |
263
|
|
|
|
264
|
|
|
$definition->expects($this->once()) |
265
|
|
|
->method('withMethodCalls') |
266
|
|
|
->with(['setValue' => ['test value']]) |
267
|
|
|
->willReturn($definition); |
268
|
|
|
|
269
|
|
|
$container->expects($this->once()) |
270
|
|
|
->method('add') |
271
|
|
|
->with('TestInterface', 'TestWithArgumentsAndMethodCalls', false) |
272
|
|
|
->willReturn($definition); |
273
|
|
|
|
274
|
|
|
$loader->populate($container, $config); |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
public function testAddDefinition() |
278
|
|
|
{ |
279
|
|
|
$loader = new ContainerHydrator(); |
280
|
|
|
|
281
|
|
|
$definition = $this->getClassDefinitionMock(); |
282
|
|
|
|
283
|
|
|
$config = [ |
284
|
|
|
'TestInterface' => [ |
285
|
|
|
'definition' => $definition, |
286
|
|
|
], |
287
|
|
|
]; |
288
|
|
|
|
289
|
|
|
$container = $this->getContainerMock(); |
290
|
|
|
|
291
|
|
|
$container->expects($this->once()) |
292
|
|
|
->method('add') |
293
|
|
|
->with('TestInterface', $definition, false); |
294
|
|
|
|
295
|
|
|
$loader->populate($container, $config); |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
/** |
299
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject | ContainerInterface |
300
|
|
|
*/ |
301
|
|
|
public function getContainerMock() |
302
|
|
|
{ |
303
|
|
|
return $this->getMockBuilder('\League\Container\ContainerInterface')->getMockForAbstractClass(); |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
/** |
307
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject | DefinitionInterface |
308
|
|
|
*/ |
309
|
|
|
public function getDefinitionMock() |
310
|
|
|
{ |
311
|
|
|
return $this->getMockBuilder('\League\Container\Definition\DefinitionInterface')->getMockForAbstractClass(); |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
/** |
315
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject | ClassDefinition |
316
|
|
|
*/ |
317
|
|
|
public function getClassDefinitionMock() |
318
|
|
|
{ |
319
|
|
|
return $this->getMockBuilder('\League\Container\Definition\ClassDefinition') |
320
|
|
|
->disableOriginalConstructor() |
321
|
|
|
->getMock(); |
322
|
|
|
} |
323
|
|
|
} |
324
|
|
|
|