Allow to get alternatives when ServiceNotFoundException occurs.
This commit is contained in:
parent
8c4a1e75be
commit
4993b1c14c
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -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()
|
||||
|
Reference in New Issue
Block a user