Browse Source

Add tests to the changes made to the CompletionShell and adjusts some code to better deal with plugins having multiple shells

Yves P 10 years ago
parent
commit
82e315aea1

+ 10 - 5
src/Shell/Task/CommandTask.php

@@ -116,12 +116,16 @@ class CommandTask extends Shell
 
         $options = [];
         foreach ($shellList as $type => $commands) {
-            $prefix = '';
-            if (!in_array(strtolower($type), ['app', 'core']) && isset($duplicates[$type])) {
-                $prefix = $type . '.';
-            }
-
             foreach ($commands as $shell) {
+                $prefix = '';
+                if (
+                    !in_array(strtolower($type), ['app', 'core']) &&
+                    isset($duplicates[$type]) &&
+                    in_array($shell, $duplicates[$type])
+                ) {
+                    $prefix = $type . '.';
+                }
+
                 $options[] = $prefix . $shell;
             }
         }
@@ -192,6 +196,7 @@ class CommandTask extends Shell
             foreach ($shellList as $plugin => $commands) {
                 if (in_array($commandName, $commands)) {
                     $pluginDot = $plugin . '.';
+                    break;
                 }
             }
         }

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

@@ -81,7 +81,7 @@ class CommandListShellTest extends TestCase
         $expected = "/\[.*TestPlugin.*\] example/";
         $this->assertRegExp($expected, $output);
 
-        $expected = "/\[.*TestPluginTwo.*\] example, welcome/";
+        $expected = "/\[.*TestPluginTwo.*\] example, unique, welcome/";
         $this->assertRegExp($expected, $output);
 
         $expected = "/\[.*CORE.*\] i18n, orm_cache, plugin, routes, server/";

+ 18 - 3
tests/TestCase/Shell/CompletionShellTest.php

@@ -119,7 +119,7 @@ class CompletionShellTest extends TestCase
         $this->Shell->runCommand(['commands']);
         $output = $this->out->output;
 
-        $expected = "TestPlugin.example TestPlugin.sample TestPluginTwo.example TestPluginTwo.welcome " .
+        $expected = "TestPlugin.example TestPlugin.sample TestPluginTwo.example unique welcome " .
             "i18n orm_cache plugin routes server i18m sample testing_dispatch\n";
         $this->assertTextEquals($expected, $output);
     }
@@ -195,13 +195,28 @@ class CompletionShellTest extends TestCase
     }
 
     /**
-     * test that subCommands with a existing plugin command returns the proper sub commands
+     * test that subCommands with an existing plugin command returns the proper sub commands
+     * when the Shell name is unique and the dot notation not mandatory
      *
      * @return void
      */
     public function testSubCommandsPlugin()
     {
-        $this->Shell->runCommand(['subcommands', 'TestPluginTwo.welcome']);
+        $this->Shell->runCommand(['subcommands', 'welcome']);
+        $output = $this->out->output;
+
+        $expected = "say_hello\n";
+        $this->assertTextEquals($expected, $output);
+    }
+
+    /**
+     * test that subCommands with an existing plugin command returns the proper sub commands
+     *
+     * @return void
+     */
+    public function testSubCommandsPluginDotNotation()
+    {
+        $this->Shell->runCommand(['subcommands', 'TestPluginTwo.example']);
         $output = $this->out->output;
 
         $expected = "say_hello\n";

+ 4 - 0
tests/test_app/Plugin/TestPluginTwo/src/Shell/ExampleShell.php

@@ -33,4 +33,8 @@ class ExampleShell extends Shell
     {
         $this->out('This is the main method called from TestPluginTwo.ExampleShell');
     }
+
+    public function say_hello() {
+    	$this->out('Hello from the TestPluginTwo.ExampleShell');
+    }
 }

+ 36 - 0
tests/test_app/Plugin/TestPluginTwo/src/Shell/UniqueShell.php

@@ -0,0 +1,36 @@
+<?php
+/**
+ * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
+ * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
+ *
+ * Licensed under The MIT License
+ * For full copyright and license information, please see the LICENSE.txt
+ * Redistributions of files must retain the above copyright notice
+ *
+ * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
+ * @link          http://cakephp.org CakePHP(tm) Project
+ * @since         1.2.0
+ * @license       http://www.opensource.org/licenses/mit-license.php MIT License
+ */
+
+/**
+ * Class UniqueShell
+ *
+ */
+namespace TestPluginTwo\Shell;
+
+use Cake\Console\Shell;
+
+class UniqueShell extends Shell
+{
+
+    /**
+     * main method
+     *
+     * @return void
+     */
+    public function main()
+    {
+        $this->out('This is the main method called from TestPluginTwo.UniqueShell');
+    }
+}