OutputFormatterTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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 testNotDecoratedFormatter()
  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. $this->assertEquals(
  200. 'some error', $formatter->format('<error>some error</error>')
  201. );
  202. $this->assertEquals(
  203. 'some info', $formatter->format('<info>some info</info>')
  204. );
  205. $this->assertEquals(
  206. 'some comment', $formatter->format('<comment>some comment</comment>')
  207. );
  208. $this->assertEquals(
  209. 'some question', $formatter->format('<question>some question</question>')
  210. );
  211. $this->assertEquals(
  212. 'some text with inline style', $formatter->format('<fg=red>some text with inline style</>')
  213. );
  214. $formatter->setDecorated(true);
  215. $this->assertEquals(
  216. "\033[37;41msome error\033[39;49m", $formatter->format('<error>some error</error>')
  217. );
  218. $this->assertEquals(
  219. "\033[32msome info\033[39m", $formatter->format('<info>some info</info>')
  220. );
  221. $this->assertEquals(
  222. "\033[33msome comment\033[39m", $formatter->format('<comment>some comment</comment>')
  223. );
  224. $this->assertEquals(
  225. "\033[30;46msome question\033[39;49m", $formatter->format('<question>some question</question>')
  226. );
  227. $this->assertEquals(
  228. "\033[31msome text with inline style\033[39m", $formatter->format('<fg=red>some text with inline style</>')
  229. );
  230. }
  231. public function testContentWithLineBreaks()
  232. {
  233. $formatter = new OutputFormatter(true);
  234. $this->assertEquals(<<<EOF
  235. \033[32m
  236. some text\033[39m
  237. EOF
  238. , $formatter->format(<<<'EOF'
  239. <info>
  240. some text</info>
  241. EOF
  242. ));
  243. $this->assertEquals(<<<EOF
  244. \033[32msome text
  245. \033[39m
  246. EOF
  247. , $formatter->format(<<<'EOF'
  248. <info>some text
  249. </info>
  250. EOF
  251. ));
  252. $this->assertEquals(<<<EOF
  253. \033[32m
  254. some text
  255. \033[39m
  256. EOF
  257. , $formatter->format(<<<'EOF'
  258. <info>
  259. some text
  260. </info>
  261. EOF
  262. ));
  263. $this->assertEquals(<<<EOF
  264. \033[32m
  265. some text
  266. more text
  267. \033[39m
  268. EOF
  269. , $formatter->format(<<<'EOF'
  270. <info>
  271. some text
  272. more text
  273. </info>
  274. EOF
  275. ));
  276. }
  277. public function testFormatAndWrap()
  278. {
  279. $formatter = new OutputFormatter(true);
  280. $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));
  281. $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));
  282. $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));
  283. $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));
  284. $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));
  285. $formatter = new OutputFormatter();
  286. $this->assertSame("fo\nob\nar\nba\nz", $formatter->formatAndWrap('foo<error>bar</error> baz', 2));
  287. $this->assertSame("pr\ne \nfo\no \nba\nr \nba\nz \npo\nst", $formatter->formatAndWrap('pre <error>foo bar baz</error> post', 2));
  288. $this->assertSame("pre\nfoo\nbar\nbaz\npos\nt", $formatter->formatAndWrap('pre <error>foo bar baz</error> post', 3));
  289. $this->assertSame("pre \nfoo \nbar \nbaz \npost", $formatter->formatAndWrap('pre <error>foo bar baz</error> post', 4));
  290. $this->assertSame("pre f\noo ba\nr baz\npost", $formatter->formatAndWrap('pre <error>foo bar baz</error> post', 5));
  291. }
  292. }
  293. class TableCell
  294. {
  295. public function __toString()
  296. {
  297. return '<info>some info</info>';
  298. }
  299. }