8000 bug #18635 [Console] Prevent fatal error when calling Command::getHel… · symfony/symfony@f999e77 · GitHub
[go: up one dir, main page]

Skip to content

Commit f999e77

Browse files
committed
bug #18635 [Console] Prevent fatal error when calling Command::getHelper 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
2 parents 93938be + 31285c2 commit f999e77

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

src/Symfony/Component/Console/Command/Command.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,10 +537,15 @@ public function getSynopsis()
537537
*
538538
* @return mixed The helper value
539539
*
540+
* @throws \LogicException if no HelperSet is defined
540541
* @throws \InvalidArgumentException if the helper is not defined
541542
*/
542543
public function getHelper($name)
543544
{
545+
if (null === $this->helperSet) {
546+
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));
547+
}
548+
544549
return $this->helperSet->get($name);
545550
}
546551

src/Symfony/Component/Console/Tests/Command/CommandTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,16 @@ public function testGetHelper()
173173
$this->assertEquals($formatterHelper->getName(), $command->getHelper('formatter')->getName(), '->getHelper() returns the correct helper');
174174
}
175175

176+
/**
177+
* @expectedException \LogicException
178+
* @expectedExceptionMessage Cannot retrieve helper "formatter" because there is no HelperSet defined.
179+
*/
180+
public function testGetHelperWithoutHelperSet()
181+
{
182+
$command = new \TestCommand();
183+
$command->getHelper('formatter');
184+
}
185+
176186
public function testMergeApplicationDefinition()
177187
{
178188
$application1 = new Application();

0 commit comments

Comments
 (0)
0