a87dc44a9cb2ef400652f2daaf3205b72efb6d21
[yaffs-website] / vendor / symfony / dependency-injection / Tests / Compiler / CheckCircularReferencesPassTest.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\Compiler\AnalyzeServiceReferencesPass;
17 use Symfony\Component\DependencyInjection\Compiler\CheckCircularReferencesPass;
18 use Symfony\Component\DependencyInjection\Compiler\Compiler;
19 use Symfony\Component\DependencyInjection\ContainerBuilder;
20 use Symfony\Component\DependencyInjection\Reference;
21
22 class CheckCircularReferencesPassTest extends TestCase
23 {
24     /**
25      * @expectedException \Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException
26      */
27     public function testProcess()
28     {
29         $container = new ContainerBuilder();
30         $container->register('a')->addArgument(new Reference('b'));
31         $container->register('b')->addArgument(new Reference('a'));
32
33         $this->process($container);
34     }
35
36     /**
37      * @expectedException \Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException
38      */
39     public function testProcessWithAliases()
40     {
41         $container = new ContainerBuilder();
42         $container->register('a')->addArgument(new Reference('b'));
43         $container->setAlias('b', 'c');
44         $container->setAlias('c', 'a');
45
46         $this->process($container);
47     }
48
49     /**
50      * @expectedException \Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException
51      */
52     public function testProcessWithFactory()
53     {
54         $container = new ContainerBuilder();
55
56         $container
57             ->register('a', 'stdClass')
58             ->setFactory(array(new Reference('b'), 'getInstance'));
59
60         $container
61             ->register('b', 'stdClass')
62             ->setFactory(array(new Reference('a'), 'getInstance'));
63
64         $this->process($container);
65     }
66
67     /**
68      * @expectedException \Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException
69      */
70     public function testProcessDetectsIndirectCircularReference()
71     {
72         $container = new ContainerBuilder();
73         $container->register('a')->addArgument(new Reference('b'));
74         $container->register('b')->addArgument(new Reference('c'));
75         $container->register('c')->addArgument(new Reference('a'));
76
77         $this->process($container);
78     }
79
80     /**
81      * @expectedException \Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException
82      */
83     public function testProcessDetectsIndirectCircularReferenceWithFactory()
84     {
85         $container = new ContainerBuilder();
86
87         $container->register('a')->addArgument(new Reference('b'));
88
89         $container
90             ->register('b', 'stdClass')
91             ->setFactory(array(new Reference('c'), 'getInstance'));
92
93         $container->register('c')->addArgument(new Reference('a'));
94
95         $this->process($container);
96     }
97
98     /**
99      * @expectedException \Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException
100      */
101     public function testDeepCircularReference()
102     {
103         $container = new ContainerBuilder();
104         $container->register('a')->addArgument(new Reference('b'));
105         $container->register('b')->addArgument(new Reference('c'));
106         $container->register('c')->addArgument(new Reference('b'));
107
108         $this->process($container);
109     }
110
111     public function testProcessIgnoresMethodCalls()
112     {
113         $container = new ContainerBuilder();
114         $container->register('a')->addArgument(new Reference('b'));
115         $container->register('b')->addMethodCall('setA', array(new Reference('a')));
116
117         $this->process($container);
118
119         $this->addToAssertionCount(1);
120     }
121
122     public function testProcessIgnoresLazyServices()
123     {
124         $container = new ContainerBuilder();
125         $container->register('a')->setLazy(true)->addArgument(new Reference('b'));
126         $container->register('b')->addArgument(new Reference('a'));
127
128         $this->process($container);
129
130         // just make sure that a lazily loaded service does not trigger a CircularReferenceException
131         $this->addToAssertionCount(1);
132     }
133
134     public function testProcessIgnoresIteratorArguments()
135     {
136         $container = new ContainerBuilder();
137         $container->register('a')->addArgument(new Reference('b'));
138         $container->register('b')->addArgument(new IteratorArgument(array(new Reference('a'))));
139
140         $this->process($container);
141
142         // just make sure that an IteratorArgument does not trigger a CircularReferenceException
143         $this->addToAssertionCount(1);
144     }
145
146     protected function process(ContainerBuilder $container)
147     {
148         $compiler = new Compiler();
149         $passConfig = $compiler->getPassConfig();
150         $passConfig->setOptimizationPasses(array(
151             new AnalyzeServiceReferencesPass(true),
152             new CheckCircularReferencesPass(),
153         ));
154         $passConfig->setRemovingPasses(array());
155
156         $compiler->compile($container);
157     }
158 }