From a8f41289c4b8b189a162d3c87987440b66beacf6 Mon Sep 17 00:00:00 2001 From: Niels Keurentjes Date: Thu, 31 May 2018 12:02:59 +0200 Subject: [PATCH] [DX] Improve exception message when AbstractController::getParameter fails --- .../Controller/AbstractController.php | 3 ++- .../Controller/AbstractControllerTest.php | 26 ++++++++++++++----- 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/src/Symfony/Bundle/FrameworkBundle/Controller/AbstractController.php b/src/Symfony/Bundle/FrameworkBundle/Controller/AbstractController.php index b317786c6858..7286162c2e5a 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Controller/AbstractController.php +++ b/src/Symfony/Bundle/FrameworkBundle/Controller/AbstractController.php @@ -13,6 +13,7 @@ use Psr\Container\ContainerInterface; use Doctrine\Common\Persistence\ManagerRegistry; +use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException; use Symfony\Component\DependencyInjection\ParameterBag\ContainerBagInterface; use Symfony\Component\DependencyInjection\ServiceSubscriberInterface; use Symfony\Component\Form\FormFactoryInterface; @@ -64,7 +65,7 @@ public function setContainer(ContainerInterface $container) protected function getParameter(string $name) { if (!$this->container->has('parameter_bag')) { - throw new \LogicException('The "parameter_bag" service is not available. Try running "composer require dependency-injection:^4.1"'); + throw new ServiceNotFoundException('parameter_bag', null, null, array(), sprintf('The "%s::getParameter()" method is missing a parameter bag to work properly. Did you forget to register your controller as a service subscriber? This can be fixed either by using autoconfiguration or by manually wiring a "parameter_bag" in the service locator passed to the controller.', get_class($this))); } return $this->container->get('parameter_bag')->get($name); diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Controller/AbstractControllerTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Controller/AbstractControllerTest.php index 0ae42c14ce31..03b451528d48 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Controller/AbstractControllerTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Controller/AbstractControllerTest.php @@ -52,20 +52,32 @@ public function testSubscribedServices() public function testGetParameter() { + if (!class_exists(ContainerBag::class)) { + $this->markTestSkipped('ContainerBag class does not exist'); + } + $container = new Container(new FrozenParameterBag(array('foo' => 'bar'))); + $container->set('parameter_bag', new ContainerBag($container)); $controller = $this->createController(); $controller->setContainer($container); - if (!class_exists(ContainerBag::class)) { - $this->expectException(\LogicException::class); - $this->expectExceptionMessage('The "parameter_bag" service is not available. Try running "composer require dependency-injection:^4.1"'); - } else { - $container->set('parameter_bag', new ContainerBag($container)); - } - $this->assertSame('bar', $controller->getParameter('foo')); } + + /** + * @expectedException \Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException + * @expectedExceptionMessage TestAbstractController::getParameter()" method is missing a parameter bag + */ + public function testMissingParameterBag() + { + $container = new Container(); + + $controller = $this->createController(); + $controller->setContainer($container); + + $controller->getParameter('foo'); + } } class TestAbstractController extends AbstractController