Skip to content

Commit

Permalink
Add abort() to Command.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
markstory committed Oct 8, 2017
1 parent cecff70 commit f7b3619
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/Console/Command.php
Expand Up @@ -15,6 +15,7 @@
namespace Cake\Console;

use Cake\Console\Exception\ConsoleException;
use Cake\Console\Exception\StopException;
use Cake\Datasource\ModelAwareTrait;
use Cake\Log\LogTrait;
use Cake\ORM\Locator\LocatorAwareTrait;
Expand Down Expand Up @@ -233,4 +234,15 @@ public function execute(Arguments $args, ConsoleIo $io)
{
return null;
}

/**
* Halt the the current process with a StopException.
*
* @param int $code The exit code to use.
* @throws \Cake\Console\Exception\ConsoleException
*/
public function abort($code = self::CODE_ERROR)
{
throw new StopException('Command aborted', $code);
}
}
26 changes: 26 additions & 0 deletions tests/TestCase/Console/CommandTest.php
Expand Up @@ -235,6 +235,32 @@ public function testRunOptionParserFailure()
$this->assertContains('Error: Missing required arguments. name is required', $messages);
}

/**
* Test abort()
*
* @expectedException \Cake\Console\Exception\StopException
* @expectedExceptionCode 1
* @return void
*/
public function testAbort()
{
$command = new Command();
$command->abort();
}

/**
* Test abort()
*
* @expectedException \Cake\Console\Exception\StopException
* @expectedExceptionCode 99
* @return void
*/
public function testAbortCustomCode()
{
$command = new Command();
$command->abort(99);
}

protected function getMockIo($output)
{
$io = $this->getMockBuilder(ConsoleIo::class)
Expand Down

0 comments on commit f7b3619

Please sign in to comment.