09ba6ab45cb62b423bdcd368a3b60b4320cc4216
[yaffs-website] / vendor / symfony / dependency-injection / Tests / Compiler / IntegrationTest.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\DependencyInjection\Tests\Compiler;
13
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;
20
21 /**
22  * This class tests the integration of the different compiler passes.
23  */
24 class IntegrationTest extends TestCase
25 {
26     /**
27      * This tests that dependencies are correctly processed.
28      *
29      * We're checking that:
30      *
31      *   * A is public, B/C are private
32      *   * A -> C
33      *   * B -> C
34      */
35     public function testProcessRemovesAndInlinesRecursively()
36     {
37         $container = new ContainerBuilder();
38         $container->setResourceTracking(false);
39
40         $a = $container
41             ->register('a', '\stdClass')
42             ->addArgument(new Reference('c'))
43         ;
44
45         $b = $container
46             ->register('b', '\stdClass')
47             ->addArgument(new Reference('c'))
48             ->setPublic(false)
49         ;
50
51         $c = $container
52             ->register('c', '\stdClass')
53             ->setPublic(false)
54         ;
55
56         $container->compile();
57
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'));
63     }
64
65     public function testProcessInlinesReferencesToAliases()
66     {
67         $container = new ContainerBuilder();
68         $container->setResourceTracking(false);
69
70         $a = $container
71             ->register('a', '\stdClass')
72             ->addArgument(new Reference('b'))
73         ;
74
75         $container->setAlias('b', new Alias('c', false));
76
77         $c = $container
78             ->register('c', '\stdClass')
79             ->setPublic(false)
80         ;
81
82         $container->compile();
83
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'));
89     }
90
91     public function testProcessInlinesWhenThereAreMultipleReferencesButFromTheSameDefinition()
92     {
93         $container = new ContainerBuilder();
94         $container->setResourceTracking(false);
95
96         $container
97             ->register('a', '\stdClass')
98             ->addArgument(new Reference('b'))
99             ->addMethodCall('setC', array(new Reference('c')))
100         ;
101
102         $container
103             ->register('b', '\stdClass')
104             ->addArgument(new Reference('c'))
105             ->setPublic(false)
106         ;
107
108         $container
109             ->register('c', '\stdClass')
110             ->setPublic(false)
111         ;
112
113         $container->compile();
114
115         $this->assertTrue($container->hasDefinition('a'));
116         $this->assertFalse($container->hasDefinition('b'));
117         $this->assertFalse($container->hasDefinition('c'), 'Service C was not inlined.');
118     }
119
120     /**
121      * @dataProvider getYamlCompileTests
122      */
123     public function testYamlContainerCompiles($directory, $actualServiceId, $expectedServiceId, ContainerBuilder $mainContainer = null)
124     {
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);
132
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);
140
141         // reset changes, we don't care if these differ
142         $actualService->setChanges(array());
143         $expectedService->setChanges(array());
144
145         $this->assertEquals($expectedService, $actualService);
146     }
147
148     public function getYamlCompileTests()
149     {
150         $container = new ContainerBuilder();
151         $container->registerForAutoconfiguration(IntegrationTestStub::class);
152         yield array(
153             'autoconfigure_child_not_applied',
154             'child_service',
155             'child_service_expected',
156             $container,
157         );
158
159         $container = new ContainerBuilder();
160         $container->registerForAutoconfiguration(IntegrationTestStub::class);
161         yield array(
162             'autoconfigure_parent_child',
163             'child_service',
164             'child_service_expected',
165             $container,
166         );
167
168         $container = new ContainerBuilder();
169         $container->registerForAutoconfiguration(IntegrationTestStub::class)
170             ->addTag('from_autoconfigure');
171         yield array(
172             'autoconfigure_parent_child_tags',
173             'child_service',
174             'child_service_expected',
175             $container,
176         );
177
178         yield array(
179             'child_parent',
180             'child_service',
181             'child_service_expected',
182         );
183
184         yield array(
185             'defaults_child_tags',
186             'child_service',
187             'child_service_expected',
188         );
189
190         yield array(
191             'defaults_instanceof_importance',
192             'main_service',
193             'main_service_expected',
194         );
195
196         yield array(
197             'defaults_parent_child',
198             'child_service',
199             'child_service_expected',
200         );
201
202         yield array(
203             'instanceof_parent_child',
204             'child_service',
205             'child_service_expected',
206         );
207     }
208 }
209
210 class IntegrationTestStub extends IntegrationTestStubParent
211 {
212 }
213
214 class IntegrationTestStubParent
215 {
216     public function enableSummer($enable)
217     {
218         // methods used in calls - added here to prevent errors for not existing
219     }
220
221     public function setSunshine($type)
222     {
223     }
224 }