[DependencyInjection] fixed exceptions thrown by get method of ContainerBuilder

This commit is contained in:
Łukasz Makuch 2016-02-07 19:10:24 +01:00 committed by Fabien Potencier
parent 01b562b2cd
commit aecb0fae3a
2 changed files with 18 additions and 13 deletions

View File

@ -19,6 +19,8 @@ use Symfony\Component\DependencyInjection\Exception\InactiveScopeException;
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException; use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
use Symfony\Component\DependencyInjection\Exception\LogicException; use Symfony\Component\DependencyInjection\Exception\LogicException;
use Symfony\Component\DependencyInjection\Exception\RuntimeException; use Symfony\Component\DependencyInjection\Exception\RuntimeException;
use Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException;
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
use Symfony\Component\DependencyInjection\Extension\ExtensionInterface; use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;
use Symfony\Component\Config\Resource\FileResource; use Symfony\Component\Config\Resource\FileResource;
use Symfony\Component\Config\Resource\ResourceInterface; use Symfony\Component\Config\Resource\ResourceInterface;
@ -415,9 +417,9 @@ class ContainerBuilder extends Container implements TaggedContainerInterface
* *
* @return object The associated service * @return object The associated service
* *
* @throws InvalidArgumentException when no definitions are available * @throws InvalidArgumentException when no definitions are available
* @throws InactiveScopeException when the current scope is not active * @throws ServiceCircularReferenceException When a circular reference is detected
* @throws LogicException when a circular dependency is detected * @throws ServiceNotFoundException When the service is not defined
* @throws \Exception * @throws \Exception
* *
* @see Reference * @see Reference
@ -440,7 +442,8 @@ class ContainerBuilder extends Container implements TaggedContainerInterface
try { try {
$definition = $this->getDefinition($id); $definition = $this->getDefinition($id);
} catch (InvalidArgumentException $e) { } catch (ServiceNotFoundException $e) {
if (ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE !== $invalidBehavior) { if (ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE !== $invalidBehavior) {
return; return;
} }
@ -790,14 +793,14 @@ class ContainerBuilder extends Container implements TaggedContainerInterface
* *
* @return Definition A Definition instance * @return Definition A Definition instance
* *
* @throws InvalidArgumentException if the service definition does not exist * @throws ServiceNotFoundException if the service definition does not exist
*/ */
public function getDefinition($id) public function getDefinition($id)
{ {
$id = strtolower($id); $id = strtolower($id);
if (!array_key_exists($id, $this->definitions)) { if (!array_key_exists($id, $this->definitions)) {
throw new InvalidArgumentException(sprintf('The service definition "%s" does not exist.', $id)); throw new ServiceNotFoundException($id);
} }
return $this->definitions[$id]; return $this->definitions[$id];
@ -812,7 +815,7 @@ class ContainerBuilder extends Container implements TaggedContainerInterface
* *
* @return Definition A Definition instance * @return Definition A Definition instance
* *
* @throws InvalidArgumentException if the service definition does not exist * @throws ServiceNotFoundException if the service definition does not exist
*/ */
public function findDefinition($id) public function findDefinition($id)
{ {

View File

@ -21,6 +21,8 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\Definition; use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Exception\RuntimeException; use Symfony\Component\DependencyInjection\Exception\RuntimeException;
use Symfony\Component\DependencyInjection\Exception\InactiveScopeException; use Symfony\Component\DependencyInjection\Exception\InactiveScopeException;
use Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException;
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
use Symfony\Component\DependencyInjection\Loader\ClosureLoader; use Symfony\Component\DependencyInjection\Loader\ClosureLoader;
use Symfony\Component\DependencyInjection\Reference; use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag; use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
@ -50,9 +52,9 @@ class ContainerBuilderTest extends \PHPUnit_Framework_TestCase
try { try {
$builder->getDefinition('baz'); $builder->getDefinition('baz');
$this->fail('->getDefinition() throws an InvalidArgumentException if the service definition does not exist'); $this->fail('->getDefinition() throws a ServiceNotFoundException if the service definition does not exist');
} catch (\InvalidArgumentException $e) { } catch (ServiceNotFoundException $e) {
$this->assertEquals('The service definition "baz" does not exist.', $e->getMessage(), '->getDefinition() throws an InvalidArgumentException if the service definition does not exist'); $this->assertEquals('You have requested a non-existent service "baz".', $e->getMessage(), '->getDefinition() throws a ServiceNotFoundException if the service definition does not exist');
} }
} }
@ -79,9 +81,9 @@ class ContainerBuilderTest extends \PHPUnit_Framework_TestCase
$builder = new ContainerBuilder(); $builder = new ContainerBuilder();
try { try {
$builder->get('foo'); $builder->get('foo');
$this->fail('->get() throws an InvalidArgumentException if the service does not exist'); $this->fail('->get() throws a ServiceNotFoundException if the service does not exist');
} catch (\InvalidArgumentException $e) { } catch (ServiceNotFoundException $e) {
$this->assertEquals('The service definition "foo" does not exist.', $e->getMessage(), '->get() throws an InvalidArgumentException if the service does not exist'); $this->assertEquals('You have requested a non-existent service "foo".', $e->getMessage(), '->get() throws a ServiceNotFoundException if the service does not exist');
} }
$this->assertNull($builder->get('foo', ContainerInterface::NULL_ON_INVALID_REFERENCE), '->get() returns null if the service does not exist and NULL_ON_INVALID_REFERENCE is passed as a second argument'); $this->assertNull($builder->get('foo', ContainerInterface::NULL_ON_INVALID_REFERENCE), '->get() returns null if the service does not exist and NULL_ON_INVALID_REFERENCE is passed as a second argument');