8000 minor #27443 [DX] Improve exception message when AbstractController::… · symfony/symfony@9660103 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9660103

Browse files
author
Robin Chalas
committed
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
2 parents 582165f + a8f4128 commit 9660103

File tree

2 files changed

+21
-8
lines changed

2 files changed

+21
-8
lines changed

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

Lines changed: 2 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,7 @@ 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(), 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)));
6869
}
6970

7071
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 TestAbstractController::getParameter()" method is missing a parameter bag
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