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\Formatter;
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\Console\Formatter\OutputFormatter;
16 use Symfony\Component\Console\Formatter\OutputFormatterStyle;
18 class OutputFormatterTest extends TestCase
20 public function testEmptyTag()
22 $formatter = new OutputFormatter(true);
23 $this->assertEquals('foo<>bar', $formatter->format('foo<>bar'));
26 public function testLGCharEscaping()
28 $formatter = new OutputFormatter(true);
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>'));
35 "\033[33mSymfony\\Component\\Console does work very well!\033[39m",
36 $formatter->format('<comment>Symfony\Component\Console does work very well!</comment>')
40 public function testBundledStyles()
42 $formatter = new OutputFormatter(true);
44 $this->assertTrue($formatter->hasStyle('error'));
45 $this->assertTrue($formatter->hasStyle('info'));
46 $this->assertTrue($formatter->hasStyle('comment'));
47 $this->assertTrue($formatter->hasStyle('question'));
50 "\033[37;41msome error\033[39;49m",
51 $formatter->format('<error>some error</error>')
54 "\033[32msome info\033[39m",
55 $formatter->format('<info>some info</info>')
58 "\033[33msome comment\033[39m",
59 $formatter->format('<comment>some comment</comment>')
62 "\033[30;46msome question\033[39;49m",
63 $formatter->format('<question>some question</question>')
67 public function testNestedStyles()
69 $formatter = new OutputFormatter(true);
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>')
77 public function testAdjacentStyles()
79 $formatter = new OutputFormatter(true);
82 "\033[37;41msome error\033[39;49m\033[32msome info\033[39m",
83 $formatter->format('<error>some error</error><info>some info</info>')
87 public function testStyleMatchingNotGreedy()
89 $formatter = new OutputFormatter(true);
92 "(\033[32m>=2.0,<2.3\033[39m)",
93 $formatter->format('(<info>>=2.0,<2.3</info>)')
97 public function testStyleEscaping()
99 $formatter = new OutputFormatter(true);
102 "(\033[32mz>=2.0,<<<a2.3\\\033[39m)",
103 $formatter->format('(<info>'.$formatter->escape('z>=2.0,<\\<<a2.3\\').'</info>)')
107 "\033[32m<error>some error</error>\033[39m",
108 $formatter->format('<info>'.$formatter->escape('<error>some error</error>').'</info>')
112 public function testDeepNestedStyles()
114 $formatter = new OutputFormatter(true);
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>')
122 public function testNewStyle()
124 $formatter = new OutputFormatter(true);
126 $style = new OutputFormatterStyle('blue', 'white');
127 $formatter->setStyle('test', $style);
129 $this->assertEquals($style, $formatter->getStyle('test'));
130 $this->assertNotEquals($style, $formatter->getStyle('info'));
132 $style = new OutputFormatterStyle('blue', 'white');
133 $formatter->setStyle('b', $style);
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>'));
138 public function testRedefineStyle()
140 $formatter = new OutputFormatter(true);
142 $style = new OutputFormatterStyle('blue', 'white');
143 $formatter->setStyle('info', $style);
145 $this->assertEquals("\033[34;47msome custom msg\033[39;49m", $formatter->format('<info>some custom msg</info>'));
148 public function testInlineStyle()
150 $formatter = new OutputFormatter(true);
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>'));
158 * @param string|null $expected
159 * @param string|null $input
161 * @dataProvider provideInlineStyleOptionsCases
163 public function testInlineStyleOptions($tag, $expected = null, $input = null)
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));
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.'>'));
182 public function provideInlineStyleOptionsCases()
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'),
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)".
201 public function testInlineStyleOptionsUnknownAreDeprecated($tag, $option)
203 $formatter = new OutputFormatter(true);
204 $formatter->format($tag);
207 public function provideInlineStyleTagsWithUnknownOptions()
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'),
217 public function testNonStyleTag()
219 $formatter = new OutputFormatter(true);
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>'));
224 public function testFormatLongString()
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));
231 public function testFormatToStringObject()
233 $formatter = new OutputFormatter(false);
235 'some info', $formatter->format(new TableCell())
239 public function testNotDecoratedFormatter()
241 $formatter = new OutputFormatter(false);
243 $this->assertTrue($formatter->hasStyle('error'));
244 $this->assertTrue($formatter->hasStyle('info'));
245 $this->assertTrue($formatter->hasStyle('comment'));
246 $this->assertTrue($formatter->hasStyle('question'));
249 'some error', $formatter->format('<error>some error</error>')
252 'some info', $formatter->format('<info>some info</info>')
255 'some comment', $formatter->format('<comment>some comment</comment>')
258 'some question', $formatter->format('<question>some question</question>')
261 'some text with inline style', $formatter->format('<fg=red>some text with inline style</>')
264 $formatter->setDecorated(true);
267 "\033[37;41msome error\033[39;49m", $formatter->format('<error>some error</error>')
270 "\033[32msome info\033[39m", $formatter->format('<info>some info</info>')
273 "\033[33msome comment\033[39m", $formatter->format('<comment>some comment</comment>')
276 "\033[30;46msome question\033[39;49m", $formatter->format('<question>some question</question>')
279 "\033[31msome text with inline style\033[39m", $formatter->format('<fg=red>some text with inline style</>')
283 public function testContentWithLineBreaks()
285 $formatter = new OutputFormatter(true);
287 $this->assertEquals(<<<EOF
291 , $formatter->format(<<<'EOF'
297 $this->assertEquals(<<<EOF
301 , $formatter->format(<<<'EOF'
307 $this->assertEquals(<<<EOF
312 , $formatter->format(<<<'EOF'
319 $this->assertEquals(<<<EOF
325 , $formatter->format(<<<'EOF'
337 public function __toString()
339 return '<info>some info</info>';