Skip to content

Commit

Permalink
Merge pull request #7784 from HavokInspiration/completion-shell-enhan…
Browse files Browse the repository at this point in the history
…cements

Completion shell enhancements
  • Loading branch information
lorenzo committed Dec 4, 2015
2 parents aa8fa71 + b34ff73 commit a6a1924
Show file tree
Hide file tree
Showing 7 changed files with 161 additions and 12 deletions.
36 changes: 29 additions & 7 deletions src/Shell/Task/CommandTask.php
Expand Up @@ -19,6 +19,7 @@
use Cake\Core\App;
use Cake\Core\Plugin;
use Cake\Filesystem\Folder;
use Cake\Utility\Hash;
use Cake\Utility\Inflector;
use ReflectionClass;
use ReflectionMethod;
Expand Down Expand Up @@ -109,15 +110,22 @@ protected function _scanDir($dir)
public function commands()
{
$shellList = $this->getShellList();
$flatten = Hash::flatten($shellList);
$duplicates = array_intersect($flatten, array_unique(array_diff_key($flatten, array_unique($flatten))));
$duplicates = Hash::expand($duplicates);

$options = [];
foreach ($shellList as $type => $commands) {
$prefix = '';
if (!in_array(strtolower($type), ['app', 'core'])) {
$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;
}
}
Expand All @@ -143,7 +151,7 @@ public function subCommands($commandName)
$return = array_keys($taskMap);
$return = array_map('Cake\Utility\Inflector::underscore', $return);

$shellMethodNames = ['main', 'help', 'getOptionParser'];
$shellMethodNames = ['main', 'help', 'getOptionParser', 'initialize', 'runCommand'];

$baseClasses = ['Object', 'Shell', 'AppShell'];

Expand Down Expand Up @@ -178,10 +186,24 @@ public function getShell($commandName)
$pluginDot = '';
}

if (!in_array($commandName, $this->commands())) {
if (!in_array($commandName, $this->commands()) && (empty($pluginDot) && !in_array($name, $this->commands()))) {
return false;
}

if (empty($pluginDot)) {
$shellList = $this->getShellList();

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;
}
}
}
}

$name = Inflector::camelize($name);
$pluginDot = Inflector::camelize($pluginDot);
$class = App::className($pluginDot . $name, 'Shell', 'Shell');
Expand Down
2 changes: 1 addition & 1 deletion tests/TestCase/Shell/CommandListShellTest.php
Expand Up @@ -81,7 +81,7 @@ public function testMain()
$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/";
Expand Down
69 changes: 65 additions & 4 deletions tests/TestCase/Shell/CompletionShellTest.php
Expand Up @@ -17,6 +17,7 @@
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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -78,6 +80,7 @@ public function tearDown()
{
parent::tearDown();
unset($this->Shell);
Configure::write('App.namespace', 'App');
Plugin::unload();
}

Expand Down Expand Up @@ -119,7 +122,7 @@ public function testCommands()
$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);
}
Expand Down Expand Up @@ -190,16 +193,31 @@ public function testSubCommandsAppPlugin()
$this->Shell->runCommand(['subcommands', 'app.sample']);
$output = $this->out->output;

$expected = '';
$this->assertEquals($expected, $output);
$expected = "derp\n";
$this->assertTextEquals($expected, $output);
}

/**
* 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', 'welcome']);
$output = $this->out->output;

$expected = "say_hello\n";
$this->assertTextEquals($expected, $output);
}

/**
* test that using the dot notation when not mandatory works to provide backward compatibility
*
* @return void
*/
public function testSubCommandsPluginDotNotationBackwardCompatibility()
{
$this->Shell->runCommand(['subcommands', 'TestPluginTwo.welcome']);
$output = $this->out->output;
Expand All @@ -208,6 +226,49 @@ public function testSubCommandsPlugin()
$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";
$this->assertTextEquals($expected, $output);
}

/**
* 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->assertTextEquals($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->assertTextEquals($expected, $output);
}

/**
* test that subcommands without arguments returns nothing
*
Expand Down
10 changes: 10 additions & 0 deletions tests/test_app/Plugin/TestPlugin/src/Shell/SampleShell.php
Expand Up @@ -33,4 +33,14 @@ public function main()
{
$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 changes: 10 additions & 0 deletions tests/test_app/Plugin/TestPluginTwo/src/Shell/ExampleShell.php
Expand Up @@ -33,4 +33,14 @@ public function main()
{
$this->out('This is the main method called from TestPluginTwo.ExampleShell');
}

/**
* say_hello method
*
* @return void
*/
public function say_hello()
{
$this->out('Hello from the TestPluginTwo.ExampleShell');
}
}
36 changes: 36 additions & 0 deletions 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');
}
}
10 changes: 10 additions & 0 deletions tests/test_app/TestApp/Shell/SampleShell.php
Expand Up @@ -35,4 +35,14 @@ public function main()
{
$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');
}
}

0 comments on commit a6a1924

Please sign in to comment.