Skip to content

Commit d5aa77e

Browse files
committed
feature #37241 [Console] Fix Docblock for CommandTester::getExitCode (Jean85)
This PR was merged into the 5.2-dev branch. Discussion ---------- [Console] Fix Docblock for CommandTester::getExitCode | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | no | Deprecations? | no | Tickets | - | License | MIT While doing static analysis on my code, I found a false positive that led me to this little fix. Commits ------- 876c64e Rework to throw exception if status code is not initialized; add tests
2 parents 8d27e45 + 876c64e commit d5aa77e

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

src/Symfony/Component/Console/Tester/TesterTrait.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ trait TesterTrait
2929
/**
3030
* Gets the display returned by the last execution of the command or application.
3131
*
32+
* @throws \RuntimeException If it's called before the execute method
33+
*
3234
* @return string The display
3335
*/
3436
public function getDisplay(bool $normalize = false)
@@ -95,10 +97,16 @@ public function getOutput()
9597
/**
9698
* Gets the status code returned by the last execution of the command or application.
9799
*
100+
* @throws \RuntimeException If it's called before the execute method
101+
*
98102
* @return int The status code
99103
*/
100104
public function getStatusCode()
101105
{
106+
if (null === $this->statusCode) {
107+
throw new \RuntimeException('Status code not initialized, did you execute the command before requesting the status code?');
108+
}
109+
102110
return $this->statusCode;
103111
}
104112

src/Symfony/Component/Console/Tests/Tester/CommandTesterTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,31 @@ public function testGetDisplay()
6767
$this->assertEquals('foo'.PHP_EOL, $this->tester->getDisplay(), '->getDisplay() returns the display of the last execution');
6868
}
6969

70+
public function testGetDisplayWithoutCallingExecuteBefore()
71+
{
72+
$tester = new CommandTester(new Command());
73+
74+
$this->expectException(\RuntimeException::class);
75+
$this->expectExceptionMessage('Output not initialized');
76+
77+
$tester->getDisplay();
78+
}
79+
7080
public function testGetStatusCode()
7181
{
7282
$this->assertSame(0, $this->tester->getStatusCode(), '->getStatusCode() returns the status code');
7383
}
7484

85+
public function testGetStatusCodeWithoutCallingExecuteBefore()
86+
{
87+
$tester = new CommandTester(new Command());
88+
89+
$this->expectException(\RuntimeException::class);
90+
$this->expectExceptionMessage('Status code not initialized');
91+
92+
$tester->getStatusCode();
93+
}
94+
7595
public function testCommandFromApplication()
7696
{
7797
$application = new Application();

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy