Skip to content
Permalink
Browse files

Catching StopException in ConsoleIntegrationTestCase

  • Loading branch information...
jeremyharris committed Jun 5, 2018
1 parent 68b3636 commit 00c156be2a700301a494b3a7053bacbd98efa7a7
@@ -13,9 +13,11 @@
*/
namespace Cake\TestSuite;
use Cake\Console\Command;
use Cake\Console\CommandRunner;
use Cake\Console\ConsoleInput;
use Cake\Console\ConsoleIo;
use Cake\Console\Exception\StopException;
use Cake\Core\Configure;
use Cake\TestSuite\Stub\ConsoleOutput;
@@ -90,7 +92,11 @@ public function exec($command, array $input = [])
$io = new ConsoleIo($this->_out, $this->_err, $this->_in);
$this->_exitCode = $runner->run($args, $io);
try {
$this->_exitCode = $runner->run($args, $io);
} catch (StopException $exception) {
$this->_exitCode = Command::CODE_ERROR;
}
}
/**
@@ -65,7 +65,7 @@ public function testMain()
$expected = "/\[.*CORE.*\] cache, help, i18n, orm_cache, plugin, routes, schema_cache, server/";
$this->assertOutputRegExp($expected);
$expected = "/\[.*app.*\] demo, i18m, integration, merge, sample/";
$expected = "/\[.*app.*\] abort, demo, i18m, integration, merge, sample/";
$this->assertOutputRegExp($expected);
$this->assertExitCode(Shell::CODE_SUCCESS);
$this->assertErrorEmpty();
@@ -86,7 +86,7 @@ public function testMainAppPriority()
$expected = "/\[.*CORE.*\] cache, help, orm_cache, plugin, routes, schema_cache, server, version/";
$this->assertOutputRegExp($expected);
$expected = "/\[.*app.*\] demo, i18n, integration, merge, sample/";
$expected = "/\[.*app.*\] abort, demo, i18n, integration, merge, sample/";
$this->assertOutputRegExp($expected);
$this->assertExitCode(Shell::CODE_SUCCESS);
$this->assertErrorEmpty();
@@ -117,7 +117,7 @@ public function testCommands()
$expected = 'TestPlugin.example TestPlugin.sample TestPluginTwo.example unique welcome ' .
'cache help i18n orm_cache plugin routes schema_cache server version ' .
"demo i18m integration merge sample shell_test testing_dispatch\n";
"abort demo i18m integration merge sample shell_test testing_dispatch\n";
$this->assertTextEquals($expected, $output);
}
@@ -59,6 +59,31 @@ public function testExec()
$this->assertExitCode(Shell::CODE_ERROR);
}
/**
* tests that exec catches a StopException
*
* @return void
*/
public function testExecShellWithStopException()
{
$this->exec('integration abort_shell');
$this->assertExitCode(Shell::CODE_ERROR);
$this->assertErrorContains('Shell aborted');
}
/**
* tests that exec catches a StopException
*
* @return void
*/
public function testExecCommandWithStopException()
{
$this->useCommandRunner();
$this->exec('abort_command');
$this->assertExitCode(Shell::CODE_ERROR);
$this->assertErrorContains('Command aborted');
}
/**
* tests a valid core command
*
@@ -16,6 +16,7 @@
use Cake\Http\BaseApplication;
use Cake\Routing\Middleware\RoutingMiddleware;
use TestApp\Command\AbortCommand;
class Application extends BaseApplication
{
@@ -25,6 +26,13 @@ public function bootstrap()
parent::bootstrap();
}
public function console($commands)
{
return $commands
->add('abort_command', AbortCommand::class)
->addMany($commands->autoDiscover());
}
public function middleware($middleware)
{
$middleware->add(new RoutingMiddleware());
@@ -0,0 +1,15 @@
<?php
namespace TestApp\Command;
use Cake\Console\Arguments;
use Cake\Console\Command;
use Cake\Console\ConsoleIo;
class AbortCommand extends Command
{
public function execute(Arguments $args, ConsoleIo $io)
{
$io->error('Command aborted');
$this->abort();
}
}
@@ -46,7 +46,8 @@ public function getOptionParser()
->addSubcommand('argsAndOptions', [
'parser' => $argAndOptionParser
])
->addSubcommand('bridge');
->addSubcommand('bridge')
->addSubcommand('abort_shell');
return $parser;
}
@@ -85,4 +86,12 @@ public function argsAndOptions()
$this->out('arg: ' . $this->args[0]);
$this->out('opt: ' . $this->param('opt'));
}
/**
* @throws \Cake\Console\Exception\StopException
*/
public function abortShell()
{
$this->abort('Shell aborted');
}
}

0 comments on commit 00c156b

Please sign in to comment.
You can’t perform that action at this time.