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

This commit is contained in:
Tamas Szijarto 2012-10-07 20:46:50 +02:00
parent f152170899
commit a4f3ea970a
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,25 @@ 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());
}
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());
}
}
public function getInvalidParentScopes()
{
return array(
@ -447,4 +466,13 @@ 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!');
return $instance;
}
}