Browse Source

Add macro integration into ConsoleIo.

Mark Story 11 years ago
parent
commit
5dff76d1fa

+ 16 - 3
src/Console/ConsoleIo.php

@@ -380,13 +380,26 @@ class ConsoleIo
      * Create and render the output for a macro object. If the macro
      * object has not already been loaded, it will be loaded and constructed.
      *
-     * This method accepts variadic arguments that are
-     *
      * @param string $name The name of the macro to render
      * @param array $args The arguments for the macro output.
      * @return void
      */
-    public function macro($name, $args)
+    public function macro($name, $args = [])
+    {
+        $name = ucfirst($name);
+        $macro = $this->_macros->load($name);
+        return $macro->output($args);
+    }
+
+    /**
+     * Conveinence wrapper around macro()
+     *
+     * @param string $method The macro to invoke.
+     * @param array $args The arguments for the macro.
+     * @return mixed
+     */
+    public function __call($method, $args)
     {
+        return $this->macro($method, $args);
     }
 }

+ 16 - 0
tests/TestCase/Console/ConsoleIoTest.php

@@ -15,6 +15,7 @@
 namespace Cake\Test\TestCase\Console;
 
 use Cake\Console\ConsoleIo;
+use Cake\Core\Configure;
 use Cake\Log\Log;
 use Cake\TestSuite\TestCase;
 
@@ -32,6 +33,7 @@ class ConsoleIoTest extends TestCase
     public function setUp()
     {
         parent::setUp();
+        Configure::write('App.namespace', 'TestApp');
 
         $this->out = $this->getMock('Cake\Console\ConsoleOutput', [], [], '', false);
         $this->err = $this->getMock('Cake\Console\ConsoleOutput', [], [], '', false);
@@ -355,4 +357,18 @@ class ConsoleIoTest extends TestCase
             ->with('name', 'props');
         $this->io->styles('name', 'props');
     }
+
+    /**
+     * Test the macro method.
+     *
+     * @return void
+     */
+    public function testMacro()
+    {
+        $this->out->expects($this->exactly(2))
+            ->method('write')
+            ->with('It works!well ish');
+        $this->io->macro('simple', ['well', 'ish']);
+        $this->io->simple('well', 'ish');
+    }
 }

+ 1 - 1
tests/test_app/TestApp/Shell/Macro/SimpleMacro.php

@@ -7,6 +7,6 @@ class SimpleMacro extends Macro
 {
     public function output($args)
     {
-        $this->_io->out('It works!');
+        $this->_io->out('It works!' . implode(' ', $args));
     }
 }