Permalink
Browse files

minor #27443 [DX] Improve exception message when AbstractController::…

…getParameter fails (curry684)

This PR was squashed before being merged into the 4.1 branch (closes #27443).

Discussion
----------

[DX] Improve exception message when AbstractController::getParameter fails

| Q             | A
| ------------- | ---
| Branch?       | 4.1
| Bug fix?      | no (DX)
| New feature?  | no
| BC breaks?    | no     <!-- see https://symfony.com/bc -->
| Deprecations? | no
| Tests pass?   | yes    <!-- please add some, will be required by reviewers -->
| Fixed tickets | #27436
| License       | MIT
| Doc PR        | symfony/symfony-docs#... <!-- required for new features -->

Improve exception message for situations where the `parameter_bag` is not present in `AbstractController`. Also fixed the exception to the correct type.

Commits
-------

a8f4128 [DX] Improve exception message when AbstractController::getParameter fails
  • Loading branch information...
chalasr committed Jun 4, 2018
2 parents 582165f + a8f4128 commit 9660103ff1141e05591f5064382c4efece491736
@@ -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);
@@ -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

0 comments on commit 9660103

Please sign in to comment.