Skip to content
Permalink
Browse files

Allow console helpers to live in the command namespace.

It will be weird if helpers used in command classes live under the shell
namespace. This allows helpers to be found in either namespace.

Refs #11137
  • Loading branch information...
markstory committed Mar 10, 2018
1 parent f9816db commit eeddb5fe22385b28aa4f7690ef4d28c02d06cb4f
Showing with 32 additions and 6 deletions.
  1. +8 −0 src/Console/HelperRegistry.php
  2. +24 −6 tests/TestCase/Console/HelperRegistryTest.php
@@ -46,13 +46,21 @@ public function setIo(ConsoleIo $io)
/**
* Resolve a helper classname.
*
* Will prefer helpers defined in Command\Helper over those
* defined in Shell\Helper.
*
* Part of the template method for Cake\Core\ObjectRegistry::load()
*
* @param string $class Partial classname to resolve.
* @return string|false Either the correct classname or false.
*/
protected function _resolveClassName($class)
{
$name = App::className($class, 'Command/Helper', 'Helper');
if ($name) {
return $name;
}
return App::className($class, 'Shell/Helper', 'Helper');
}
@@ -16,6 +16,9 @@
use Cake\Console\HelperRegistry;
use Cake\Core\Plugin;
use Cake\TestSuite\TestCase;
use TestApp\Command\Helper\CommandHelper;
use TestApp\Shell\Helper\SimpleHelper;
use TestPlugin\Shell\Helper\ExampleHelper;
/**
* HelperRegistryTest
@@ -58,13 +61,28 @@ public function tearDown()
public function testLoad()
{
$result = $this->helpers->load('Simple');
$this->assertInstanceOf('TestApp\Shell\Helper\SimpleHelper', $result);
$this->assertInstanceOf('TestApp\Shell\Helper\SimpleHelper', $this->helpers->Simple);
$this->assertInstanceOf(SimpleHelper::class, $result);
$this->assertInstanceOf(SimpleHelper::class, $this->helpers->Simple);
$result = $this->helpers->loaded();
$this->assertEquals(['Simple'], $result, 'loaded() results are wrong.');
}
/**
* test loading helpers.
*
* @return void
*/
public function testLoadCommandNamespace()
{
$result = $this->helpers->load('Command');
$this->assertInstanceOf(CommandHelper::class, $result);
$this->assertInstanceOf(CommandHelper::class, $this->helpers->Command);
$result = $this->helpers->loaded();
$this->assertEquals(['Command'], $result, 'loaded() results are wrong.');
}
/**
* test triggering callbacks on loaded helpers
*
@@ -97,15 +115,15 @@ public function testLoadWithAlias()
Plugin::load('TestPlugin');
$result = $this->helpers->load('SimpleAliased', ['className' => 'Simple']);
$this->assertInstanceOf('TestApp\Shell\Helper\SimpleHelper', $result);
$this->assertInstanceOf('TestApp\Shell\Helper\SimpleHelper', $this->helpers->SimpleAliased);
$this->assertInstanceOf(SimpleHelper::class, $result);
$this->assertInstanceOf(SimpleHelper::class, $this->helpers->SimpleAliased);
$result = $this->helpers->loaded();
$this->assertEquals(['SimpleAliased'], $result, 'loaded() results are wrong.');
$result = $this->helpers->load('SomeHelper', ['className' => 'TestPlugin.Example']);
$this->assertInstanceOf('TestPlugin\Shell\Helper\ExampleHelper', $result);
$this->assertInstanceOf('TestPlugin\Shell\Helper\ExampleHelper', $this->helpers->SomeHelper);
$this->assertInstanceOf(ExampleHelper::class, $result);
$this->assertInstanceOf(ExampleHelper::class, $this->helpers->SomeHelper);
$result = $this->helpers->loaded();
$this->assertEquals(['SimpleAliased', 'SomeHelper'], $result, 'loaded() results are wrong.');

0 comments on commit eeddb5f

Please sign in to comment.
You can’t perform that action at this time.