Skip to content

Commit

Permalink
[DependencyInjection] fixed setting a synthetic service on a frozen c…
Browse files Browse the repository at this point in the history
…ontainer
  • Loading branch information
fabpot committed Dec 28, 2012
1 parent 32ad6cc commit d808dfb
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
5 changes: 4 additions & 1 deletion ContainerBuilder.php
Expand Up @@ -311,7 +311,10 @@ public function getScopeChildren()
public function set($id, $service, $scope = self::SCOPE_CONTAINER)
{
if ($this->isFrozen()) {
throw new BadMethodCallException('Setting service on a frozen container is not allowed');
// setting a synthetic service on a frozen container is alright
if (!isset($this->definitions[$id]) || !$this->definitions[$id]->isSynthetic()) {
throw new BadMethodCallException('Setting service on a frozen container is not allowed');
}
}

$id = strtolower($id);
Expand Down
30 changes: 30 additions & 0 deletions Tests/ContainerBuilderTest.php
Expand Up @@ -531,10 +531,40 @@ public function testThrowsExceptionWhenSetServiceOnAFrozenContainer()
}

$container = new ContainerBuilder();
$container->setDefinition('a', new Definition('stdClass'));
$container->compile();
$container->set('a', new \stdClass());
}

/**
* @expectedException BadMethodCallException
*/
public function testThrowsExceptionWhenAddServiceOnAFrozenContainer()
{
if (!class_exists('Symfony\Component\Config\Resource\FileResource')) {
$this->markTestSkipped('The "Config" component is not available');
}

$container = new ContainerBuilder();
$container->compile();
$container->set('a', new \stdClass());
}

public function testNoExceptionWhenSetSyntheticServiceOnAFrozenContainer()
{
if (!class_exists('Symfony\Component\Config\Resource\FileResource')) {
$this->markTestSkipped('The "Config" component is not available');
}

$container = new ContainerBuilder();
$def = new Definition('stdClass');
$def->setSynthetic(true);
$container->setDefinition('a', $def);
$container->compile();
$container->set('a', $a = new \stdClass());
$this->assertEquals($a, $container->get('a'));
}

/**
* @expectedException BadMethodCallException
*/
Expand Down

0 comments on commit d808dfb

Please sign in to comment.