Browse Source

Merge pull request #11150 from cakephp/console-test-update

Update 2 more shell tests to use ConsoleIntegrationTestCase
Mark Story 8 years ago
parent
commit
fd6ed92b74
2 changed files with 48 additions and 83 deletions
  1. 25 44
      tests/TestCase/Shell/CommandListShellTest.php
  2. 23 39
      tests/TestCase/Shell/HelpShellTest.php

+ 25 - 44
tests/TestCase/Shell/CommandListShellTest.php

@@ -14,16 +14,15 @@
  */
 namespace Cake\Test\TestCase\Shell;
 
-use Cake\Console\ConsoleIo;
+use Cake\Console\Shell;
 use Cake\Core\Configure;
 use Cake\Core\Plugin;
-use Cake\TestSuite\Stub\ConsoleOutput;
-use Cake\TestSuite\TestCase;
+use Cake\TestSuite\ConsoleIntegrationTestCase;
 
 /**
  * CommandListShellTest
  */
-class CommandListShellTest extends TestCase
+class CommandListShellTest extends ConsoleIntegrationTestCase
 {
 
     /**
@@ -35,19 +34,6 @@ class CommandListShellTest extends TestCase
     {
         parent::setUp();
         Plugin::load(['TestPlugin', 'TestPluginTwo']);
-
-        $this->out = new ConsoleOutput();
-        $io = new ConsoleIo($this->out);
-
-        $this->Shell = $this->getMockBuilder('Cake\Shell\CommandListShell')
-            ->setMethods(['in', 'err', '_stop', 'clear'])
-            ->setConstructorArgs([$io])
-            ->getMock();
-
-        $this->Shell->Command = $this->getMockBuilder('Cake\Shell\Task\CommandTask')
-            ->setMethods(['in', '_stop', 'err', 'clear'])
-            ->setConstructorArgs([$io])
-            ->getMock();
     }
 
     /**
@@ -58,7 +44,6 @@ class CommandListShellTest extends TestCase
     public function tearDown()
     {
         parent::tearDown();
-        unset($this->Shell);
         Plugin::unload();
     }
 
@@ -69,21 +54,21 @@ class CommandListShellTest extends TestCase
      */
     public function testMain()
     {
-        $this->Shell->main();
-        $output = $this->out->messages();
-        $output = implode("\n", $output);
+        $this->exec('command_list');
 
         $expected = "/\[.*TestPlugin.*\] example/";
-        $this->assertRegExp($expected, $output);
+        $this->assertOutputRegExp($expected);
 
         $expected = "/\[.*TestPluginTwo.*\] example, unique, welcome/";
-        $this->assertRegExp($expected, $output);
+        $this->assertOutputRegExp($expected);
 
         $expected = "/\[.*CORE.*\] cache, help, i18n, orm_cache, plugin, routes, server/";
-        $this->assertRegExp($expected, $output);
+        $this->assertOutputRegExp($expected);
 
         $expected = "/\[.*app.*\] i18m, integration, sample/";
-        $this->assertRegExp($expected, $output);
+        $this->assertOutputRegExp($expected);
+        $this->assertExitCode(Shell::CODE_SUCCESS);
+        $this->assertErrorEmpty();
     }
 
     /**
@@ -95,16 +80,16 @@ class CommandListShellTest extends TestCase
     public function testMainAppPriority()
     {
         rename(APP . 'Shell' . DS . 'I18mShell.php', APP . 'Shell' . DS . 'I18nShell.php');
-        $this->Shell->main();
-        $output = $this->out->messages();
-        $output = implode("\n", $output);
+        $this->exec('command_list');
         rename(APP . 'Shell' . DS . 'I18nShell.php', APP . 'Shell' . DS . 'I18mShell.php');
 
         $expected = "/\[.*CORE.*\] cache, help, orm_cache, plugin, routes, server/";
-        $this->assertRegExp($expected, $output);
+        $this->assertOutputRegExp($expected);
 
         $expected = "/\[.*app.*\] i18n, integration, sample/";
-        $this->assertRegExp($expected, $output);
+        $this->assertOutputRegExp($expected);
+        $this->assertExitCode(Shell::CODE_SUCCESS);
+        $this->assertErrorEmpty();
     }
 
     /**
@@ -114,20 +99,18 @@ class CommandListShellTest extends TestCase
      */
     public function testMainXml()
     {
-        $this->Shell->params['xml'] = true;
-        $this->Shell->main();
-
-        $output = $this->out->messages();
-        $output = implode("\n", $output);
+        $this->exec('command_list --xml');
 
         $find = '<shell name="sample" call_as="sample" provider="app" help="sample -h"';
-        $this->assertContains($find, $output);
+        $this->assertOutputContains($find);
 
         $find = '<shell name="orm_cache" call_as="orm_cache" provider="CORE" help="orm_cache -h"';
-        $this->assertContains($find, $output);
+        $this->assertOutputContains($find);
 
         $find = '<shell name="welcome" call_as="TestPluginTwo.welcome" provider="TestPluginTwo" help="TestPluginTwo.welcome -h"';
-        $this->assertContains($find, $output);
+        $this->assertOutputContains($find);
+        $this->assertExitCode(Shell::CODE_SUCCESS);
+        $this->assertErrorEmpty();
     }
 
     /**
@@ -137,12 +120,10 @@ class CommandListShellTest extends TestCase
      */
     public function testMainVersion()
     {
-        $this->Shell->params['version'] = true;
-        $this->Shell->main();
-        $output = $this->out->messages();
-        $output = implode("\n", $output);
-
+        $this->exec('command_list --version');
         $expected = Configure::version();
-        $this->assertEquals($expected, $output);
+        $this->assertOutputContains($expected);
+        $this->assertExitCode(Shell::CODE_SUCCESS);
+        $this->assertErrorEmpty();
     }
 }

