4b715c680d3d3e6f450804d4a9a77cf1549e4e62
[yaffs-website] / vendor / symfony / console / Tests / Formatter / OutputFormatterTest.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\Formatter;
13
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\Console\Formatter\OutputFormatter;
16 use Symfony\Component\Console\Formatter\OutputFormatterStyle;
17
18 class OutputFormatterTest extends TestCase
19 {
20     public function testEmptyTag()
21     {
22         $formatter = new OutputFormatter(true);
23         $this->assertEquals('foo<>bar', $formatter->format('foo<>bar'));
24     }
25
26     public function testLGCharEscaping()
27     {
28         $formatter = new OutputFormatter(true);
29
30         $this->assertEquals('foo<bar', $formatter->format('foo\\<bar'));
31         $this->assertEquals('foo << bar', $formatter->format('foo << bar'));
32         $this->assertEquals('foo << bar \\', $formatter->format('foo << bar \\'));
33         $this->assertEquals("foo << \033[32mbar \\ baz\033[39m \\", $formatter->format('foo << <info>bar \\ baz</info> \\'));
34         $this->assertEquals('<info>some info</info>', $formatter->format('\\<info>some info\\</info>'));
35         $this->assertEquals('\\<info>some info\\</info>', OutputFormatter::escape('<info>some info</info>'));
36
37         $this->assertEquals(
38             "\033[33mSymfony\\Component\\Console does work very well!\033[39m",
39             $formatter->format('<comment>Symfony\Component\Console does work very well!</comment>')
40         );
41     }
42
43     public function testBundledStyles()
44     {
45         $formatter = new OutputFormatter(true);
46
47         $this->assertTrue($formatter->hasStyle('error'));
48         $this->assertTrue($formatter->hasStyle('info'));
49         $this->assertTrue($formatter->hasStyle('comment'));
50         $this->assertTrue($formatter->hasStyle('question'));
51
52         $this->assertEquals(
53             "\033[37;41msome error\033[39;49m",
54             $formatter->format('<error>some error</error>')
55         );
56         $this->assertEquals(
57             "\033[32msome info\033[39m",
58             $formatter->format('<info>some info</info>')
59         );
60         $this->assertEquals(
61             "\033[33msome comment\033[39m",
62             $formatter->format('<comment>some comment</comment>')
63         );
64         $this->assertEquals(
65             "\033[30;46msome question\033[39;49m",
66             $formatter->format('<question>some question</question>')
67         );
68     }
69
70     public function testNestedStyles()
71     {
72         $formatter = new OutputFormatter(true);
73
74         $this->assertEquals(
75             "\033[37;41msome \033[39;49m\033[32msome info\033[39m\033[37;41m error\033[39;49m",
76             $formatter->format('<error>some <info>some info</info> error</error>')
77         );
78     }
79
80     public function testAdjacentStyles()
81     {
82         $formatter = new OutputFormatter(true);
83
84         $this->assertEquals(
85             "\033[37;41msome error\033[39;49m\033[32msome info\033[39m",
86             $formatter->format('<error>some error</error><info>some info</info>')
87         );
88     }
89
90     public function testStyleMatchingNotGreedy()
91     {
92         $formatter = new OutputFormatter(true);
93
94         $this->assertEquals(
95             "(\033[32m>=2.0,<2.3\033[39m)",
96             $formatter->format('(<info>>=2.0,<2.3</info>)')
97         );
98     }
99
100     public function testStyleEscaping()
101     {
102         $formatter = new OutputFormatter(true);
103
104         $this->assertEquals(
105             "(\033[32mz>=2.0,<<<a2.3\\\033[39m)",
106             $formatter->format('(<info>'.$formatter->escape('z>=2.0,<\\<<a2.3\\').'</info>)')
107         );
108
109         $this->assertEquals(
110             "\033[32m<error>some error</error>\033[39m",
111             $formatter->format('<info>'.$formatter->escape('<error>some error</error>').'</info>')
112         );
113     }
114
115     public function testDeepNestedStyles()
116     {
117         $formatter = new OutputFormatter(true);
118
119         $this->assertEquals(
120             "\033[37;41merror\033[39;49m\033[32minfo\033[39m\033[33mcomment\033[39m\033[37;41merror\033[39;49m",
121             $formatter->format('<error>error<info>info<comment>comment</info>error</error>')
122         );
123     }
124
125     public function testNewStyle()
126     {
127         $formatter = new OutputFormatter(true);
128
129         $style = new OutputFormatterStyle('blue', 'white');
130         $formatter->setStyle('test', $style);
131
132         $this->assertEquals($style, $formatter->getStyle('test'));
133         $this->assertNotEquals($style, $formatter->getStyle('info'));
134
135         $style = new OutputFormatterStyle('blue', 'white');
136         $formatter->setStyle('b', $style);
137
138         $this->assertEquals("\033[34;47msome \033[39;49m\033[34;47mcustom\033[39;49m\033[34;47m msg\033[39;49m", $formatter->format('<test>some <b>custom</b> msg</test>'));
139     }
140
141     public function testRedefineStyle()
142     {
143         $formatter = new OutputFormatter(true);
144
145         $style = new OutputFormatterStyle('blue', 'white');
146         $formatter->setStyle('info', $style);
147
148         $this->assertEquals("\033[34;47msome custom msg\033[39;49m", $formatter->format('<info>some custom msg</info>'));
149     }
150
151     public function testInlineStyle()
152     {
153         $formatter = new OutputFormatter(true);
154
155         $this->assertEquals("\033[34;41msome text\033[39;49m", $formatter->format('<fg=blue;bg=red>some text</>'));
156         $this->assertEquals("\033[34;41msome text\033[39;49m", $formatter->format('<fg=blue;bg=red>some text</fg=blue;bg=red>'));
157     }
158
159     /**
160      * @param string      $tag
161      * @param string|null $expected
162      * @param string|null $input
163      *
164      * @dataProvider provideInlineStyleOptionsCases
165      */
166     public function testInlineStyleOptions($tag, $expected = null, $input = null)
167     {
168         $styleString = substr($tag, 1, -1);
169         $formatter = new OutputFormatter(true);
170         $method = new \ReflectionMethod($formatter, 'createStyleFromString');
171         $method->setAccessible(true);
172         $result = $method->invoke($formatter, $styleString);
173         if (null === $expected) {
174             $this->assertFalse($result);
175             $expected = $tag.$input.'</'.$styleString.'>';
176             $this->assertSame($expected, $formatter->format($expected));
177         } else {
178             /* @var OutputFormatterStyle $result */
179             $this->assertInstanceOf(OutputFormatterStyle::class, $result);
180             $this->assertSame($expected, $formatter->format($tag.$input.'</>'));
181             $this->assertSame($expected, $formatter->format($tag.$input.'</'.$styleString.'>'));
182         }
183     }
184
185     public function provideInlineStyleOptionsCases()
186     {
187         return array(
188             array('<unknown=_unknown_>'),
189             array('<unknown=_unknown_;a=1;b>'),
190             array('<fg=green;>', "\033[32m[test]\033[39m", '[test]'),
191             array('<fg=green;bg=blue;>', "\033[32;44ma\033[39;49m", 'a'),
192             array('<fg=green;options=bold>', "\033[32;1mb\033[39;22m", 'b'),
193             array('<fg=green;options=reverse;>', "\033[32;7m<a>\033[39;27m", '<a>'),
194             array('<fg=green;options=bold,underscore>', "\033[32;1;4mz\033[39;22;24m", 'z'),
195             array('<fg=green;options=bold,underscore,reverse;>', "\033[32;1;4;7md\033[39;22;24;27m", 'd'),
196         );
197     }
198
199     /**
200      * @group legacy
201      * @dataProvider provideInlineStyleTagsWithUnknownOptions
202      * @expectedDeprecation Unknown style options are deprecated since Symfony 3.2 and will be removed in 4.0. Exception "Invalid option specified: "%s". Expected one of (bold, underscore, blink, reverse, conceal)".
203      */
204     public function testInlineStyleOptionsUnknownAreDeprecated($tag, $option)
205     {
206         $formatter = new OutputFormatter(true);
207         $formatter->format($tag);
208     }
209
210     public function provideInlineStyleTagsWithUnknownOptions()
211     {
212         return array(
213             array('<options=abc;>', 'abc'),
214             array('<options=abc,def;>', 'abc'),
215             array('<fg=green;options=xyz;>', 'xyz'),
216             array('<fg=green;options=efg,abc>', 'efg'),
217         );
218     }
219
220     public function testNonStyleTag()
221     {
222         $formatter = new OutputFormatter(true);
223
224         $this->assertEquals("\033[32msome \033[39m\033[32m<tag>\033[39m\033[32m \033[39m\033[32m<setting=value>\033[39m\033[32m styled \033[39m\033[32m<p>\033[39m\033[32msingle-char tag\033[39m\033[32m</p>\033[39m", $formatter->format('<info>some <tag> <setting=value> styled <p>single-char tag</p></info>'));
225     }
226
227     public function testFormatLongString()
228     {
229         $formatter = new OutputFormatter(true);
230         $long = str_repeat('\\', 14000);
231         $this->assertEquals("\033[37;41msome error\033[39;49m".$long, $formatter->format('<error>some error</error>'.$long));
232     }
233
234     public function testFormatToStringObject()
235     {
236         $formatter = new OutputFormatter(false);
237         $this->assertEquals(
238             'some info', $formatter->format(new TableCell())
239         );
240     }
241
242     public function testNotDecoratedFormatter()
243     {
244         $formatter = new OutputFormatter(false);
245
246         $this->assertTrue($formatter->hasStyle('error'));
247         $this->assertTrue($formatter->hasStyle('info'));
248         $this->assertTrue($formatter->hasStyle('comment'));
249         $this->assertTrue($formatter->hasStyle('question'));
250
251         $this->assertEquals(
252             'some error', $formatter->format('<error>some error</error>')
253         );
254         $this->assertEquals(
255             'some info', $formatter->format('<info>some info</info>')
256         );
257         $this->assertEquals(
258             'some comment', $formatter->format('<comment>some comment</comment>')
259         );
260         $this->assertEquals(
261             'some question', $formatter->format('<question>some question</question>')
262         );
263         $this->assertEquals(
264             'some text with inline style', $formatter->format('<fg=red>some text with inline style</>')
265         );
266
267         $formatter->setDecorated(true);
268
269         $this->assertEquals(
270             "\033[37;41msome error\033[39;49m", $formatter->format('<error>some error</error>')
271         );
272         $this->assertEquals(
273             "\033[32msome info\033[39m", $formatter->format('<info>some info</info>')
274         );
275         $this->assertEquals(
276             "\033[33msome comment\033[39m", $formatter->format('<comment>some comment</comment>')
277         );
278         $this->assertEquals(
279             "\033[30;46msome question\033[39;49m", $formatter->format('<question>some question</question>')
280         );
281         $this->assertEquals(
282             "\033[31msome text with inline style\033[39m", $formatter->format('<fg=red>some text with inline style</>')
283         );
284     }
285
286     public function testContentWithLineBreaks()
287     {
288         $formatter = new OutputFormatter(true);
289
290         $this->assertEquals(<<<EOF
291 \033[32m
292 some text\033[39m
293 EOF
294             , $formatter->format(<<<'EOF'
295 <info>
296 some text</info>
297 EOF
298         ));
299
300         $this->assertEquals(<<<EOF
301 \033[32msome text
302 \033[39m
303 EOF
304             , $formatter->format(<<<'EOF'
305 <info>some text
306 </info>
307 EOF
308         ));
309
310         $this->assertEquals(<<<EOF
311 \033[32m
312 some text
313 \033[39m
314 EOF
315             , $formatter->format(<<<'EOF'
316 <info>
317 some text
318 </info>
319 EOF
320         ));
321
322         $this->assertEquals(<<<EOF
323 \033[32m
324 some text
325 more text
326 \033[39m
327 EOF
328             , $formatter->format(<<<'EOF'
329 <info>
330 some text
331 more text
332 </info>
333 EOF
334         ));
335     }
336 }
337
338 class TableCell
339 {
340     public function __toString()
341     {
342         return '<info>some info</info>';
343     }
344 }