Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c47ffc3

Browse files
committed
Allow to get alternatives when ServiceNotFoundException occurs.
1 parent 8c4a1e7 commit c47ffc3

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

src/Symfony/Component/DependencyInjection/Exception/ServiceNotFoundException.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class ServiceNotFoundException extends InvalidArgumentException implements NotFo
2222
{
2323
private $id;
2424
private $sourceId;
25+
private $alternatives;
2526

2627
public function __construct($id, $sourceId = null, \Exception $previous = null, array $alternatives = array())
2728
{
@@ -44,6 +45,7 @@ public function __construct($id, $sourceId = null, \Exception $previous = null,
4445

4546
$this->id = $id;
4647
$this->sourceId = $sourceId;
48+
$this->alternatives = $alternatives;
4749
}
4850

4951
public function getId()
@@ -55,4 +57,9 @@ public function getSourceId()
5557
{
5658
return $this->sourceId;
5759
}
60+
61+
public function getAlternatives()
62+
{
63+
return $this->alternatives;
64+
}
5865
}

src/Symfony/Component/DependencyInjection/Tests/ServiceLocatorTest.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\DependencyInjection\Tests;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
1516
use Symfony\Component\DependencyInjection\ServiceLocator;
1617

1718
class ServiceLocatorTest extends TestCase
@@ -58,7 +59,7 @@ public function testGetDoesNotMemoize()
5859

5960
/**
6061
* @expectedException \Psr\Container\NotFoundExceptionInterface
61-
* @expectedExceptionMessage You have requested a non-existent service "dummy"
62+
* @expectedExceptionMessage You have requested a non-existent service "dummy". Did you mean one of these: "foo", "bar"?
6263
*/
6364
public function testGetThrowsOnUndefinedService()
6465
{
@@ -67,7 +68,13 @@ public function testGetThrowsOnUndefinedService()
6768
'bar' => function () { return 'baz'; },
6869
));
6970

70-
$locator->get('dummy');
71+
try {
72+
$locator->get('dummy');
73+
} catch (ServiceNotFoundException $e) {
74+
$this->assertSame(['foo', 'bar'], $e->getAlternatives());
75+
76+
throw $e;
77+
}
7178
}
7279

7380
public function testInvoke()

0 commit comments

Comments
 (0)