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