Browse Source

Makes sure that App Shells and plugin Shells with the same name can co-exist

If an app shell and a plugin shell shares the same name, we have to make sure the correct subcommands will be returned for each one of them.
Yves P 10 years ago
parent
commit
c208e88413

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

@@ -192,11 +192,14 @@ class CommandTask extends Shell
 
         if (empty($pluginDot)) {
             $shellList = $this->getShellList();
-            unset($shellList['CORE'], $shellList['app']);
-            foreach ($shellList as $plugin => $commands) {
-                if (in_array($commandName, $commands)) {
-                    $pluginDot = $plugin . '.';
-                    break;
+
+            if (!in_array($commandName, $shellList['app']) && !in_array($commandName, $shellList['CORE'])) {
+                unset($shellList['CORE'], $shellList['app']);
+                foreach ($shellList as $plugin => $commands) {
+                    if (in_array($commandName, $commands)) {
+                        $pluginDot = $plugin . '.';
+                        break;
+                    }
                 }
             }
         }

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

@@ -17,6 +17,7 @@ namespace Cake\Test\TestCase\Shell;
 use Cake\Console\ConsoleIo;
 use Cake\Console\ConsoleOutput;
 use Cake\Console\Shell;
+use Cake\Core\Configure;
 use Cake\Core\Plugin;
 use Cake\Shell\CompletionShell;
 use Cake\Shell\Task\CommandTask;
@@ -51,6 +52,7 @@ class CompletionShellTest extends TestCase
     public function setUp()
     {
         parent::setUp();
+        Configure::write('App.namespace', 'TestApp');
         Plugin::load(['TestPlugin', 'TestPluginTwo']);
 
         $this->out = new TestCompletionStringOutput();
@@ -78,6 +80,7 @@ class CompletionShellTest extends TestCase
     {
         parent::tearDown();
         unset($this->Shell);
+        Configure::write('App.namespace', 'App');
         Plugin::unload();
     }
 
@@ -190,7 +193,7 @@ class CompletionShellTest extends TestCase
         $this->Shell->runCommand(['subcommands', 'app.sample']);
         $output = $this->out->output;
 
-        $expected = '';
+        $expected = "derp\n";
         $this->assertEquals($expected, $output);
     }
 
@@ -238,6 +241,35 @@ class CompletionShellTest extends TestCase
     }
 
     /**
+     * test that subCommands with an app shell that is also defined in a plugin and without the prefix "app."
+     * returns proper sub commands
+     *
+     * @return void
+     */
+    public function testSubCommandsAppDuplicatePluginNoDot()
+    {
+        $this->Shell->runCommand(['subcommands', 'sample']);
+        $output = $this->out->output;
+
+        $expected = "derp\n";
+        $this->assertEquals($expected, $output);
+    }
+
+    /**
+     * test that subCommands with a plugin shell that is also defined in the returns proper sub commands
+     *
+     * @return void
+     */
+    public function testSubCommandsPluginDuplicateApp()
+    {
+        $this->Shell->runCommand(['subcommands', 'TestPlugin.sample']);
+        $output = $this->out->output;
+
+        $expected = "example\n";
+        $this->assertEquals($expected, $output);
+    }
+
+    /**
      * test that subcommands without arguments returns nothing
      *
      * @return void

+ 10 - 0
tests/test_app/Plugin/TestPlugin/src/Shell/SampleShell.php

@@ -33,4 +33,14 @@ class SampleShell extends Shell
     {
         $this->out('This is the main method called from SampleShell');
     }
+
+    /**
+     * example method
+     *
+     * @return void
+     */
+    public function example()
+    {
+        $this->out('This is the example method called from TestPlugin.SampleShell');
+    }
 }

+ 10 - 0
tests/test_app/TestApp/Shell/SampleShell.php

@@ -35,4 +35,14 @@ class SampleShell extends Shell
     {
         $this->out('This is the main method called from SampleShell');
     }
+
+    /**
+     * derp method
+     *
+     * @return void
+     */
+    public function derp()
+    {
+        $this->out('This is the example method called from TestPlugin.SampleShell');
+    }
 }