Browse Source

Made LegacyCommandRunner simply mimic CommandRunner interface

Created LegacyShellDispatcher again as it is the only way to inject the
IO mock into the shell, but we cannot add a non-static run() method
because ShellDispatcher has a static::run
Jeremy Harris 8 years ago
parent
commit
2e3ca9c733

+ 16 - 15
src/TestSuite/ConsoleIntegrationTestCase.php

@@ -13,6 +13,7 @@
  */
 namespace Cake\TestSuite;
 
+use Cake\Console\CommandRunner;
 use Cake\Console\ConsoleInput;
 use Cake\Console\ConsoleIo;
 use Cake\TestSuite\Stub\ConsoleOutput;
@@ -67,7 +68,14 @@ class ConsoleIntegrationTestCase extends TestCase
      */
     public function exec($command, array $input = [])
     {
-        $runner = $this->_makeRunner("bin/cake $command");
+        $runner = $this->_makeRunner();
+
+        $this->_out = new ConsoleOutput();
+        $this->_err = new ConsoleOutput();
+        $this->_in = $this->getMockBuilder(ConsoleInput::class)
+            ->disableOriginalConstructor()
+            ->setMethods(['read'])
+            ->getMock();
 
         $i = 0;
         foreach ($input as $in) {
@@ -77,7 +85,11 @@ class ConsoleIntegrationTestCase extends TestCase
                 ->will($this->returnValue($in));
         }
 
-        $this->_exitCode = $runner->dispatch();
+        $args = $this->_commandStringToArgs("bin/cake $command");
+
+        $io = new ConsoleIo($this->_out, $this->_err, $this->_in);
+
+        $this->_exitCode = $runner->run($args, $io);
     }
 
     /**
@@ -148,25 +160,14 @@ class ConsoleIntegrationTestCase extends TestCase
      * @param string $command Command
      * @return LegacyCommandRunner
      */
-    protected function _makeRunner($command)
+    protected function _makeRunner()
     {
-        $args = $this->_commandStringToArgs($command);
-
         if ($this->_useCommandRunner) {
             // not implemented yet
             return;
         }
 
-        $this->_out = new ConsoleOutput();
-        $this->_err = new ConsoleOutput();
-        $this->_in = $this->getMockBuilder(ConsoleInput::class)
-            ->disableOriginalConstructor()
-            ->setMethods(['read'])
-            ->getMock();
-
-        $io = new ConsoleIo($this->_out, $this->_err, $this->_in);
-
-        return new LegacyCommandRunner($args, true, $io);
+        return new LegacyCommandRunner();
     }
 
     /**

+ 8 - 27
src/TestSuite/LegacyCommandRunner.php

@@ -1,9 +1,9 @@
 <?php
 namespace Cake\TestSuite;
 
-use Cake\Console\ShellDispatcher;
+use Cake\Console\ConsoleIo;
 
-class LegacyCommandRunner extends ShellDispatcher
+class LegacyCommandRunner
 {
     /**
      * @var \Cake\Console\ConsoleIo
@@ -11,33 +11,14 @@ class LegacyCommandRunner extends ShellDispatcher
     protected $_io;
 
     /**
-     * Constructor
+     * Mimics functionality of Cake\Console\CommandRunner
      *
-     * @param array $args the argv from PHP
-     * @param bool $bootstrap Should the environment be bootstrapped.
-     * @param \Cake\Console\ConsoleIo $io The ConsoleIo class to use.
-     * @return void
+     * @param array $argv Argument array
+     * @param ConsoleIo $io ConsoleIo
      */
-    public function __construct($args = [], $bootstrap = true, $io = null)
+    public function run(array $argv, ConsoleIo $io = null)
     {
-        $this->_io = $io;
-
-        parent::__construct($args, $bootstrap);
-    }
-
-    /**
-     * Injects mock and stub io components into the shell
-     *
-     * @param string $className Class name
-     * @param string $shortName Short name
-     * @return \Cake\Console\Shell
-     */
-    protected function _createShell($className, $shortName)
-    {
-        list($plugin) = pluginSplit($shortName);
-        $instance = new $className($this->_io);
-        $instance->plugin = trim($plugin, '.');
-
-        return $instance;
+        $dispatcher = new LegacyShellDispatcher($argv, true, $io);
+        return $dispatcher->dispatch();
     }
 }

+ 41 - 0
src/TestSuite/LegacyShellDispatcher.php

@@ -0,0 +1,41 @@
+<?php
+namespace Cake\TestSuite;
+
+use Cake\Console\ShellDispatcher;
+
+class LegacyShellDispatcher extends ShellDispatcher
+{
+    /**
+     * @var \Cake\Console\ConsoleIo
+     */
+    protected $_io;
+
+    /**
+     * Constructor
+     *
+     * @param array $args Argument array
+     * @param bool $bootstrap Initialize environment
+     * @param \Cake\Console\ConsoleIo $io ConsoleIo
+     */
+    public function __construct($args = array(), $bootstrap = true, $io)
+    {
+        $this->_io = $io;
+        parent::__construct($args, $bootstrap);
+    }
+
+    /**
+     * Injects mock and stub io components into the shell
+     *
+     * @param string $className Class name
+     * @param string $shortName Short name
+     * @return \Cake\Console\Shell
+     */
+    protected function _createShell($className, $shortName)
+    {
+        list($plugin) = pluginSplit($shortName);
+        $instance = new $className($this->_io);
+        $instance->plugin = trim($plugin, '.');
+
+        return $instance;
+    }
+}