Skip to content

Commit

Permalink
bug #18635 [Console] Prevent fatal error when calling Command::getHel…
Browse files Browse the repository at this point in the history
…per without helperSet (chalasr)

This PR was submitted for the master branch but it was merged into the 2.3 branch instead (closes #18635).

Discussion
----------

[Console] Prevent fatal error when calling Command::getHelper without helperSet

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #18619
| License       | MIT
| Doc PR        | n/a

Patch attached to #18619

Commits
-------

31285c2 [Console][#18619] Prevent fatal error when calling Command#getHelper() without helperSet
  • Loading branch information
fabpot committed May 13, 2016
2 parents 93938be + 31285c2 commit f999e77
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/Symfony/Component/Console/Command/Command.php
Expand Up @@ -537,10 +537,15 @@ public function getSynopsis()
*
* @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);
}

Expand Down
10 changes: 10 additions & 0 deletions src/Symfony/Component/Console/Tests/Command/CommandTest.php
Expand Up @@ -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();
Expand Down

0 comments on commit f999e77

Please sign in to comment.