OutputFormatterTest.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. public function testNonStyleTag()
  128. {
  129. $formatter = new OutputFormatter(true);
  130. $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>'));
  131. }
  132. public function testFormatLongString()
  133. {
  134. $formatter = new OutputFormatter(true);
  135. $long = str_repeat('\\', 14000);
  136. $this->assertEquals("\033[37;41msome error\033[39;49m".$long, $formatter->format('<error>some error</error>'.$long));
  137. }
  138. public function testFormatToStringObject()
  139. {
  140. $formatter = new OutputFormatter(false);
  141. $this->assertEquals(
  142. 'some info', $formatter->format(new TableCell())
  143. );
  144. }
  145. public function testNotDecoratedFormatter()
  146. {
  147. $formatter = new OutputFormatter(false);
  148. $this->assertTrue($formatter->hasStyle('error'));
  149. $this->assertTrue($formatter->hasStyle('info'));
  150. $this->assertTrue($formatter->hasStyle('comment'));
  151. $this->assertTrue($formatter->hasStyle('question'));
  152. $this->assertEquals(
  153. 'some error', $formatter->format('<error>some error</error>')
  154. );
  155. $this->assertEquals(
  156. 'some info', $formatter->format('<info>some info</info>')
  157. );
  158. $this->assertEquals(
  159. 'some comment', $formatter->format('<comment>some comment</comment>')
  160. );
  161. $this->assertEquals(
  162. 'some question', $formatter->format('<question>some question</question>')
  163. );
  164. $this->assertEquals(
  165. 'some text with inline style', $formatter->format('<fg=red>some text with inline style</>')
  166. );
  167. $formatter->setDecorated(true);
  168. $this->assertEquals(
  169. "\033[37;41msome error\033[39;49m", $formatter->format('<error>some error</error>')
  170. );
  171. $this->assertEquals(
  172. "\033[32msome info\033[39m", $formatter->format('<info>some info</info>')
  173. );
  174. $this->assertEquals(
  175. "\033[33msome comment\033[39m", $formatter->format('<comment>some comment</comment>')
  176. );
  177. $this->assertEquals(
  178. "\033[30;46msome question\033[39;49m", $formatter->format('<question>some question</question>')
  179. );
  180. $this->assertEquals(
  181. "\033[31msome text with inline style\033[39m", $formatter->format('<fg=red>some text with inline style</>')
  182. );
  183. }
  184. public function testContentWithLineBreaks()
  185. {
  186. $formatter = new OutputFormatter(true);
  187. $this->assertEquals(<<<EOF
  188. \033[32m
  189. some text\033[39m
  190. EOF
  191. , $formatter->format(<<<'EOF'
  192. <info>
  193. some text</info>
  194. EOF
  195. ));
  196. $this->assertEquals(<<<EOF
  197. \033[32msome text
  198. \033[39m
  199. EOF
  200. , $formatter->format(<<<'EOF'
  201. <info>some text
  202. </info>
  203. EOF
  204. ));
  205. $this->assertEquals(<<<EOF
  206. \033[32m
  207. some text
  208. \033[39m
  209. EOF
  210. , $formatter->format(<<<'EOF'
  211. <info>
  212. some text
  213. </info>
  214. EOF
  215. ));
  216. $this->assertEquals(<<<EOF
  217. \033[32m
  218. some text
  219. more text
  220. \033[39m
  221. EOF
  222. , $formatter->format(<<<'EOF'
  223. <info>
  224. some text
  225. more text
  226. </info>
  227. EOF
  228. ));
  229. }
  230. }
  231. class TableCell
  232. {
  233. public function __toString()
  234. {
  235. return '<info>some info</info>';
  236. }
  237. }