Browse Source

Catching StopException in ConsoleIntegrationTestCase

Jeremy Harris 7 years ago
parent
commit
00c156be2a

+ 7 - 1
src/TestSuite/ConsoleIntegrationTestCase.php

@@ -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 @@ abstract class ConsoleIntegrationTestCase extends TestCase
 
         $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;
+        }
     }
 
     /**

+ 2 - 2
tests/TestCase/Shell/CommandListShellTest.php

@@ -65,7 +65,7 @@ class CommandListShellTest extends ConsoleIntegrationTestCase
         $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 @@ class CommandListShellTest extends ConsoleIntegrationTestCase
         $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();

+ 1 - 1
tests/TestCase/Shell/CompletionShellTest.php

@@ -117,7 +117,7 @@ class CompletionShellTest extends TestCase
 
         $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);
     }
 

+ 25 - 0
tests/TestCase/TestSuite/ConsoleIntegrationTestCaseTest.php

@@ -60,6 +60,31 @@ class ConsoleIntegrationTestCaseTest extends ConsoleIntegrationTestCase
     }
 
     /**
+     * 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
      *
      * @return void

+ 8 - 0
tests/test_app/TestApp/Application.php

@@ -16,6 +16,7 @@ namespace TestApp;
 
 use Cake\Http\BaseApplication;
 use Cake\Routing\Middleware\RoutingMiddleware;
+use TestApp\Command\AbortCommand;
 
 class Application extends BaseApplication
 {
@@ -25,6 +26,13 @@ class Application extends BaseApplication
         parent::bootstrap();
     }
 
+    public function console($commands)
+    {
+        return $commands
+            ->add('abort_command', AbortCommand::class)
+            ->addMany($commands->autoDiscover());
+    }
+
     public function middleware($middleware)
     {
         $middleware->add(new RoutingMiddleware());

+ 15 - 0
tests/test_app/TestApp/Command/AbortCommand.php

@@ -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();
+    }
+}

+ 10 - 1
tests/test_app/TestApp/Shell/IntegrationShell.php

@@ -46,7 +46,8 @@ class IntegrationShell extends Shell
             ->addSubcommand('argsAndOptions', [
                 'parser' => $argAndOptionParser
             ])
-            ->addSubcommand('bridge');
+            ->addSubcommand('bridge')
+            ->addSubcommand('abort_shell');
 
         return $parser;
     }
@@ -85,4 +86,12 @@ class IntegrationShell extends Shell
         $this->out('arg: ' . $this->args[0]);
         $this->out('opt: ' . $this->param('opt'));
     }
+
+    /**
+     * @throws \Cake\Console\Exception\StopException
+     */
+    public function abortShell()
+    {
+        $this->abort('Shell aborted');
+    }
 }