From 5c317b77e86981dca4fc3d581e7d1946b370594a Mon Sep 17 00:00:00 2001 From: Tobias Schultze Date: Fri, 17 May 2013 23:54:02 +0200 Subject: [PATCH] [Console] fix and refactor exit code handling --- src/Symfony/Component/Console/Application.php | 4 ++-- src/Symfony/Component/Console/Command/Command.php | 2 +- .../Component/Console/Tests/ApplicationTest.php | 15 +++++++++++++++ .../Console/Tests/Command/CommandTest.php | 14 ++++++++++++++ 4 files changed, 32 insertions(+), 3 deletions(-) diff --git a/src/Symfony/Component/Console/Application.php b/src/Symfony/Component/Console/Application.php index 7f7da75858..81bc1bf104 100644 --- a/src/Symfony/Component/Console/Application.php +++ b/src/Symfony/Component/Console/Application.php @@ -115,7 +115,7 @@ class Application } $statusCode = $e->getCode(); - $statusCode = is_numeric($statusCode) && $statusCode ? $statusCode : 1; + $statusCode = $statusCode ? (is_numeric($statusCode) ? (int) $statusCode : 1) : 0; } if ($this->autoExit) { @@ -192,7 +192,7 @@ class Application $statusCode = $command->run($input, $output); $this->runningCommand = null; - return is_numeric($statusCode) ? $statusCode : 0; + return $statusCode; } /** diff --git a/src/Symfony/Component/Console/Command/Command.php b/src/Symfony/Component/Console/Command/Command.php index 6c54d99d28..a83ccc0041 100644 --- a/src/Symfony/Component/Console/Command/Command.php +++ b/src/Symfony/Component/Console/Command/Command.php @@ -238,7 +238,7 @@ class Command $statusCode = $this->execute($input, $output); } - return is_numeric($statusCode) ? $statusCode : 0; + return is_numeric($statusCode) ? (int) $statusCode : 0; } /** diff --git a/src/Symfony/Component/Console/Tests/ApplicationTest.php b/src/Symfony/Component/Console/Tests/ApplicationTest.php index bb4e11e6a2..8f5bce956c 100644 --- a/src/Symfony/Component/Console/Tests/ApplicationTest.php +++ b/src/Symfony/Component/Console/Tests/ApplicationTest.php @@ -501,6 +501,21 @@ class ApplicationTest extends \PHPUnit_Framework_TestCase $this->assertSame('called'.PHP_EOL, $tester->getDisplay(), '->run() does not call interact() if -n is passed'); } + public function testRunReturnsIntegerExitCode() + { + $exception = new \Exception('', 4); + + $application = $this->getMock('Symfony\Component\Console\Application', array('doRun')); + $application->setAutoExit(false); + $application->expects($this->once()) + ->method('doRun') + ->will($this->throwException($exception)); + + $exitCode = $application->run(new ArrayInput(array()), new NullOutput()); + + $this->assertSame(4, $exitCode, '->run() returns integer exit code extracted from raised exception'); + } + /** * @expectedException \LogicException * @dataProvider getAddingAlreadySetDefinitionElementData diff --git a/src/Symfony/Component/Console/Tests/Command/CommandTest.php b/src/Symfony/Component/Console/Tests/Command/CommandTest.php index eb78b8911e..df29c821dd 100644 --- a/src/Symfony/Component/Console/Tests/Command/CommandTest.php +++ b/src/Symfony/Component/Console/Tests/Command/CommandTest.php @@ -221,6 +221,20 @@ class CommandTest extends \PHPUnit_Framework_TestCase } } + public function testRunReturnsIntegerExitCode() + { + $command = new \TestCommand(); + $exitCode = $command->run(new StringInput(''), new NullOutput()); + $this->assertSame(0, $exitCode, '->run() returns integer exit code (treats null as 0)'); + + $command = $this->getMock('TestCommand', array('execute')); + $command->expects($this->once()) + ->method('execute') + ->will($this->returnValue('2.3')); + $exitCode = $command->run(new StringInput(''), new NullOutput()); + $this->assertSame(2, $exitCode, '->run() returns integer exit code (casts numeric to int)'); + } + public function testRunReturnsAlwaysInteger() { $command = new \TestCommand();