Skip to content

Commit

Permalink
Add check for valid output type
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Hoffmann committed Apr 17, 2017
1 parent 781466c commit ce8bc83
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/Console/ConsoleOutput.php
Expand Up @@ -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()
Expand Down Expand Up @@ -317,9 +319,14 @@ public function getOutputAs()
*
* @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;
}

Expand Down
11 changes: 11 additions & 0 deletions tests/TestCase/Console/ConsoleOutputTest.php
Expand Up @@ -263,6 +263,17 @@ public function testSetOutputAsPlain()
$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.
*
Expand Down

0 comments on commit ce8bc83

Please sign in to comment.