Yaffs site version 1.1
[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\Reference;
16 use Symfony\Component\DependencyInjection\Compiler\CheckCircularReferencesPass;
17 use Symfony\Component\DependencyInjection\Compiler\AnalyzeServiceReferencesPass;
18 use Symfony\Component\DependencyInjection\Compiler\Compiler;
19 use Symfony\Component\DependencyInjection\ContainerBuilder;
20
21 class CheckCircularReferencesPassTest extends TestCase
22 {
23     /**
24      * @expectedException \Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException
25      */
26     public function testProcess()
27     {
28         $container = new ContainerBuilder();
29         $container->register('a')->addArgument(new Reference('b'));
30         $container->register('b')->addArgument(new Reference('a'));
31
32         $this->process($container);
33     }
34
35     /**
36      * @expectedException \Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException
37      */
38     public function testProcessWithAliases()
39     {
40         $container = new ContainerBuilder();
41         $container->register('a')->addArgument(new Reference('b'));
42         $container->setAlias('b', 'c');
43         $container->setAlias('c', 'a');
44
45         $this->process($container);
46     }
47
48     /**
49      * @expectedException \Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException
50      */
51     public function testProcessWithFactory()
52     {
53         $container = new ContainerBuilder();
54
55         $container
56             ->register('a', 'stdClass')
57             ->setFactory(array(new Reference('b'), 'getInstance'));
58
59         $container
60             ->register('b', 'stdClass')
61             ->setFactory(array(new Reference('a'), 'getInstance'));
62
63         $this->process($container);
64     }
65
66     /**
67      * @expectedException \Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException
68      */
69     public function testProcessDetectsIndirectCircularReference()
70     {
71         $container = new ContainerBuilder();
72         $container->register('a')->addArgument(new Reference('b'));
73         $container->register('b')->addArgument(new Reference('c'));
74         $container->register('c')->addArgument(new Reference('a'));
75
76         $this->process($container);
77     }
78
79     /**
80      * @expectedException \Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException
81      */
82     public function testProcessDetectsIndirectCircularReferenceWithFactory()
83     {
84         $container = new ContainerBuilder();
85
86         $container->register('a')->addArgument(new Reference('b'));
87
88         $container
89             ->register('b', 'stdClass')
90             ->setFactory(array(new Reference('c'), 'getInstance'));
91
92         $container->register('c')->addArgument(new Reference('a'));
93
94         $this->process($container);
95     }
96
97     /**
98      * @expectedException \Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException
99      */
100     public function testDeepCircularReference()
101     {
102         $container = new ContainerBuilder();
103         $container->register('a')->addArgument(new Reference('b'));
104         $container->register('b')->addArgument(new Reference('c'));
105         $container->register('c')->addArgument(new Reference('b'));
106
107         $this->process($container);
108     }
109
110     public function testProcessIgnoresMethodCalls()
111     {
112         $container = new ContainerBuilder();
113         $container->register('a')->addArgument(new Reference('b'));
114         $container->register('b')->addMethodCall('setA', array(new Reference('a')));
115
116         $this->process($container);
117
118         $this->addToAssertionCount(1);
119     }
120
121     protected function process(ContainerBuilder $container)
122     {
123         $compiler = new Compiler();
124         $passConfig = $compiler->getPassConfig();
125         $passConfig->setOptimizationPasses(array(
126             new AnalyzeServiceReferencesPass(true),
127             new CheckCircularReferencesPass(),
128         ));
129         $passConfig->setRemovingPasses(array());
130
131         $compiler->compile($container);
132     }
133 }