Browse Source

Add check for valid output type

Michael Hoffmann 9 years ago
parent
commit
ce8bc83a2e
2 changed files with 18 additions and 0 deletions
  1. 7 0
      src/Console/ConsoleOutput.php
  2. 11 0
      tests/TestCase/Console/ConsoleOutputTest.php

+ 7 - 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()
@@ -317,9 +319,14 @@ class ConsoleOutput
      *
      * @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) === false) {
+            throw new InvalidArgumentException(sprintf('Invalid output type "%s".', $type));
+        }
+
         $this->_outputAs = $type;
     }
 

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

@@ -264,6 +264,17 @@ class ConsoleOutputTest extends TestCase
     }
 
     /**
+     * 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