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\Compiler\AnalyzeServiceReferencesPass;
16 use Symfony\Component\DependencyInjection\Compiler\RepeatedPass;
17 use Symfony\Component\DependencyInjection\Compiler\RemoveUnusedDefinitionsPass;
18 use Symfony\Component\DependencyInjection\Definition;
19 use Symfony\Component\DependencyInjection\Reference;
20 use Symfony\Component\DependencyInjection\ContainerBuilder;
22 class RemoveUnusedDefinitionsPassTest extends TestCase
24 public function testProcess()
26 $container = new ContainerBuilder();
37 ->setArguments(array(new Reference('bar')))
40 $this->process($container);
42 $this->assertFalse($container->hasDefinition('foo'));
43 $this->assertTrue($container->hasDefinition('bar'));
44 $this->assertTrue($container->hasDefinition('moo'));
47 public function testProcessRemovesUnusedDefinitionsRecursively()
49 $container = new ContainerBuilder();
56 ->setArguments(array(new Reference('foo')))
60 $this->process($container);
62 $this->assertFalse($container->hasDefinition('foo'));
63 $this->assertFalse($container->hasDefinition('bar'));
66 public function testProcessWorksWithInlinedDefinitions()
68 $container = new ContainerBuilder();
75 ->setArguments(array(new Definition(null, array(new Reference('foo')))))
78 $this->process($container);
80 $this->assertTrue($container->hasDefinition('foo'));
81 $this->assertTrue($container->hasDefinition('bar'));
84 public function testProcessWontRemovePrivateFactory()
86 $container = new ContainerBuilder();
89 ->register('foo', 'stdClass')
90 ->setFactory(array('stdClass', 'getInstance'))
94 ->register('bar', 'stdClass')
95 ->setFactory(array(new Reference('foo'), 'getInstance'))
100 ->addArgument(new Reference('bar'));
102 $this->process($container);
104 $this->assertTrue($container->hasDefinition('foo'));
105 $this->assertTrue($container->hasDefinition('bar'));
106 $this->assertTrue($container->hasDefinition('foobar'));
109 protected function process(ContainerBuilder $container)
111 $repeatedPass = new RepeatedPass(array(new AnalyzeServiceReferencesPass(), new RemoveUnusedDefinitionsPass()));
112 $repeatedPass->process($container);