From ede4d92713c2ece81226bc9fb8a9deaa5fffbde6 Mon Sep 17 00:00:00 2001 From: Robin Chalas Date: Mon, 25 Apr 2016 19:03:00 +0200 Subject: [PATCH] [Console][#18619] Prevent fatal error when calling Command#getHelper() without helperSet Use Command::setHelperSet rather than Command#setHelperSet in exception msg Simplify exception message Add DidYouForget to exception msg --- src/Symfony/Component/Console/Command/Command.php | 7 ++++++- .../Component/Console/Tests/Command/CommandTest.php | 10 ++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/Console/Command/Command.php b/src/Symfony/Component/Console/Command/Command.php index c8429845a2634..915999e893291 100644 --- a/src/Symfony/Component/Console/Command/Command.php +++ b/src/Symfony/Component/Console/Command/Command.php @@ -601,10 +601,15 @@ public function getUsages() * * @return mixed The helper value * - * @throws InvalidArgumentException if the helper is not defined + * @throws LogicException If no HelperSet is defined + * @throws InvalidArgumentException If the helper is not defined */ public function getHelper($name) { + if (null === $this->helperSet) { + throw new LogicException(sprintf('Cannot retrieve helper "%s" because there is no HelperSet defined. Did you forget to add your command to the application or to set the application on the command using the setApplication() method? You can also set the HelperSet directly using the setHelperSet() method.', $name)); + } + return $this->helperSet->get($name); } diff --git a/src/Symfony/Component/Console/Tests/Command/CommandTest.php b/src/Symfony/Component/Console/Tests/Command/CommandTest.php index c39adab4943fb..53a6009e6c1eb 100644 --- a/src/Symfony/Component/Console/Tests/Command/CommandTest.php +++ b/src/Symfony/Component/Console/Tests/Command/CommandTest.php @@ -173,6 +173,16 @@ public function testGetHelper() $this->assertEquals($formatterHelper->getName(), $command->getHelper('formatter')->getName(), '->getHelper() returns the correct helper'); } + /** + * @expectedException \LogicException + * @expectedExceptionMessage Cannot retrieve helper "formatter" because there is no HelperSet defined. + */ + public function testGetHelperWithoutHelperSet() + { + $command = new \TestCommand(); + $command->getHelper('formatter'); + } + public function testMergeApplicationDefinition() { $application1 = new Application();