Skip to content

Commit 92399ff

Browse files
committed
merged branch Tobion/console-exit-code (PR #8080)
This PR was merged into the 2.1 branch. Discussion ---------- [Console] fix and refactor exit code handling Split of #8038 Commits ------- 5c317b7 [Console] fix and refactor exit code handling
2 parents b31769b + 5c317b7 commit 92399ff

File tree

4 files changed

+32
-3
lines changed

4 files changed

+32
-3
lines changed

src/Symfony/Component/Console/Application.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ public function run(InputInterface $input = null, OutputInterface $output = null
115115
}
116116
$statusCode = $e->getCode();
117117

118-
$statusCode = is_numeric($statusCode) && $statusCode ? $statusCode : 1;
118+
$statusCode = $statusCode ? (is_numeric($statusCode) ? (int) $statusCode : 1) : 0;
119119
}
120120

121121
if ($this->autoExit) {
@@ -192,7 +192,7 @@ public function doRun(InputInterface $input, OutputInterface $output)
192192
$statusCode = $command->run($input, $output);
193193
$this->runningCommand = null;
194194

195-
return is_numeric($statusCode) ? $statusCode : 0;
195+
return $statusCode;
196196
}
197197

198198
/**

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ public function run(InputInterface $input, OutputInterface $output)
238238
$statusCode = $this->execute($input, $output);
239239
}
240240

241-
return is_numeric($statusCode) ? $statusCode : 0;
241+
return is_numeric($statusCode) ? (int) $statusCode : 0;
242242
}
243243

244244
/**

src/Symfony/Component/Console/Tests/ApplicationTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,21 @@ public function testRun()
501501
$this->assertSame('called'.PHP_EOL, $tester->getDisplay(), '->run() does not call interact() if -n is passed');
502502
}
503503

504+
public function testRunReturnsIntegerExitCode()
505+
{
506+
$exception = new \Exception('', 4);
507+
508+
$application = $this->getMock('Symfony\Component\Console\Application', array('doRun'));
509+
$application->setAutoExit(false);
510+
$application->expects($this->once())
511+
->method('doRun')
512+
->will($this->throwException($exception));
513+
514+
$exitCode = $application->run(new ArrayInput(array()), new NullOutput());
515+
516+
$this->assertSame(4, $exitCode, '->run() returns integer exit code extracted from raised exception');
517+
}
518+
504519
/**
505520
* @expectedException \LogicException
506521
* @dataProvider getAddingAlreadySetDefinitionElementData

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,20 @@ public function testRun()
221221
}
222222
}
223223

224+
public function testRunReturnsIntegerExitCode()
225+
{
226+
$command = new \TestCommand();
227+
$exitCode = $command->run(new StringInput(''), new NullOutput());
228+
$this->assertSame(0, $exitCode, '->run() returns integer exit code (treats null as 0)');
229+
230+
$command = $this->getMock('TestCommand', array('execute'));
231+
$command->expects($this->once())
232+
->method('execute')
233+
->will($this->returnValue('2.3'));
234+
$exitCode = $command->run(new StringInput(''), new NullOutput());
235+
$this->assertSame(2, $exitCode, '->run() returns integer exit code (casts numeric to int)');
236+
}
237+
224238
public function testRunReturnsAlwaysInteger()
225239
{
226240
$command = new \TestCommand();

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