From fd6bdc92d24e28e00723cd9cf723fc38a656afcd Mon Sep 17 00:00:00 2001 From: Mark Story Date: Sun, 17 May 2015 23:00:35 -0400 Subject: [PATCH] Rename Macro -> Helper. A few people think that Macro is the wrong term to use as it implies C/C++/Lisp macros which is nothing like this feature. By using Helper, we can better convey what the classes are for (helping in shells). I've kept the shortform output() method, and also made it possible to get a helper instance in case someone wants to get instances and call non-output() methods on it. --- src/Console/ConsoleIo.php | 25 ++++++----- ...ception.php => MissingHelperException.php} | 6 +-- src/Console/{Macro.php => Helper.php} | 6 +-- .../{MacroRegistry.php => HelperRegistry.php} | 28 ++++++------- .../TableMacro.php => Helper/TableHelper.php} | 9 ++-- src/Shell/RoutesShell.php | 4 +- tests/TestCase/Console/ConsoleIoTest.php | 24 ++++++++--- ...egistryTest.php => HelperRegistryTest.php} | 42 +++++++++---------- .../TableHelperTest.php} | 12 +++--- tests/TestCase/Shell/RoutesShellTest.php | 8 ++-- .../ExampleHelper.php} | 6 +-- .../SimpleHelper.php} | 6 +-- 12 files changed, 95 insertions(+), 81 deletions(-) rename src/Console/Exception/{MissingMacroException.php => MissingHelperException.php} (77%) rename src/Console/{Macro.php => Helper.php} (90%) rename src/Console/{MacroRegistry.php => HelperRegistry.php} (74%) rename src/Shell/{Macro/TableMacro.php => Helper/TableHelper.php} (93%) rename tests/TestCase/Console/{MacroRegistryTest.php => HelperRegistryTest.php} (55%) rename tests/TestCase/Shell/{Macro/TableMacroTest.php => Helper/TableHelperTest.php} (89%) rename tests/test_app/Plugin/TestPlugin/src/Shell/{Macro/ExampleMacro.php => Helper/ExampleHelper.php} (51%) rename tests/test_app/TestApp/Shell/{Macro/SimpleMacro.php => Helper/SimpleHelper.php} (56%) diff --git a/src/Console/ConsoleIo.php b/src/Console/ConsoleIo.php index 6788ef3752a..9213c0add06 100644 --- a/src/Console/ConsoleIo.php +++ b/src/Console/ConsoleIo.php @@ -16,7 +16,7 @@ use Cake\Console\ConsoleInput; use Cake\Console\ConsoleOutput; -use Cake\Console\MacroRegistry; +use Cake\Console\HelperRegistry; use Cake\Log\Engine\ConsoleLog; use Cake\Log\Log; @@ -54,9 +54,9 @@ class ConsoleIo /** * The macro registry. * - * @var \Cake\Console\MacroRegistry + * @var \Cake\Console\HelperRegistry */ - protected $_macros; + protected $_helpers; /** * Output constant making verbose shells. @@ -100,14 +100,14 @@ class ConsoleIo * @param \Cake\Console\ConsoleOutput|null $out A ConsoleOutput object for stdout. * @param \Cake\Console\ConsoleOutput|null $err A ConsoleOutput object for stderr. * @param \Cake\Console\ConsoleInput|null $in A ConsoleInput object for stdin. - * @param \Cake\Console\MacroRegistry|null $macros A MacroRegistry instance + * @param \Cake\Console\HelperRegistry|null $helpers A HelperRegistry instance */ - public function __construct(ConsoleOutput $out = null, ConsoleOutput $err = null, ConsoleInput $in = null, MacroRegistry $macros = null) + public function __construct(ConsoleOutput $out = null, ConsoleOutput $err = null, ConsoleInput $in = null, HelperRegistry $helpers = null) { $this->_out = $out ? $out : new ConsoleOutput('php://stdout'); $this->_err = $err ? $err : new ConsoleOutput('php://stderr'); $this->_in = $in ? $in : new ConsoleInput('php://stdin'); - $this->_macros = $macros ? $macros : new MacroRegistry($this); + $this->_helpers = $helpers ? $helpers : new HelperRegistry($this); } /** @@ -375,20 +375,18 @@ public function setLoggers($enable) } /** - * Render a Console Macro + * Render a Console Helper * * Create and render the output for a macro object. If the macro * object has not already been loaded, it will be loaded and constructed. * * @param string $name The name of the macro to render - * @param array $args The arguments for the macro output. - * @return void + * @return Cake\Console\Helper The created helper instance. */ - public function macro($name, $args = []) + public function helper($name) { $name = ucfirst($name); - $macro = $this->_macros->load($name); - return $macro->output($args); + return $this->_helpers->load($name); } /** @@ -400,6 +398,7 @@ public function macro($name, $args = []) */ public function __call($method, $args) { - return $this->macro($method, $args); + $helper = $this->helper($method, $args); + return $helper->output($args); } } diff --git a/src/Console/Exception/MissingMacroException.php b/src/Console/Exception/MissingHelperException.php similarity index 77% rename from src/Console/Exception/MissingMacroException.php rename to src/Console/Exception/MissingHelperException.php index 9295569f3da..bbff6623373 100644 --- a/src/Console/Exception/MissingMacroException.php +++ b/src/Console/Exception/MissingHelperException.php @@ -15,11 +15,11 @@ use Cake\Core\Exception\Exception; /** - * Used when a Macro cannot be found. + * Used when a Helper cannot be found. * */ -class MissingMacroException extends Exception +class MissingHelperException extends Exception { - protected $_messageTemplate = 'Macro class %s could not be found.'; + protected $_messageTemplate = 'Helper class %s could not be found.'; } diff --git a/src/Console/Macro.php b/src/Console/Helper.php similarity index 90% rename from src/Console/Macro.php rename to src/Console/Helper.php index 90d1058aef7..240595b95f8 100644 --- a/src/Console/Macro.php +++ b/src/Console/Helper.php @@ -17,13 +17,13 @@ use Cake\Console\ConsoleIo; /** - * Base class for Macros. + * Base class for Helpers. * - * Console Macros allow you to package up reusable blocks + * Console Helpers allow you to package up reusable blocks * of Console output logic. For example creating tables, * progress bars or ascii art. */ -abstract class Macro +abstract class Helper { public function __construct(ConsoleIo $io) { diff --git a/src/Console/MacroRegistry.php b/src/Console/HelperRegistry.php similarity index 74% rename from src/Console/MacroRegistry.php rename to src/Console/HelperRegistry.php index dfd104763ed..6712e4e7280 100644 --- a/src/Console/MacroRegistry.php +++ b/src/Console/HelperRegistry.php @@ -14,16 +14,16 @@ */ namespace Cake\Console; -use Cake\Console\Exception\MissingMacroException; +use Cake\Console\Exception\MissingHelperException; use Cake\Core\App; use Cake\Core\ObjectRegistry; use Cake\Console\ConsoleIo; /** - * Registry for Macros. Provides features - * for lazily loading macros. + * Registry for Helpers. Provides features + * for lazily loading helpers. */ -class MacroRegistry extends ObjectRegistry +class HelperRegistry extends ObjectRegistry { /** @@ -44,7 +44,7 @@ public function __construct(ConsoleIo $io) } /** - * Resolve a macro classname. + * Resolve a helper classname. * * Part of the template method for Cake\Core\ObjectRegistry::load() * @@ -53,36 +53,36 @@ public function __construct(ConsoleIo $io) */ protected function _resolveClassName($class) { - return App::className($class, 'Shell/Macro', 'Macro'); + return App::className($class, 'Shell/Helper', 'Helper'); } /** - * Throws an exception when a macro is missing. + * Throws an exception when a helper is missing. * * Part of the template method for Cake\Core\ObjectRegistry::load() * * @param string $class The classname that is missing. - * @param string $plugin The plugin the macro is missing in. + * @param string $plugin The plugin the helper is missing in. * @return void - * @throws \Cake\Console\Exception\MissingMacroException + * @throws \Cake\Console\Exception\MissingHelperException */ protected function _throwMissingClassError($class, $plugin) { - throw new MissingMacroException([ + throw new MissingHelperException([ 'class' => $class, 'plugin' => $plugin ]); } /** - * Create the macro instance. + * Create the helper instance. * * Part of the template method for Cake\Core\ObjectRegistry::load() * * @param string $class The classname to create. - * @param string $alias The alias of the macro. - * @param array $settings An array of settings to use for the macro. - * @return \Cake\Console\Macro The constructed macro class. + * @param string $alias The alias of the helper. + * @param array $settings An array of settings to use for the helper. + * @return \Cake\Console\Helper The constructed helper class. */ protected function _create($class, $alias, $settings) { diff --git a/src/Shell/Macro/TableMacro.php b/src/Shell/Helper/TableHelper.php similarity index 93% rename from src/Shell/Macro/TableMacro.php rename to src/Shell/Helper/TableHelper.php index 0044ae7ce65..929fe3836c5 100644 --- a/src/Shell/Macro/TableMacro.php +++ b/src/Shell/Helper/TableHelper.php @@ -12,15 +12,15 @@ * @since 3.1.0 * @license http://www.opensource.org/licenses/mit-license.php MIT License */ -namespace Cake\Shell\Macro; +namespace Cake\Shell\Helper; -use Cake\Console\Macro; +use Cake\Console\Helper; /** * Create a visually pleasing ASCII art table * from 2 dimensional array data. */ -class TableMacro extends Macro +class TableHelper extends Helper { /** * Calculate the column widths @@ -81,6 +81,9 @@ protected function _render($row, $widths) */ public function output($rows) { + if (count($rows) === 1) { + $rows = $rows[0]; + } $widths = $this->_calculateWidths($rows); $this->_rowSeparator($widths); diff --git a/src/Shell/RoutesShell.php b/src/Shell/RoutesShell.php index 905c6e709fd..a4a08b6b3c2 100644 --- a/src/Shell/RoutesShell.php +++ b/src/Shell/RoutesShell.php @@ -40,7 +40,7 @@ public function main() foreach (Router::routes() as $route) { $output[] = [$route->getName(), $route->template, json_encode($route->defaults)]; } - $this->_io->macro('table', $output); + $this->_io->table($output); } /** @@ -57,7 +57,7 @@ public function check($url) ['Route name', 'URI template', 'Defaults'], ['', $url, json_encode($route)] ]; - $this->_io->macro('table', $output); + $this->_io->table($output); } catch (MissingRouteException $e) { $this->err("'$url' did not match any routes."); return false; diff --git a/tests/TestCase/Console/ConsoleIoTest.php b/tests/TestCase/Console/ConsoleIoTest.php index 391bdb3bf94..fdbbcc15c4b 100644 --- a/tests/TestCase/Console/ConsoleIoTest.php +++ b/tests/TestCase/Console/ConsoleIoTest.php @@ -359,16 +359,30 @@ public function testStyles() } /** - * Test the macro method. + * Test the helper method. * * @return void */ - public function testMacro() + public function testHelper() { - $this->out->expects($this->exactly(2)) + $this->out->expects($this->once()) + ->method('write') + ->with('It works!well ish'); + $helper = $this->io->helper('simple'); + $this->assertInstanceOf('Cake\Console\Helper', $helper); + $helper->output(['well', 'ish']); + } + + /** + * Test the helper __call. + * + * @return void + */ + public function testHelperCall() + { + $this->out->expects($this->once()) ->method('write') ->with('It works!well ish'); - $this->io->macro('simple', ['well', 'ish']); - $this->io->simple('well', 'ish'); + $helper = $this->io->simple('well', 'ish'); } } diff --git a/tests/TestCase/Console/MacroRegistryTest.php b/tests/TestCase/Console/HelperRegistryTest.php similarity index 55% rename from tests/TestCase/Console/MacroRegistryTest.php rename to tests/TestCase/Console/HelperRegistryTest.php index 81fa1d2be7e..8546b72b0b5 100644 --- a/tests/TestCase/Console/MacroRegistryTest.php +++ b/tests/TestCase/Console/HelperRegistryTest.php @@ -14,16 +14,16 @@ */ namespace Cake\Test\TestCase\Console; -use Cake\Console\MacroRegistry; +use Cake\Console\HelperRegistry; use Cake\Core\Configure; use Cake\Core\Plugin; use Cake\TestSuite\TestCase; /** - * Class MacroRegistryTest + * Class HelperRegistryTest * */ -class MacroRegistryTest extends TestCase +class HelperRegistryTest extends TestCase { /** @@ -36,7 +36,7 @@ public function setUp() parent::setUp(); Configure::write('App.namespace', 'TestApp'); $io = $this->getMock('Cake\Console\ConsoleIo', [], [], '', false); - $this->macros = new MacroRegistry($io); + $this->helpers = new HelperRegistry($io); } /** @@ -46,7 +46,7 @@ public function setUp() */ public function tearDown() { - unset($this->macros); + unset($this->helpers); parent::tearDown(); } @@ -57,23 +57,23 @@ public function tearDown() */ public function testLoad() { - $result = $this->macros->load('Simple'); - $this->assertInstanceOf('TestApp\Shell\Macro\SimpleMacro', $result); - $this->assertInstanceOf('TestApp\Shell\Macro\SimpleMacro', $this->macros->Simple); + $result = $this->helpers->load('Simple'); + $this->assertInstanceOf('TestApp\Shell\Helper\SimpleHelper', $result); + $this->assertInstanceOf('TestApp\Shell\Helper\SimpleHelper', $this->helpers->Simple); - $result = $this->macros->loaded(); + $result = $this->helpers->loaded(); $this->assertEquals(['Simple'], $result, 'loaded() results are wrong.'); } /** * test missingtask exception * - * @expectedException \Cake\Console\Exception\MissingMacroException + * @expectedException \Cake\Console\Exception\MissingHelperException * @return void */ - public function testLoadMissingMacro() + public function testLoadMissingHelper() { - $this->macros->load('ThisTaskShouldAlwaysBeMissing'); + $this->helpers->load('ThisTaskShouldAlwaysBeMissing'); } /** @@ -85,18 +85,18 @@ public function testLoadWithAlias() { Plugin::load('TestPlugin'); - $result = $this->macros->load('SimpleAliased', ['className' => 'Simple']); - $this->assertInstanceOf('TestApp\Shell\Macro\SimpleMacro', $result); - $this->assertInstanceOf('TestApp\Shell\Macro\SimpleMacro', $this->macros->SimpleAliased); + $result = $this->helpers->load('SimpleAliased', ['className' => 'Simple']); + $this->assertInstanceOf('TestApp\Shell\Helper\SimpleHelper', $result); + $this->assertInstanceOf('TestApp\Shell\Helper\SimpleHelper', $this->helpers->SimpleAliased); - $result = $this->macros->loaded(); + $result = $this->helpers->loaded(); $this->assertEquals(['SimpleAliased'], $result, 'loaded() results are wrong.'); - $result = $this->macros->load('SomeMacro', ['className' => 'TestPlugin.Example']); - $this->assertInstanceOf('TestPlugin\Shell\Macro\ExampleMacro', $result); - $this->assertInstanceOf('TestPlugin\Shell\Macro\ExampleMacro', $this->macros->SomeMacro); + $result = $this->helpers->load('SomeHelper', ['className' => 'TestPlugin.Example']); + $this->assertInstanceOf('TestPlugin\Shell\Helper\ExampleHelper', $result); + $this->assertInstanceOf('TestPlugin\Shell\Helper\ExampleHelper', $this->helpers->SomeHelper); - $result = $this->macros->loaded(); - $this->assertEquals(['SimpleAliased', 'SomeMacro'], $result, 'loaded() results are wrong.'); + $result = $this->helpers->loaded(); + $this->assertEquals(['SimpleAliased', 'SomeHelper'], $result, 'loaded() results are wrong.'); } } diff --git a/tests/TestCase/Shell/Macro/TableMacroTest.php b/tests/TestCase/Shell/Helper/TableHelperTest.php similarity index 89% rename from tests/TestCase/Shell/Macro/TableMacroTest.php rename to tests/TestCase/Shell/Helper/TableHelperTest.php index fa21531f941..63d2c3db8e2 100644 --- a/tests/TestCase/Shell/Macro/TableMacroTest.php +++ b/tests/TestCase/Shell/Helper/TableHelperTest.php @@ -12,11 +12,11 @@ * @since 3.1.0 * @license http://www.opensource.org/licenses/mit-license.php MIT License */ -namespace Cake\Test\TestCase\Shell\Macro; +namespace Cake\Test\TestCase\Shell\Helper; use Cake\Console\ConsoleIo; use Cake\Console\ConsoleOutput; -use Cake\Shell\Macro\TableMacro; +use Cake\Shell\Helper\TableHelper; use Cake\TestSuite\TestCase; /** @@ -38,9 +38,9 @@ public function messages() } /** - * TableMacro test. + * TableHelper test. */ -class TableMacroTest extends TestCase +class TableHelperTest extends TestCase { /** @@ -54,7 +54,7 @@ public function setUp() $this->stub = new StubOutput(); $this->io = new ConsoleIo($this->stub); - $this->macro = new TableMacro($this->io); + $this->helper = new TableHelper($this->io); } /** @@ -69,7 +69,7 @@ public function testOutput() ['short', 'Longish thing', 'short'], ['Longer thing', 'short', 'Longest Value'], ]; - $this->macro->output($data); + $this->helper->output($data); $expected = [ '+--------------+---------------+---------------+', '| Header 1 | Header | Long Header |', diff --git a/tests/TestCase/Shell/RoutesShellTest.php b/tests/TestCase/Shell/RoutesShellTest.php index a34ac90e41b..d8d6f9414c8 100644 --- a/tests/TestCase/Shell/RoutesShellTest.php +++ b/tests/TestCase/Shell/RoutesShellTest.php @@ -35,7 +35,7 @@ class RoutesShellTest extends TestCase public function setUp() { parent::setUp(); - $this->io = $this->getMock('Cake\Console\ConsoleIo'); + $this->io = $this->getMock('Cake\Console\ConsoleIo', ['table', 'out', 'err']); $this->shell = new RoutesShell($this->io); Router::connect('/articles/:action/*', ['controller' => 'Articles']); @@ -62,9 +62,8 @@ public function tearDown() public function testMain() { $this->io->expects($this->at(0)) - ->method('macro') + ->method('table') ->with( - 'table', $this->logicalAnd( $this->contains(['Route name', 'URI template', 'Defaults']), $this->contains([ @@ -85,9 +84,8 @@ public function testMain() public function testCheck() { $this->io->expects($this->at(0)) - ->method('macro') + ->method('table') ->with( - 'table', $this->logicalAnd( $this->contains(['Route name', 'URI template', 'Defaults']), $this->contains([ diff --git a/tests/test_app/Plugin/TestPlugin/src/Shell/Macro/ExampleMacro.php b/tests/test_app/Plugin/TestPlugin/src/Shell/Helper/ExampleHelper.php similarity index 51% rename from tests/test_app/Plugin/TestPlugin/src/Shell/Macro/ExampleMacro.php rename to tests/test_app/Plugin/TestPlugin/src/Shell/Helper/ExampleHelper.php index 8248d8d8763..3892b8b0432 100644 --- a/tests/test_app/Plugin/TestPlugin/src/Shell/Macro/ExampleMacro.php +++ b/tests/test_app/Plugin/TestPlugin/src/Shell/Helper/ExampleHelper.php @@ -1,9 +1,9 @@