bug #18467 [DependencyInjection] Resolve aliases before removing abstract services + add tests (nicolas-grekas)

This PR was merged into the 2.3 branch.

Discussion
----------

[DependencyInjection] Resolve aliases before removing abstract services + add tests

| Q             | A
| ------------- | ---
| Branch?       | 2.3
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | -
| License       | MIT
| Doc PR        | -

Commits
-------

9802a41 [DependencyInjection] Resolve aliases before removing abstract services + add tests
This commit is contained in:
Fabien Potencier 2016-04-07 17:04:06 +02:00
commit 004a6678d2
4 changed files with 44 additions and 6 deletions

View File

@ -59,8 +59,8 @@ class PassConfig
$this->removingPasses = array(
new RemovePrivateAliasesPass(),
new RemoveAbstractDefinitionsPass(),
new ReplaceAliasByActualDefinitionPass(),
new RemoveAbstractDefinitionsPass(),
new RepeatedPass(array(
new AnalyzeServiceReferencesPass(),
new InlineServiceDefinitionsPass(),
@ -103,8 +103,7 @@ class PassConfig
throw new InvalidArgumentException(sprintf('Invalid type "%s".', $type));
}
$passes = &$this->$property;
$passes[] = $pass;
$this->{$property}[] = $pass;
}
/**

View File

@ -707,6 +707,21 @@ class ContainerBuilderTest extends \PHPUnit_Framework_TestCase
$this->assertEquals(array($second, $first), $configs);
}
public function testAbstractAlias()
{
$container = new ContainerBuilder();
$abstract = new Definition('AbstractClass');
$abstract->setAbstract(true);
$container->setDefinition('abstract_service', $abstract);
$container->setAlias('abstract_alias', 'abstract_service');
$container->compile();
$this->assertSame('abstract_service', (string) $container->getAlias('abstract_alias'));
}
public function testLazyLoadedService()
{
$loader = new ClosureLoader($container = new ContainerBuilder());

View File

@ -14,8 +14,12 @@
</service>
</argument>
<property name="p" type="service">
<service class="BazClass" />
<service class="BuzClass" />
</property>
</service>
<service id="bar" parent="foo" />
<service class="BizClass">
<tag name="biz_tag" />
</service>
</services>
</container>

View File

@ -121,7 +121,7 @@ class XmlFileLoaderTest extends \PHPUnit_Framework_TestCase
$loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
$loader->load('services5.xml');
$services = $container->getDefinitions();
$this->assertCount(4, $services, '->load() attributes unique ids to anonymous services');
$this->assertCount(6, $services, '->load() attributes unique ids to anonymous services');
// anonymous service as an argument
$args = $services['foo']->getArguments();
@ -130,6 +130,7 @@ class XmlFileLoaderTest extends \PHPUnit_Framework_TestCase
$this->assertTrue(isset($services[(string) $args[0]]), '->load() makes a reference to the created ones');
$inner = $services[(string) $args[0]];
$this->assertEquals('BarClass', $inner->getClass(), '->load() uses the same configuration as for the anonymous ones');
$this->assertFalse($inner->isPublic());
// inner anonymous services
$args = $inner->getArguments();
@ -138,6 +139,7 @@ class XmlFileLoaderTest extends \PHPUnit_Framework_TestCase
$this->assertTrue(isset($services[(string) $args[0]]), '->load() makes a reference to the created ones');
$inner = $services[(string) $args[0]];
$this->assertEquals('BazClass', $inner->getClass(), '->load() uses the same configuration as for the anonymous ones');
$this->assertFalse($inner->isPublic());
// anonymous service as a property
$properties = $services['foo']->getProperties();
@ -145,7 +147,25 @@ class XmlFileLoaderTest extends \PHPUnit_Framework_TestCase
$this->assertInstanceOf('Symfony\\Component\\DependencyInjection\\Reference', $property, '->load() converts anonymous services to references to "normal" services');
$this->assertTrue(isset($services[(string) $property]), '->load() makes a reference to the created ones');
$inner = $services[(string) $property];
$this->assertEquals('BazClass', $inner->getClass(), '->load() uses the same configuration as for the anonymous ones');
$this->assertEquals('BuzClass', $inner->getClass(), '->load() uses the same configuration as for the anonymous ones');
$this->assertFalse($inner->isPublic());
// "wild" service
$service = $container->findTaggedServiceIds('biz_tag');
$this->assertCount(1, $service);
foreach ($service as $id => $tag) {
$service = $container->getDefinition($id);
}
$this->assertEquals('BizClass', $service->getClass(), '->load() uses the same configuration as for the anonymous ones');
$this->assertFalse($service->isPublic());
// anonymous services are shared when using decoration definitions
$container->compile();
$services = $container->getDefinitions();
$fooArgs = $services['foo']->getArguments();
$barArgs = $services['bar']->getArguments();
$this->assertSame($fooArgs[0], $barArgs[0]);
}
public function testLoadServices()