diff --git a/src/Symfony/Component/DependencyInjection/Exception/ServiceNotFoundException.php b/src/Symfony/Component/DependencyInjection/Exception/ServiceNotFoundException.php index 0194c4f372..5de10d3bcb 100644 --- a/src/Symfony/Component/DependencyInjection/Exception/ServiceNotFoundException.php +++ b/src/Symfony/Component/DependencyInjection/Exception/ServiceNotFoundException.php @@ -22,6 +22,7 @@ class ServiceNotFoundException extends InvalidArgumentException implements NotFo { private $id; private $sourceId; + private $alternatives; public function __construct($id, $sourceId = null, \Exception $previous = null, array $alternatives = array()) { @@ -44,6 +45,7 @@ class ServiceNotFoundException extends InvalidArgumentException implements NotFo $this->id = $id; $this->sourceId = $sourceId; + $this->alternatives = $alternatives; } public function getId() @@ -55,4 +57,9 @@ class ServiceNotFoundException extends InvalidArgumentException implements NotFo { return $this->sourceId; } + + public function getAlternatives() + { + return $this->alternatives; + } } diff --git a/src/Symfony/Component/DependencyInjection/Tests/ServiceLocatorTest.php b/src/Symfony/Component/DependencyInjection/Tests/ServiceLocatorTest.php index 6900fd7ea4..a1e2fff50f 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/ServiceLocatorTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/ServiceLocatorTest.php @@ -12,6 +12,7 @@ namespace Symfony\Component\DependencyInjection\Tests; use PHPUnit\Framework\TestCase; +use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException; use Symfony\Component\DependencyInjection\ServiceLocator; class ServiceLocatorTest extends TestCase @@ -58,7 +59,7 @@ class ServiceLocatorTest extends TestCase /** * @expectedException \Psr\Container\NotFoundExceptionInterface - * @expectedExceptionMessage You have requested a non-existent service "dummy" + * @expectedExceptionMessage You have requested a non-existent service "dummy". Did you mean one of these: "foo", "bar"? */ public function testGetThrowsOnUndefinedService() { @@ -67,7 +68,13 @@ class ServiceLocatorTest extends TestCase 'bar' => function () { return 'baz'; }, )); - $locator->get('dummy'); + try { + $locator->get('dummy'); + } catch (ServiceNotFoundException $e) { + $this->assertSame(array('foo', 'bar'), $e->getAlternatives()); + + throw $e; + } } public function testInvoke()