| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253 |
- <?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\ChildDefinition;
- use Symfony\Component\DependencyInjection\Compiler\ResolveInstanceofConditionalsPass;
- use Symfony\Component\DependencyInjection\Compiler\ResolveDefinitionTemplatesPass;
- use Symfony\Component\DependencyInjection\ContainerBuilder;
- class ResolveInstanceofConditionalsPassTest extends TestCase
- {
- public function testProcess()
- {
- $container = new ContainerBuilder();
- $def = $container->register('foo', self::class)->addTag('tag')->setAutowired(true)->setChanges(array());
- $def->setInstanceofConditionals(array(
- parent::class => (new ChildDefinition(''))->setProperty('foo', 'bar')->addTag('baz', array('attr' => 123)),
- ));
- (new ResolveInstanceofConditionalsPass())->process($container);
- $parent = 'instanceof.'.parent::class.'.0.foo';
- $def = $container->getDefinition('foo');
- $this->assertEmpty($def->getInstanceofConditionals());
- $this->assertInstanceOf(ChildDefinition::class, $def);
- $this->assertTrue($def->isAutowired());
- $this->assertSame($parent, $def->getParent());
- $this->assertSame(array('tag' => array(array()), 'baz' => array(array('attr' => 123))), $def->getTags());
- $parent = $container->getDefinition($parent);
- $this->assertSame(array('foo' => 'bar'), $parent->getProperties());
- $this->assertSame(array(), $parent->getTags());
- }
- public function testProcessInheritance()
- {
- $container = new ContainerBuilder();
- $def = $container
- ->register('parent', parent::class)
- ->addMethodCall('foo', array('foo'));
- $def->setInstanceofConditionals(array(
- parent::class => (new ChildDefinition(''))->addMethodCall('foo', array('bar')),
- ));
- $def = (new ChildDefinition('parent'))->setClass(self::class);
- $container->setDefinition('child', $def);
- (new ResolveInstanceofConditionalsPass())->process($container);
- (new ResolveDefinitionTemplatesPass())->process($container);
- $expected = array(
- array('foo', array('bar')),
- array('foo', array('foo')),
- );
- $this->assertSame($expected, $container->getDefinition('parent')->getMethodCalls());
- $this->assertSame($expected, $container->getDefinition('child')->getMethodCalls());
- }
- public function testProcessDoesReplaceShared()
- {
- $container = new ContainerBuilder();
- $def = $container->register('foo', 'stdClass');
- $def->setInstanceofConditionals(array(
- 'stdClass' => (new ChildDefinition(''))->setShared(false),
- ));
- (new ResolveInstanceofConditionalsPass())->process($container);
- $def = $container->getDefinition('foo');
- $this->assertFalse($def->isShared());
- }
- public function testProcessHandlesMultipleInheritance()
- {
- $container = new ContainerBuilder();
- $def = $container->register('foo', self::class)->setShared(true);
- $def->setInstanceofConditionals(array(
- parent::class => (new ChildDefinition(''))->setLazy(true)->setShared(false),
- self::class => (new ChildDefinition(''))->setAutowired(true),
- ));
- (new ResolveInstanceofConditionalsPass())->process($container);
- (new ResolveDefinitionTemplatesPass())->process($container);
- $def = $container->getDefinition('foo');
- $this->assertTrue($def->isAutowired());
- $this->assertTrue($def->isLazy());
- $this->assertTrue($def->isShared());
- }
- public function testProcessUsesAutoconfiguredInstanceof()
- {
- $container = new ContainerBuilder();
- $def = $container->register('normal_service', self::class);
- $def->setInstanceofConditionals(array(
- parent::class => (new ChildDefinition(''))
- ->addTag('local_instanceof_tag')
- ->setFactory('locally_set_factory'),
- ));
- $def->setAutoconfigured(true);
- $container->registerForAutoconfiguration(parent::class)
- ->addTag('autoconfigured_tag')
- ->setAutowired(true)
- ->setFactory('autoconfigured_factory');
- (new ResolveInstanceofConditionalsPass())->process($container);
- (new ResolveDefinitionTemplatesPass())->process($container);
- $def = $container->getDefinition('normal_service');
- // autowired thanks to the autoconfigured instanceof
- $this->assertTrue($def->isAutowired());
- // factory from the specific instanceof overrides global one
- $this->assertEquals('locally_set_factory', $def->getFactory());
- // tags are merged, the locally set one is first
- $this->assertSame(array('local_instanceof_tag' => array(array()), 'autoconfigured_tag' => array(array())), $def->getTags());
- }
- public function testAutoconfigureInstanceofDoesNotDuplicateTags()
- {
- $container = new ContainerBuilder();
- $def = $container->register('normal_service', self::class);
- $def
- ->addTag('duplicated_tag')
- ->addTag('duplicated_tag', array('and_attributes' => 1))
- ;
- $def->setInstanceofConditionals(array(
- parent::class => (new ChildDefinition(''))->addTag('duplicated_tag'),
- ));
- $def->setAutoconfigured(true);
- $container->registerForAutoconfiguration(parent::class)
- ->addTag('duplicated_tag', array('and_attributes' => 1))
- ;
- (new ResolveInstanceofConditionalsPass())->process($container);
- (new ResolveDefinitionTemplatesPass())->process($container);
- $def = $container->getDefinition('normal_service');
- $this->assertSame(array('duplicated_tag' => array(array(), array('and_attributes' => 1))), $def->getTags());
- }
- public function testProcessDoesNotUseAutoconfiguredInstanceofIfNotEnabled()
- {
- $container = new ContainerBuilder();
- $def = $container->register('normal_service', self::class);
- $def->setInstanceofConditionals(array(
- parent::class => (new ChildDefinition(''))
- ->addTag('foo_tag'),
- ));
- $container->registerForAutoconfiguration(parent::class)
- ->setAutowired(true);
- (new ResolveInstanceofConditionalsPass())->process($container);
- (new ResolveDefinitionTemplatesPass())->process($container);
- $def = $container->getDefinition('normal_service');
- $this->assertFalse($def->isAutowired());
- }
- /**
- * @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException
- * @expectedExceptionMessage "App\FakeInterface" is set as an "instanceof" conditional, but it does not exist.
- */
- public function testBadInterfaceThrowsException()
- {
- $container = new ContainerBuilder();
- $def = $container->register('normal_service', self::class);
- $def->setInstanceofConditionals(array(
- 'App\\FakeInterface' => (new ChildDefinition(''))
- ->addTag('foo_tag'),
- ));
- (new ResolveInstanceofConditionalsPass())->process($container);
- }
- public function testBadInterfaceForAutomaticInstanceofIsOk()
- {
- $container = new ContainerBuilder();
- $container->register('normal_service', self::class)
- ->setAutoconfigured(true);
- $container->registerForAutoconfiguration('App\\FakeInterface')
- ->setAutowired(true);
- (new ResolveInstanceofConditionalsPass())->process($container);
- $this->assertTrue($container->hasDefinition('normal_service'));
- }
- /**
- * @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
- * @expectedExceptionMessage Autoconfigured instanceof for type "PHPUnit\Framework\TestCase" defines method calls but these are not supported and should be removed.
- */
- public function testProcessThrowsExceptionForAutoconfiguredCalls()
- {
- $container = new ContainerBuilder();
- $container->registerForAutoconfiguration(parent::class)
- ->addMethodCall('setFoo');
- (new ResolveInstanceofConditionalsPass())->process($container);
- }
- /**
- * @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
- * @expectedExceptionMessage Autoconfigured instanceof for type "PHPUnit\Framework\TestCase" defines arguments but these are not supported and should be removed.
- */
- public function testProcessThrowsExceptionForArguments()
- {
- $container = new ContainerBuilder();
- $container->registerForAutoconfiguration(parent::class)
- ->addArgument('bar');
- (new ResolveInstanceofConditionalsPass())->process($container);
- }
- public function testMergeReset()
- {
- $container = new ContainerBuilder();
- $container
- ->register('bar', self::class)
- ->addArgument('a')
- ->addMethodCall('setB')
- ->setDecoratedService('foo')
- ->addTag('t')
- ->setInstanceofConditionals(array(
- parent::class => (new ChildDefinition(''))->addTag('bar'),
- ))
- ;
- (new ResolveInstanceofConditionalsPass())->process($container);
- $abstract = $container->getDefinition('abstract.instanceof.bar');
- $this->assertEmpty($abstract->getArguments());
- $this->assertEmpty($abstract->getMethodCalls());
- $this->assertNull($abstract->getDecoratedService());
- $this->assertEmpty($abstract->getTags());
- $this->assertTrue($abstract->isAbstract());
- }
- }
|