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