8000 [DX] Improve exception message when AbstractController::getParameter fails by curry684 · Pull Request #27443 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
0