This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/src/Symfony/Component/DependencyInjection/Tests/Compiler/ResolveChildDefinitionsPassTest.php

400 lines
12 KiB
PHP
Raw Normal View History

2011-01-26 23:14:31 +00:00
<?php
2011-05-31 09:57:06 +01:00
/*
2012-03-31 22:00:32 +01:00
* This file is part of the Symfony package.
2011-05-31 09:57:06 +01:00
*
* (c) Fabien Potencier <fabien@symfony.com>
*
2012-03-31 22:00:32 +01:00
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
2011-05-31 09:57:06 +01:00
*/
namespace Symfony\Component\DependencyInjection\Tests\Compiler;
2011-01-26 23:14:31 +00:00
2017-02-08 07:24:27 +00:00
use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\ChildDefinition;
use Symfony\Component\DependencyInjection\Compiler\ResolveChildDefinitionsPass;
2011-01-26 23:14:31 +00:00
use Symfony\Component\DependencyInjection\ContainerBuilder;
class ResolveChildDefinitionsPassTest extends TestCase
2011-01-26 23:14:31 +00:00
{
public function testProcess()
{
$container = new ContainerBuilder();
2011-03-10 14:31:00 +00:00
$container->register('parent', 'foo')->setArguments(array('moo', 'b'))->setProperty('foo', 'moo');
$container->setDefinition('child', new ChildDefinition('parent'))
->replaceArgument(0, 'a')
2011-03-10 14:31:00 +00:00
->setProperty('foo', 'bar')
2011-01-26 23:14:31 +00:00
->setClass('bar')
;
$this->process($container);
$def = $container->getDefinition('child');
$this->assertNotInstanceOf(ChildDefinition::class, $def);
2011-01-26 23:14:31 +00:00
$this->assertEquals('bar', $def->getClass());
$this->assertEquals(array('a', 'b'), $def->getArguments());
2011-03-10 14:31:00 +00:00
$this->assertEquals(array('foo' => 'bar'), $def->getProperties());
2011-01-26 23:14:31 +00:00
}
public function testProcessAppendsMethodCallsAlways()
{
$container = new ContainerBuilder();
$container
->register('parent')
->addMethodCall('foo', array('bar'))
;
$container
->setDefinition('child', new ChildDefinition('parent'))
2011-01-26 23:14:31 +00:00
->addMethodCall('bar', array('foo'))
;
$this->process($container);
$def = $container->getDefinition('child');
$this->assertEquals(array(
array('foo', array('bar')),
array('bar', array('foo')),
), $def->getMethodCalls());
}
public function testProcessDoesNotCopyAbstract()
{
$container = new ContainerBuilder();
$container
->register('parent')
->setAbstract(true)
;
$container
->setDefinition('child', new ChildDefinition('parent'))
2011-01-26 23:14:31 +00:00
;
$this->process($container);
$def = $container->getDefinition('child');
$this->assertFalse($def->isAbstract());
}
public function testProcessDoesNotCopyShared()
{
$container = new ContainerBuilder();
$container
->register('parent')
->setShared(false)
;
$container
->setDefinition('child', new ChildDefinition('parent'))
;
$this->process($container);
$def = $container->getDefinition('child');
$this->assertTrue($def->isShared());
}
2011-01-26 23:14:31 +00:00
public function testProcessDoesNotCopyTags()
{
$container = new ContainerBuilder();
$container
->register('parent')
->addTag('foo')
;
$container
->setDefinition('child', new ChildDefinition('parent'))
2011-01-26 23:14:31 +00:00
;
$this->process($container);
$def = $container->getDefinition('child');
$this->assertEquals(array(), $def->getTags());
}
public function testProcessDoesNotCopyDecoratedService()
{
$container = new ContainerBuilder();
$container
->register('parent')
->setDecoratedService('foo')
;
$container
->setDefinition('child', new ChildDefinition('parent'))
;
$this->process($container);
$def = $container->getDefinition('child');
$this->assertNull($def->getDecoratedService());
}
public function testProcessDoesNotDropShared()
{
$container = new ContainerBuilder();
$container
->register('parent')
;
$container
->setDefinition('child', new ChildDefinition('parent'))
->setShared(false)
;
$this->process($container);
$def = $container->getDefinition('child');
$this->assertFalse($def->isShared());
}
2011-01-26 23:14:31 +00:00
public function testProcessHandlesMultipleInheritance()
{
$container = new ContainerBuilder();
$container
->register('parent', 'foo')
->setArguments(array('foo', 'bar', 'c'))
;
$container
->setDefinition('child2', new ChildDefinition('child1'))
->replaceArgument(1, 'b')
2011-01-26 23:14:31 +00:00
;
$container
->setDefinition('child1', new ChildDefinition('parent'))
->replaceArgument(0, 'a')
2011-01-26 23:14:31 +00:00
;
$this->process($container);
$def = $container->getDefinition('child2');
$this->assertEquals(array('a', 'b', 'c'), $def->getArguments());
$this->assertEquals('foo', $def->getClass());
}
2013-07-25 06:55:15 +01:00
public function testSetLazyOnServiceHasParent()
{
$container = new ContainerBuilder();
2014-12-02 19:42:47 +00:00
$container->register('parent', 'stdClass');
2013-07-25 06:55:15 +01:00
$container->setDefinition('child1', new ChildDefinition('parent'))
2013-07-25 06:55:15 +01:00
->setLazy(true)
;
$this->process($container);
$this->assertTrue($container->getDefinition('child1')->isLazy());
}
public function testSetLazyOnServiceIsParent()
{
$container = new ContainerBuilder();
2014-12-02 19:42:47 +00:00
$container->register('parent', 'stdClass')
2013-07-25 06:55:15 +01:00
->setLazy(true)
;
$container->setDefinition('child1', new ChildDefinition('parent'));
2013-07-25 06:55:15 +01:00
$this->process($container);
$this->assertTrue($container->getDefinition('child1')->isLazy());
}
public function testSetAutowiredOnServiceHasParent()
{
$container = new ContainerBuilder();
$container->register('parent', 'stdClass')
->setAutowired(true)
2017-02-07 18:39:15 +00:00
;
$container->setDefinition('child1', new ChildDefinition('parent'))
->setAutowired(false)
;
$this->process($container);
$this->assertFalse($container->getDefinition('child1')->isAutowired());
}
public function testSetAutowiredOnServiceIsParent()
{
$container = new ContainerBuilder();
$container->register('parent', 'stdClass')
->setAutowired(true)
;
$container->setDefinition('child1', new ChildDefinition('parent'));
$this->process($container);
$this->assertTrue($container->getDefinition('child1')->isAutowired());
}
public function testDeepDefinitionsResolving()
{
$container = new ContainerBuilder();
$container->register('parent', 'parentClass');
$container->register('sibling', 'siblingClass')
->setConfigurator(new ChildDefinition('parent'), 'foo')
->setFactory(array(new ChildDefinition('parent'), 'foo'))
->addArgument(new ChildDefinition('parent'))
->setProperty('prop', new ChildDefinition('parent'))
->addMethodCall('meth', array(new ChildDefinition('parent')))
;
$this->process($container);
$configurator = $container->getDefinition('sibling')->getConfigurator();
$this->assertSame('Symfony\Component\DependencyInjection\Definition', \get_class($configurator));
$this->assertSame('parentClass', $configurator->getClass());
$factory = $container->getDefinition('sibling')->getFactory();
$this->assertSame('Symfony\Component\DependencyInjection\Definition', \get_class($factory[0]));
$this->assertSame('parentClass', $factory[0]->getClass());
$argument = $container->getDefinition('sibling')->getArgument(0);
$this->assertSame('Symfony\Component\DependencyInjection\Definition', \get_class($argument));
$this->assertSame('parentClass', $argument->getClass());
$properties = $container->getDefinition('sibling')->getProperties();
$this->assertSame('Symfony\Component\DependencyInjection\Definition', \get_class($properties['prop']));
$this->assertSame('parentClass', $properties['prop']->getClass());
$methodCalls = $container->getDefinition('sibling')->getMethodCalls();
$this->assertSame('Symfony\Component\DependencyInjection\Definition', \get_class($methodCalls[0][1][0]));
$this->assertSame('parentClass', $methodCalls[0][1][0]->getClass());
}
public function testSetDecoratedServiceOnServiceHasParent()
{
$container = new ContainerBuilder();
$container->register('parent', 'stdClass');
$container->setDefinition('child1', new ChildDefinition('parent'))
->setDecoratedService('foo', 'foo_inner', 5)
;
$this->process($container);
$this->assertEquals(array('foo', 'foo_inner', 5), $container->getDefinition('child1')->getDecoratedService());
}
public function testDecoratedServiceCopiesDeprecatedStatusFromParent()
{
$container = new ContainerBuilder();
$container->register('deprecated_parent')
->setDeprecated(true)
;
$container->setDefinition('decorated_deprecated_parent', new ChildDefinition('deprecated_parent'));
$this->process($container);
$this->assertTrue($container->getDefinition('decorated_deprecated_parent')->isDeprecated());
}
public function testDecoratedServiceCanOverwriteDeprecatedParentStatus()
{
$container = new ContainerBuilder();
$container->register('deprecated_parent')
->setDeprecated(true)
;
$container->setDefinition('decorated_deprecated_parent', new ChildDefinition('deprecated_parent'))
->setDeprecated(false)
;
$this->process($container);
$this->assertFalse($container->getDefinition('decorated_deprecated_parent')->isDeprecated());
}
public function testProcessResolvesAliases()
{
$container = new ContainerBuilder();
$container->register('parent', 'ParentClass');
$container->setAlias('parent_alias', 'parent');
$container->setDefinition('child', new ChildDefinition('parent_alias'));
$this->process($container);
$def = $container->getDefinition('child');
$this->assertSame('ParentClass', $def->getClass());
}
2017-04-10 15:38:35 +01:00
public function testProcessSetsArguments()
{
$container = new ContainerBuilder();
$container->register('parent', 'ParentClass')->setArguments(array(0));
$container->setDefinition('child', (new ChildDefinition('parent'))->setArguments(array(
1,
'index_0' => 2,
'foo' => 3,
)));
$this->process($container);
$def = $container->getDefinition('child');
$this->assertSame(array(2, 1, 'foo' => 3), $def->getArguments());
}
public function testBindings()
{
$container = new ContainerBuilder();
$container->register('parent', 'stdClass')
->setBindings(array('a' => '1', 'b' => '2'))
;
$child = $container->setDefinition('child', new ChildDefinition('parent'))
->setBindings(array('b' => 'B', 'c' => 'C'))
;
$this->process($container);
$bindings = array();
foreach ($container->getDefinition('child')->getBindings() as $k => $v) {
$bindings[$k] = $v->getValues()[0];
}
$this->assertEquals(array('b' => 'B', 'c' => 'C', 'a' => '1'), $bindings);
}
public function testSetAutoconfiguredOnServiceIsParent()
{
$container = new ContainerBuilder();
$container->register('parent', 'stdClass')
->setAutoconfigured(true)
;
$container->setDefinition('child1', new ChildDefinition('parent'));
$this->process($container);
Not allowing autoconfigure, instanceofConditionals or defaults for ChildDefinition Also, not allowing arguments or method calls for autoconfigure. This is a safety mechanism, since we don't have merging logic. It will allow us to add this in the future if we want to. The reason is that parent-child definitions are a different mechanism for "inheritance" than instanceofConditionas and defaults... creating some edge cases when trying to figure out which settings "win". For example: Suppose a child and parent definitions are defined in different YAML files. The child receives public: false from its _defaults, and the parent receives public: true from its _defaults. Should the final child definition be public: true (so the parent overrides the child, even though it only came from _defaults) or public: false (where the child wins... even though it was only set from its _defaults). Or, if the parent is explicitly set to public: true, should that override the public: false of the child (which it got from its _defaults)? On one hand, the parent is being explicitly set. On the other hand, the child is explicitly in a file settings _defaults public to false. There's no correct answer. There are also problems with instanceof. The importance goes: defaults < instanceof < service definition But how does parent-child relationships fit into that? If a child has public: false from an _instanceof, but the parent explicitly sets public: true, which wins? Should we assume the parent definition wins because it's explicitly set? Or would the _instanceof win, because that's being explicitly applied to the child definition's class by an _instanceof that lives in the same file as that class (whereas the parent definition may live in a different file). Because of this, @nicolas-grekas and I (we also talked a bit to Fabien) decided that the complexity was growing too much. The solution is to not allow any of these new feature to be used by ChildDefinition objects. In other words, when you want some sort of "inheritance" for your service, you should *either* giving your service a parent *or* using defaults and instanceof. And instead of silently not applying defaults and instanceof to child definitions, I think it's better to scream that it's not supported.
2017-04-27 16:48:07 +01:00
$this->assertFalse($container->getDefinition('child1')->isAutoconfigured());
}
2011-01-26 23:14:31 +00:00
protected function process(ContainerBuilder $container)
{
$pass = new ResolveChildDefinitionsPass();
2011-01-26 23:14:31 +00:00
$pass->process($container);
}
}