5 * Contains \Drupal\Tests\Component\DependencyInjection\ContainerTest.
8 namespace Drupal\Tests\Component\DependencyInjection;
10 use Drupal\Component\Utility\Crypt;
11 use PHPUnit\Framework\TestCase;
12 use Symfony\Component\DependencyInjection\ContainerInterface;
13 use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
14 use Symfony\Component\DependencyInjection\Exception\LogicException;
15 use Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException;
16 use Symfony\Component\DependencyInjection\Exception\RuntimeException;
17 use Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException;
18 use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
19 use Prophecy\Argument;
22 * @coversDefaultClass \Drupal\Component\DependencyInjection\Container
23 * @group DependencyInjection
25 class ContainerTest extends TestCase {
28 * The tested container.
30 * @var \Symfony\Component\DependencyInjection\ContainerInterface
35 * The container definition used for the test.
39 protected $containerDefinition;
42 * The container class to be tested.
46 protected $containerClass;
49 * Whether the container uses the machine-optimized format or not.
53 protected $machineFormat;
58 protected function setUp() {
59 $this->machineFormat = TRUE;
60 $this->containerClass = '\Drupal\Component\DependencyInjection\Container';
61 $this->containerDefinition = $this->getMockContainerDefinition();
62 $this->container = new $this->containerClass($this->containerDefinition);
66 * Tests that passing a non-supported format throws an InvalidArgumentException.
68 * @covers ::__construct
70 public function testConstruct() {
71 $container_definition = $this->getMockContainerDefinition();
72 $container_definition['machine_format'] = !$this->machineFormat;
73 $this->setExpectedException(InvalidArgumentException::class);
74 $container = new $this->containerClass($container_definition);
78 * Tests that Container::getParameter() works properly.
80 * @covers ::getParameter
82 public function testGetParameter() {
83 $this->assertEquals($this->containerDefinition['parameters']['some_config'], $this->container->getParameter('some_config'), 'Container parameter matches for %some_config%.');
84 $this->assertEquals($this->containerDefinition['parameters']['some_other_config'], $this->container->getParameter('some_other_config'), 'Container parameter matches for %some_other_config%.');
88 * Tests that Container::getParameter() works properly for non-existing
91 * @covers ::getParameter
92 * @covers ::getParameterAlternatives
93 * @covers ::getAlternatives
95 public function testGetParameterIfNotFound() {
96 $this->setExpectedException(ParameterNotFoundException::class);
97 $this->container->getParameter('parameter_that_does_not_exist');
101 * Tests that Container::getParameter() works properly for NULL parameters.
103 * @covers ::getParameter
105 public function testGetParameterIfNotFoundBecauseNull() {
106 $this->setExpectedException(ParameterNotFoundException::class);
107 $this->container->getParameter(NULL);
111 * Tests that Container::hasParameter() works properly.
113 * @covers ::hasParameter
115 public function testHasParameter() {
116 $this->assertTrue($this->container->hasParameter('some_config'), 'Container parameters include %some_config%.');
117 $this->assertFalse($this->container->hasParameter('some_config_not_exists'), 'Container parameters do not include %some_config_not_exists%.');
121 * Tests that Container::setParameter() in an unfrozen case works properly.
123 * @covers ::setParameter
125 public function testSetParameterWithUnfrozenContainer() {
126 $container_definition = $this->containerDefinition;
127 $container_definition['frozen'] = FALSE;
128 $this->container = new $this->containerClass($container_definition);
129 $this->container->setParameter('some_config', 'new_value');
130 $this->assertEquals('new_value', $this->container->getParameter('some_config'), 'Container parameters can be set.');
134 * Tests that Container::setParameter() in a frozen case works properly.
136 * @covers ::setParameter
138 public function testSetParameterWithFrozenContainer() {
139 $this->container = new $this->containerClass($this->containerDefinition);
140 $this->setExpectedException(LogicException::class);
141 $this->container->setParameter('some_config', 'new_value');
145 * Tests that Container::get() works properly.
148 * @covers ::createService
150 public function testGet() {
151 $container = $this->container->get('service_container');
152 $this->assertSame($this->container, $container, 'Container can be retrieved from itself.');
154 // Retrieve services of the container.
155 $other_service_class = $this->containerDefinition['services']['other.service']['class'];
156 $other_service = $this->container->get('other.service');
157 $this->assertInstanceOf($other_service_class, $other_service, 'other.service has the right class.');
159 $some_parameter = $this->containerDefinition['parameters']['some_config'];
160 $some_other_parameter = $this->containerDefinition['parameters']['some_other_config'];
162 $service = $this->container->get('service.provider');
164 $this->assertEquals($other_service, $service->getSomeOtherService(), '@other.service was injected via constructor.');
165 $this->assertEquals($some_parameter, $service->getSomeParameter(), '%some_config% was injected via constructor.');
166 $this->assertEquals($this->container, $service->getContainer(), 'Container was injected via setter injection.');
167 $this->assertEquals($some_other_parameter, $service->getSomeOtherParameter(), '%some_other_config% was injected via setter injection.');
168 $this->assertEquals($service->_someProperty, 'foo', 'Service has added properties.');
172 * Tests that Container::get() for non-shared services works properly.
175 * @covers ::createService
177 public function testGetForNonSharedService() {
178 $service = $this->container->get('non_shared_service');
179 $service2 = $this->container->get('non_shared_service');
181 $this->assertNotSame($service, $service2, 'Non shared services are always re-instantiated.');
185 * Tests that Container::get() works properly for class from parameters.
188 * @covers ::createService
190 public function testGetForClassFromParameter() {
191 $container_definition = $this->containerDefinition;
192 $container_definition['frozen'] = FALSE;
193 $container = new $this->containerClass($container_definition);
195 $other_service_class = $this->containerDefinition['parameters']['some_parameter_class'];
196 $other_service = $container->get('other.service_class_from_parameter');
197 $this->assertInstanceOf($other_service_class, $other_service, 'other.service_class_from_parameter has the right class.');
201 * Tests that Container::set() works properly.
205 public function testSet() {
206 $this->assertNull($this->container->get('new_id', ContainerInterface::NULL_ON_INVALID_REFERENCE));
207 $mock_service = new MockService();
208 $this->container->set('new_id', $mock_service);
210 $this->assertSame($mock_service, $this->container->get('new_id'), 'A manual set service works as expected.');
214 * Tests that Container::has() works properly.
218 public function testHas() {
219 $this->assertTrue($this->container->has('other.service'));
220 $this->assertFalse($this->container->has('another.service'));
222 // Set the service manually, ensure that its also respected.
223 $mock_service = new MockService();
224 $this->container->set('another.service', $mock_service);
225 $this->assertTrue($this->container->has('another.service'));
229 * Tests that Container::has() for aliased services works properly.
233 public function testHasForAliasedService() {
234 $service = $this->container->has('service.provider');
235 $aliased_service = $this->container->has('service.provider_alias');
236 $this->assertSame($service, $aliased_service);
240 * Tests that Container::get() for circular dependencies works properly.
242 * @covers ::createService
244 public function testGetForCircularServices() {
245 $this->setExpectedException(ServiceCircularReferenceException::class);
246 $this->container->get('circular_dependency');
250 * Tests that Container::get() for non-existent services works properly.
253 * @covers ::createService
254 * @covers ::getAlternatives
255 * @covers ::getServiceAlternatives
257 public function testGetForNonExistantService() {
258 $this->setExpectedException(ServiceNotFoundException::class);
259 $this->container->get('service_not_exists');
263 * Tests that Container::get() for a serialized definition works properly.
266 * @covers ::createService
268 public function testGetForSerializedServiceDefinition() {
269 $container_definition = $this->containerDefinition;
270 $container_definition['services']['other.service'] = serialize($container_definition['services']['other.service']);
271 $container = new $this->containerClass($container_definition);
273 // Retrieve services of the container.
274 $other_service_class = $this->containerDefinition['services']['other.service']['class'];
275 $other_service = $container->get('other.service');
276 $this->assertInstanceOf($other_service_class, $other_service, 'other.service has the right class.');
278 $service = $container->get('service.provider');
279 $this->assertEquals($other_service, $service->getSomeOtherService(), '@other.service was injected via constructor.');
283 * Tests that Container::get() for non-existent parameters works properly.
286 * @covers ::createService
287 * @covers ::resolveServicesAndParameters
289 public function testGetForNonExistantParameterDependency() {
290 $service = $this->container->get('service_parameter_not_exists', ContainerInterface::NULL_ON_INVALID_REFERENCE);
291 $this->assertNull($service, 'Service is NULL.');
295 * Tests Container::get() with an exception due to missing parameter on the second call.
298 * @covers ::createService
299 * @covers ::resolveServicesAndParameters
301 public function testGetForParameterDependencyWithExceptionOnSecondCall() {
302 $service = $this->container->get('service_parameter_not_exists', ContainerInterface::NULL_ON_INVALID_REFERENCE);
303 $this->assertNull($service, 'Service is NULL.');
305 // Reset the service.
306 $this->container->set('service_parameter_not_exists', NULL);
307 $this->setExpectedException(InvalidArgumentException::class);
308 $this->container->get('service_parameter_not_exists');
312 * Tests that Container::get() for non-existent parameters works properly.
315 * @covers ::createService
316 * @covers ::resolveServicesAndParameters
318 public function testGetForNonExistantParameterDependencyWithException() {
319 $this->setExpectedException(InvalidArgumentException::class);
320 $this->container->get('service_parameter_not_exists');
324 * Tests that Container::get() for non-existent dependencies works properly.
327 * @covers ::createService
328 * @covers ::resolveServicesAndParameters
330 public function testGetForNonExistantServiceDependency() {
331 $service = $this->container->get('service_dependency_not_exists', ContainerInterface::NULL_ON_INVALID_REFERENCE);
332 $this->assertNull($service, 'Service is NULL.');
336 * Tests that Container::get() for non-existent dependencies works properly.
339 * @covers ::createService
340 * @covers ::resolveServicesAndParameters
341 * @covers ::getAlternatives
343 public function testGetForNonExistantServiceDependencyWithException() {
344 $this->setExpectedException(ServiceNotFoundException::class);
345 $this->container->get('service_dependency_not_exists');
349 * Tests that Container::get() for non-existent services works properly.
352 * @covers ::createService
354 public function testGetForNonExistantServiceWhenUsingNull() {
355 $this->assertNull($this->container->get('service_not_exists', ContainerInterface::NULL_ON_INVALID_REFERENCE), 'Not found service does not throw exception.');
359 * Tests that Container::get() for NULL service works properly.
361 * @covers ::createService
363 public function testGetForNonExistantNULLService() {
364 $this->setExpectedException(ServiceNotFoundException::class);
365 $this->container->get(NULL);
369 * Tests multiple Container::get() calls for non-existing dependencies work.
372 * @covers ::createService
374 public function testGetForNonExistantServiceMultipleTimes() {
375 $container = new $this->containerClass();
377 $this->assertNull($container->get('service_not_exists', ContainerInterface::NULL_ON_INVALID_REFERENCE), 'Not found service does not throw exception.');
378 $this->assertNull($container->get('service_not_exists', ContainerInterface::NULL_ON_INVALID_REFERENCE), 'Not found service does not throw exception on second call.');
382 * Tests multiple Container::get() calls with exception on the second time.
385 * @covers ::createService
386 * @covers ::getAlternatives
388 public function testGetForNonExistantServiceWithExceptionOnSecondCall() {
389 $this->assertNull($this->container->get('service_not_exists', ContainerInterface::NULL_ON_INVALID_REFERENCE), 'Not found service does nto throw exception.');
390 $this->setExpectedException(ServiceNotFoundException::class);
391 $this->container->get('service_not_exists');
395 * Tests that Container::get() for aliased services works properly.
398 * @covers ::createService
400 public function testGetForAliasedService() {
401 $service = $this->container->get('service.provider');
402 $aliased_service = $this->container->get('service.provider_alias');
403 $this->assertSame($service, $aliased_service);
407 * Tests that Container::get() for synthetic services works - if defined.
410 * @covers ::createService
412 public function testGetForSyntheticService() {
413 $synthetic_service = new \stdClass();
414 $this->container->set('synthetic', $synthetic_service);
415 $test_service = $this->container->get('synthetic');
416 $this->assertSame($synthetic_service, $test_service);
420 * Tests that Container::get() for synthetic services throws an Exception if not defined.
423 * @covers ::createService
425 public function testGetForSyntheticServiceWithException() {
426 $this->setExpectedException(RuntimeException::class);
427 $this->container->get('synthetic');
431 * Tests that Container::get() for services with file includes works.
434 * @covers ::createService
436 public function testGetWithFileInclude() {
437 $file_service = $this->container->get('container_test_file_service_test');
438 $this->assertTrue(function_exists('container_test_file_service_test_service_function'));
439 $this->assertEquals('Hello Container', container_test_file_service_test_service_function());
443 * Tests that Container::get() for various arguments lengths works.
446 * @covers ::createService
447 * @covers ::resolveServicesAndParameters
449 public function testGetForInstantiationWithVariousArgumentLengths() {
451 for ($i = 0; $i < 12; $i++) {
452 $instantiation_service = $this->container->get('service_test_instantiation_' . $i);
453 $this->assertEquals($args, $instantiation_service->getArguments());
454 $args[] = 'arg_' . $i;
459 * Tests that Container::get() for wrong factories works correctly.
462 * @covers ::createService
464 public function testGetForWrongFactory() {
465 $this->setExpectedException(RuntimeException::class);
466 $this->container->get('wrong_factory');
470 * Tests Container::get() for factories via services (Symfony 2.7.0).
473 * @covers ::createService
475 public function testGetForFactoryService() {
476 $factory_service = $this->container->get('factory_service');
477 $factory_service_class = $this->container->getParameter('factory_service_class');
478 $this->assertInstanceOf($factory_service_class, $factory_service);
482 * Tests that Container::get() for factories via class works (Symfony 2.7.0).
485 * @covers ::createService
487 public function testGetForFactoryClass() {
488 $service = $this->container->get('service.provider');
489 $factory_service = $this->container->get('factory_class');
491 $this->assertInstanceOf(get_class($service), $factory_service);
492 $this->assertEquals('bar', $factory_service->getSomeParameter(), 'Correct parameter was passed via the factory class instantiation.');
493 $this->assertEquals($this->container, $factory_service->getContainer(), 'Container was injected via setter injection.');
497 * Tests that Container::get() for configurable services throws an Exception.
500 * @covers ::createService
502 public function testGetForConfiguratorWithException() {
503 $this->setExpectedException(InvalidArgumentException::class);
504 $this->container->get('configurable_service_exception');
508 * Tests that Container::get() for configurable services works.
511 * @covers ::createService
513 public function testGetForConfigurator() {
514 $container = $this->container;
516 // Setup a configurator.
517 $configurator = $this->prophesize('\Drupal\Tests\Component\DependencyInjection\MockConfiguratorInterface');
518 $configurator->configureService(Argument::type('object'))
520 ->will(function ($args) use ($container) {
521 $args[0]->setContainer($container);
523 $container->set('configurator', $configurator->reveal());
525 // Test that the configurator worked.
526 $service = $container->get('configurable_service');
527 $this->assertSame($container, $service->getContainer(), 'Container was injected via configurator.');
531 * Tests that private services work correctly.
534 * @covers ::createService
535 * @covers ::resolveServicesAndParameters
537 public function testResolveServicesAndParametersForPrivateService() {
538 $service = $this->container->get('service_using_private');
539 $private_service = $service->getSomeOtherService();
540 $this->assertEquals($private_service->getSomeParameter(), 'really_private_lama', 'Private was found successfully.');
542 // Test that sharing the same private services works.
543 $service = $this->container->get('another_service_using_private');
544 $another_private_service = $service->getSomeOtherService();
545 $this->assertNotSame($private_service, $another_private_service, 'Private service is not shared.');
546 $this->assertEquals($private_service->getSomeParameter(), 'really_private_lama', 'Private was found successfully.');
550 * Tests that private service sharing works correctly.
553 * @covers ::createService
554 * @covers ::resolveServicesAndParameters
556 public function testResolveServicesAndParametersForSharedPrivateService() {
557 $service = $this->container->get('service_using_shared_private');
558 $private_service = $service->getSomeOtherService();
559 $this->assertEquals($private_service->getSomeParameter(), 'really_private_lama', 'Private was found successfully.');
561 // Test that sharing the same private services works.
562 $service = $this->container->get('another_service_using_shared_private');
563 $same_private_service = $service->getSomeOtherService();
564 $this->assertSame($private_service, $same_private_service, 'Private service is shared.');
565 $this->assertEquals($private_service->getSomeParameter(), 'really_private_lama', 'Private was found successfully.');
569 * Tests that services with an array of arguments work correctly.
572 * @covers ::createService
573 * @covers ::resolveServicesAndParameters
575 public function testResolveServicesAndParametersForArgumentsUsingDeepArray() {
576 $service = $this->container->get('service_using_array');
577 $other_service = $this->container->get('other.service');
578 $this->assertEquals($other_service, $service->getSomeOtherService(), '@other.service was injected via constructor.');
582 * Tests that services that are optional work correctly.
585 * @covers ::createService
586 * @covers ::resolveServicesAndParameters
588 public function testResolveServicesAndParametersForOptionalServiceDependencies() {
589 $service = $this->container->get('service_with_optional_dependency');
590 $this->assertNull($service->getSomeOtherService(), 'other service was NULL was expected.');
594 * Tests that an invalid argument throw an Exception.
597 * @covers ::createService
598 * @covers ::resolveServicesAndParameters
600 public function testResolveServicesAndParametersForInvalidArgument() {
601 $this->setExpectedException(InvalidArgumentException::class);
602 $this->container->get('invalid_argument_service');
606 * Tests that invalid arguments throw an Exception.
609 * @covers ::createService
610 * @covers ::resolveServicesAndParameters
612 public function testResolveServicesAndParametersForInvalidArguments() {
613 // In case the machine-optimized format is not used, we need to simulate the
615 $this->setExpectedException(InvalidArgumentException::class);
616 if (!$this->machineFormat) {
617 throw new InvalidArgumentException('Simulating the test failure.');
619 $this->container->get('invalid_arguments_service');
623 * Tests that a parameter that points to a service works correctly.
626 * @covers ::createService
627 * @covers ::resolveServicesAndParameters
629 public function testResolveServicesAndParametersForServiceInstantiatedFromParameter() {
630 $service = $this->container->get('service.provider');
631 $test_service = $this->container->get('service_with_parameter_service');
632 $this->assertSame($service, $test_service->getSomeOtherService(), 'Service was passed via parameter.');
636 * Tests that Container::initialized works correctly.
638 * @covers ::initialized
640 public function testInitialized() {
641 $this->assertFalse($this->container->initialized('late.service'), 'Late service is not initialized.');
642 $this->container->get('late.service');
643 $this->assertTrue($this->container->initialized('late.service'), 'Late service is initialized after it was retrieved once.');
647 * Tests that Container::initialized works correctly for aliases.
649 * @covers ::initialized
651 public function testInitializedForAliases() {
652 $this->assertFalse($this->container->initialized('late.service_alias'), 'Late service is not initialized.');
653 $this->container->get('late.service');
654 $this->assertTrue($this->container->initialized('late.service_alias'), 'Late service is initialized after it was retrieved once.');
658 * Tests that Container::getServiceIds() works properly.
660 * @covers ::getServiceIds
662 public function testGetServiceIds() {
663 $service_definition_keys = array_keys($this->containerDefinition['services']);
664 $this->assertEquals($service_definition_keys, $this->container->getServiceIds(), 'Retrieved service IDs match definition.');
666 $mock_service = new MockService();
667 $this->container->set('bar', $mock_service);
668 $this->container->set('service.provider', $mock_service);
669 $service_definition_keys[] = 'bar';
671 $this->assertEquals($service_definition_keys, $this->container->getServiceIds(), 'Retrieved service IDs match definition after setting new services.');
675 * Gets a mock container definition.
678 * Associated array with parameters and services.
680 protected function getMockContainerDefinition() {
681 $fake_service = new \stdClass();
683 $parameters['some_parameter_class'] = get_class($fake_service);
684 $parameters['some_private_config'] = 'really_private_lama';
685 $parameters['some_config'] = 'foo';
686 $parameters['some_other_config'] = 'lama';
687 $parameters['factory_service_class'] = get_class($fake_service);
688 // Also test alias resolving.
689 $parameters['service_from_parameter'] = $this->getServiceCall('service.provider_alias');
692 $services['service_container'] = [
693 'class' => '\Drupal\service_container\DependencyInjection\Container',
695 $services['other.service'] = [
696 'class' => get_class($fake_service),
699 $services['non_shared_service'] = [
700 'class' => get_class($fake_service),
704 $services['other.service_class_from_parameter'] = [
705 'class' => $this->getParameterCall('some_parameter_class'),
707 $services['late.service'] = [
708 'class' => get_class($fake_service),
710 $services['service.provider'] = [
711 'class' => '\Drupal\Tests\Component\DependencyInjection\MockService',
712 'arguments' => $this->getCollection([
713 $this->getServiceCall('other.service'),
714 $this->getParameterCall('some_config'),
716 'properties' => $this->getCollection(['_someProperty' => 'foo']),
720 $this->getCollection([
721 $this->getServiceCall('service_container'),
725 'setOtherConfigParameter',
726 $this->getCollection([
727 $this->getParameterCall('some_other_config'),
734 // Test private services.
736 'class' => '\Drupal\Tests\Component\DependencyInjection\MockService',
737 'arguments' => $this->getCollection([
738 $this->getServiceCall('other.service'),
739 $this->getParameterCall('some_private_config'),
744 $services['service_using_private'] = [
745 'class' => '\Drupal\Tests\Component\DependencyInjection\MockService',
746 'arguments' => $this->getCollection([
747 $this->getPrivateServiceCall(NULL, $private_service),
748 $this->getParameterCall('some_config'),
751 $services['another_service_using_private'] = [
752 'class' => '\Drupal\Tests\Component\DependencyInjection\MockService',
753 'arguments' => $this->getCollection([
754 $this->getPrivateServiceCall(NULL, $private_service),
755 $this->getParameterCall('some_config'),
759 // Test shared private services.
760 $id = 'private_service_shared_1';
762 $services['service_using_shared_private'] = [
763 'class' => '\Drupal\Tests\Component\DependencyInjection\MockService',
764 'arguments' => $this->getCollection([
765 $this->getPrivateServiceCall($id, $private_service, TRUE),
766 $this->getParameterCall('some_config'),
769 $services['another_service_using_shared_private'] = [
770 'class' => '\Drupal\Tests\Component\DependencyInjection\MockService',
771 'arguments' => $this->getCollection([
772 $this->getPrivateServiceCall($id, $private_service, TRUE),
773 $this->getParameterCall('some_config'),
777 // Tests service with invalid argument.
778 $services['invalid_argument_service'] = [
779 'class' => '\Drupal\Tests\Component\DependencyInjection\MockService',
780 'arguments' => $this->getCollection([
781 // Test passing non-strings, too.
789 $services['invalid_arguments_service'] = [
790 'class' => '\Drupal\Tests\Component\DependencyInjection\MockService',
791 'arguments' => (object) [
796 // Test service that needs deep-traversal.
797 $services['service_using_array'] = [
798 'class' => '\Drupal\Tests\Component\DependencyInjection\MockService',
799 'arguments' => $this->getCollection([
800 $this->getCollection([
801 $this->getServiceCall('other.service'),
803 $this->getParameterCall('some_private_config'),
807 $services['service_with_optional_dependency'] = [
808 'class' => '\Drupal\Tests\Component\DependencyInjection\MockService',
809 'arguments' => $this->getCollection([
810 $this->getServiceCall('service.does_not_exist', ContainerInterface::NULL_ON_INVALID_REFERENCE),
811 $this->getParameterCall('some_private_config'),
816 $services['factory_service'] = [
817 'class' => '\Drupal\service_container\ServiceContainer\ControllerInterface',
819 $this->getServiceCall('service.provider'),
822 'arguments' => $this->getCollection([
823 $this->getParameterCall('factory_service_class'),
826 $services['factory_class'] = [
827 'class' => '\Drupal\service_container\ServiceContainer\ControllerInterface',
828 'factory' => '\Drupal\Tests\Component\DependencyInjection\MockService::getFactoryMethod',
830 '\Drupal\Tests\Component\DependencyInjection\MockService',
836 $this->getCollection([
837 $this->getServiceCall('service_container'),
843 $services['wrong_factory'] = [
844 'class' => '\Drupal\service_container\ServiceContainer\ControllerInterface',
845 'factory' => (object) ['I am not a factory, but I pretend to be.'],
848 $services['circular_dependency'] = [
849 'class' => '\Drupal\Tests\Component\DependencyInjection\MockService',
850 'arguments' => $this->getCollection([
851 $this->getServiceCall('circular_dependency'),
854 $services['synthetic'] = [
857 // The file could have been named as a .php file. The reason it is a .data
858 // file is that SimpleTest tries to load it. SimpleTest does not like such
859 // fixtures and hence we use a neutral name like .data.
860 $services['container_test_file_service_test'] = [
861 'class' => '\stdClass',
862 'file' => __DIR__ . '/Fixture/container_test_file_service_test_service_function.data',
865 // Test multiple arguments.
867 for ($i = 0; $i < 12; $i++) {
868 $services['service_test_instantiation_' . $i] = [
869 'class' => '\Drupal\Tests\Component\DependencyInjection\MockInstantiationService',
870 // Also test a collection that does not need resolving.
871 'arguments' => $this->getCollection($args, FALSE),
873 $args[] = 'arg_' . $i;
876 $services['service_parameter_not_exists'] = [
877 'class' => '\Drupal\Tests\Component\DependencyInjection\MockService',
878 'arguments' => $this->getCollection([
879 $this->getServiceCall('service.provider'),
880 $this->getParameterCall('not_exists'),
883 $services['service_dependency_not_exists'] = [
884 'class' => '\Drupal\Tests\Component\DependencyInjection\MockService',
885 'arguments' => $this->getCollection([
886 $this->getServiceCall('service_not_exists'),
887 $this->getParameterCall('some_config'),
891 $services['service_with_parameter_service'] = [
892 'class' => '\Drupal\Tests\Component\DependencyInjection\MockService',
893 'arguments' => $this->getCollection([
894 $this->getParameterCall('service_from_parameter'),
895 // Also test deep collections that don't need resolving.
896 $this->getCollection([
902 // To ensure getAlternatives() finds something.
903 $services['service_not_exists_similar'] = [
907 // Test configurator.
908 $services['configurator'] = [
911 $services['configurable_service'] = [
912 'class' => '\Drupal\Tests\Component\DependencyInjection\MockService',
915 $this->getServiceCall('configurator'),
919 $services['configurable_service_exception'] = [
920 'class' => '\Drupal\Tests\Component\DependencyInjection\MockService',
922 'configurator' => 'configurator_service_test_does_not_exist',
926 $aliases['service.provider_alias'] = 'service.provider';
927 $aliases['late.service_alias'] = 'late.service';
930 'aliases' => $aliases,
931 'parameters' => $parameters,
932 'services' => $services,
934 'machine_format' => $this->machineFormat,
939 * Helper function to return a service definition.
941 protected function getServiceCall($id, $invalid_behavior = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE) {
945 'invalidBehavior' => $invalid_behavior,
950 * Helper function to return a service definition.
952 protected function getParameterCall($name) {
954 'type' => 'parameter',
960 * Helper function to return a private service definition.
962 protected function getPrivateServiceCall($id, $service_definition, $shared = FALSE) {
964 $hash = Crypt::hashBase64(serialize($service_definition));
965 $id = 'private__' . $hash;
968 'type' => 'private_service',
970 'value' => $service_definition,
976 * Helper function to return a machine-optimized collection.
978 protected function getCollection($collection, $resolve = TRUE) {
980 'type' => 'collection',
981 'value' => $collection,
982 'resolve' => $resolve,
989 * Helper interface to test Container::get() with configurator.
991 * @group DependencyInjection
993 interface MockConfiguratorInterface {
996 * Configures a service.
998 * @param object $service
999 * The service to configure.
1001 public function configureService($service);
1007 * Helper class to test Container::get() method for varying number of parameters.
1009 * @group DependencyInjection
1011 class MockInstantiationService {
1016 protected $arguments;
1019 * Construct a mock instantiation service.
1021 public function __construct() {
1022 $this->arguments = func_get_args();
1026 * Return arguments injected into the service.
1029 * Return the passed arguments.
1031 public function getArguments() {
1032 return $this->arguments;
1039 * Helper class to test Container::get() method.
1041 * @group DependencyInjection
1046 * @var \Symfony\Component\DependencyInjection\ContainerInterface
1048 protected $container;
1053 protected $someOtherService;
1058 protected $someParameter;
1063 protected $someOtherParameter;
1066 * Constructs a MockService object.
1068 * @param object $some_other_service
1069 * (optional) Another injected service.
1070 * @param string $some_parameter
1071 * (optional) An injected parameter.
1073 public function __construct($some_other_service = NULL, $some_parameter = NULL) {
1074 if (is_array($some_other_service)) {
1075 $some_other_service = $some_other_service[0];
1077 $this->someOtherService = $some_other_service;
1078 $this->someParameter = $some_parameter;
1082 * Sets the container object.
1084 * @param \Symfony\Component\DependencyInjection\ContainerInterface $container
1085 * The container to inject via setter injection.
1087 public function setContainer(ContainerInterface $container) {
1088 $this->container = $container;
1092 * Gets the container object.
1094 * @return \Symfony\Component\DependencyInjection\ContainerInterface
1095 * The internally set container.
1097 public function getContainer() {
1098 return $this->container;
1102 * Gets the someOtherService object.
1105 * The injected service.
1107 public function getSomeOtherService() {
1108 return $this->someOtherService;
1112 * Gets the someParameter property.
1115 * The injected parameter.
1117 public function getSomeParameter() {
1118 return $this->someParameter;
1122 * Sets the someOtherParameter property.
1124 * @param string $some_other_parameter
1125 * The setter injected parameter.
1127 public function setOtherConfigParameter($some_other_parameter) {
1128 $this->someOtherParameter = $some_other_parameter;
1132 * Gets the someOtherParameter property.
1135 * The injected parameter.
1137 public function getSomeOtherParameter() {
1138 return $this->someOtherParameter;
1142 * Provides a factory method to get a service.
1144 * @param string $class
1145 * The class name of the class to instantiate
1146 * @param array $arguments
1147 * (optional) Arguments to pass to the new class.
1150 * The instantiated service object.
1152 public static function getFactoryMethod($class, $arguments = []) {
1153 $r = new \ReflectionClass($class);
1154 $service = ($r->getConstructor() === NULL) ? $r->newInstance() : $r->newInstanceArgs($arguments);