Yaffs site version 1.1
[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\Definition;
16 use Symfony\Component\DependencyInjection\Compiler\AnalyzeServiceReferencesPass;
17 use Symfony\Component\DependencyInjection\Compiler\RepeatedPass;
18 use Symfony\Component\DependencyInjection\Reference;
19 use Symfony\Component\DependencyInjection\ContainerBuilder;
20
21 class AnalyzeServiceReferencesPassTest extends TestCase
22 {
23     public function testProcess()
24     {
25         $container = new ContainerBuilder();
26
27         $a = $container
28             ->register('a')
29             ->addArgument($ref1 = new Reference('b'))
30         ;
31
32         $b = $container
33             ->register('b')
34             ->addMethodCall('setA', array($ref2 = new Reference('a')))
35         ;
36
37         $c = $container
38             ->register('c')
39             ->addArgument($ref3 = new Reference('a'))
40             ->addArgument($ref4 = new Reference('b'))
41         ;
42
43         $d = $container
44             ->register('d')
45             ->setProperty('foo', $ref5 = new Reference('b'))
46         ;
47
48         $e = $container
49             ->register('e')
50             ->setConfigurator(array($ref6 = new Reference('b'), 'methodName'))
51         ;
52
53         $graph = $this->process($container);
54
55         $this->assertCount(4, $edges = $graph->getNode('b')->getInEdges());
56
57         $this->assertSame($ref1, $edges[0]->getValue());
58         $this->assertSame($ref4, $edges[1]->getValue());
59         $this->assertSame($ref5, $edges[2]->getValue());
60         $this->assertSame($ref6, $edges[3]->getValue());
61     }
62
63     public function testProcessDetectsReferencesFromInlinedDefinitions()
64     {
65         $container = new ContainerBuilder();
66
67         $container
68             ->register('a')
69         ;
70
71         $container
72             ->register('b')
73             ->addArgument(new Definition(null, array($ref = new Reference('a'))))
74         ;
75
76         $graph = $this->process($container);
77
78         $this->assertCount(1, $refs = $graph->getNode('a')->getInEdges());
79         $this->assertSame($ref, $refs[0]->getValue());
80     }
81
82     public function testProcessDetectsReferencesFromInlinedFactoryDefinitions()
83     {
84         $container = new ContainerBuilder();
85
86         $container
87             ->register('a')
88         ;
89
90         $factory = new Definition();
91         $factory->setFactory(array(new Reference('a'), 'a'));
92
93         $container
94             ->register('b')
95             ->addArgument($factory)
96         ;
97
98         $graph = $this->process($container);
99
100         $this->assertTrue($graph->hasNode('a'));
101         $this->assertCount(1, $refs = $graph->getNode('a')->getInEdges());
102     }
103
104     public function testProcessDoesNotSaveDuplicateReferences()
105     {
106         $container = new ContainerBuilder();
107
108         $container
109             ->register('a')
110         ;
111         $container
112             ->register('b')
113             ->addArgument(new Definition(null, array($ref1 = new Reference('a'))))
114             ->addArgument(new Definition(null, array($ref2 = new Reference('a'))))
115         ;
116
117         $graph = $this->process($container);
118
119         $this->assertCount(2, $graph->getNode('a')->getInEdges());
120     }
121
122     public function testProcessDetectsFactoryReferences()
123     {
124         $container = new ContainerBuilder();
125
126         $container
127             ->register('foo', 'stdClass')
128             ->setFactory(array('stdClass', 'getInstance'));
129
130         $container
131             ->register('bar', 'stdClass')
132             ->setFactory(array(new Reference('foo'), 'getInstance'));
133
134         $graph = $this->process($container);
135
136         $this->assertTrue($graph->hasNode('foo'));
137         $this->assertCount(1, $graph->getNode('foo')->getInEdges());
138     }
139
140     protected function process(ContainerBuilder $container)
141     {
142         $pass = new RepeatedPass(array(new AnalyzeServiceReferencesPass()));
143         $pass->process($container);
144
145         return $container->getCompiler()->getServiceReferenceGraph();
146     }
147 }