Browse Source

Add compatibility to HelpShell for ShellDispatcher apps.

Not everyone will upgrade right away, and HelpShell shouldn't brick
until they do update.
Mark Story 8 years ago
parent
commit
49aeb1a1b1
2 changed files with 48 additions and 9 deletions
  1. 29 3
      src/Shell/HelpShell.php
  2. 19 6
      tests/TestCase/Shell/HelpShellTest.php

+ 29 - 3
src/Shell/HelpShell.php

@@ -19,6 +19,8 @@ use Cake\Console\CommandCollection;
 use Cake\Console\CommandCollectionAwareInterface;
 use Cake\Console\ConsoleOutput;
 use Cake\Console\Shell;
+use Cake\Shell\Task\CommandTask;
+use Cake\Utility\Inflector;
 use SimpleXmlElement;
 
 /**
@@ -71,9 +73,7 @@ class HelpShell extends Shell implements CommandCollectionAwareInterface
         }
 
         if (!$this->commands) {
-            $this->err('Could not print command list, no CommandCollection was set using setCommandCollection()');
-
-            return;
+            $this->commands = $this->getCommands();
         }
 
         if ($this->param('xml')) {
@@ -85,6 +85,32 @@ class HelpShell extends Shell implements CommandCollectionAwareInterface
     }
 
     /**
+     * Get the list of commands using the CommandTask
+     *
+     * Provides backwards compatibility when an application doesn't use
+     * CommandRunner.
+     *
+     * @return array
+     */
+    protected function getCommands()
+    {
+        $task = new CommandTask($this->getIo());
+        $nested = $task->getShellList();
+        $out = [];
+        foreach ($nested as $section => $commands) {
+            $prefix = '';
+            if ($section !== 'CORE' && $section !== 'app') {
+                $prefix = Inflector::underscore($section) . '.';
+            }
+            foreach ($commands as $command) {
+                $out[$prefix . $command] = $command;
+            }
+        }
+
+        return $out;
+    }
+
+    /**
      * Output text.
      *
      * @param \Cake\Console\CommandCollection $commands The command collection to output.

+ 19 - 6
tests/TestCase/Shell/HelpShellTest.php

@@ -48,16 +48,17 @@ class HelpShellTest extends TestCase
     }
 
     /**
-     * Test the command listing
+     * Test the command listing fallback when no commands are set
      *
      * @return void
      */
-    public function testMainNoCommands()
+    public function testMainNoCommandsFallback()
     {
         $shell = new HelpShell($this->io);
         $this->assertNull($shell->main());
-        $err = $this->err->messages();
-        $this->assertContains('Could not print command list', $err[0]);
+
+        $output = implode("\n", $this->out->messages());
+        $this->assertOutput($output);
     }
 
     /**
@@ -67,12 +68,24 @@ class HelpShellTest extends TestCase
      */
     public function testMain()
     {
-        $this->shell->main();
+        $this->assertNull($this->shell->main());
+
         $output = implode("\n", $this->out->messages());
+        $this->assertOutput($output);
+    }
+
+    /**
+     * Assert the help output.
+     *
+     * @param string $output The output to check.
+     * @return void
+     */
+    protected function assertOutput($output)
+    {
         $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('- example', $output, 'Short plugin name');
         $this->assertContains('To run a command', $output, 'more info present');
         $this->assertContains('To get help', $output, 'more info present');
     }