4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\DependencyInjection\Tests\Compiler;
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;
22 class CheckCircularReferencesPassTest extends TestCase
25 * @expectedException \Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException
27 public function testProcess()
29 $container = new ContainerBuilder();
30 $container->register('a')->addArgument(new Reference('b'));
31 $container->register('b')->addArgument(new Reference('a'));
33 $this->process($container);
37 * @expectedException \Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException
39 public function testProcessWithAliases()
41 $container = new ContainerBuilder();
42 $container->register('a')->addArgument(new Reference('b'));
43 $container->setAlias('b', 'c');
44 $container->setAlias('c', 'a');
46 $this->process($container);
50 * @expectedException \Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException
52 public function testProcessWithFactory()
54 $container = new ContainerBuilder();
57 ->register('a', 'stdClass')
58 ->setFactory(array(new Reference('b'), 'getInstance'));
61 ->register('b', 'stdClass')
62 ->setFactory(array(new Reference('a'), 'getInstance'));
64 $this->process($container);
68 * @expectedException \Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException
70 public function testProcessDetectsIndirectCircularReference()
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'));
77 $this->process($container);
81 * @expectedException \Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException
83 public function testProcessDetectsIndirectCircularReferenceWithFactory()
85 $container = new ContainerBuilder();
87 $container->register('a')->addArgument(new Reference('b'));
90 ->register('b', 'stdClass')
91 ->setFactory(array(new Reference('c'), 'getInstance'));
93 $container->register('c')->addArgument(new Reference('a'));
95 $this->process($container);
99 * @expectedException \Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException
101 public function testDeepCircularReference()
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'));
108 $this->process($container);
111 public function testProcessIgnoresMethodCalls()
113 $container = new ContainerBuilder();
114 $container->register('a')->addArgument(new Reference('b'));
115 $container->register('b')->addMethodCall('setA', array(new Reference('a')));
117 $this->process($container);
119 $this->addToAssertionCount(1);
122 public function testProcessIgnoresLazyServices()
124 $container = new ContainerBuilder();
125 $container->register('a')->setLazy(true)->addArgument(new Reference('b'));
126 $container->register('b')->addArgument(new Reference('a'));
128 $this->process($container);
130 // just make sure that a lazily loaded service does not trigger a CircularReferenceException
131 $this->addToAssertionCount(1);
134 public function testProcessIgnoresIteratorArguments()
136 $container = new ContainerBuilder();
137 $container->register('a')->addArgument(new Reference('b'));
138 $container->register('b')->addArgument(new IteratorArgument(array(new Reference('a'))));
140 $this->process($container);
142 // just make sure that an IteratorArgument does not trigger a CircularReferenceException
143 $this->addToAssertionCount(1);
146 protected function process(ContainerBuilder $container)
148 $compiler = new Compiler();
149 $passConfig = $compiler->getPassConfig();
150 $passConfig->setOptimizationPasses(array(
151 new AnalyzeServiceReferencesPass(true),
152 new CheckCircularReferencesPass(),
154 $passConfig->setRemovingPasses(array());
156 $compiler->compile($container);