Skip to content

Commit f7b3619

Browse files
committed
Add abort() to Command.
Messages can be handled in the command via the `$io` object. I thought it was a bit silly to pass in ConsoleIo so that a message could be printed using it.
1 parent cecff70 commit f7b3619

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

src/Console/Command.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
namespace Cake\Console;
1616

1717
use Cake\Console\Exception\ConsoleException;
18+
use Cake\Console\Exception\StopException;
1819
use Cake\Datasource\ModelAwareTrait;
1920
use Cake\Log\LogTrait;
2021
use Cake\ORM\Locator\LocatorAwareTrait;
@@ -233,4 +234,15 @@ public function execute(Arguments $args, ConsoleIo $io)
233234
{
234235
return null;
235236
}
237+
238+
/**
239+
* Halt the the current process with a StopException.
240+
*
241+
* @param int $code The exit code to use.
242+
* @throws \Cake\Console\Exception\ConsoleException
243+
*/
244+
public function abort($code = self::CODE_ERROR)
245+
{
246+
throw new StopException('Command aborted', $code);
247+
}
236248
}

tests/TestCase/Console/CommandTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,32 @@ public function testRunOptionParserFailure()
235235
$this->assertContains('Error: Missing required arguments. name is required', $messages);
236236
}
237237

238+
/**
239+
* Test abort()
240+
*
241+
* @expectedException \Cake\Console\Exception\StopException
242+
* @expectedExceptionCode 1
243+
* @return void
244+
*/
245+
public function testAbort()
246+
{
247+
$command = new Command();
248+
$command->abort();
249+
}
250+
251+
/**
252+
* Test abort()
253+
*
254+
* @expectedException \Cake\Console\Exception\StopException
255+
* @expectedExceptionCode 99
256+
* @return void
257+
*/
258+
public function testAbortCustomCode()
259+
{
260+
$command = new Command();
261+
$command->abort(99);
262+
}
263+
238264
protected function getMockIo($output)
239265
{
240266
$io = $this->getMockBuilder(ConsoleIo::class)

0 commit comments

Comments
 (0)