+ 23 - 39
tests/TestCase/Shell/HelpShellTest.php

@@ -14,17 +14,14 @@
  */
 namespace Cake\Test\TestCase\Shell;
 
-use Cake\Console\CommandCollection;
-use Cake\Console\ConsoleIo;
+use Cake\Console\Shell;
 use Cake\Core\Plugin;
-use Cake\Shell\HelpShell;
-use Cake\TestSuite\Stub\ConsoleOutput;
-use Cake\TestSuite\TestCase;
+use Cake\TestSuite\ConsoleIntegrationTestCase;
 
 /**
  * HelpShell test.
  */
-class HelpShellTest extends TestCase
+class HelpShellTest extends ConsoleIntegrationTestCase
 {
     /**
      * setup method
@@ -35,16 +32,8 @@ class HelpShellTest extends TestCase
     {
         parent::setUp();
         $this->setAppNamespace();
+        $this->useCommandRunner(true);
         Plugin::load('TestPlugin');
-
-        $this->out = new ConsoleOutput();
-        $this->err = new ConsoleOutput();
-        $this->io = new ConsoleIo($this->out, $this->err);
-        $this->shell = new HelpShell($this->io);
-
-        $commands = new CommandCollection();
-        $commands->addMany($commands->autoDiscover());
-        $this->shell->setCommandCollection($commands);
     }
 
     /**
@@ -54,11 +43,9 @@ class HelpShellTest extends TestCase
      */
     public function testMainNoCommandsFallback()
     {
-        $shell = new HelpShell($this->io);
-        $this->assertNull($shell->main());
-
-        $output = implode("\n", $this->out->messages());
-        $this->assertOutput($output);
+        $this->exec('help');
+        $this->assertExitCode(Shell::CODE_SUCCESS);
+        $this->assertCommandList();
     }
 
     /**
@@ -68,10 +55,9 @@ class HelpShellTest extends TestCase
      */
     public function testMain()
     {
-        $this->assertNull($this->shell->main());
-
-        $output = implode("\n", $this->out->messages());
-        $this->assertOutput($output);
+        $this->exec('help');
+        $this->assertExitCode(Shell::CODE_SUCCESS);
+        $this->assertCommandList();
     }
 
     /**
@@ -80,14 +66,14 @@ class HelpShellTest extends TestCase
      * @param string $output The output to check.
      * @return void
      */
-    protected function assertOutput($output)
+    protected function assertCommandList()
     {
-        $this->assertContains('- sample', $output, 'app shell');
-        $this->assertContains('- test_plugin.sample', $output, 'Long plugin name');
-        $this->assertContains('- routes', $output, 'core shell');
-        $this->assertContains('- test_plugin.example', $output, 'Long plugin name');
-        $this->assertContains('To run a command', $output, 'more info present');
-        $this->assertContains('To get help', $output, 'more info present');
+        $this->assertOutputContains('- sample', 'app shell');
+        $this->assertOutputContains('- test_plugin.sample', 'Long plugin name');
+        $this->assertOutputContains('- routes', 'core shell');
+        $this->assertOutputContains('- test_plugin.example', 'Long plugin name');
+        $this->assertOutputContains('To run a command', 'more info present');
+        $this->assertOutputContains('To get help', 'more info present');
     }
 
     /**
@@ -97,19 +83,17 @@ class HelpShellTest extends TestCase
      */
     public function testMainAsXml()
     {
-        $this->shell->params['xml'] = true;
-        $this->shell->main();
-        $output = implode("\n", $this->out->messages());
-
-        $this->assertContains('<shells>', $output);
+        $this->exec('help --xml');
+        $this->assertExitCode(Shell::CODE_SUCCESS);
+        $this->assertOutputContains('<shells>');
 
         $find = '<shell name="sample" call_as="sample" provider="TestApp\Shell\SampleShell" help="sample -h"';
-        $this->assertContains($find, $output);
+        $this->assertOutputContains($find);
 
         $find = '<shell name="orm_cache" call_as="orm_cache" provider="Cake\Shell\OrmCacheShell" help="orm_cache -h"';
-        $this->assertContains($find, $output);
+        $this->assertOutputContains($find);
 
         $find = '<shell name="test_plugin.sample" call_as="test_plugin.sample" provider="TestPlugin\Shell\SampleShell" help="test_plugin.sample -h"';
-        $this->assertContains($find, $output);
+        $this->assertOutputContains($find);
     }
 }