[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
This commit is contained in:
Robin Chalas 2016-04-25 19:03:00 +02:00 committed by Fabien Potencier
parent 93938be691
commit 31285c22fa
2 changed files with 15 additions and 0 deletions

View File

@ -537,10 +537,15 @@ class Command
*
* @return mixed The helper value
*
* @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);
}

View File

@ -173,6 +173,16 @@ class CommandTest extends \PHPUnit_Framework_TestCase
$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();