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 PHPUnit\Framework\TestCase;
15 use Symfony\Component\Console\Formatter\OutputFormatterStyle;
16 use Symfony\Component\Console\Output\Output;
18 class OutputTest extends TestCase
20 public function testConstructor()
22 $output = new TestOutput(Output::VERBOSITY_QUIET, true);
23 $this->assertEquals(Output::VERBOSITY_QUIET, $output->getVerbosity(), '__construct() takes the verbosity as its first argument');
24 $this->assertTrue($output->isDecorated(), '__construct() takes the decorated flag as its second argument');
27 public function testSetIsDecorated()
29 $output = new TestOutput();
30 $output->setDecorated(true);
31 $this->assertTrue($output->isDecorated(), 'setDecorated() sets the decorated flag');
34 public function testSetGetVerbosity()
36 $output = new TestOutput();
37 $output->setVerbosity(Output::VERBOSITY_QUIET);
38 $this->assertEquals(Output::VERBOSITY_QUIET, $output->getVerbosity(), '->setVerbosity() sets the verbosity');
40 $this->assertTrue($output->isQuiet());
41 $this->assertFalse($output->isVerbose());
42 $this->assertFalse($output->isVeryVerbose());
43 $this->assertFalse($output->isDebug());
45 $output->setVerbosity(Output::VERBOSITY_NORMAL);
46 $this->assertFalse($output->isQuiet());
47 $this->assertFalse($output->isVerbose());
48 $this->assertFalse($output->isVeryVerbose());
49 $this->assertFalse($output->isDebug());
51 $output->setVerbosity(Output::VERBOSITY_VERBOSE);
52 $this->assertFalse($output->isQuiet());
53 $this->assertTrue($output->isVerbose());
54 $this->assertFalse($output->isVeryVerbose());
55 $this->assertFalse($output->isDebug());
57 $output->setVerbosity(Output::VERBOSITY_VERY_VERBOSE);
58 $this->assertFalse($output->isQuiet());
59 $this->assertTrue($output->isVerbose());
60 $this->assertTrue($output->isVeryVerbose());
61 $this->assertFalse($output->isDebug());
63 $output->setVerbosity(Output::VERBOSITY_DEBUG);
64 $this->assertFalse($output->isQuiet());
65 $this->assertTrue($output->isVerbose());
66 $this->assertTrue($output->isVeryVerbose());
67 $this->assertTrue($output->isDebug());
70 public function testWriteWithVerbosityQuiet()
72 $output = new TestOutput(Output::VERBOSITY_QUIET);
73 $output->writeln('foo');
74 $this->assertEquals('', $output->output, '->writeln() outputs nothing if verbosity is set to VERBOSITY_QUIET');
77 public function testWriteAnArrayOfMessages()
79 $output = new TestOutput();
80 $output->writeln(array('foo', 'bar'));
81 $this->assertEquals("foo\nbar\n", $output->output, '->writeln() can take an array of messages to output');
85 * @dataProvider provideWriteArguments
87 public function testWriteRawMessage($message, $type, $expectedOutput)
89 $output = new TestOutput();
90 $output->writeln($message, $type);
91 $this->assertEquals($expectedOutput, $output->output);
94 public function provideWriteArguments()
97 array('<info>foo</info>', Output::OUTPUT_RAW, "<info>foo</info>\n"),
98 array('<info>foo</info>', Output::OUTPUT_PLAIN, "foo\n"),
102 public function testWriteWithDecorationTurnedOff()
104 $output = new TestOutput();
105 $output->setDecorated(false);
106 $output->writeln('<info>foo</info>');
107 $this->assertEquals("foo\n", $output->output, '->writeln() strips decoration tags if decoration is set to false');
110 public function testWriteDecoratedMessage()
112 $fooStyle = new OutputFormatterStyle('yellow', 'red', array('blink'));
113 $output = new TestOutput();
114 $output->getFormatter()->setStyle('FOO', $fooStyle);
115 $output->setDecorated(true);
116 $output->writeln('<foo>foo</foo>');
117 $this->assertEquals("\033[33;41;5mfoo\033[39;49;25m\n", $output->output, '->writeln() decorates the output');
120 public function testWriteWithInvalidStyle()
122 $output = new TestOutput();
125 $output->write('<bar>foo</bar>');
126 $this->assertEquals('<bar>foo</bar>', $output->output, '->write() do nothing when a style does not exist');
129 $output->writeln('<bar>foo</bar>');
130 $this->assertEquals("<bar>foo</bar>\n", $output->output, '->writeln() do nothing when a style does not exist');
134 * @dataProvider verbosityProvider
136 public function testWriteWithVerbosityOption($verbosity, $expected, $msg)
138 $output = new TestOutput();
140 $output->setVerbosity($verbosity);
142 $output->write('1', false);
143 $output->write('2', false, Output::VERBOSITY_QUIET);
144 $output->write('3', false, Output::VERBOSITY_NORMAL);
145 $output->write('4', false, Output::VERBOSITY_VERBOSE);
146 $output->write('5', false, Output::VERBOSITY_VERY_VERBOSE);
147 $output->write('6', false, Output::VERBOSITY_DEBUG);
148 $this->assertEquals($expected, $output->output, $msg);
151 public function verbosityProvider()
154 array(Output::VERBOSITY_QUIET, '2', '->write() in QUIET mode only outputs when an explicit QUIET verbosity is passed'),
155 array(Output::VERBOSITY_NORMAL, '123', '->write() in NORMAL mode outputs anything below an explicit VERBOSE verbosity'),
156 array(Output::VERBOSITY_VERBOSE, '1234', '->write() in VERBOSE mode outputs anything below an explicit VERY_VERBOSE verbosity'),
157 array(Output::VERBOSITY_VERY_VERBOSE, '12345', '->write() in VERY_VERBOSE mode outputs anything below an explicit DEBUG verbosity'),
158 array(Output::VERBOSITY_DEBUG, '123456', '->write() in DEBUG mode outputs everything'),
163 class TestOutput extends Output
167 public function clear()
172 protected function doWrite($message, $newline)
174 $this->output .= $message.($newline ? "\n" : '');