OutputFormatterTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Console\Tests\Formatter;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\Console\Formatter\OutputFormatter;
  13. use Symfony\Component\Console\Formatter\OutputFormatterStyle;
  14. class OutputFormatterTest extends TestCase
  15. {
  16. public function testEmptyTag()
  17. {
  18. $formatter = new OutputFormatter(true);
  19. $this->assertEquals('foo<>bar', $formatter->format('foo<>bar'));
  20. }
  21. public function testLGCharEscaping()
  22. {
  23. $formatter = new OutputFormatter(true);
  24. $this->assertEquals('foo<bar', $formatter->format('foo\\<bar'));
  25. $this->assertEquals('foo << bar', $formatter->format('foo << bar'));
  26. $this->assertEquals('foo << bar \\', $formatter->format('foo << bar \\'));
  27. $this->assertEquals("foo << \033[32mbar \\ baz\033[39m \\", $formatter->format('foo << <info>bar \\ baz</info> \\'));
  28. $this->assertEquals('<info>some info</info>', $formatter->format('\\<info>some info\\</info>'));
  29. $this->assertEquals('\\<info>some info\\</info>', OutputFormatter::escape('<info>some info</info>'));
  30. $this->assertEquals(
  31. "\033[33mSymfony\\Component\\Console does work very well!\033[39m",
  32. $formatter->format('<comment>Symfony\Component\Console does work very well!</comment>')
  33. );
  34. }
  35. public function testBundledStyles()
  36. {
  37. $formatter = new OutputFormatter(true);
  38. $this->assertTrue($formatter->hasStyle('error'));
  39. $this->assertTrue($formatter->hasStyle('info'));
  40. $this->assertTrue($formatter->hasStyle('comment'));
  41. $this->assertTrue($formatter->hasStyle('question'));
  42. $this->assertEquals(
  43. "\033[37;41msome error\033[39;49m",
  44. $formatter->format('<error>some error</error>')
  45. );
  46. $this->assertEquals(
  47. "\033[32msome info\033[39m",
  48. $formatter->format('<info>some info</info>')
  49. );
  50. $this->assertEquals(
  51. "\033[33msome comment\033[39m",
  52. $formatter->format('<comment>some comment</comment>')
  53. );
  54. $this->assertEquals(
  55. "\033[30;46msome question\033[39;49m",
  56. $formatter->format('<question>some question</question>')
  57. );
  58. }
  59. public function testNestedStyles()
  60. {
  61. $formatter = new OutputFormatter(true);
  62. $this->assertEquals(
  63. "\033[37;41msome \033[39;49m\033[32msome info\033[39m\033[37;41m error\033[39;49m",
  64. $formatter->format('<error>some <info>some info</info> error</error>')
  65. );
  66. }
  67. public function testAdjacentStyles()
  68. {
  69. $formatter = new OutputFormatter(true);
  70. $this->assertEquals(
  71. "\033[37;41msome error\033[39;49m\033[32msome info\033[39m",
  72. $formatter->format('<error>some error</error><info>some info</info>')
  73. );
  74. }
  75. public function testStyleMatchingNotGreedy()
  76. {
  77. $formatter = new OutputFormatter(true);
  78. $this->assertEquals(
  79. "(\033[32m>=2.0,<2.3\033[39m)",
  80. $formatter->format('(<info>>=2.0,<2.3</info>)')
  81. );
  82. }
  83. public function testStyleEscaping()
  84. {
  85. $formatter = new OutputFormatter(true);
  86. $this->assertEquals(
  87. "(\033[32mz>=2.0,<<<a2.3\\\033[39m)",
  88. $formatter->format('(<info>'.$formatter->escape('z>=2.0,<\\<<a2.3\\').'</info>)')
  89. );
  90. $this->assertEquals(
  91. "\033[32m<error>some error</error>\033[39m",
  92. $formatter->format('<info>'.$formatter->escape('<error>some error</error>').'</info>')
  93. );
  94. }
  95. public function testDeepNestedStyles()
  96. {
  97. $formatter = new OutputFormatter(true);
  98. $this->assertEquals(
  99. "\033[37;41merror\033[39;49m\033[32minfo\033[39m\033[33mcomment\033[39m\033[37;41merror\033[39;49m",
  100. $formatter->format('<error>error<info>info<comment>comment</info>error</error>')
  101. );
  102. }
  103. public function testNewStyle()
  104. {
  105. $formatter = new OutputFormatter(true);
  106. $style = new OutputFormatterStyle('blue', 'white');
  107. $formatter->setStyle('test', $style);
  108. $this->assertEquals($style, $formatter->getStyle('test'));
  109. $this->assertNotEquals($style, $formatter->getStyle('info'));
  110. $style = new OutputFormatterStyle('blue', 'white');
  111. $formatter->setStyle('b', $style);
  112. $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>'));
  113. }
  114. public function testRedefineStyle()
  115. {
  116. $formatter = new OutputFormatter(true);
  117. $style = new OutputFormatterStyle('blue', 'white');
  118. $formatter->setStyle('info', $style);
  119. $this->assertEquals("\033[34;47msome custom msg\033[39;49m", $formatter->format('<info>some custom msg</info>'));
  120. }
  121. public function testInlineStyle()
  122. {
  123. $formatter = new OutputFormatter(true);
  124. $this->assertEquals("\033[34;41msome text\033[39;49m", $formatter->format('<fg=blue;bg=red>some text</>'));
  125. $this->assertEquals("\033[34;41msome text\033[39;49m", $formatter->format('<fg=blue;bg=red>some text</fg=blue;bg=red>'));
  126. }
  127. /**
  128. * @param string $tag
  129. * @param string|null $expected
  130. * @param string|null $input
  131. *
  132. * @dataProvider provideInlineStyleOptionsCases
  133. */
  134. public function testInlineStyleOptions($tag, $expected = null, $input = null)
  135. {
  136. $styleString = substr($tag, 1, -1);
  137. $formatter = new OutputFormatter(true);
  138. $method = new \ReflectionMethod($formatter, 'createStyleFromString');
  139. $method->setAccessible(true);
  140. $result = $method->invoke($formatter, $styleString);
  141. if (null === $expected) {
  142. $this->assertFalse($result);
  143. $expected = $tag.$input.'</'.$styleString.'>';
  144. $this->assertSame($expected, $formatter->format($expected));
  145. } else {
  146. /* @var OutputFormatterStyle $result */
  147. $this->assertInstanceOf(OutputFormatterStyle::class, $result);
  148. $this->assertSame($expected, $formatter->format($tag.$input.'</>'));
  149. $this->assertSame($expected, $formatter->format($tag.$input.'</'.$styleString.'>'));
  150. }
  151. }
  152. public function provideInlineStyleOptionsCases()
  153. {
  154. return [
  155. ['<unknown=_unknown_>'],
  156. ['<unknown=_unknown_;a=1;b>'],
  157. ['<fg=green;>', "\033[32m[test]\033[39m", '[test]'],
  158. ['<fg=green;bg=blue;>', "\033[32;44ma\033[39;49m", 'a'],
  159. ['<fg=green;options=bold>', "\033[32;1mb\033[39;22m", 'b'],
  160. ['<fg=green;options=reverse;>', "\033[32;7m<a>\033[39;27m", '<a>'],
  161. ['<fg=green;options=bold,underscore>', "\033[32;1;4mz\033[39;22;24m", 'z'],
  162. ['<fg=green;options=bold,underscore,reverse;>', "\033[32;1;4;7md\033[39;22;24;27m", 'd'],
  163. ];
  164. }
  165. public function provideInlineStyleTagsWithUnknownOptions()
  166. {
  167. return [
  168. ['<options=abc;>', 'abc'],
  169. ['<options=abc,def;>', 'abc'],
  170. ['<fg=green;options=xyz;>', 'xyz'],
  171. ['<fg=green;options=efg,abc>', 'efg'],
  172. ];
  173. }
  174. public function testNonStyleTag()
  175. {
  176. $formatter = new OutputFormatter(true);
  177. $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>'));
  178. }
  179. public function testFormatLongString()
  180. {
  181. $formatter = new OutputFormatter(true);
  182. $long = str_repeat('\\', 14000);
  183. $this->assertEquals("\033[37;41msome error\033[39;49m".$long, $formatter->format('<error>some error</error>'.$long));
  184. }
  185. public function testFormatToStringObject()
  186. {
  187. $formatter = new OutputFormatter(false);
  188. $this->assertEquals(
  189. 'some info', $formatter->format(new TableCell())
  190. );
  191. }
  192. public function testFormatterHasStyles()
  193. {
  194. $formatter = new OutputFormatter(false);
  195. $this->assertTrue($formatter->hasStyle('error'));
  196. $this->assertTrue($formatter->hasStyle('info'));
  197. $this->assertTrue($formatter->hasStyle('comment'));
  198. $this->assertTrue($formatter->hasStyle('question'));
  199. }
  200. /**
  201. * @dataProvider provideDecoratedAndNonDecoratedOutput
  202. */
  203. public function testNotDecoratedFormatter(string $input, string $expectedNonDecoratedOutput, string $expectedDecoratedOutput, string $terminalEmulator = 'foo')
  204. {
  205. $prevTerminalEmulator = getenv('TERMINAL_EMULATOR');
  206. putenv('TERMINAL_EMULATOR='.$terminalEmulator);
  207. try {
  208. $this->assertEquals($expectedDecoratedOutput, (new OutputFormatter(true))->format($input));
  209. $this->assertEquals($expectedNonDecoratedOutput, (new OutputFormatter(false))->format($input));
  210. } finally {
  211. putenv('TERMINAL_EMULATOR'.($prevTerminalEmulator ? "=$prevTerminalEmulator" : ''));
  212. }
  213. }
  214. public function provideDecoratedAndNonDecoratedOutput()
  215. {
  216. return [
  217. ['<error>some error</error>', 'some error', "\033[37;41msome error\033[39;49m"],
  218. ['<info>some info</info>', 'some info', "\033[32msome info\033[39m"],
  219. ['<comment>some comment</comment>', 'some comment', "\033[33msome comment\033[39m"],
  220. ['<question>some question</question>', 'some question', "\033[30;46msome question\033[39;49m"],
  221. ['<fg=red>some text with inline style</>', 'some text with inline style', "\033[31msome text with inline style\033[39m"],
  222. ['<href=idea://open/?file=/path/SomeFile.php&line=12>some URL</>', 'some URL', "\033]8;;idea://open/?file=/path/SomeFile.php&line=12\033\\some URL\033]8;;\033\\"],
  223. ['<href=idea://open/?file=/path/SomeFile.php&line=12>some URL</>', 'some URL', 'some URL', 'JetBrains-JediTerm'],
  224. ];
  225. }
  226. public function testContentWithLineBreaks()
  227. {
  228. $formatter = new OutputFormatter(true);
  229. $this->assertEquals(<<<EOF
  230. \033[32m
  231. some text\033[39m
  232. EOF
  233. , $formatter->format(<<<'EOF'
  234. <info>
  235. some text</info>
  236. EOF
  237. ));
  238. $this->assertEquals(<<<EOF
  239. \033[32msome text
  240. \033[39m
  241. EOF
  242. , $formatter->format(<<<'EOF'
  243. <info>some text
  244. </info>
  245. EOF
  246. ));
  247. $this->assertEquals(<<<EOF
  248. \033[32m
  249. some text
  250. \033[39m
  251. EOF
  252. , $formatter->format(<<<'EOF'
  253. <info>
  254. some text
  255. </info>
  256. EOF
  257. ));
  258. $this->assertEquals(<<<EOF
  259. \033[32m
  260. some text
  261. more text
  262. \033[39m
  263. EOF
  264. , $formatter->format(<<<'EOF'
  265. <info>
  266. some text
  267. more text
  268. </info>
  269. EOF
  270. ));
  271. }
  272. public function testFormatAndWrap()
  273. {
  274. $formatter = new OutputFormatter(true);
  275. $this->assertSame("fo\no\e[37;41mb\e[39;49m\n\e[37;41mar\e[39;49m\nba\nz", $formatter->formatAndWrap('foo<error>bar</error> baz', 2));
  276. $this->assertSame("pr\ne \e[37;41m\e[39;49m\n\e[37;41mfo\e[39;49m\n\e[37;41mo \e[39;49m\n\e[37;41mba\e[39;49m\n\e[37;41mr \e[39;49m\n\e[37;41mba\e[39;49m\n\e[37;41mz\e[39;49m \npo\nst", $formatter->formatAndWrap('pre <error>foo bar baz</error> post', 2));
  277. $this->assertSame("pre\e[37;41m\e[39;49m\n\e[37;41mfoo\e[39;49m\n\e[37;41mbar\e[39;49m\n\e[37;41mbaz\e[39;49m\npos\nt", $formatter->formatAndWrap('pre <error>foo bar baz</error> post', 3));
  278. $this->assertSame("pre \e[37;41m\e[39;49m\n\e[37;41mfoo \e[39;49m\n\e[37;41mbar \e[39;49m\n\e[37;41mbaz\e[39;49m \npost", $formatter->formatAndWrap('pre <error>foo bar baz</error> post', 4));
  279. $this->assertSame("pre \e[37;41mf\e[39;49m\n\e[37;41moo ba\e[39;49m\n\e[37;41mr baz\e[39;49m\npost", $formatter->formatAndWrap('pre <error>foo bar baz</error> post', 5));
  280. $this->assertSame("Lore\nm \e[37;41mip\e[39;49m\n\e[37;41msum\e[39;49m \ndolo\nr \e[32msi\e[39m\n\e[32mt\e[39m am\net", $formatter->formatAndWrap('Lorem <error>ipsum</error> dolor <info>sit</info> amet', 4));
  281. $this->assertSame("Lorem \e[37;41mip\e[39;49m\n\e[37;41msum\e[39;49m dolo\nr \e[32msit\e[39m am\net", $formatter->formatAndWrap('Lorem <error>ipsum</error> dolor <info>sit</info> amet', 8));
  282. $this->assertSame("Lorem \e[37;41mipsum\e[39;49m dolor \e[32m\e[39m\n\e[32msit\e[39m, \e[37;41mamet\e[39;49m et \e[32mlauda\e[39m\n\e[32mntium\e[39m architecto", $formatter->formatAndWrap('Lorem <error>ipsum</error> dolor <info>sit</info>, <error>amet</error> et <info>laudantium</info> architecto', 18));
  283. $formatter = new OutputFormatter();
  284. $this->assertSame("fo\nob\nar\nba\nz", $formatter->formatAndWrap('foo<error>bar</error> baz', 2));
  285. $this->assertSame("pr\ne \nfo\no \nba\nr \nba\nz \npo\nst", $formatter->formatAndWrap('pre <error>foo bar baz</error> post', 2));
  286. $this->assertSame("pre\nfoo\nbar\nbaz\npos\nt", $formatter->formatAndWrap('pre <error>foo bar baz</error> post', 3));
  287. $this->assertSame("pre \nfoo \nbar \nbaz \npost", $formatter->formatAndWrap('pre <error>foo bar baz</error> post', 4));
  288. $this->assertSame("pre f\noo ba\nr baz\npost", $formatter->formatAndWrap('pre <error>foo bar baz</error> post', 5));
  289. }
  290. }
  291. class TableCell
  292. {
  293. public function __toString()
  294. {
  295. return '<info>some info</info>';
  296. }
  297. }