8000 Improve exception message when AbstractController::getParameter fails · symfony/symfony@a39bed8 · GitHub
[go: up one dir, main page]

Skip to content

Commit a39bed8

Browse files
committed
Improve exception message when AbstractController::getParameter fails
Fixes #27436
1 parent 3d3d5c4 commit a39bed8

File tree

2 files changed

+23
-8
lines changed

2 files changed

+23
-8
lines changed

src/Symfony/Bundle/FrameworkBundle/Controller/AbstractController.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Psr\Container\ContainerInterface;
1515
use Doctrine\Common\Persistence\ManagerRegistry;
16+
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
1617
use Symfony\Component\DependencyInjection\ParameterBag\ContainerBagInterface;
1718
use Symfony\Component\DependencyInjection\ServiceSubscriberInterface;
1819
use Symfony\Component\Form\FormFactoryInterface;
@@ -64,7 +65,9 @@ public function setContainer(ContainerInterface $container)
6465
protected function getParameter(string $name)
6566
{
6667
if (!$this->container->has('parameter_bag')) {
67-
throw new \LogicException('The "parameter_bag" service is not available. Try running "composer require dependency-injection:^4.1"');
68+
throw new ServiceNotFoundException('parameter_bag', null, null, array(),
69+
'The "parameter_bag" service could not be located. Ensure that symfony/dependency-injection 4.1.0 or higher '.
70+
'is installed and that the controller is either set to be autoconfigured or wired manually to inject it');
6871
}
6972

7073
return $this->container->get('parameter_bag')->get($name);

src/Symfony/Bundle/FrameworkBundle/Tests/Controller/AbstractControllerTest.php

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,20 +52,32 @@ public function testSubscribedServices()
5252

5353
public function testGetParameter()
5454
{
55+
if (!class_exists(ContainerBag::class)) {
56+
$this->markTestSkipped('ContainerBag class does not exist');
57+
}
58+
5559
$container = new Container(new FrozenParameterBag(array('foo' => 'bar')));
60+
$container->set('parameter_bag', new ContainerBag($container));
5661

5762
$controller = $this->createController();
5863
$controller->setContainer($container);
5964

60-
if (!class_exists(ContainerBag::class)) {
61-
$this->expectException(\LogicException::class);
62-
$this->expectExceptionMessage('The "parameter_bag" service is not available. Try running "composer require dependency-injection:^4.1"');
63-
} else {
64-
$container->set('parameter_bag', new ContainerBag($container));
65-
}
66-
6765
$this->assertSame('bar', $controller->getParameter('foo'));
6866
}
67+
68+
/**
69+
* @expectedException \Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException
70+
* @expectedExceptionMessage The "parameter_bag" service could not be located
71+
*/
72+
public function testMissingParameterBag()
73+
{
74+
$container = new Container();
75+
76+
$controller = $this->createController();
77+
$controller->setContainer($container);
78+
79+
$controller->getParameter('foo');
80+
}
6981
}
7082

7183
class TestAbstractController extends AbstractController

0 commit comments

Comments
 (0)
0