Browse Source

Add a method that will be in charge of parsing arguments passed to a Shell::dispatchShell() call.
Add tests for that method.

Yves P 11 years ago
parent
commit
1cf42b8fa8
2 changed files with 98 additions and 12 deletions
  1. 52 12
      src/Console/Shell.php
  2. 46 0
      tests/TestCase/Console/ShellTest.php

+ 52 - 12
src/Console/Shell.php

@@ -211,7 +211,7 @@ class Shell
      */
     public function startup()
     {
-        if (!(bool)$this->param('requested')) {
+        if (!$this->param('requested')) {
             $this->_welcome();
         }
     }
@@ -300,22 +300,29 @@ class Shell
      *
      * `return $this->dispatchShell('schema', 'create', 'i18n', '--dry');`
      *
+     * With an array having two key / value pairs:
+     *  - `command` can accept either a string or an array. Represents the command to dispatch
+     *  - `extra` can accept an array of extra parameters to pass on to the dispatcher. This
+     *  parameters will be available in the `param` property of the called `Shell`
+     *
+     * `return $this->dispatchShell([
+     *      'command' => 'schema create DbAcl',
+     *      'extra' => ['param' => 'value']
+     * ]);`
+     *
+     * or
+     *
+     * `return $this->dispatchShell([
+     *      'command' => ['schema', 'create', 'DbAcl'],
+     *      'extra' => ['param' => 'value']
+     * ]);`
+     *
      * @return mixed The return of the other shell.
      * @link http://book.cakephp.org/3.0/en/console-and-shells.html#invoking-other-shells-from-your-shell
      */
     public function dispatchShell()
     {
-        $args = func_get_args();
-        $extra = [];
-        if (is_array($args[0]) && isset($args[0]['command'])) {
-            if (!empty($args[0]['extra'])) {
-                $extra = $args[0]['extra'];
-            }
-
-            $args = explode(' ', $args[0]['command']);
-        } elseif (is_string($args[0]) && count($args) === 1) {
-            $args = explode(' ', $args[0]);
-        }
+        list($args, $extra) = $this->parseDispatchArguments(func_get_args());
 
         if (!isset($extra['requested'])) {
             $extra['requested'] = true;
@@ -326,6 +333,39 @@ class Shell
     }
 
     /**
+     * Parses the arguments for the dispatchShell() method.
+     *
+     * @param array $args Arguments fetch from the dispatchShell() method with
+     * func_get_args()
+     * @return array First value has to be an array of the command arguments.
+     * Second value has to be an array of extra parameter to pass on to the dispatcher
+     */
+    public function parseDispatchArguments($args)
+    {
+        $extra = [];
+
+        if (is_string($args[0]) && count($args) === 1) {
+            $args = explode(' ', $args[0]);
+            return [$args, $extra];
+        }
+
+        if (is_array($args[0]) && !empty($args[0]['command'])) {
+            $command = $args[0]['command'];
+            if (is_string($command)) {
+                $command = explode(' ', $command);
+            }
+
+            if (!empty($args[0]['extra'])) {
+                $extra = $args[0]['extra'];
+            }
+
+            return [$command, $extra];
+        }
+
+        return [$args, $extra];
+    }
+
+    /**
      * Runs the Shell with the provided argv.
      *
      * Delegates calls to Tasks and resolves methods inside the class. Commands are looked

+ 46 - 0
tests/TestCase/Console/ShellTest.php

@@ -643,6 +643,52 @@ class ShellTest extends TestCase
     }
 
     /**
+     * Test the dispatchShell() arguments parser
+     *
+     * @return void
+     */
+    public function testDispatchShellArgsParser()
+    {
+        $Shell = new Shell();
+
+        $expected = [['schema', 'create', 'DbAcl'], []];
+        // Shell::dispatchShell('schema create DbAcl');
+        $result = $Shell->parseDispatchArguments(['schema create DbAcl']);
+        $this->assertEquals($expected, $result);
+
+        // Shell::dispatchShell('schema', 'create', 'DbAcl');
+        $result = $Shell->parseDispatchArguments(['schema', 'create', 'DbAcl']);
+        $this->assertEquals($expected, $result);
+
+        // Shell::dispatchShell(['command' => 'schema create DbAcl']);
+        $result = $Shell->parseDispatchArguments([[
+            'command' => 'schema create DbAcl'
+        ]]);
+        $this->assertEquals($expected, $result);
+
+        // Shell::dispatchShell(['command' => ['schema', 'create', 'DbAcl']]);
+        $result = $Shell->parseDispatchArguments([[
+            'command' => ['schema', 'create', 'DbAcl']
+        ]]);
+        $this->assertEquals($expected, $result);
+
+        $expected[1] = ['param' => 'value'];
+        // Shell::dispatchShell(['command' => 'schema create DbAcl', 'extra' => ['param' => 'value']]);
+        $result = $Shell->parseDispatchArguments([[
+            'command' => 'schema create DbAcl',
+            'extra' => ['param' => 'value']
+        ]]);
+        $this->assertEquals($expected, $result);
+
+        // Shell::dispatchShell(['command' => ['schema', 'create', 'DbAcl'], 'extra' => ['param' => 'value']]);
+        $result = $Shell->parseDispatchArguments([[
+            'command' => ['schema', 'create', 'DbAcl'],
+            'extra' => ['param' => 'value']
+        ]]);
+        $this->assertEquals($expected, $result);
+    }
+
+    /**
      * Test that runCommand() doesn't call public methods when the second arg is false.
      *
      * @return void