Skip to content

Commit

Permalink
Makes sure that App Shells and plugin Shells with the same name can c…
Browse files Browse the repository at this point in the history
…o-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.
  • Loading branch information
HavokInspiration committed Dec 3, 2015
1 parent 58ca430 commit c208e88
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 6 deletions.
13 changes: 8 additions & 5 deletions src/Shell/Task/CommandTask.php
Expand Up @@ -192,11 +192,14 @@ public function getShell($commandName)

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;
}
}
}
}
Expand Down
34 changes: 33 additions & 1 deletion 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 @@ -190,7 +193,7 @@ public function testSubCommandsAppPlugin()
$this->Shell->runCommand(['subcommands', 'app.sample']);
$output = $this->out->output;

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

Expand Down Expand Up @@ -237,6 +240,35 @@ public function testSubCommandsPluginDotNotation()
$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->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
*
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/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 c208e88

Please sign in to comment.