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\Scope;
16 use Symfony\Component\DependencyInjection\Definition;
17 use Symfony\Component\DependencyInjection\Compiler\AnalyzeServiceReferencesPass;
18 use Symfony\Component\DependencyInjection\Compiler\RepeatedPass;
19 use Symfony\Component\DependencyInjection\Compiler\InlineServiceDefinitionsPass;
20 use Symfony\Component\DependencyInjection\Reference;
21 use Symfony\Component\DependencyInjection\ContainerBuilder;
23 class InlineServiceDefinitionsPassTest extends TestCase
25 public function testProcess()
27 $container = new ContainerBuilder();
29 ->register('inlinable.service')
35 ->setArguments(array(new Reference('inlinable.service')))
38 $this->process($container);
40 $arguments = $container->getDefinition('service')->getArguments();
41 $this->assertInstanceOf('Symfony\Component\DependencyInjection\Definition', $arguments[0]);
42 $this->assertSame($container->getDefinition('inlinable.service'), $arguments[0]);
45 public function testProcessDoesNotInlinesWhenAliasedServiceIsShared()
47 $container = new ContainerBuilder();
52 $container->setAlias('moo', 'foo');
56 ->setArguments(array($ref = new Reference('foo')))
59 $this->process($container);
61 $arguments = $container->getDefinition('service')->getArguments();
62 $this->assertSame($ref, $arguments[0]);
68 public function testProcessDoesNotInlineWhenAliasedServiceIsNotOfPrototypeScope()
70 $container = new ContainerBuilder();
75 $container->setAlias('moo', 'foo');
79 ->setArguments(array($ref = new Reference('foo')))
82 $this->process($container);
84 $arguments = $container->getDefinition('service')->getArguments();
85 $this->assertSame($ref, $arguments[0]);
88 public function testProcessDoesInlineNonSharedService()
90 $container = new ContainerBuilder();
100 $container->setAlias('moo', 'bar');
103 ->register('service')
104 ->setArguments(array(new Reference('foo'), $ref = new Reference('moo'), new Reference('bar')))
107 $this->process($container);
109 $arguments = $container->getDefinition('service')->getArguments();
110 $this->assertEquals($container->getDefinition('foo'), $arguments[0]);
111 $this->assertNotSame($container->getDefinition('foo'), $arguments[0]);
112 $this->assertSame($ref, $arguments[1]);
113 $this->assertEquals($container->getDefinition('bar'), $arguments[2]);
114 $this->assertNotSame($container->getDefinition('bar'), $arguments[2]);
120 public function testProcessDoesInlineServiceOfPrototypeScope()
122 $container = new ContainerBuilder();
125 ->setScope('prototype')
130 ->setScope('prototype')
132 $container->setAlias('moo', 'bar');
135 ->register('service')
136 ->setArguments(array(new Reference('foo'), $ref = new Reference('moo'), new Reference('bar')))
139 $this->process($container);
141 $arguments = $container->getDefinition('service')->getArguments();
142 $this->assertEquals($container->getDefinition('foo'), $arguments[0]);
143 $this->assertNotSame($container->getDefinition('foo'), $arguments[0]);
144 $this->assertSame($ref, $arguments[1]);
145 $this->assertEquals($container->getDefinition('bar'), $arguments[2]);
146 $this->assertNotSame($container->getDefinition('bar'), $arguments[2]);
149 public function testProcessInlinesIfMultipleReferencesButAllFromTheSameDefinition()
151 $container = new ContainerBuilder();
153 $a = $container->register('a')->setPublic(false);
156 ->addArgument(new Reference('a'))
157 ->addArgument(new Definition(null, array(new Reference('a'))))
160 $this->process($container);
162 $arguments = $b->getArguments();
163 $this->assertSame($a, $arguments[0]);
165 $inlinedArguments = $arguments[1]->getArguments();
166 $this->assertSame($a, $inlinedArguments[0]);
169 public function testProcessInlinesPrivateFactoryReference()
171 $container = new ContainerBuilder();
173 $container->register('a')->setPublic(false);
177 ->setFactory(array(new Reference('a'), 'a'))
182 ->setArguments(array(
183 $ref = new Reference('b'),
186 $this->process($container);
188 $inlinedArguments = $container->getDefinition('foo')->getArguments();
189 $this->assertSame($b, $inlinedArguments[0]);
192 public function testProcessDoesNotInlinePrivateFactoryIfReferencedMultipleTimesWithinTheSameDefinition()
194 $container = new ContainerBuilder();
201 ->setFactory(array(new Reference('a'), 'a'))
206 ->setArguments(array(
207 $ref1 = new Reference('b'),
208 $ref2 = new Reference('b'),
211 $this->process($container);
213 $args = $container->getDefinition('foo')->getArguments();
214 $this->assertSame($ref1, $args[0]);
215 $this->assertSame($ref2, $args[1]);
218 public function testProcessDoesNotInlineReferenceWhenUsedByInlineFactory()
220 $container = new ContainerBuilder();
227 ->setFactory(array(new Reference('a'), 'a'))
230 $inlineFactory = new Definition();
231 $inlineFactory->setPublic(false);
232 $inlineFactory->setFactory(array(new Reference('b'), 'b'));
236 ->setArguments(array(
237 $ref = new Reference('b'),
241 $this->process($container);
243 $args = $container->getDefinition('foo')->getArguments();
244 $this->assertSame($ref, $args[0]);
250 public function testProcessInlinesOnlyIfSameScope()
252 $container = new ContainerBuilder();
254 $container->addScope(new Scope('foo'));
255 $a = $container->register('a')->setPublic(false)->setScope('foo');
256 $b = $container->register('b')->addArgument(new Reference('a'));
258 $this->process($container);
259 $arguments = $b->getArguments();
260 $this->assertEquals(new Reference('a'), $arguments[0]);
261 $this->assertTrue($container->hasDefinition('a'));
264 public function testProcessDoesNotInlineWhenServiceIsPrivateButLazy()
266 $container = new ContainerBuilder();
274 ->register('service')
275 ->setArguments(array($ref = new Reference('foo')))
278 $this->process($container);
280 $arguments = $container->getDefinition('service')->getArguments();
281 $this->assertSame($ref, $arguments[0]);
284 public function testProcessDoesNotInlineWhenServiceReferencesItself()
286 $container = new ContainerBuilder();
290 ->addMethodCall('foo', array($ref = new Reference('foo')))
293 $this->process($container);
295 $calls = $container->getDefinition('foo')->getMethodCalls();
296 $this->assertSame($ref, $calls[0][1][0]);
299 protected function process(ContainerBuilder $container)
301 $repeatedPass = new RepeatedPass(array(new AnalyzeServiceReferencesPass(), new InlineServiceDefinitionsPass()));
302 $repeatedPass->process($container);