Skip to content

Commit

Permalink
bug #18467 [DependencyInjection] Resolve aliases before removing abst…
Browse files Browse the repository at this point in the history
…ract 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
  • Loading branch information
fabpot committed Apr 7, 2016
2 parents abf4f67 + 9802a41 commit 004a667
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 6 deletions.
Expand Up @@ -59,8 +59,8 @@ public function __construct()

$this->removingPasses = array(
new RemovePrivateAliasesPass(),
new RemoveAbstractDefinitionsPass(),
new ReplaceAliasByActualDefinitionPass(),
new RemoveAbstractDefinitionsPass(),
new RepeatedPass(array(
new AnalyzeServiceReferencesPass(),
new InlineServiceDefinitionsPass(),
Expand Down Expand Up @@ -103,8 +103,7 @@ public function addPass(CompilerPassInterface $pass, $type = self::TYPE_BEFORE_O
throw new InvalidArgumentException(sprintf('Invalid type "%s".', $type));
}

$passes = &$this->$property;
$passes[] = $pass;
$this->{$property}[] = $pass;
}

/**
Expand Down
Expand Up @@ -707,6 +707,21 @@ public function testExtensionConfig()
$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());
Expand Down
Expand Up @@ -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>
Expand Up @@ -121,7 +121,7 @@ public function testLoadAnonymousServices()
$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();
Expand All @@ -130,6 +130,7 @@ public function testLoadAnonymousServices()
$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();
Expand All @@ -138,14 +139,33 @@ public function testLoadAnonymousServices()
$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();
$property = $properties['p'];
$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()
Expand Down

0 comments on commit 004a667

Please sign in to comment.