Yaffs site version 1.1
[yaffs-website] / vendor / symfony / console / Tests / Output / OutputTest.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\Console\Tests\Output;
13
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\Console\Output\Output;
16 use Symfony\Component\Console\Formatter\OutputFormatterStyle;
17
18 class OutputTest extends TestCase
19 {
20     public function testConstructor()
21     {
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');
25     }
26
27     public function testSetIsDecorated()
28     {
29         $output = new TestOutput();
30         $output->setDecorated(true);
31         $this->assertTrue($output->isDecorated(), 'setDecorated() sets the decorated flag');
32     }
33
34     public function testSetGetVerbosity()
35     {
36         $output = new TestOutput();
37         $output->setVerbosity(Output::VERBOSITY_QUIET);
38         $this->assertEquals(Output::VERBOSITY_QUIET, $output->getVerbosity(), '->setVerbosity() sets the verbosity');
39
40         $this->assertTrue($output->isQuiet());
41         $this->assertFalse($output->isVerbose());
42         $this->assertFalse($output->isVeryVerbose());
43         $this->assertFalse($output->isDebug());
44
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());
50
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());
56
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());
62
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());
68     }
69
70     public function testWriteWithVerbosityQuiet()
71     {
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');
75     }
76
77     public function testWriteAnArrayOfMessages()
78     {
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');
82     }
83
84     /**
85      * @dataProvider provideWriteArguments
86      */
87     public function testWriteRawMessage($message, $type, $expectedOutput)
88     {
89         $output = new TestOutput();
90         $output->writeln($message, $type);
91         $this->assertEquals($expectedOutput, $output->output);
92     }
93
94     public function provideWriteArguments()
95     {
96         return array(
97             array('<info>foo</info>', Output::OUTPUT_RAW, "<info>foo</info>\n"),
98             array('<info>foo</info>', Output::OUTPUT_PLAIN, "foo\n"),
99         );
100     }
101
102     public function testWriteWithDecorationTurnedOff()
103     {
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');
108     }
109
110     public function testWriteDecoratedMessage()
111     {
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');
118     }
119
120     public function testWriteWithInvalidStyle()
121     {
122         $output = new TestOutput();
123
124         $output->clear();
125         $output->write('<bar>foo</bar>');
126         $this->assertEquals('<bar>foo</bar>', $output->output, '->write() do nothing when a style does not exist');
127
128         $output->clear();
129         $output->writeln('<bar>foo</bar>');
130         $this->assertEquals("<bar>foo</bar>\n", $output->output, '->writeln() do nothing when a style does not exist');
131     }
132
133     /**
134      * @dataProvider verbosityProvider
135      */
136     public function testWriteWithVerbosityOption($verbosity, $expected, $msg)
137     {
138         $output = new TestOutput();
139
140         $output->setVerbosity($verbosity);
141         $output->clear();
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);
149     }
150
151     public function verbosityProvider()
152     {
153         return array(
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'),
159         );
160     }
161 }
162
163 class TestOutput extends Output
164 {
165     public $output = '';
166
167     public function clear()
168     {
169         $this->output = '';
170     }
171
172     protected function doWrite($message, $newline)
173     {
174         $this->output .= $message.($newline ? "\n" : '');
175     }
176 }