123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304 |
- <?php
- /*
- * This file is part of the Symfony package.
- *
- * (c) Fabien Potencier <fabien@symfony.com>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace Symfony\Component\DependencyInjection\Tests\Compiler;
- use PHPUnit\Framework\TestCase;
- use Symfony\Component\DependencyInjection\Compiler\AnalyzeServiceReferencesPass;
- use Symfony\Component\DependencyInjection\Compiler\InlineServiceDefinitionsPass;
- use Symfony\Component\DependencyInjection\Compiler\RepeatedPass;
- use Symfony\Component\DependencyInjection\ContainerBuilder;
- use Symfony\Component\DependencyInjection\Definition;
- use Symfony\Component\DependencyInjection\Reference;
- use Symfony\Component\DependencyInjection\Scope;
- class InlineServiceDefinitionsPassTest extends TestCase
- {
- public function testProcess()
- {
- $container = new ContainerBuilder();
- $container
- ->register('inlinable.service')
- ->setPublic(false)
- ;
- $container
- ->register('service')
- ->setArguments(array(new Reference('inlinable.service')))
- ;
- $this->process($container);
- $arguments = $container->getDefinition('service')->getArguments();
- $this->assertInstanceOf('Symfony\Component\DependencyInjection\Definition', $arguments[0]);
- $this->assertSame($container->getDefinition('inlinable.service'), $arguments[0]);
- }
- public function testProcessDoesNotInlinesWhenAliasedServiceIsShared()
- {
- $container = new ContainerBuilder();
- $container
- ->register('foo')
- ->setPublic(false)
- ;
- $container->setAlias('moo', 'foo');
- $container
- ->register('service')
- ->setArguments(array($ref = new Reference('foo')))
- ;
- $this->process($container);
- $arguments = $container->getDefinition('service')->getArguments();
- $this->assertSame($ref, $arguments[0]);
- }
- /**
- * @group legacy
- */
- public function testProcessDoesNotInlineWhenAliasedServiceIsNotOfPrototypeScope()
- {
- $container = new ContainerBuilder();
- $container
- ->register('foo')
- ->setPublic(false)
- ;
- $container->setAlias('moo', 'foo');
- $container
- ->register('service')
- ->setArguments(array($ref = new Reference('foo')))
- ;
- $this->process($container);
- $arguments = $container->getDefinition('service')->getArguments();
- $this->assertSame($ref, $arguments[0]);
- }
- public function testProcessDoesInlineNonSharedService()
- {
- $container = new ContainerBuilder();
- $container
- ->register('foo')
- ->setShared(false)
- ;
- $container
- ->register('bar')
- ->setPublic(false)
- ->setShared(false)
- ;
- $container->setAlias('moo', 'bar');
- $container
- ->register('service')
- ->setArguments(array(new Reference('foo'), $ref = new Reference('moo'), new Reference('bar')))
- ;
- $this->process($container);
- $arguments = $container->getDefinition('service')->getArguments();
- $this->assertEquals($container->getDefinition('foo'), $arguments[0]);
- $this->assertNotSame($container->getDefinition('foo'), $arguments[0]);
- $this->assertSame($ref, $arguments[1]);
- $this->assertEquals($container->getDefinition('bar'), $arguments[2]);
- $this->assertNotSame($container->getDefinition('bar'), $arguments[2]);
- }
- /**
- * @group legacy
- */
- public function testProcessDoesInlineServiceOfPrototypeScope()
- {
- $container = new ContainerBuilder();
- $container
- ->register('foo')
- ->setScope('prototype')
- ;
- $container
- ->register('bar')
- ->setPublic(false)
- ->setScope('prototype')
- ;
- $container->setAlias('moo', 'bar');
- $container
- ->register('service')
- ->setArguments(array(new Reference('foo'), $ref = new Reference('moo'), new Reference('bar')))
- ;
- $this->process($container);
- $arguments = $container->getDefinition('service')->getArguments();
- $this->assertEquals($container->getDefinition('foo'), $arguments[0]);
- $this->assertNotSame($container->getDefinition('foo'), $arguments[0]);
- $this->assertSame($ref, $arguments[1]);
- $this->assertEquals($container->getDefinition('bar'), $arguments[2]);
- $this->assertNotSame($container->getDefinition('bar'), $arguments[2]);
- }
- public function testProcessInlinesIfMultipleReferencesButAllFromTheSameDefinition()
- {
- $container = new ContainerBuilder();
- $a = $container->register('a')->setPublic(false);
- $b = $container
- ->register('b')
- ->addArgument(new Reference('a'))
- ->addArgument(new Definition(null, array(new Reference('a'))))
- ;
- $this->process($container);
- $arguments = $b->getArguments();
- $this->assertSame($a, $arguments[0]);
- $inlinedArguments = $arguments[1]->getArguments();
- $this->assertSame($a, $inlinedArguments[0]);
- }
- public function testProcessInlinesPrivateFactoryReference()
- {
- $container = new ContainerBuilder();
- $container->register('a')->setPublic(false);
- $b = $container
- ->register('b')
- ->setPublic(false)
- ->setFactory(array(new Reference('a'), 'a'))
- ;
- $container
- ->register('foo')
- ->setArguments(array(
- $ref = new Reference('b'),
- ));
- $this->process($container);
- $inlinedArguments = $container->getDefinition('foo')->getArguments();
- $this->assertSame($b, $inlinedArguments[0]);
- }
- public function testProcessDoesNotInlinePrivateFactoryIfReferencedMultipleTimesWithinTheSameDefinition()
- {
- $container = new ContainerBuilder();
- $container
- ->register('a')
- ;
- $container
- ->register('b')
- ->setPublic(false)
- ->setFactory(array(new Reference('a'), 'a'))
- ;
- $container
- ->register('foo')
- ->setArguments(array(
- $ref1 = new Reference('b'),
- $ref2 = new Reference('b'),
- ))
- ;
- $this->process($container);
- $args = $container->getDefinition('foo')->getArguments();
- $this->assertSame($ref1, $args[0]);
- $this->assertSame($ref2, $args[1]);
- }
- public function testProcessDoesNotInlineReferenceWhenUsedByInlineFactory()
- {
- $container = new ContainerBuilder();
- $container
- ->register('a')
- ;
- $container
- ->register('b')
- ->setPublic(false)
- ->setFactory(array(new Reference('a'), 'a'))
- ;
- $inlineFactory = new Definition();
- $inlineFactory->setPublic(false);
- $inlineFactory->setFactory(array(new Reference('b'), 'b'));
- $container
- ->register('foo')
- ->setArguments(array(
- $ref = new Reference('b'),
- $inlineFactory,
- ))
- ;
- $this->process($container);
- $args = $container->getDefinition('foo')->getArguments();
- $this->assertSame($ref, $args[0]);
- }
- /**
- * @group legacy
- */
- public function testProcessInlinesOnlyIfSameScope()
- {
- $container = new ContainerBuilder();
- $container->addScope(new Scope('foo'));
- $a = $container->register('a')->setPublic(false)->setScope('foo');
- $b = $container->register('b')->addArgument(new Reference('a'));
- $this->process($container);
- $arguments = $b->getArguments();
- $this->assertEquals(new Reference('a'), $arguments[0]);
- $this->assertTrue($container->hasDefinition('a'));
- }
- public function testProcessDoesNotInlineWhenServiceIsPrivateButLazy()
- {
- $container = new ContainerBuilder();
- $container
- ->register('foo')
- ->setPublic(false)
- ->setLazy(true)
- ;
- $container
- ->register('service')
- ->setArguments(array($ref = new Reference('foo')))
- ;
- $this->process($container);
- $arguments = $container->getDefinition('service')->getArguments();
- $this->assertSame($ref, $arguments[0]);
- }
- public function testProcessDoesNotInlineWhenServiceReferencesItself()
- {
- $container = new ContainerBuilder();
- $container
- ->register('foo')
- ->setPublic(false)
- ->addMethodCall('foo', array($ref = new Reference('foo')))
- ;
- $this->process($container);
- $calls = $container->getDefinition('foo')->getMethodCalls();
- $this->assertSame($ref, $calls[0][1][0]);
- }
- protected function process(ContainerBuilder $container)
- {
- $repeatedPass = new RepeatedPass(array(new AnalyzeServiceReferencesPass(), new InlineServiceDefinitionsPass()));
- $repeatedPass->process($container);
- }
- }
|