4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Console\Tests\Output;
14 use Symfony\Component\Console\Output\Output;
15 use Symfony\Component\Console\Formatter\OutputFormatterStyle;
17 class OutputTest extends \PHPUnit_Framework_TestCase
19 public function testConstructor()
21 $output = new TestOutput(Output::VERBOSITY_QUIET, true);
22 $this->assertEquals(Output::VERBOSITY_QUIET, $output->getVerbosity(), '__construct() takes the verbosity as its first argument');
23 $this->assertTrue($output->isDecorated(), '__construct() takes the decorated flag as its second argument');
26 public function testSetIsDecorated()
28 $output = new TestOutput();
29 $output->setDecorated(true);
30 $this->assertTrue($output->isDecorated(), 'setDecorated() sets the decorated flag');
33 public function testSetGetVerbosity()
35 $output = new TestOutput();
36 $output->setVerbosity(Output::VERBOSITY_QUIET);
37 $this->assertEquals(Output::VERBOSITY_QUIET, $output->getVerbosity(), '->setVerbosity() sets the verbosity');
40 public function testWrite()
42 $fooStyle = new OutputFormatterStyle('yellow', 'red', array('blink'));
43 $output = new TestOutput(Output::VERBOSITY_QUIET);
44 $output->writeln('foo');
45 $this->assertEquals('', $output->output, '->writeln() outputs nothing if verbosity is set to VERBOSITY_QUIET');
47 $output = new TestOutput();
48 $output->writeln(array('foo', 'bar'));
49 $this->assertEquals("foo\nbar\n", $output->output, '->writeln() can take an array of messages to output');
51 $output = new TestOutput();
52 $output->writeln('<info>foo</info>', Output::OUTPUT_RAW);
53 $this->assertEquals("<info>foo</info>\n", $output->output, '->writeln() outputs the raw message if OUTPUT_RAW is specified');
55 $output = new TestOutput();
56 $output->writeln('<info>foo</info>', Output::OUTPUT_PLAIN);
57 $this->assertEquals("foo\n", $output->output, '->writeln() strips decoration tags if OUTPUT_PLAIN is specified');
59 $output = new TestOutput();
60 $output->setDecorated(false);
61 $output->writeln('<info>foo</info>');
62 $this->assertEquals("foo\n", $output->output, '->writeln() strips decoration tags if decoration is set to false');
64 $output = new TestOutput();
65 $output->getFormatter()->setStyle('FOO', $fooStyle);
66 $output->setDecorated(true);
67 $output->writeln('<foo>foo</foo>');
68 $this->assertEquals("\033[33;41;5mfoo\033[0m\n", $output->output, '->writeln() decorates the output');
71 $output->writeln('<foo>foo</foo>', 24);
72 $this->fail('->writeln() throws an \InvalidArgumentException when the type does not exist');
73 } catch (\Exception $e) {
74 $this->assertInstanceOf('\InvalidArgumentException', $e, '->writeln() throws an \InvalidArgumentException when the type does not exist');
75 $this->assertEquals('Unknown output type given (24)', $e->getMessage());
79 $output->write('<bar>foo</bar>');
80 $this->assertEquals('<bar>foo</bar>', $output->output, '->write() do nothing when a style does not exist');
83 $output->writeln('<bar>foo</bar>');
84 $this->assertEquals("<bar>foo</bar>\n", $output->output, '->writeln() do nothing when a style does not exist');
88 class TestOutput extends Output
92 public function clear()
97 protected function doWrite($message, $newline)
99 $this->output .= $message.($newline ? "\n" : '');