Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 /**
|
Chris@0
|
4 * @file
|
Chris@0
|
5 * Contains \Drupal\Tests\Component\DependencyInjection\ContainerTest.
|
Chris@0
|
6 */
|
Chris@0
|
7
|
Chris@0
|
8 namespace Drupal\Tests\Component\DependencyInjection;
|
Chris@0
|
9
|
Chris@0
|
10 use Drupal\Component\Utility\Crypt;
|
Chris@0
|
11 use PHPUnit\Framework\TestCase;
|
Chris@0
|
12 use Symfony\Component\DependencyInjection\ContainerInterface;
|
Chris@0
|
13 use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
|
Chris@0
|
14 use Symfony\Component\DependencyInjection\Exception\LogicException;
|
Chris@0
|
15 use Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException;
|
Chris@0
|
16 use Symfony\Component\DependencyInjection\Exception\RuntimeException;
|
Chris@0
|
17 use Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException;
|
Chris@0
|
18 use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
|
Chris@0
|
19 use Prophecy\Argument;
|
Chris@0
|
20
|
Chris@0
|
21 /**
|
Chris@0
|
22 * @coversDefaultClass \Drupal\Component\DependencyInjection\Container
|
Chris@0
|
23 * @group DependencyInjection
|
Chris@0
|
24 */
|
Chris@0
|
25 class ContainerTest extends TestCase {
|
Chris@0
|
26
|
Chris@0
|
27 /**
|
Chris@0
|
28 * The tested container.
|
Chris@0
|
29 *
|
Chris@0
|
30 * @var \Symfony\Component\DependencyInjection\ContainerInterface
|
Chris@0
|
31 */
|
Chris@0
|
32 protected $container;
|
Chris@0
|
33
|
Chris@0
|
34 /**
|
Chris@0
|
35 * The container definition used for the test.
|
Chris@0
|
36 *
|
Chris@0
|
37 * @var array
|
Chris@0
|
38 */
|
Chris@0
|
39 protected $containerDefinition;
|
Chris@0
|
40
|
Chris@0
|
41 /**
|
Chris@0
|
42 * The container class to be tested.
|
Chris@0
|
43 *
|
Chris@0
|
44 * @var bool
|
Chris@0
|
45 */
|
Chris@0
|
46 protected $containerClass;
|
Chris@0
|
47
|
Chris@0
|
48 /**
|
Chris@0
|
49 * Whether the container uses the machine-optimized format or not.
|
Chris@0
|
50 *
|
Chris@0
|
51 * @var bool
|
Chris@0
|
52 */
|
Chris@0
|
53 protected $machineFormat;
|
Chris@0
|
54
|
Chris@0
|
55 /**
|
Chris@0
|
56 * {@inheritdoc}
|
Chris@0
|
57 */
|
Chris@0
|
58 protected function setUp() {
|
Chris@0
|
59 $this->machineFormat = TRUE;
|
Chris@0
|
60 $this->containerClass = '\Drupal\Component\DependencyInjection\Container';
|
Chris@0
|
61 $this->containerDefinition = $this->getMockContainerDefinition();
|
Chris@0
|
62 $this->container = new $this->containerClass($this->containerDefinition);
|
Chris@0
|
63 }
|
Chris@0
|
64
|
Chris@0
|
65 /**
|
Chris@0
|
66 * Tests that passing a non-supported format throws an InvalidArgumentException.
|
Chris@0
|
67 *
|
Chris@0
|
68 * @covers ::__construct
|
Chris@0
|
69 */
|
Chris@0
|
70 public function testConstruct() {
|
Chris@0
|
71 $container_definition = $this->getMockContainerDefinition();
|
Chris@0
|
72 $container_definition['machine_format'] = !$this->machineFormat;
|
Chris@14
|
73 if (method_exists($this, 'expectException')) {
|
Chris@14
|
74 $this->expectException(InvalidArgumentException::class);
|
Chris@14
|
75 }
|
Chris@14
|
76 else {
|
Chris@14
|
77 $this->setExpectedException(InvalidArgumentException::class);
|
Chris@14
|
78 }
|
Chris@0
|
79 $container = new $this->containerClass($container_definition);
|
Chris@0
|
80 }
|
Chris@0
|
81
|
Chris@0
|
82 /**
|
Chris@0
|
83 * Tests that Container::getParameter() works properly.
|
Chris@0
|
84 *
|
Chris@0
|
85 * @covers ::getParameter
|
Chris@0
|
86 */
|
Chris@0
|
87 public function testGetParameter() {
|
Chris@0
|
88 $this->assertEquals($this->containerDefinition['parameters']['some_config'], $this->container->getParameter('some_config'), 'Container parameter matches for %some_config%.');
|
Chris@0
|
89 $this->assertEquals($this->containerDefinition['parameters']['some_other_config'], $this->container->getParameter('some_other_config'), 'Container parameter matches for %some_other_config%.');
|
Chris@0
|
90 }
|
Chris@0
|
91
|
Chris@0
|
92 /**
|
Chris@0
|
93 * Tests that Container::getParameter() works properly for non-existing
|
Chris@0
|
94 * parameters.
|
Chris@0
|
95 *
|
Chris@0
|
96 * @covers ::getParameter
|
Chris@0
|
97 * @covers ::getParameterAlternatives
|
Chris@0
|
98 * @covers ::getAlternatives
|
Chris@0
|
99 */
|
Chris@0
|
100 public function testGetParameterIfNotFound() {
|
Chris@14
|
101 if (method_exists($this, 'expectException')) {
|
Chris@14
|
102 $this->expectException(ParameterNotFoundException::class);
|
Chris@14
|
103 }
|
Chris@14
|
104 else {
|
Chris@14
|
105 $this->setExpectedException(ParameterNotFoundException::class);
|
Chris@14
|
106 }
|
Chris@0
|
107 $this->container->getParameter('parameter_that_does_not_exist');
|
Chris@0
|
108 }
|
Chris@0
|
109
|
Chris@0
|
110 /**
|
Chris@0
|
111 * Tests that Container::getParameter() works properly for NULL parameters.
|
Chris@0
|
112 *
|
Chris@0
|
113 * @covers ::getParameter
|
Chris@0
|
114 */
|
Chris@0
|
115 public function testGetParameterIfNotFoundBecauseNull() {
|
Chris@14
|
116 if (method_exists($this, 'expectException')) {
|
Chris@14
|
117 $this->expectException(ParameterNotFoundException::class);
|
Chris@14
|
118 }
|
Chris@14
|
119 else {
|
Chris@14
|
120 $this->setExpectedException(ParameterNotFoundException::class);
|
Chris@14
|
121 }
|
Chris@0
|
122 $this->container->getParameter(NULL);
|
Chris@0
|
123 }
|
Chris@0
|
124
|
Chris@0
|
125 /**
|
Chris@0
|
126 * Tests that Container::hasParameter() works properly.
|
Chris@0
|
127 *
|
Chris@0
|
128 * @covers ::hasParameter
|
Chris@0
|
129 */
|
Chris@0
|
130 public function testHasParameter() {
|
Chris@0
|
131 $this->assertTrue($this->container->hasParameter('some_config'), 'Container parameters include %some_config%.');
|
Chris@0
|
132 $this->assertFalse($this->container->hasParameter('some_config_not_exists'), 'Container parameters do not include %some_config_not_exists%.');
|
Chris@0
|
133 }
|
Chris@0
|
134
|
Chris@0
|
135 /**
|
Chris@0
|
136 * Tests that Container::setParameter() in an unfrozen case works properly.
|
Chris@0
|
137 *
|
Chris@0
|
138 * @covers ::setParameter
|
Chris@0
|
139 */
|
Chris@0
|
140 public function testSetParameterWithUnfrozenContainer() {
|
Chris@0
|
141 $container_definition = $this->containerDefinition;
|
Chris@0
|
142 $container_definition['frozen'] = FALSE;
|
Chris@0
|
143 $this->container = new $this->containerClass($container_definition);
|
Chris@0
|
144 $this->container->setParameter('some_config', 'new_value');
|
Chris@0
|
145 $this->assertEquals('new_value', $this->container->getParameter('some_config'), 'Container parameters can be set.');
|
Chris@0
|
146 }
|
Chris@0
|
147
|
Chris@0
|
148 /**
|
Chris@0
|
149 * Tests that Container::setParameter() in a frozen case works properly.
|
Chris@0
|
150 *
|
Chris@0
|
151 * @covers ::setParameter
|
Chris@0
|
152 */
|
Chris@0
|
153 public function testSetParameterWithFrozenContainer() {
|
Chris@0
|
154 $this->container = new $this->containerClass($this->containerDefinition);
|
Chris@14
|
155 if (method_exists($this, 'expectException')) {
|
Chris@14
|
156 $this->expectException(LogicException::class);
|
Chris@14
|
157 }
|
Chris@14
|
158 else {
|
Chris@14
|
159 $this->setExpectedException(LogicException::class);
|
Chris@14
|
160 }
|
Chris@0
|
161 $this->container->setParameter('some_config', 'new_value');
|
Chris@0
|
162 }
|
Chris@0
|
163
|
Chris@0
|
164 /**
|
Chris@0
|
165 * Tests that Container::get() works properly.
|
Chris@0
|
166 *
|
Chris@0
|
167 * @covers ::get
|
Chris@0
|
168 * @covers ::createService
|
Chris@0
|
169 */
|
Chris@0
|
170 public function testGet() {
|
Chris@0
|
171 $container = $this->container->get('service_container');
|
Chris@0
|
172 $this->assertSame($this->container, $container, 'Container can be retrieved from itself.');
|
Chris@0
|
173
|
Chris@0
|
174 // Retrieve services of the container.
|
Chris@0
|
175 $other_service_class = $this->containerDefinition['services']['other.service']['class'];
|
Chris@0
|
176 $other_service = $this->container->get('other.service');
|
Chris@0
|
177 $this->assertInstanceOf($other_service_class, $other_service, 'other.service has the right class.');
|
Chris@0
|
178
|
Chris@0
|
179 $some_parameter = $this->containerDefinition['parameters']['some_config'];
|
Chris@0
|
180 $some_other_parameter = $this->containerDefinition['parameters']['some_other_config'];
|
Chris@0
|
181
|
Chris@0
|
182 $service = $this->container->get('service.provider');
|
Chris@0
|
183
|
Chris@0
|
184 $this->assertEquals($other_service, $service->getSomeOtherService(), '@other.service was injected via constructor.');
|
Chris@0
|
185 $this->assertEquals($some_parameter, $service->getSomeParameter(), '%some_config% was injected via constructor.');
|
Chris@0
|
186 $this->assertEquals($this->container, $service->getContainer(), 'Container was injected via setter injection.');
|
Chris@0
|
187 $this->assertEquals($some_other_parameter, $service->getSomeOtherParameter(), '%some_other_config% was injected via setter injection.');
|
Chris@0
|
188 $this->assertEquals($service->_someProperty, 'foo', 'Service has added properties.');
|
Chris@0
|
189 }
|
Chris@0
|
190
|
Chris@0
|
191 /**
|
Chris@0
|
192 * Tests that Container::get() for non-shared services works properly.
|
Chris@0
|
193 *
|
Chris@0
|
194 * @covers ::get
|
Chris@0
|
195 * @covers ::createService
|
Chris@0
|
196 */
|
Chris@0
|
197 public function testGetForNonSharedService() {
|
Chris@0
|
198 $service = $this->container->get('non_shared_service');
|
Chris@0
|
199 $service2 = $this->container->get('non_shared_service');
|
Chris@0
|
200
|
Chris@0
|
201 $this->assertNotSame($service, $service2, 'Non shared services are always re-instantiated.');
|
Chris@0
|
202 }
|
Chris@0
|
203
|
Chris@0
|
204 /**
|
Chris@0
|
205 * Tests that Container::get() works properly for class from parameters.
|
Chris@0
|
206 *
|
Chris@0
|
207 * @covers ::get
|
Chris@0
|
208 * @covers ::createService
|
Chris@0
|
209 */
|
Chris@0
|
210 public function testGetForClassFromParameter() {
|
Chris@0
|
211 $container_definition = $this->containerDefinition;
|
Chris@0
|
212 $container_definition['frozen'] = FALSE;
|
Chris@0
|
213 $container = new $this->containerClass($container_definition);
|
Chris@0
|
214
|
Chris@0
|
215 $other_service_class = $this->containerDefinition['parameters']['some_parameter_class'];
|
Chris@0
|
216 $other_service = $container->get('other.service_class_from_parameter');
|
Chris@0
|
217 $this->assertInstanceOf($other_service_class, $other_service, 'other.service_class_from_parameter has the right class.');
|
Chris@0
|
218 }
|
Chris@0
|
219
|
Chris@0
|
220 /**
|
Chris@0
|
221 * Tests that Container::set() works properly.
|
Chris@0
|
222 *
|
Chris@0
|
223 * @covers ::set
|
Chris@0
|
224 */
|
Chris@0
|
225 public function testSet() {
|
Chris@0
|
226 $this->assertNull($this->container->get('new_id', ContainerInterface::NULL_ON_INVALID_REFERENCE));
|
Chris@0
|
227 $mock_service = new MockService();
|
Chris@0
|
228 $this->container->set('new_id', $mock_service);
|
Chris@0
|
229
|
Chris@0
|
230 $this->assertSame($mock_service, $this->container->get('new_id'), 'A manual set service works as expected.');
|
Chris@0
|
231 }
|
Chris@0
|
232
|
Chris@0
|
233 /**
|
Chris@0
|
234 * Tests that Container::has() works properly.
|
Chris@0
|
235 *
|
Chris@0
|
236 * @covers ::has
|
Chris@0
|
237 */
|
Chris@0
|
238 public function testHas() {
|
Chris@0
|
239 $this->assertTrue($this->container->has('other.service'));
|
Chris@0
|
240 $this->assertFalse($this->container->has('another.service'));
|
Chris@0
|
241
|
Chris@0
|
242 // Set the service manually, ensure that its also respected.
|
Chris@0
|
243 $mock_service = new MockService();
|
Chris@0
|
244 $this->container->set('another.service', $mock_service);
|
Chris@0
|
245 $this->assertTrue($this->container->has('another.service'));
|
Chris@0
|
246 }
|
Chris@0
|
247
|
Chris@0
|
248 /**
|
Chris@0
|
249 * Tests that Container::has() for aliased services works properly.
|
Chris@0
|
250 *
|
Chris@0
|
251 * @covers ::has
|
Chris@0
|
252 */
|
Chris@0
|
253 public function testHasForAliasedService() {
|
Chris@0
|
254 $service = $this->container->has('service.provider');
|
Chris@0
|
255 $aliased_service = $this->container->has('service.provider_alias');
|
Chris@0
|
256 $this->assertSame($service, $aliased_service);
|
Chris@0
|
257 }
|
Chris@0
|
258
|
Chris@0
|
259 /**
|
Chris@0
|
260 * Tests that Container::get() for circular dependencies works properly.
|
Chris@0
|
261 * @covers ::get
|
Chris@0
|
262 * @covers ::createService
|
Chris@0
|
263 */
|
Chris@0
|
264 public function testGetForCircularServices() {
|
Chris@14
|
265 if (method_exists($this, 'expectException')) {
|
Chris@14
|
266 $this->expectException(ServiceCircularReferenceException::class);
|
Chris@14
|
267 }
|
Chris@14
|
268 else {
|
Chris@14
|
269 $this->setExpectedException(ServiceCircularReferenceException::class);
|
Chris@14
|
270 }
|
Chris@0
|
271 $this->container->get('circular_dependency');
|
Chris@0
|
272 }
|
Chris@0
|
273
|
Chris@0
|
274 /**
|
Chris@0
|
275 * Tests that Container::get() for non-existent services works properly.
|
Chris@0
|
276 *
|
Chris@0
|
277 * @covers ::get
|
Chris@0
|
278 * @covers ::createService
|
Chris@0
|
279 * @covers ::getAlternatives
|
Chris@0
|
280 * @covers ::getServiceAlternatives
|
Chris@0
|
281 */
|
Chris@0
|
282 public function testGetForNonExistantService() {
|
Chris@14
|
283 if (method_exists($this, 'expectException')) {
|
Chris@14
|
284 $this->expectException(ServiceNotFoundException::class);
|
Chris@14
|
285 }
|
Chris@14
|
286 else {
|
Chris@14
|
287 $this->setExpectedException(ServiceNotFoundException::class);
|
Chris@14
|
288 }
|
Chris@0
|
289 $this->container->get('service_not_exists');
|
Chris@0
|
290 }
|
Chris@0
|
291
|
Chris@0
|
292 /**
|
Chris@0
|
293 * Tests that Container::get() for a serialized definition works properly.
|
Chris@0
|
294 *
|
Chris@0
|
295 * @covers ::get
|
Chris@0
|
296 * @covers ::createService
|
Chris@0
|
297 */
|
Chris@0
|
298 public function testGetForSerializedServiceDefinition() {
|
Chris@0
|
299 $container_definition = $this->containerDefinition;
|
Chris@0
|
300 $container_definition['services']['other.service'] = serialize($container_definition['services']['other.service']);
|
Chris@0
|
301 $container = new $this->containerClass($container_definition);
|
Chris@0
|
302
|
Chris@0
|
303 // Retrieve services of the container.
|
Chris@0
|
304 $other_service_class = $this->containerDefinition['services']['other.service']['class'];
|
Chris@0
|
305 $other_service = $container->get('other.service');
|
Chris@0
|
306 $this->assertInstanceOf($other_service_class, $other_service, 'other.service has the right class.');
|
Chris@0
|
307
|
Chris@0
|
308 $service = $container->get('service.provider');
|
Chris@0
|
309 $this->assertEquals($other_service, $service->getSomeOtherService(), '@other.service was injected via constructor.');
|
Chris@0
|
310 }
|
Chris@0
|
311
|
Chris@0
|
312 /**
|
Chris@0
|
313 * Tests that Container::get() for non-existent parameters works properly.
|
Chris@0
|
314 *
|
Chris@0
|
315 * @covers ::get
|
Chris@0
|
316 * @covers ::createService
|
Chris@0
|
317 * @covers ::resolveServicesAndParameters
|
Chris@0
|
318 */
|
Chris@0
|
319 public function testGetForNonExistantParameterDependency() {
|
Chris@0
|
320 $service = $this->container->get('service_parameter_not_exists', ContainerInterface::NULL_ON_INVALID_REFERENCE);
|
Chris@0
|
321 $this->assertNull($service, 'Service is NULL.');
|
Chris@0
|
322 }
|
Chris@0
|
323
|
Chris@0
|
324 /**
|
Chris@0
|
325 * Tests Container::get() with an exception due to missing parameter on the second call.
|
Chris@0
|
326 *
|
Chris@0
|
327 * @covers ::get
|
Chris@0
|
328 * @covers ::createService
|
Chris@0
|
329 * @covers ::resolveServicesAndParameters
|
Chris@0
|
330 */
|
Chris@0
|
331 public function testGetForParameterDependencyWithExceptionOnSecondCall() {
|
Chris@0
|
332 $service = $this->container->get('service_parameter_not_exists', ContainerInterface::NULL_ON_INVALID_REFERENCE);
|
Chris@0
|
333 $this->assertNull($service, 'Service is NULL.');
|
Chris@0
|
334
|
Chris@0
|
335 // Reset the service.
|
Chris@0
|
336 $this->container->set('service_parameter_not_exists', NULL);
|
Chris@14
|
337 if (method_exists($this, 'expectException')) {
|
Chris@14
|
338 $this->expectException(InvalidArgumentException::class);
|
Chris@14
|
339 }
|
Chris@14
|
340 else {
|
Chris@14
|
341 $this->setExpectedException(InvalidArgumentException::class);
|
Chris@14
|
342 }
|
Chris@0
|
343 $this->container->get('service_parameter_not_exists');
|
Chris@0
|
344 }
|
Chris@0
|
345
|
Chris@0
|
346 /**
|
Chris@0
|
347 * Tests that Container::get() for non-existent parameters works properly.
|
Chris@0
|
348 *
|
Chris@0
|
349 * @covers ::get
|
Chris@0
|
350 * @covers ::createService
|
Chris@0
|
351 * @covers ::resolveServicesAndParameters
|
Chris@0
|
352 */
|
Chris@0
|
353 public function testGetForNonExistantParameterDependencyWithException() {
|
Chris@14
|
354 if (method_exists($this, 'expectException')) {
|
Chris@14
|
355 $this->expectException(InvalidArgumentException::class);
|
Chris@14
|
356 }
|
Chris@14
|
357 else {
|
Chris@14
|
358 $this->setExpectedException(InvalidArgumentException::class);
|
Chris@14
|
359 }
|
Chris@0
|
360 $this->container->get('service_parameter_not_exists');
|
Chris@0
|
361 }
|
Chris@0
|
362
|
Chris@0
|
363 /**
|
Chris@0
|
364 * Tests that Container::get() for non-existent dependencies works properly.
|
Chris@0
|
365 *
|
Chris@0
|
366 * @covers ::get
|
Chris@0
|
367 * @covers ::createService
|
Chris@0
|
368 * @covers ::resolveServicesAndParameters
|
Chris@0
|
369 */
|
Chris@0
|
370 public function testGetForNonExistantServiceDependency() {
|
Chris@0
|
371 $service = $this->container->get('service_dependency_not_exists', ContainerInterface::NULL_ON_INVALID_REFERENCE);
|
Chris@0
|
372 $this->assertNull($service, 'Service is NULL.');
|
Chris@0
|
373 }
|
Chris@0
|
374
|
Chris@0
|
375 /**
|
Chris@0
|
376 * Tests that Container::get() for non-existent dependencies works properly.
|
Chris@0
|
377 *
|
Chris@0
|
378 * @covers ::get
|
Chris@0
|
379 * @covers ::createService
|
Chris@0
|
380 * @covers ::resolveServicesAndParameters
|
Chris@0
|
381 * @covers ::getAlternatives
|
Chris@0
|
382 */
|
Chris@0
|
383 public function testGetForNonExistantServiceDependencyWithException() {
|
Chris@14
|
384 if (method_exists($this, 'expectException')) {
|
Chris@14
|
385 $this->expectException(ServiceNotFoundException::class);
|
Chris@14
|
386 }
|
Chris@14
|
387 else {
|
Chris@14
|
388 $this->setExpectedException(ServiceNotFoundException::class);
|
Chris@14
|
389 }
|
Chris@0
|
390 $this->container->get('service_dependency_not_exists');
|
Chris@0
|
391 }
|
Chris@0
|
392
|
Chris@0
|
393 /**
|
Chris@0
|
394 * Tests that Container::get() for non-existent services works properly.
|
Chris@0
|
395 *
|
Chris@0
|
396 * @covers ::get
|
Chris@0
|
397 * @covers ::createService
|
Chris@0
|
398 */
|
Chris@0
|
399 public function testGetForNonExistantServiceWhenUsingNull() {
|
Chris@0
|
400 $this->assertNull($this->container->get('service_not_exists', ContainerInterface::NULL_ON_INVALID_REFERENCE), 'Not found service does not throw exception.');
|
Chris@0
|
401 }
|
Chris@0
|
402
|
Chris@0
|
403 /**
|
Chris@0
|
404 * Tests that Container::get() for NULL service works properly.
|
Chris@0
|
405 * @covers ::get
|
Chris@0
|
406 * @covers ::createService
|
Chris@0
|
407 */
|
Chris@0
|
408 public function testGetForNonExistantNULLService() {
|
Chris@14
|
409 if (method_exists($this, 'expectException')) {
|
Chris@14
|
410 $this->expectException(ServiceNotFoundException::class);
|
Chris@14
|
411 }
|
Chris@14
|
412 else {
|
Chris@14
|
413 $this->setExpectedException(ServiceNotFoundException::class);
|
Chris@14
|
414 }
|
Chris@0
|
415 $this->container->get(NULL);
|
Chris@0
|
416 }
|
Chris@0
|
417
|
Chris@0
|
418 /**
|
Chris@0
|
419 * Tests multiple Container::get() calls for non-existing dependencies work.
|
Chris@0
|
420 *
|
Chris@0
|
421 * @covers ::get
|
Chris@0
|
422 * @covers ::createService
|
Chris@0
|
423 */
|
Chris@0
|
424 public function testGetForNonExistantServiceMultipleTimes() {
|
Chris@0
|
425 $container = new $this->containerClass();
|
Chris@0
|
426
|
Chris@0
|
427 $this->assertNull($container->get('service_not_exists', ContainerInterface::NULL_ON_INVALID_REFERENCE), 'Not found service does not throw exception.');
|
Chris@0
|
428 $this->assertNull($container->get('service_not_exists', ContainerInterface::NULL_ON_INVALID_REFERENCE), 'Not found service does not throw exception on second call.');
|
Chris@0
|
429 }
|
Chris@0
|
430
|
Chris@0
|
431 /**
|
Chris@0
|
432 * Tests multiple Container::get() calls with exception on the second time.
|
Chris@0
|
433 *
|
Chris@0
|
434 * @covers ::get
|
Chris@0
|
435 * @covers ::createService
|
Chris@0
|
436 * @covers ::getAlternatives
|
Chris@0
|
437 */
|
Chris@0
|
438 public function testGetForNonExistantServiceWithExceptionOnSecondCall() {
|
Chris@0
|
439 $this->assertNull($this->container->get('service_not_exists', ContainerInterface::NULL_ON_INVALID_REFERENCE), 'Not found service does nto throw exception.');
|
Chris@14
|
440 if (method_exists($this, 'expectException')) {
|
Chris@14
|
441 $this->expectException(ServiceNotFoundException::class);
|
Chris@14
|
442 }
|
Chris@14
|
443 else {
|
Chris@14
|
444 $this->setExpectedException(ServiceNotFoundException::class);
|
Chris@14
|
445 }
|
Chris@0
|
446 $this->container->get('service_not_exists');
|
Chris@0
|
447 }
|
Chris@0
|
448
|
Chris@0
|
449 /**
|
Chris@0
|
450 * Tests that Container::get() for aliased services works properly.
|
Chris@0
|
451 *
|
Chris@0
|
452 * @covers ::get
|
Chris@0
|
453 * @covers ::createService
|
Chris@0
|
454 */
|
Chris@0
|
455 public function testGetForAliasedService() {
|
Chris@0
|
456 $service = $this->container->get('service.provider');
|
Chris@0
|
457 $aliased_service = $this->container->get('service.provider_alias');
|
Chris@0
|
458 $this->assertSame($service, $aliased_service);
|
Chris@0
|
459 }
|
Chris@0
|
460
|
Chris@0
|
461 /**
|
Chris@0
|
462 * Tests that Container::get() for synthetic services works - if defined.
|
Chris@0
|
463 *
|
Chris@0
|
464 * @covers ::get
|
Chris@0
|
465 * @covers ::createService
|
Chris@0
|
466 */
|
Chris@0
|
467 public function testGetForSyntheticService() {
|
Chris@0
|
468 $synthetic_service = new \stdClass();
|
Chris@0
|
469 $this->container->set('synthetic', $synthetic_service);
|
Chris@0
|
470 $test_service = $this->container->get('synthetic');
|
Chris@0
|
471 $this->assertSame($synthetic_service, $test_service);
|
Chris@0
|
472 }
|
Chris@0
|
473
|
Chris@0
|
474 /**
|
Chris@0
|
475 * Tests that Container::get() for synthetic services throws an Exception if not defined.
|
Chris@0
|
476 *
|
Chris@0
|
477 * @covers ::get
|
Chris@0
|
478 * @covers ::createService
|
Chris@0
|
479 */
|
Chris@0
|
480 public function testGetForSyntheticServiceWithException() {
|
Chris@14
|
481 if (method_exists($this, 'expectException')) {
|
Chris@14
|
482 $this->expectException(RuntimeException::class);
|
Chris@14
|
483 }
|
Chris@14
|
484 else {
|
Chris@14
|
485 $this->setExpectedException(RuntimeException::class);
|
Chris@14
|
486 }
|
Chris@0
|
487 $this->container->get('synthetic');
|
Chris@0
|
488 }
|
Chris@0
|
489
|
Chris@0
|
490 /**
|
Chris@0
|
491 * Tests that Container::get() for services with file includes works.
|
Chris@0
|
492 *
|
Chris@0
|
493 * @covers ::get
|
Chris@0
|
494 * @covers ::createService
|
Chris@0
|
495 */
|
Chris@0
|
496 public function testGetWithFileInclude() {
|
Chris@0
|
497 $file_service = $this->container->get('container_test_file_service_test');
|
Chris@0
|
498 $this->assertTrue(function_exists('container_test_file_service_test_service_function'));
|
Chris@0
|
499 $this->assertEquals('Hello Container', container_test_file_service_test_service_function());
|
Chris@0
|
500 }
|
Chris@0
|
501
|
Chris@0
|
502 /**
|
Chris@0
|
503 * Tests that Container::get() for various arguments lengths works.
|
Chris@0
|
504 *
|
Chris@0
|
505 * @covers ::get
|
Chris@0
|
506 * @covers ::createService
|
Chris@0
|
507 * @covers ::resolveServicesAndParameters
|
Chris@0
|
508 */
|
Chris@0
|
509 public function testGetForInstantiationWithVariousArgumentLengths() {
|
Chris@0
|
510 $args = [];
|
Chris@0
|
511 for ($i = 0; $i < 12; $i++) {
|
Chris@0
|
512 $instantiation_service = $this->container->get('service_test_instantiation_' . $i);
|
Chris@0
|
513 $this->assertEquals($args, $instantiation_service->getArguments());
|
Chris@0
|
514 $args[] = 'arg_' . $i;
|
Chris@0
|
515 }
|
Chris@0
|
516 }
|
Chris@0
|
517
|
Chris@0
|
518 /**
|
Chris@0
|
519 * Tests that Container::get() for wrong factories works correctly.
|
Chris@0
|
520 *
|
Chris@0
|
521 * @covers ::get
|
Chris@0
|
522 * @covers ::createService
|
Chris@0
|
523 */
|
Chris@0
|
524 public function testGetForWrongFactory() {
|
Chris@14
|
525 if (method_exists($this, 'expectException')) {
|
Chris@14
|
526 $this->expectException(RuntimeException::class);
|
Chris@14
|
527 }
|
Chris@14
|
528 else {
|
Chris@14
|
529 $this->setExpectedException(RuntimeException::class);
|
Chris@14
|
530 }
|
Chris@0
|
531 $this->container->get('wrong_factory');
|
Chris@0
|
532 }
|
Chris@0
|
533
|
Chris@0
|
534 /**
|
Chris@0
|
535 * Tests Container::get() for factories via services (Symfony 2.7.0).
|
Chris@0
|
536 *
|
Chris@0
|
537 * @covers ::get
|
Chris@0
|
538 * @covers ::createService
|
Chris@0
|
539 */
|
Chris@0
|
540 public function testGetForFactoryService() {
|
Chris@0
|
541 $factory_service = $this->container->get('factory_service');
|
Chris@0
|
542 $factory_service_class = $this->container->getParameter('factory_service_class');
|
Chris@0
|
543 $this->assertInstanceOf($factory_service_class, $factory_service);
|
Chris@0
|
544 }
|
Chris@0
|
545
|
Chris@0
|
546 /**
|
Chris@0
|
547 * Tests that Container::get() for factories via class works (Symfony 2.7.0).
|
Chris@0
|
548 *
|
Chris@0
|
549 * @covers ::get
|
Chris@0
|
550 * @covers ::createService
|
Chris@0
|
551 */
|
Chris@0
|
552 public function testGetForFactoryClass() {
|
Chris@0
|
553 $service = $this->container->get('service.provider');
|
Chris@0
|
554 $factory_service = $this->container->get('factory_class');
|
Chris@0
|
555
|
Chris@0
|
556 $this->assertInstanceOf(get_class($service), $factory_service);
|
Chris@0
|
557 $this->assertEquals('bar', $factory_service->getSomeParameter(), 'Correct parameter was passed via the factory class instantiation.');
|
Chris@0
|
558 $this->assertEquals($this->container, $factory_service->getContainer(), 'Container was injected via setter injection.');
|
Chris@0
|
559 }
|
Chris@0
|
560
|
Chris@0
|
561 /**
|
Chris@0
|
562 * Tests that Container::get() for configurable services throws an Exception.
|
Chris@0
|
563 *
|
Chris@0
|
564 * @covers ::get
|
Chris@0
|
565 * @covers ::createService
|
Chris@0
|
566 */
|
Chris@0
|
567 public function testGetForConfiguratorWithException() {
|
Chris@14
|
568 if (method_exists($this, 'expectException')) {
|
Chris@14
|
569 $this->expectException(InvalidArgumentException::class);
|
Chris@14
|
570 }
|
Chris@14
|
571 else {
|
Chris@14
|
572 $this->setExpectedException(InvalidArgumentException::class);
|
Chris@14
|
573 }
|
Chris@0
|
574 $this->container->get('configurable_service_exception');
|
Chris@0
|
575 }
|
Chris@0
|
576
|
Chris@0
|
577 /**
|
Chris@0
|
578 * Tests that Container::get() for configurable services works.
|
Chris@0
|
579 *
|
Chris@0
|
580 * @covers ::get
|
Chris@0
|
581 * @covers ::createService
|
Chris@0
|
582 */
|
Chris@0
|
583 public function testGetForConfigurator() {
|
Chris@0
|
584 $container = $this->container;
|
Chris@0
|
585
|
Chris@0
|
586 // Setup a configurator.
|
Chris@0
|
587 $configurator = $this->prophesize('\Drupal\Tests\Component\DependencyInjection\MockConfiguratorInterface');
|
Chris@0
|
588 $configurator->configureService(Argument::type('object'))
|
Chris@0
|
589 ->shouldBeCalled(1)
|
Chris@0
|
590 ->will(function ($args) use ($container) {
|
Chris@0
|
591 $args[0]->setContainer($container);
|
Chris@0
|
592 });
|
Chris@0
|
593 $container->set('configurator', $configurator->reveal());
|
Chris@0
|
594
|
Chris@0
|
595 // Test that the configurator worked.
|
Chris@0
|
596 $service = $container->get('configurable_service');
|
Chris@0
|
597 $this->assertSame($container, $service->getContainer(), 'Container was injected via configurator.');
|
Chris@0
|
598 }
|
Chris@0
|
599
|
Chris@0
|
600 /**
|
Chris@0
|
601 * Tests that private services work correctly.
|
Chris@0
|
602 *
|
Chris@0
|
603 * @covers ::get
|
Chris@0
|
604 * @covers ::createService
|
Chris@0
|
605 * @covers ::resolveServicesAndParameters
|
Chris@0
|
606 */
|
Chris@0
|
607 public function testResolveServicesAndParametersForPrivateService() {
|
Chris@0
|
608 $service = $this->container->get('service_using_private');
|
Chris@0
|
609 $private_service = $service->getSomeOtherService();
|
Chris@0
|
610 $this->assertEquals($private_service->getSomeParameter(), 'really_private_lama', 'Private was found successfully.');
|
Chris@0
|
611
|
Chris@0
|
612 // Test that sharing the same private services works.
|
Chris@0
|
613 $service = $this->container->get('another_service_using_private');
|
Chris@0
|
614 $another_private_service = $service->getSomeOtherService();
|
Chris@0
|
615 $this->assertNotSame($private_service, $another_private_service, 'Private service is not shared.');
|
Chris@0
|
616 $this->assertEquals($private_service->getSomeParameter(), 'really_private_lama', 'Private was found successfully.');
|
Chris@0
|
617 }
|
Chris@0
|
618
|
Chris@0
|
619 /**
|
Chris@0
|
620 * Tests that private service sharing works correctly.
|
Chris@0
|
621 *
|
Chris@0
|
622 * @covers ::get
|
Chris@0
|
623 * @covers ::createService
|
Chris@0
|
624 * @covers ::resolveServicesAndParameters
|
Chris@0
|
625 */
|
Chris@0
|
626 public function testResolveServicesAndParametersForSharedPrivateService() {
|
Chris@0
|
627 $service = $this->container->get('service_using_shared_private');
|
Chris@0
|
628 $private_service = $service->getSomeOtherService();
|
Chris@0
|
629 $this->assertEquals($private_service->getSomeParameter(), 'really_private_lama', 'Private was found successfully.');
|
Chris@0
|
630
|
Chris@0
|
631 // Test that sharing the same private services works.
|
Chris@0
|
632 $service = $this->container->get('another_service_using_shared_private');
|
Chris@0
|
633 $same_private_service = $service->getSomeOtherService();
|
Chris@0
|
634 $this->assertSame($private_service, $same_private_service, 'Private service is shared.');
|
Chris@0
|
635 $this->assertEquals($private_service->getSomeParameter(), 'really_private_lama', 'Private was found successfully.');
|
Chris@0
|
636 }
|
Chris@0
|
637
|
Chris@0
|
638 /**
|
Chris@0
|
639 * Tests that services with an array of arguments work correctly.
|
Chris@0
|
640 *
|
Chris@0
|
641 * @covers ::get
|
Chris@0
|
642 * @covers ::createService
|
Chris@0
|
643 * @covers ::resolveServicesAndParameters
|
Chris@0
|
644 */
|
Chris@0
|
645 public function testResolveServicesAndParametersForArgumentsUsingDeepArray() {
|
Chris@0
|
646 $service = $this->container->get('service_using_array');
|
Chris@0
|
647 $other_service = $this->container->get('other.service');
|
Chris@0
|
648 $this->assertEquals($other_service, $service->getSomeOtherService(), '@other.service was injected via constructor.');
|
Chris@0
|
649 }
|
Chris@0
|
650
|
Chris@0
|
651 /**
|
Chris@0
|
652 * Tests that services that are optional work correctly.
|
Chris@0
|
653 *
|
Chris@0
|
654 * @covers ::get
|
Chris@0
|
655 * @covers ::createService
|
Chris@0
|
656 * @covers ::resolveServicesAndParameters
|
Chris@0
|
657 */
|
Chris@0
|
658 public function testResolveServicesAndParametersForOptionalServiceDependencies() {
|
Chris@0
|
659 $service = $this->container->get('service_with_optional_dependency');
|
Chris@0
|
660 $this->assertNull($service->getSomeOtherService(), 'other service was NULL was expected.');
|
Chris@0
|
661 }
|
Chris@0
|
662
|
Chris@0
|
663 /**
|
Chris@0
|
664 * Tests that an invalid argument throw an Exception.
|
Chris@0
|
665 *
|
Chris@0
|
666 * @covers ::get
|
Chris@0
|
667 * @covers ::createService
|
Chris@0
|
668 * @covers ::resolveServicesAndParameters
|
Chris@0
|
669 */
|
Chris@0
|
670 public function testResolveServicesAndParametersForInvalidArgument() {
|
Chris@14
|
671 if (method_exists($this, 'expectException')) {
|
Chris@14
|
672 $this->expectException(InvalidArgumentException::class);
|
Chris@14
|
673 }
|
Chris@14
|
674 else {
|
Chris@14
|
675 $this->setExpectedException(InvalidArgumentException::class);
|
Chris@14
|
676 }
|
Chris@0
|
677 $this->container->get('invalid_argument_service');
|
Chris@0
|
678 }
|
Chris@0
|
679
|
Chris@0
|
680 /**
|
Chris@0
|
681 * Tests that invalid arguments throw an Exception.
|
Chris@0
|
682 *
|
Chris@0
|
683 * @covers ::get
|
Chris@0
|
684 * @covers ::createService
|
Chris@0
|
685 * @covers ::resolveServicesAndParameters
|
Chris@0
|
686 */
|
Chris@0
|
687 public function testResolveServicesAndParametersForInvalidArguments() {
|
Chris@0
|
688 // In case the machine-optimized format is not used, we need to simulate the
|
Chris@0
|
689 // test failure.
|
Chris@14
|
690 if (method_exists($this, 'expectException')) {
|
Chris@14
|
691 $this->expectException(InvalidArgumentException::class);
|
Chris@14
|
692 }
|
Chris@14
|
693 else {
|
Chris@14
|
694 $this->setExpectedException(InvalidArgumentException::class);
|
Chris@14
|
695 }
|
Chris@0
|
696 if (!$this->machineFormat) {
|
Chris@0
|
697 throw new InvalidArgumentException('Simulating the test failure.');
|
Chris@0
|
698 }
|
Chris@0
|
699 $this->container->get('invalid_arguments_service');
|
Chris@0
|
700 }
|
Chris@0
|
701
|
Chris@0
|
702 /**
|
Chris@0
|
703 * Tests that a parameter that points to a service works correctly.
|
Chris@0
|
704 *
|
Chris@0
|
705 * @covers ::get
|
Chris@0
|
706 * @covers ::createService
|
Chris@0
|
707 * @covers ::resolveServicesAndParameters
|
Chris@0
|
708 */
|
Chris@0
|
709 public function testResolveServicesAndParametersForServiceInstantiatedFromParameter() {
|
Chris@0
|
710 $service = $this->container->get('service.provider');
|
Chris@0
|
711 $test_service = $this->container->get('service_with_parameter_service');
|
Chris@0
|
712 $this->assertSame($service, $test_service->getSomeOtherService(), 'Service was passed via parameter.');
|
Chris@0
|
713 }
|
Chris@0
|
714
|
Chris@0
|
715 /**
|
Chris@0
|
716 * Tests that Container::initialized works correctly.
|
Chris@0
|
717 *
|
Chris@0
|
718 * @covers ::initialized
|
Chris@0
|
719 */
|
Chris@0
|
720 public function testInitialized() {
|
Chris@0
|
721 $this->assertFalse($this->container->initialized('late.service'), 'Late service is not initialized.');
|
Chris@0
|
722 $this->container->get('late.service');
|
Chris@0
|
723 $this->assertTrue($this->container->initialized('late.service'), 'Late service is initialized after it was retrieved once.');
|
Chris@0
|
724 }
|
Chris@0
|
725
|
Chris@0
|
726 /**
|
Chris@0
|
727 * Tests that Container::initialized works correctly for aliases.
|
Chris@0
|
728 *
|
Chris@0
|
729 * @covers ::initialized
|
Chris@0
|
730 */
|
Chris@0
|
731 public function testInitializedForAliases() {
|
Chris@0
|
732 $this->assertFalse($this->container->initialized('late.service_alias'), 'Late service is not initialized.');
|
Chris@0
|
733 $this->container->get('late.service');
|
Chris@0
|
734 $this->assertTrue($this->container->initialized('late.service_alias'), 'Late service is initialized after it was retrieved once.');
|
Chris@0
|
735 }
|
Chris@0
|
736
|
Chris@0
|
737 /**
|
Chris@0
|
738 * Tests that Container::getServiceIds() works properly.
|
Chris@0
|
739 *
|
Chris@0
|
740 * @covers ::getServiceIds
|
Chris@0
|
741 */
|
Chris@0
|
742 public function testGetServiceIds() {
|
Chris@0
|
743 $service_definition_keys = array_keys($this->containerDefinition['services']);
|
Chris@0
|
744 $this->assertEquals($service_definition_keys, $this->container->getServiceIds(), 'Retrieved service IDs match definition.');
|
Chris@0
|
745
|
Chris@0
|
746 $mock_service = new MockService();
|
Chris@0
|
747 $this->container->set('bar', $mock_service);
|
Chris@0
|
748 $this->container->set('service.provider', $mock_service);
|
Chris@0
|
749 $service_definition_keys[] = 'bar';
|
Chris@0
|
750
|
Chris@0
|
751 $this->assertEquals($service_definition_keys, $this->container->getServiceIds(), 'Retrieved service IDs match definition after setting new services.');
|
Chris@0
|
752 }
|
Chris@0
|
753
|
Chris@0
|
754 /**
|
Chris@0
|
755 * Gets a mock container definition.
|
Chris@0
|
756 *
|
Chris@0
|
757 * @return array
|
Chris@0
|
758 * Associated array with parameters and services.
|
Chris@0
|
759 */
|
Chris@0
|
760 protected function getMockContainerDefinition() {
|
Chris@0
|
761 $fake_service = new \stdClass();
|
Chris@0
|
762 $parameters = [];
|
Chris@0
|
763 $parameters['some_parameter_class'] = get_class($fake_service);
|
Chris@0
|
764 $parameters['some_private_config'] = 'really_private_lama';
|
Chris@0
|
765 $parameters['some_config'] = 'foo';
|
Chris@0
|
766 $parameters['some_other_config'] = 'lama';
|
Chris@0
|
767 $parameters['factory_service_class'] = get_class($fake_service);
|
Chris@0
|
768 // Also test alias resolving.
|
Chris@0
|
769 $parameters['service_from_parameter'] = $this->getServiceCall('service.provider_alias');
|
Chris@0
|
770
|
Chris@0
|
771 $services = [];
|
Chris@0
|
772 $services['service_container'] = [
|
Chris@0
|
773 'class' => '\Drupal\service_container\DependencyInjection\Container',
|
Chris@0
|
774 ];
|
Chris@0
|
775 $services['other.service'] = [
|
Chris@0
|
776 'class' => get_class($fake_service),
|
Chris@0
|
777 ];
|
Chris@0
|
778
|
Chris@0
|
779 $services['non_shared_service'] = [
|
Chris@0
|
780 'class' => get_class($fake_service),
|
Chris@0
|
781 'shared' => FALSE,
|
Chris@0
|
782 ];
|
Chris@0
|
783
|
Chris@0
|
784 $services['other.service_class_from_parameter'] = [
|
Chris@0
|
785 'class' => $this->getParameterCall('some_parameter_class'),
|
Chris@0
|
786 ];
|
Chris@0
|
787 $services['late.service'] = [
|
Chris@0
|
788 'class' => get_class($fake_service),
|
Chris@0
|
789 ];
|
Chris@0
|
790 $services['service.provider'] = [
|
Chris@0
|
791 'class' => '\Drupal\Tests\Component\DependencyInjection\MockService',
|
Chris@0
|
792 'arguments' => $this->getCollection([
|
Chris@0
|
793 $this->getServiceCall('other.service'),
|
Chris@0
|
794 $this->getParameterCall('some_config'),
|
Chris@0
|
795 ]),
|
Chris@0
|
796 'properties' => $this->getCollection(['_someProperty' => 'foo']),
|
Chris@0
|
797 'calls' => [
|
Chris@0
|
798 [
|
Chris@0
|
799 'setContainer',
|
Chris@0
|
800 $this->getCollection([
|
Chris@0
|
801 $this->getServiceCall('service_container'),
|
Chris@0
|
802 ]),
|
Chris@0
|
803 ],
|
Chris@0
|
804 [
|
Chris@0
|
805 'setOtherConfigParameter',
|
Chris@0
|
806 $this->getCollection([
|
Chris@0
|
807 $this->getParameterCall('some_other_config'),
|
Chris@0
|
808 ]),
|
Chris@0
|
809 ],
|
Chris@0
|
810 ],
|
Chris@0
|
811 'priority' => 0,
|
Chris@0
|
812 ];
|
Chris@0
|
813
|
Chris@0
|
814 // Test private services.
|
Chris@0
|
815 $private_service = [
|
Chris@0
|
816 'class' => '\Drupal\Tests\Component\DependencyInjection\MockService',
|
Chris@0
|
817 'arguments' => $this->getCollection([
|
Chris@0
|
818 $this->getServiceCall('other.service'),
|
Chris@0
|
819 $this->getParameterCall('some_private_config'),
|
Chris@0
|
820 ]),
|
Chris@0
|
821 'public' => FALSE,
|
Chris@0
|
822 ];
|
Chris@0
|
823
|
Chris@0
|
824 $services['service_using_private'] = [
|
Chris@0
|
825 'class' => '\Drupal\Tests\Component\DependencyInjection\MockService',
|
Chris@0
|
826 'arguments' => $this->getCollection([
|
Chris@0
|
827 $this->getPrivateServiceCall(NULL, $private_service),
|
Chris@0
|
828 $this->getParameterCall('some_config'),
|
Chris@0
|
829 ]),
|
Chris@0
|
830 ];
|
Chris@0
|
831 $services['another_service_using_private'] = [
|
Chris@0
|
832 'class' => '\Drupal\Tests\Component\DependencyInjection\MockService',
|
Chris@0
|
833 'arguments' => $this->getCollection([
|
Chris@0
|
834 $this->getPrivateServiceCall(NULL, $private_service),
|
Chris@0
|
835 $this->getParameterCall('some_config'),
|
Chris@0
|
836 ]),
|
Chris@0
|
837 ];
|
Chris@0
|
838
|
Chris@0
|
839 // Test shared private services.
|
Chris@0
|
840 $id = 'private_service_shared_1';
|
Chris@0
|
841
|
Chris@0
|
842 $services['service_using_shared_private'] = [
|
Chris@0
|
843 'class' => '\Drupal\Tests\Component\DependencyInjection\MockService',
|
Chris@0
|
844 'arguments' => $this->getCollection([
|
Chris@0
|
845 $this->getPrivateServiceCall($id, $private_service, TRUE),
|
Chris@0
|
846 $this->getParameterCall('some_config'),
|
Chris@0
|
847 ]),
|
Chris@0
|
848 ];
|
Chris@0
|
849 $services['another_service_using_shared_private'] = [
|
Chris@0
|
850 'class' => '\Drupal\Tests\Component\DependencyInjection\MockService',
|
Chris@0
|
851 'arguments' => $this->getCollection([
|
Chris@0
|
852 $this->getPrivateServiceCall($id, $private_service, TRUE),
|
Chris@0
|
853 $this->getParameterCall('some_config'),
|
Chris@0
|
854 ]),
|
Chris@0
|
855 ];
|
Chris@0
|
856
|
Chris@0
|
857 // Tests service with invalid argument.
|
Chris@0
|
858 $services['invalid_argument_service'] = [
|
Chris@0
|
859 'class' => '\Drupal\Tests\Component\DependencyInjection\MockService',
|
Chris@0
|
860 'arguments' => $this->getCollection([
|
Chris@0
|
861 // Test passing non-strings, too.
|
Chris@0
|
862 1,
|
Chris@0
|
863 (object) [
|
Chris@0
|
864 'type' => 'invalid',
|
Chris@0
|
865 ],
|
Chris@0
|
866 ]),
|
Chris@0
|
867 ];
|
Chris@0
|
868
|
Chris@0
|
869 $services['invalid_arguments_service'] = [
|
Chris@0
|
870 'class' => '\Drupal\Tests\Component\DependencyInjection\MockService',
|
Chris@0
|
871 'arguments' => (object) [
|
Chris@0
|
872 'type' => 'invalid',
|
Chris@0
|
873 ],
|
Chris@0
|
874 ];
|
Chris@0
|
875
|
Chris@0
|
876 // Test service that needs deep-traversal.
|
Chris@0
|
877 $services['service_using_array'] = [
|
Chris@0
|
878 'class' => '\Drupal\Tests\Component\DependencyInjection\MockService',
|
Chris@0
|
879 'arguments' => $this->getCollection([
|
Chris@0
|
880 $this->getCollection([
|
Chris@0
|
881 $this->getServiceCall('other.service'),
|
Chris@0
|
882 ]),
|
Chris@0
|
883 $this->getParameterCall('some_private_config'),
|
Chris@0
|
884 ]),
|
Chris@0
|
885 ];
|
Chris@0
|
886
|
Chris@0
|
887 $services['service_with_optional_dependency'] = [
|
Chris@0
|
888 'class' => '\Drupal\Tests\Component\DependencyInjection\MockService',
|
Chris@0
|
889 'arguments' => $this->getCollection([
|
Chris@0
|
890 $this->getServiceCall('service.does_not_exist', ContainerInterface::NULL_ON_INVALID_REFERENCE),
|
Chris@0
|
891 $this->getParameterCall('some_private_config'),
|
Chris@0
|
892 ]),
|
Chris@0
|
893
|
Chris@0
|
894 ];
|
Chris@0
|
895
|
Chris@0
|
896 $services['factory_service'] = [
|
Chris@0
|
897 'class' => '\Drupal\service_container\ServiceContainer\ControllerInterface',
|
Chris@0
|
898 'factory' => [
|
Chris@0
|
899 $this->getServiceCall('service.provider'),
|
Chris@0
|
900 'getFactoryMethod',
|
Chris@0
|
901 ],
|
Chris@0
|
902 'arguments' => $this->getCollection([
|
Chris@0
|
903 $this->getParameterCall('factory_service_class'),
|
Chris@0
|
904 ]),
|
Chris@0
|
905 ];
|
Chris@0
|
906 $services['factory_class'] = [
|
Chris@0
|
907 'class' => '\Drupal\service_container\ServiceContainer\ControllerInterface',
|
Chris@0
|
908 'factory' => '\Drupal\Tests\Component\DependencyInjection\MockService::getFactoryMethod',
|
Chris@0
|
909 'arguments' => [
|
Chris@0
|
910 '\Drupal\Tests\Component\DependencyInjection\MockService',
|
Chris@0
|
911 [NULL, 'bar'],
|
Chris@0
|
912 ],
|
Chris@0
|
913 'calls' => [
|
Chris@0
|
914 [
|
Chris@0
|
915 'setContainer',
|
Chris@0
|
916 $this->getCollection([
|
Chris@0
|
917 $this->getServiceCall('service_container'),
|
Chris@0
|
918 ]),
|
Chris@0
|
919 ],
|
Chris@0
|
920 ],
|
Chris@0
|
921 ];
|
Chris@0
|
922
|
Chris@0
|
923 $services['wrong_factory'] = [
|
Chris@0
|
924 'class' => '\Drupal\service_container\ServiceContainer\ControllerInterface',
|
Chris@0
|
925 'factory' => (object) ['I am not a factory, but I pretend to be.'],
|
Chris@0
|
926 ];
|
Chris@0
|
927
|
Chris@0
|
928 $services['circular_dependency'] = [
|
Chris@0
|
929 'class' => '\Drupal\Tests\Component\DependencyInjection\MockService',
|
Chris@0
|
930 'arguments' => $this->getCollection([
|
Chris@0
|
931 $this->getServiceCall('circular_dependency'),
|
Chris@0
|
932 ]),
|
Chris@0
|
933 ];
|
Chris@0
|
934 $services['synthetic'] = [
|
Chris@0
|
935 'synthetic' => TRUE,
|
Chris@0
|
936 ];
|
Chris@0
|
937 // The file could have been named as a .php file. The reason it is a .data
|
Chris@0
|
938 // file is that SimpleTest tries to load it. SimpleTest does not like such
|
Chris@0
|
939 // fixtures and hence we use a neutral name like .data.
|
Chris@0
|
940 $services['container_test_file_service_test'] = [
|
Chris@0
|
941 'class' => '\stdClass',
|
Chris@0
|
942 'file' => __DIR__ . '/Fixture/container_test_file_service_test_service_function.data',
|
Chris@0
|
943 ];
|
Chris@0
|
944
|
Chris@0
|
945 // Test multiple arguments.
|
Chris@0
|
946 $args = [];
|
Chris@0
|
947 for ($i = 0; $i < 12; $i++) {
|
Chris@0
|
948 $services['service_test_instantiation_' . $i] = [
|
Chris@0
|
949 'class' => '\Drupal\Tests\Component\DependencyInjection\MockInstantiationService',
|
Chris@0
|
950 // Also test a collection that does not need resolving.
|
Chris@0
|
951 'arguments' => $this->getCollection($args, FALSE),
|
Chris@0
|
952 ];
|
Chris@0
|
953 $args[] = 'arg_' . $i;
|
Chris@0
|
954 }
|
Chris@0
|
955
|
Chris@0
|
956 $services['service_parameter_not_exists'] = [
|
Chris@0
|
957 'class' => '\Drupal\Tests\Component\DependencyInjection\MockService',
|
Chris@0
|
958 'arguments' => $this->getCollection([
|
Chris@0
|
959 $this->getServiceCall('service.provider'),
|
Chris@0
|
960 $this->getParameterCall('not_exists'),
|
Chris@0
|
961 ]),
|
Chris@0
|
962 ];
|
Chris@0
|
963 $services['service_dependency_not_exists'] = [
|
Chris@0
|
964 'class' => '\Drupal\Tests\Component\DependencyInjection\MockService',
|
Chris@0
|
965 'arguments' => $this->getCollection([
|
Chris@0
|
966 $this->getServiceCall('service_not_exists'),
|
Chris@0
|
967 $this->getParameterCall('some_config'),
|
Chris@0
|
968 ]),
|
Chris@0
|
969 ];
|
Chris@0
|
970
|
Chris@0
|
971 $services['service_with_parameter_service'] = [
|
Chris@0
|
972 'class' => '\Drupal\Tests\Component\DependencyInjection\MockService',
|
Chris@0
|
973 'arguments' => $this->getCollection([
|
Chris@0
|
974 $this->getParameterCall('service_from_parameter'),
|
Chris@0
|
975 // Also test deep collections that don't need resolving.
|
Chris@0
|
976 $this->getCollection([
|
Chris@0
|
977 1,
|
Chris@0
|
978 ], FALSE),
|
Chris@0
|
979 ]),
|
Chris@0
|
980 ];
|
Chris@0
|
981
|
Chris@0
|
982 // To ensure getAlternatives() finds something.
|
Chris@0
|
983 $services['service_not_exists_similar'] = [
|
Chris@0
|
984 'synthetic' => TRUE,
|
Chris@0
|
985 ];
|
Chris@0
|
986
|
Chris@0
|
987 // Test configurator.
|
Chris@0
|
988 $services['configurator'] = [
|
Chris@0
|
989 'synthetic' => TRUE,
|
Chris@0
|
990 ];
|
Chris@0
|
991 $services['configurable_service'] = [
|
Chris@0
|
992 'class' => '\Drupal\Tests\Component\DependencyInjection\MockService',
|
Chris@0
|
993 'arguments' => [],
|
Chris@0
|
994 'configurator' => [
|
Chris@0
|
995 $this->getServiceCall('configurator'),
|
Chris@17
|
996 'configureService',
|
Chris@0
|
997 ],
|
Chris@0
|
998 ];
|
Chris@0
|
999 $services['configurable_service_exception'] = [
|
Chris@0
|
1000 'class' => '\Drupal\Tests\Component\DependencyInjection\MockService',
|
Chris@0
|
1001 'arguments' => [],
|
Chris@0
|
1002 'configurator' => 'configurator_service_test_does_not_exist',
|
Chris@0
|
1003 ];
|
Chris@0
|
1004
|
Chris@0
|
1005 $aliases = [];
|
Chris@0
|
1006 $aliases['service.provider_alias'] = 'service.provider';
|
Chris@0
|
1007 $aliases['late.service_alias'] = 'late.service';
|
Chris@0
|
1008
|
Chris@0
|
1009 return [
|
Chris@0
|
1010 'aliases' => $aliases,
|
Chris@0
|
1011 'parameters' => $parameters,
|
Chris@0
|
1012 'services' => $services,
|
Chris@0
|
1013 'frozen' => TRUE,
|
Chris@0
|
1014 'machine_format' => $this->machineFormat,
|
Chris@0
|
1015 ];
|
Chris@0
|
1016 }
|
Chris@0
|
1017
|
Chris@0
|
1018 /**
|
Chris@0
|
1019 * Helper function to return a service definition.
|
Chris@0
|
1020 */
|
Chris@0
|
1021 protected function getServiceCall($id, $invalid_behavior = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE) {
|
Chris@0
|
1022 return (object) [
|
Chris@0
|
1023 'type' => 'service',
|
Chris@0
|
1024 'id' => $id,
|
Chris@0
|
1025 'invalidBehavior' => $invalid_behavior,
|
Chris@0
|
1026 ];
|
Chris@0
|
1027 }
|
Chris@0
|
1028
|
Chris@0
|
1029 /**
|
Chris@0
|
1030 * Helper function to return a service definition.
|
Chris@0
|
1031 */
|
Chris@0
|
1032 protected function getParameterCall($name) {
|
Chris@0
|
1033 return (object) [
|
Chris@0
|
1034 'type' => 'parameter',
|
Chris@0
|
1035 'name' => $name,
|
Chris@0
|
1036 ];
|
Chris@0
|
1037 }
|
Chris@0
|
1038
|
Chris@0
|
1039 /**
|
Chris@0
|
1040 * Helper function to return a private service definition.
|
Chris@0
|
1041 */
|
Chris@0
|
1042 protected function getPrivateServiceCall($id, $service_definition, $shared = FALSE) {
|
Chris@0
|
1043 if (!$id) {
|
Chris@0
|
1044 $hash = Crypt::hashBase64(serialize($service_definition));
|
Chris@0
|
1045 $id = 'private__' . $hash;
|
Chris@0
|
1046 }
|
Chris@0
|
1047 return (object) [
|
Chris@0
|
1048 'type' => 'private_service',
|
Chris@0
|
1049 'id' => $id,
|
Chris@0
|
1050 'value' => $service_definition,
|
Chris@0
|
1051 'shared' => $shared,
|
Chris@0
|
1052 ];
|
Chris@0
|
1053 }
|
Chris@0
|
1054
|
Chris@0
|
1055 /**
|
Chris@0
|
1056 * Helper function to return a machine-optimized collection.
|
Chris@0
|
1057 */
|
Chris@0
|
1058 protected function getCollection($collection, $resolve = TRUE) {
|
Chris@0
|
1059 return (object) [
|
Chris@0
|
1060 'type' => 'collection',
|
Chris@0
|
1061 'value' => $collection,
|
Chris@0
|
1062 'resolve' => $resolve,
|
Chris@0
|
1063 ];
|
Chris@0
|
1064 }
|
Chris@0
|
1065
|
Chris@0
|
1066 }
|
Chris@0
|
1067
|
Chris@0
|
1068 /**
|
Chris@0
|
1069 * Helper interface to test Container::get() with configurator.
|
Chris@0
|
1070 *
|
Chris@0
|
1071 * @group DependencyInjection
|
Chris@0
|
1072 */
|
Chris@0
|
1073 interface MockConfiguratorInterface {
|
Chris@0
|
1074
|
Chris@0
|
1075 /**
|
Chris@0
|
1076 * Configures a service.
|
Chris@0
|
1077 *
|
Chris@0
|
1078 * @param object $service
|
Chris@0
|
1079 * The service to configure.
|
Chris@0
|
1080 */
|
Chris@0
|
1081 public function configureService($service);
|
Chris@0
|
1082
|
Chris@0
|
1083 }
|
Chris@0
|
1084
|
Chris@0
|
1085
|
Chris@0
|
1086 /**
|
Chris@0
|
1087 * Helper class to test Container::get() method for varying number of parameters.
|
Chris@0
|
1088 *
|
Chris@0
|
1089 * @group DependencyInjection
|
Chris@0
|
1090 */
|
Chris@0
|
1091 class MockInstantiationService {
|
Chris@0
|
1092
|
Chris@0
|
1093 /**
|
Chris@0
|
1094 * @var mixed[]
|
Chris@0
|
1095 */
|
Chris@0
|
1096 protected $arguments;
|
Chris@0
|
1097
|
Chris@0
|
1098 /**
|
Chris@0
|
1099 * Construct a mock instantiation service.
|
Chris@0
|
1100 */
|
Chris@0
|
1101 public function __construct() {
|
Chris@0
|
1102 $this->arguments = func_get_args();
|
Chris@0
|
1103 }
|
Chris@0
|
1104
|
Chris@0
|
1105 /**
|
Chris@0
|
1106 * Return arguments injected into the service.
|
Chris@0
|
1107 *
|
Chris@0
|
1108 * @return mixed[]
|
Chris@0
|
1109 * Return the passed arguments.
|
Chris@0
|
1110 */
|
Chris@0
|
1111 public function getArguments() {
|
Chris@0
|
1112 return $this->arguments;
|
Chris@0
|
1113 }
|
Chris@0
|
1114
|
Chris@0
|
1115 }
|
Chris@0
|
1116
|
Chris@0
|
1117
|
Chris@0
|
1118 /**
|
Chris@0
|
1119 * Helper class to test Container::get() method.
|
Chris@0
|
1120 *
|
Chris@0
|
1121 * @group DependencyInjection
|
Chris@0
|
1122 */
|
Chris@0
|
1123 class MockService {
|
Chris@0
|
1124
|
Chris@0
|
1125 /**
|
Chris@12
|
1126 * @var \Symfony\Component\DependencyInjection\ContainerInterface
|
Chris@0
|
1127 */
|
Chris@0
|
1128 protected $container;
|
Chris@0
|
1129
|
Chris@0
|
1130 /**
|
Chris@0
|
1131 * @var object
|
Chris@0
|
1132 */
|
Chris@0
|
1133 protected $someOtherService;
|
Chris@0
|
1134
|
Chris@0
|
1135 /**
|
Chris@0
|
1136 * @var string
|
Chris@0
|
1137 */
|
Chris@0
|
1138 protected $someParameter;
|
Chris@0
|
1139
|
Chris@0
|
1140 /**
|
Chris@0
|
1141 * @var string
|
Chris@0
|
1142 */
|
Chris@0
|
1143 protected $someOtherParameter;
|
Chris@0
|
1144
|
Chris@0
|
1145 /**
|
Chris@0
|
1146 * Constructs a MockService object.
|
Chris@0
|
1147 *
|
Chris@0
|
1148 * @param object $some_other_service
|
Chris@0
|
1149 * (optional) Another injected service.
|
Chris@0
|
1150 * @param string $some_parameter
|
Chris@0
|
1151 * (optional) An injected parameter.
|
Chris@0
|
1152 */
|
Chris@0
|
1153 public function __construct($some_other_service = NULL, $some_parameter = NULL) {
|
Chris@0
|
1154 if (is_array($some_other_service)) {
|
Chris@0
|
1155 $some_other_service = $some_other_service[0];
|
Chris@0
|
1156 }
|
Chris@0
|
1157 $this->someOtherService = $some_other_service;
|
Chris@0
|
1158 $this->someParameter = $some_parameter;
|
Chris@0
|
1159 }
|
Chris@0
|
1160
|
Chris@0
|
1161 /**
|
Chris@0
|
1162 * Sets the container object.
|
Chris@0
|
1163 *
|
Chris@12
|
1164 * @param \Symfony\Component\DependencyInjection\ContainerInterface $container
|
Chris@0
|
1165 * The container to inject via setter injection.
|
Chris@0
|
1166 */
|
Chris@0
|
1167 public function setContainer(ContainerInterface $container) {
|
Chris@0
|
1168 $this->container = $container;
|
Chris@0
|
1169 }
|
Chris@0
|
1170
|
Chris@0
|
1171 /**
|
Chris@0
|
1172 * Gets the container object.
|
Chris@0
|
1173 *
|
Chris@12
|
1174 * @return \Symfony\Component\DependencyInjection\ContainerInterface
|
Chris@0
|
1175 * The internally set container.
|
Chris@0
|
1176 */
|
Chris@0
|
1177 public function getContainer() {
|
Chris@0
|
1178 return $this->container;
|
Chris@0
|
1179 }
|
Chris@0
|
1180
|
Chris@0
|
1181 /**
|
Chris@0
|
1182 * Gets the someOtherService object.
|
Chris@0
|
1183 *
|
Chris@0
|
1184 * @return object
|
Chris@0
|
1185 * The injected service.
|
Chris@0
|
1186 */
|
Chris@0
|
1187 public function getSomeOtherService() {
|
Chris@0
|
1188 return $this->someOtherService;
|
Chris@0
|
1189 }
|
Chris@0
|
1190
|
Chris@0
|
1191 /**
|
Chris@0
|
1192 * Gets the someParameter property.
|
Chris@0
|
1193 *
|
Chris@0
|
1194 * @return string
|
Chris@0
|
1195 * The injected parameter.
|
Chris@0
|
1196 */
|
Chris@0
|
1197 public function getSomeParameter() {
|
Chris@0
|
1198 return $this->someParameter;
|
Chris@0
|
1199 }
|
Chris@0
|
1200
|
Chris@0
|
1201 /**
|
Chris@0
|
1202 * Sets the someOtherParameter property.
|
Chris@0
|
1203 *
|
Chris@0
|
1204 * @param string $some_other_parameter
|
Chris@0
|
1205 * The setter injected parameter.
|
Chris@0
|
1206 */
|
Chris@0
|
1207 public function setOtherConfigParameter($some_other_parameter) {
|
Chris@0
|
1208 $this->someOtherParameter = $some_other_parameter;
|
Chris@0
|
1209 }
|
Chris@0
|
1210
|
Chris@0
|
1211 /**
|
Chris@0
|
1212 * Gets the someOtherParameter property.
|
Chris@0
|
1213 *
|
Chris@0
|
1214 * @return string
|
Chris@0
|
1215 * The injected parameter.
|
Chris@0
|
1216 */
|
Chris@0
|
1217 public function getSomeOtherParameter() {
|
Chris@0
|
1218 return $this->someOtherParameter;
|
Chris@0
|
1219 }
|
Chris@0
|
1220
|
Chris@0
|
1221 /**
|
Chris@0
|
1222 * Provides a factory method to get a service.
|
Chris@0
|
1223 *
|
Chris@0
|
1224 * @param string $class
|
Chris@0
|
1225 * The class name of the class to instantiate
|
Chris@0
|
1226 * @param array $arguments
|
Chris@0
|
1227 * (optional) Arguments to pass to the new class.
|
Chris@0
|
1228 *
|
Chris@0
|
1229 * @return object
|
Chris@0
|
1230 * The instantiated service object.
|
Chris@0
|
1231 */
|
Chris@0
|
1232 public static function getFactoryMethod($class, $arguments = []) {
|
Chris@0
|
1233 $r = new \ReflectionClass($class);
|
Chris@0
|
1234 $service = ($r->getConstructor() === NULL) ? $r->newInstance() : $r->newInstanceArgs($arguments);
|
Chris@0
|
1235
|
Chris@0
|
1236 return $service;
|
Chris@0
|
1237 }
|
Chris@0
|
1238
|
Chris@0
|
1239 }
|