Browse Source

Merge pull request #10503 from cleptric/outputas-getter-setter

Split ConsoleOutput::outputAs() into getter/setter
Mark Story 9 years ago
parent
commit
25e5dc5667

+ 14 - 1
src/Console/ConsoleIo.php

@@ -262,11 +262,24 @@ class ConsoleIo
      *
      * @param int $mode The output mode.
      * @return void
+     * @see \Cake\Console\ConsoleOutput::setOutputAs()
+     */
+    public function setOutputAs($mode)
+    {
+        $this->_out->setOutputAs($mode);
+    }
+
+    /**
+     * Change the output mode of the stdout stream
+     *
+     * @deprecated 3.5.0 Use setOutputAs() instead.
+     * @param int $mode The output mode.
+     * @return void
      * @see \Cake\Console\ConsoleOutput::outputAs()
      */
     public function outputAs($mode)
     {
-        $this->_out->outputAs($mode);
+        $this->_out->setOutputAs($mode);
     }
 
     /**

+ 29 - 0
src/Console/ConsoleOutput.php

@@ -14,6 +14,8 @@
  */
 namespace Cake\Console;
 
+use InvalidArgumentException;
+
 /**
  * Object wrapper for outputting information from a shell application.
  * Can be connected to any stream resource that can be used with fopen()
@@ -303,8 +305,35 @@ class ConsoleOutput
     }
 
     /**
+     * Get the output type on how formatting tags are treated.
+     *
+     * @return int
+     */
+    public function getOutputAs()
+    {
+        return $this->_outputAs;
+    }
+
+    /**
+     * Set the output type on how formatting tags are treated.
+     *
+     * @param int $type The output type to use. Should be one of the class constants.
+     * @return void
+     * @throws \InvalidArgumentException in case of a not supported output type.
+     */
+    public function setOutputAs($type)
+    {
+        if (!in_array($type, [self::RAW, self::PLAIN, self::COLOR], true)) {
+            throw new InvalidArgumentException(sprintf('Invalid output type "%s".', $type));
+        }
+
+        $this->_outputAs = $type;
+    }
+
+    /**
      * Get/Set the output type to use. The output type how formatting tags are treated.
      *
+     * @deprecated 3.5.0 Use getOutputAs()/setOutputAs() instead.
      * @param int|null $type The output type to use. Should be one of the class constants.
      * @return int|null Either null or the value if getting.
      */

+ 1 - 1
src/Console/Shell.php

@@ -531,7 +531,7 @@ class Shell
         $format = 'text';
         if (!empty($this->args[0]) && $this->args[0] === 'xml') {
             $format = 'xml';
-            $this->_io->outputAs(ConsoleOutput::RAW);
+            $this->_io->setOutputAs(ConsoleOutput::RAW);
         } else {
             $this->_welcome();
         }

+ 1 - 1
src/Log/Engine/ConsoleLog.php

@@ -75,7 +75,7 @@ class ConsoleLog extends BaseLog
         } else {
             throw new InvalidArgumentException('`stream` not a ConsoleOutput nor string');
         }
-        $this->_output->outputAs($config['outputAs']);
+        $this->_output->setOutputAs($config['outputAs']);
     }
 
     /**

+ 1 - 1
src/Shell/CommandListShell.php

@@ -126,7 +126,7 @@ class CommandListShell extends Shell
                 $shell->addAttribute('help', $callable . ' -h');
             }
         }
-        $this->_io->outputAs(ConsoleOutput::RAW);
+        $this->_io->setOutputAs(ConsoleOutput::RAW);
         $this->out($shells->saveXML());
     }
 

+ 25 - 0
tests/TestCase/Console/ConsoleOutputTest.php

@@ -250,6 +250,31 @@ class ConsoleOutputTest extends TestCase
     }
 
     /**
+     * test set plain output.
+     *
+     * @return void
+     */
+    public function testSetOutputAsPlain()
+    {
+        $this->output->setOutputAs(ConsoleOutput::PLAIN);
+        $this->output->expects($this->once())->method('_write')
+            ->with('Bad Regular');
+
+        $this->output->write('<error>Bad</error> Regular', false);
+    }
+
+    /**
+     * test set wrong type.
+     *
+     * @expectedException \InvalidArgumentException
+     * @expectedExceptionMessage Invalid output type "Foo".
+     */
+    public function testSetOutputWrongType()
+    {
+        $this->output->setOutputAs('Foo');
+    }
+
+    /**
      * test plain output only strips tags used for formatting.
      *
      * @return void

+ 1 - 1
tests/TestCase/Log/Engine/ConsoleLogTest.php

@@ -76,7 +76,7 @@ class ConsoleLogTest extends TestCase
         $output = $this->getMockBuilder('Cake\Console\ConsoleOutput')->getMock();
 
         $output->expects($this->at(0))
-            ->method('outputAs')
+            ->method('setOutputAs')
             ->with($expected);
 
         $log = new ConsoleLog([