[DependencyInjection] Fix #16461 Let Container::set() replace existing aliases

`Container::set()` now overrides any previously alias defined with the same name.
This commit is contained in:
Matthieu Napoli 2016-02-09 20:58:01 +01:00 committed by Fabien Potencier
parent 92d291a17c
commit be85d16940
3 changed files with 22 additions and 0 deletions

View File

@ -200,6 +200,10 @@ class Container implements IntrospectableContainerInterface
$this->scopedServices[$scope][$id] = $service;
}
if (isset($this->aliases[$id])) {
unset($this->aliases[$id]);
}
$this->services[$id] = $service;
if (method_exists($this, $method = 'synchronize'.strtr($id, $this->underscoreMap).'Service')) {

View File

@ -217,6 +217,16 @@ class ContainerBuilderTest extends \PHPUnit_Framework_TestCase
$this->assertTrue(isset($aliases['foobar']));
}
public function testSetReplacesAlias()
{
$builder = new ContainerBuilder();
$builder->setAlias('alias', 'aliased');
$builder->set('aliased', new \stdClass());
$builder->set('alias', $foo = new \stdClass());
$this->assertSame($foo, $builder->get('alias'), '->set() replaces an existing alias');
}
public function testAddGetCompilerPass()
{
$builder = new ContainerBuilder();

View File

@ -152,6 +152,14 @@ class ContainerTest extends \PHPUnit_Framework_TestCase
$this->assertSame($foo, $services['foo']['foo']);
}
public function testSetReplacesAlias()
{
$c = new ProjectServiceContainer();
$c->set('alias', $foo = new \stdClass());
$this->assertSame($foo, $c->get('alias'), '->set() replaces an existing alias');
}
public function testGet()
{
$sc = new ProjectServiceContainer();