Rework to throw exception if status code is not initialized; add tests

This commit is contained in:
Alessandro Lai 2020-06-12 15:57:15 +02:00
parent 8d27e453b4
commit 876c64e52e
No known key found for this signature in database
GPG Key ID: 5D9C513BE4F5798D
2 changed files with 28 additions and 0 deletions

View File

@ -29,6 +29,8 @@ trait TesterTrait
/**
* Gets the display returned by the last execution of the command or application.
*
* @throws \RuntimeException If it's called before the execute method
*
* @return string The display
*/
public function getDisplay(bool $normalize = false)
@ -95,10 +97,16 @@ trait TesterTrait
/**
* Gets the status code returned by the last execution of the command or application.
*
* @throws \RuntimeException If it's called before the execute method
*
* @return int The status code
*/
public function getStatusCode()
{
if (null === $this->statusCode) {
throw new \RuntimeException('Status code not initialized, did you execute the command before requesting the status code?');
}
return $this->statusCode;
}

View File

@ -67,11 +67,31 @@ class CommandTesterTest extends TestCase
$this->assertEquals('foo'.PHP_EOL, $this->tester->getDisplay(), '->getDisplay() returns the display of the last execution');
}
public function testGetDisplayWithoutCallingExecuteBefore()
{
$tester = new CommandTester(new Command());
$this->expectException(\RuntimeException::class);
$this->expectExceptionMessage('Output not initialized');
$tester->getDisplay();
}
public function testGetStatusCode()
{
$this->assertSame(0, $this->tester->getStatusCode(), '->getStatusCode() returns the status code');
}
public function testGetStatusCodeWithoutCallingExecuteBefore()
{
$tester = new CommandTester(new Command());
$this->expectException(\RuntimeException::class);
$this->expectExceptionMessage('Status code not initialized');
$tester->getStatusCode();
}
public function testCommandFromApplication()
{
$application = new Application();