4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\DependencyInjection\Tests\Compiler;
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\Config\FileLocator;
16 use Symfony\Component\DependencyInjection\Alias;
17 use Symfony\Component\DependencyInjection\ContainerBuilder;
18 use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
19 use Symfony\Component\DependencyInjection\Reference;
22 * This class tests the integration of the different compiler passes.
24 class IntegrationTest extends TestCase
27 * This tests that dependencies are correctly processed.
29 * We're checking that:
31 * * A is public, B/C are private
35 public function testProcessRemovesAndInlinesRecursively()
37 $container = new ContainerBuilder();
38 $container->setResourceTracking(false);
41 ->register('a', '\stdClass')
42 ->addArgument(new Reference('c'))
46 ->register('b', '\stdClass')
47 ->addArgument(new Reference('c'))
52 ->register('c', '\stdClass')
56 $container->compile();
58 $this->assertTrue($container->hasDefinition('a'));
59 $arguments = $a->getArguments();
60 $this->assertSame($c, $arguments[0]);
61 $this->assertFalse($container->hasDefinition('b'));
62 $this->assertFalse($container->hasDefinition('c'));
65 public function testProcessInlinesReferencesToAliases()
67 $container = new ContainerBuilder();
68 $container->setResourceTracking(false);
71 ->register('a', '\stdClass')
72 ->addArgument(new Reference('b'))
75 $container->setAlias('b', new Alias('c', false));
78 ->register('c', '\stdClass')
82 $container->compile();
84 $this->assertTrue($container->hasDefinition('a'));
85 $arguments = $a->getArguments();
86 $this->assertSame($c, $arguments[0]);
87 $this->assertFalse($container->hasAlias('b'));
88 $this->assertFalse($container->hasDefinition('c'));
91 public function testProcessInlinesWhenThereAreMultipleReferencesButFromTheSameDefinition()
93 $container = new ContainerBuilder();
94 $container->setResourceTracking(false);
97 ->register('a', '\stdClass')
98 ->addArgument(new Reference('b'))
99 ->addMethodCall('setC', array(new Reference('c')))
103 ->register('b', '\stdClass')
104 ->addArgument(new Reference('c'))
109 ->register('c', '\stdClass')
113 $container->compile();
115 $this->assertTrue($container->hasDefinition('a'));
116 $this->assertFalse($container->hasDefinition('b'));
117 $this->assertFalse($container->hasDefinition('c'), 'Service C was not inlined.');
121 * @dataProvider getYamlCompileTests
123 public function testYamlContainerCompiles($directory, $actualServiceId, $expectedServiceId, ContainerBuilder $mainContainer = null)
125 // allow a container to be passed in, which might have autoconfigure settings
126 $container = $mainContainer ?: new ContainerBuilder();
127 $container->setResourceTracking(false);
128 $loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Fixtures/yaml/integration/'.$directory));
129 $loader->load('main.yml');
130 $container->compile();
131 $actualService = $container->getDefinition($actualServiceId);
133 // create a fresh ContainerBuilder, to avoid autoconfigure stuff
134 $container = new ContainerBuilder();
135 $container->setResourceTracking(false);
136 $loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Fixtures/yaml/integration/'.$directory));
137 $loader->load('expected.yml');
138 $container->compile();
139 $expectedService = $container->getDefinition($expectedServiceId);
141 // reset changes, we don't care if these differ
142 $actualService->setChanges(array());
143 $expectedService->setChanges(array());
145 $this->assertEquals($expectedService, $actualService);
148 public function getYamlCompileTests()
150 $container = new ContainerBuilder();
151 $container->registerForAutoconfiguration(IntegrationTestStub::class);
153 'autoconfigure_child_not_applied',
155 'child_service_expected',
159 $container = new ContainerBuilder();
160 $container->registerForAutoconfiguration(IntegrationTestStub::class);
162 'autoconfigure_parent_child',
164 'child_service_expected',
168 $container = new ContainerBuilder();
169 $container->registerForAutoconfiguration(IntegrationTestStub::class)
170 ->addTag('from_autoconfigure');
172 'autoconfigure_parent_child_tags',
174 'child_service_expected',
181 'child_service_expected',
185 'defaults_child_tags',
187 'child_service_expected',
191 'defaults_instanceof_importance',
193 'main_service_expected',
197 'defaults_parent_child',
199 'child_service_expected',
203 'instanceof_parent_child',
205 'child_service_expected',
210 class IntegrationTestStub extends IntegrationTestStubParent
214 class IntegrationTestStubParent
216 public function enableSummer($enable)
218 // methods used in calls - added here to prevent errors for not existing
221 public function setSunshine($type)