diff --git a/src/Console/Command.php b/src/Console/Command.php index eb1d6a61f61..dad1a5342e3 100644 --- a/src/Console/Command.php +++ b/src/Console/Command.php @@ -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; @@ -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); + } } diff --git a/tests/TestCase/Console/CommandTest.php b/tests/TestCase/Console/CommandTest.php index dd583fb3ceb..52431c9e678 100644 --- a/tests/TestCase/Console/CommandTest.php +++ b/tests/TestCase/Console/CommandTest.php @@ -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)