merged branch szicsu/ContainerExceptionHandling-FIX (PR #5693)

This PR was merged into the 2.1 branch.

Commits
-------

9d8f689 UnitTest fix
02b0b39 UnitTest fix
a4f3ea9 [2.1][DependencyInjection] Incomplete error handling in the container

Discussion
----------

[2.1][DependencyInjection] Incomplete error handling in the container

Bug fix: yes
Feature addition: no
Backwards compatibility break: no
Symfony2 tests pass: yes
Fixes the following tickets: ~
Todo: ~
License of the code: MIT
Documentation PR: ~

The Container::get method, error handling has been handled incompletely because the created wrong service was not removed from the container.

---------------------------------------------------------------------------

by stof at 2012-10-13T23:12:11Z

@fabpot anything missig in this PR ? It looks ready to be merged to me.
This commit is contained in:
Fabien Potencier 2012-10-14 11:16:36 +02:00
commit 77a6eb6a62
2 changed files with 34 additions and 1 deletions

View File

@ -258,6 +258,11 @@ class Container implements IntrospectableContainerInterface
$service = $this->$method();
} catch (\Exception $e) {
unset($this->loading[$id]);
if (isset($this->services[$id])) {
unset($this->services[$id]);
}
throw $e;
}

View File

@ -98,7 +98,7 @@ class ContainerTest extends \PHPUnit_Framework_TestCase
$this->assertEquals(array('service_container', 'foo', 'bar'), $sc->getServiceIds(), '->getServiceIds() returns all defined service ids');
$sc = new ProjectServiceContainer();
$this->assertEquals(array('scoped', 'scoped_foo', 'bar', 'foo_bar', 'foo.baz', 'circular', 'throw_exception', 'service_container'), $sc->getServiceIds(), '->getServiceIds() returns defined service ids by getXXXService() methods');
$this->assertEquals(array('scoped', 'scoped_foo', 'bar', 'foo_bar', 'foo.baz', 'circular', 'throw_exception', 'throws_exception_on_service_configuration', 'service_container'), $sc->getServiceIds(), '->getServiceIds() returns defined service ids by getXXXService() methods');
}
/**
@ -367,6 +367,27 @@ class ContainerTest extends \PHPUnit_Framework_TestCase
}
}
public function testGetThrowsExceptionOnServiceConfiguration()
{
$c = new ProjectServiceContainer();
try {
$c->get('throws_exception_on_service_configuration');
$this->fail('The container can not contain invalid service!');
} catch (\Exception $e) {
$this->assertEquals('Something was terribly wrong while trying to configure the service!', $e->getMessage());
}
$this->assertFalse($c->initialized('throws_exception_on_service_configuration'));
try {
$c->get('throws_exception_on_service_configuration');
$this->fail('The container can not contain invalid service!');
} catch (\Exception $e) {
$this->assertEquals('Something was terribly wrong while trying to configure the service!', $e->getMessage());
}
$this->assertFalse($c->initialized('throws_exception_on_service_configuration'));
}
public function getInvalidParentScopes()
{
return array(
@ -447,4 +468,11 @@ class ProjectServiceContainer extends Container
{
throw new \Exception('Something went terribly wrong!');
}
protected function getThrowsExceptionOnServiceConfigurationService()
{
$this->services['throws_exception_on_service_configuration'] = $instance = new \stdClass();
throw new \Exception('Something was terribly wrong while trying to configure the service!');
}
}