Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / vendor / symfony / dependency-injection / Tests / Compiler / AnalyzeServiceReferencesPassTest.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\DependencyInjection\Argument\IteratorArgument;
16 use Symfony\Component\DependencyInjection\Definition;
17 use Symfony\Component\DependencyInjection\Compiler\AnalyzeServiceReferencesPass;
18 use Symfony\Component\DependencyInjection\Compiler\RepeatedPass;
19 use Symfony\Component\DependencyInjection\Reference;
20 use Symfony\Component\DependencyInjection\ContainerBuilder;
21
22 class AnalyzeServiceReferencesPassTest extends TestCase
23 {
24     public function testProcess()
25     {
26         $container = new ContainerBuilder();
27
28         $a = $container
29             ->register('a')
30             ->addArgument($ref1 = new Reference('b'))
31         ;
32
33         $b = $container
34             ->register('b')
35             ->addMethodCall('setA', array($ref2 = new Reference('a')))
36         ;
37
38         $c = $container
39             ->register('c')
40             ->addArgument($ref3 = new Reference('a'))
41             ->addArgument($ref4 = new Reference('b'))
42         ;
43
44         $d = $container
45             ->register('d')
46             ->setProperty('foo', $ref5 = new Reference('b'))
47         ;
48
49         $e = $container
50             ->register('e')
51             ->setConfigurator(array($ref6 = new Reference('b'), 'methodName'))
52         ;
53
54         $graph = $this->process($container);
55
56         $this->assertCount(4, $edges = $graph->getNode('b')->getInEdges());
57
58         $this->assertSame($ref1, $edges[0]->getValue());
59         $this->assertSame($ref4, $edges[1]->getValue());
60         $this->assertSame($ref5, $edges[2]->getValue());
61         $this->assertSame($ref6, $edges[3]->getValue());
62     }
63
64     public function testProcessMarksEdgesLazyWhenReferencedServiceIsLazy()
65     {
66         $container = new ContainerBuilder();
67
68         $container
69             ->register('a')
70             ->setLazy(true)
71             ->addArgument($ref1 = new Reference('b'))
72         ;
73
74         $container
75             ->register('b')
76             ->addArgument($ref2 = new Reference('a'))
77         ;
78
79         $graph = $this->process($container);
80
81         $this->assertCount(1, $graph->getNode('b')->getInEdges());
82         $this->assertCount(1, $edges = $graph->getNode('a')->getInEdges());
83
84         $this->assertSame($ref2, $edges[0]->getValue());
85         $this->assertTrue($edges[0]->isLazy());
86     }
87
88     public function testProcessMarksEdgesLazyWhenReferencedFromIteratorArgument()
89     {
90         $container = new ContainerBuilder();
91         $container->register('a');
92         $container->register('b');
93
94         $container
95             ->register('c')
96             ->addArgument($ref1 = new Reference('a'))
97             ->addArgument(new IteratorArgument(array($ref2 = new Reference('b'))))
98         ;
99
100         $graph = $this->process($container);
101
102         $this->assertCount(1, $graph->getNode('a')->getInEdges());
103         $this->assertCount(1, $graph->getNode('b')->getInEdges());
104         $this->assertCount(2, $edges = $graph->getNode('c')->getOutEdges());
105
106         $this->assertSame($ref1, $edges[0]->getValue());
107         $this->assertFalse($edges[0]->isLazy());
108         $this->assertSame($ref2, $edges[1]->getValue());
109         $this->assertTrue($edges[1]->isLazy());
110     }
111
112     public function testProcessDetectsReferencesFromInlinedDefinitions()
113     {
114         $container = new ContainerBuilder();
115
116         $container
117             ->register('a')
118         ;
119
120         $container
121             ->register('b')
122             ->addArgument(new Definition(null, array($ref = new Reference('a'))))
123         ;
124
125         $graph = $this->process($container);
126
127         $this->assertCount(1, $refs = $graph->getNode('a')->getInEdges());
128         $this->assertSame($ref, $refs[0]->getValue());
129     }
130
131     public function testProcessDetectsReferencesFromIteratorArguments()
132     {
133         $container = new ContainerBuilder();
134
135         $container
136             ->register('a')
137         ;
138
139         $container
140             ->register('b')
141             ->addArgument(new IteratorArgument(array($ref = new Reference('a'))))
142         ;
143
144         $graph = $this->process($container);
145
146         $this->assertCount(1, $refs = $graph->getNode('a')->getInEdges());
147         $this->assertSame($ref, $refs[0]->getValue());
148     }
149
150     public function testProcessDetectsReferencesFromInlinedFactoryDefinitions()
151     {
152         $container = new ContainerBuilder();
153
154         $container
155             ->register('a')
156         ;
157
158         $factory = new Definition();
159         $factory->setFactory(array(new Reference('a'), 'a'));
160
161         $container
162             ->register('b')
163             ->addArgument($factory)
164         ;
165
166         $graph = $this->process($container);
167
168         $this->assertTrue($graph->hasNode('a'));
169         $this->assertCount(1, $refs = $graph->getNode('a')->getInEdges());
170     }
171
172     public function testProcessDoesNotSaveDuplicateReferences()
173     {
174         $container = new ContainerBuilder();
175
176         $container
177             ->register('a')
178         ;
179         $container
180             ->register('b')
181             ->addArgument(new Definition(null, array($ref1 = new Reference('a'))))
182             ->addArgument(new Definition(null, array($ref2 = new Reference('a'))))
183         ;
184
185         $graph = $this->process($container);
186
187         $this->assertCount(2, $graph->getNode('a')->getInEdges());
188     }
189
190     public function testProcessDetectsFactoryReferences()
191     {
192         $container = new ContainerBuilder();
193
194         $container
195             ->register('foo', 'stdClass')
196             ->setFactory(array('stdClass', 'getInstance'));
197
198         $container
199             ->register('bar', 'stdClass')
200             ->setFactory(array(new Reference('foo'), 'getInstance'));
201
202         $graph = $this->process($container);
203
204         $this->assertTrue($graph->hasNode('foo'));
205         $this->assertCount(1, $graph->getNode('foo')->getInEdges());
206     }
207
208     protected function process(ContainerBuilder $container)
209     {
210         $pass = new RepeatedPass(array(new AnalyzeServiceReferencesPass()));
211         $pass->process($container);
212
213         return $container->getCompiler()->getServiceReferenceGraph();
214     }
215 }