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\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;
21 class CheckCircularReferencesPassTest extends TestCase
24 * @expectedException \Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException
26 public function testProcess()
28 $container = new ContainerBuilder();
29 $container->register('a')->addArgument(new Reference('b'));
30 $container->register('b')->addArgument(new Reference('a'));
32 $this->process($container);
36 * @expectedException \Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException
38 public function testProcessWithAliases()
40 $container = new ContainerBuilder();
41 $container->register('a')->addArgument(new Reference('b'));
42 $container->setAlias('b', 'c');
43 $container->setAlias('c', 'a');
45 $this->process($container);
49 * @expectedException \Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException
51 public function testProcessWithFactory()
53 $container = new ContainerBuilder();
56 ->register('a', 'stdClass')
57 ->setFactory(array(new Reference('b'), 'getInstance'));
60 ->register('b', 'stdClass')
61 ->setFactory(array(new Reference('a'), 'getInstance'));
63 $this->process($container);
67 * @expectedException \Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException
69 public function testProcessDetectsIndirectCircularReference()
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'));
76 $this->process($container);
80 * @expectedException \Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException
82 public function testProcessDetectsIndirectCircularReferenceWithFactory()
84 $container = new ContainerBuilder();
86 $container->register('a')->addArgument(new Reference('b'));
89 ->register('b', 'stdClass')
90 ->setFactory(array(new Reference('c'), 'getInstance'));
92 $container->register('c')->addArgument(new Reference('a'));
94 $this->process($container);
98 * @expectedException \Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException
100 public function testDeepCircularReference()
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'));
107 $this->process($container);
110 public function testProcessIgnoresMethodCalls()
112 $container = new ContainerBuilder();
113 $container->register('a')->addArgument(new Reference('b'));
114 $container->register('b')->addMethodCall('setA', array(new Reference('a')));
116 $this->process($container);
118 $this->addToAssertionCount(1);
121 protected function process(ContainerBuilder $container)
123 $compiler = new Compiler();
124 $passConfig = $compiler->getPassConfig();
125 $passConfig->setOptimizationPasses(array(
126 new AnalyzeServiceReferencesPass(true),
127 new CheckCircularReferencesPass(),
129 $passConfig->setRemovingPasses(array());
131 $compiler->compile($container);