diff --git a/src/Symfony/Component/Console/Application.php b/src/Symfony/Component/Console/Application.php index e2ea78cb7c..5c232698e2 100644 --- a/src/Symfony/Component/Console/Application.php +++ b/src/Symfony/Component/Console/Application.php @@ -131,7 +131,7 @@ class Application } $exitCode = $e->getCode(); - $exitCode = is_numeric($exitCode) && $exitCode ? $exitCode : 1; + $exitCode = $exitCode ? (is_numeric($exitCode) ? (int) $exitCode : 1) : 0; } if ($this->autoExit) { @@ -184,7 +184,7 @@ class Application $exitCode = $this->doRunCommand($command, $input, $output); $this->runningCommand = null; - return is_numeric($exitCode) ? $exitCode : 0; + return $exitCode; } /** diff --git a/src/Symfony/Component/Console/Command/Command.php b/src/Symfony/Component/Console/Command/Command.php index c5fae3453c..af1b91f2b1 100644 --- a/src/Symfony/Component/Console/Command/Command.php +++ b/src/Symfony/Component/Console/Command/Command.php @@ -244,7 +244,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 f912338e04..b33eeb1bd2 100644 --- a/src/Symfony/Component/Console/Tests/ApplicationTest.php +++ b/src/Symfony/Component/Console/Tests/ApplicationTest.php @@ -558,6 +558,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 731f44277d..872bb7fc42 100644 --- a/src/Symfony/Component/Console/Tests/Command/CommandTest.php +++ b/src/Symfony/Component/Console/Tests/Command/CommandTest.php @@ -260,6 +260,20 @@ class CommandTest extends \PHPUnit_Framework_TestCase $tester->execute(array('--bar' => true)); } + 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();