OutputFormatterTest.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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 Symfony\Component\Console\Formatter\OutputFormatter;
  12. use Symfony\Component\Console\Formatter\OutputFormatterStyle;
  13. class OutputFormatterTest extends \PHPUnit_Framework_TestCase
  14. {
  15. public function testEmptyTag()
  16. {
  17. $formatter = new OutputFormatter(true);
  18. $this->assertEquals('foo<>bar', $formatter->format('foo<>bar'));
  19. }
  20. public function testLGCharEscaping()
  21. {
  22. $formatter = new OutputFormatter(true);
  23. $this->assertEquals('foo<bar', $formatter->format('foo\\<bar'));
  24. $this->assertEquals('<info>some info</info>', $formatter->format('\\<info>some info\\</info>'));
  25. $this->assertEquals('\\<info>some info\\</info>', OutputFormatter::escape('<info>some info</info>'));
  26. $this->assertEquals(
  27. "\033[33mSymfony\\Component\\Console does work very well!\033[39m",
  28. $formatter->format('<comment>Symfony\Component\Console does work very well!</comment>')
  29. );
  30. }
  31. public function testBundledStyles()
  32. {
  33. $formatter = new OutputFormatter(true);
  34. $this->assertTrue($formatter->hasStyle('error'));
  35. $this->assertTrue($formatter->hasStyle('info'));
  36. $this->assertTrue($formatter->hasStyle('comment'));
  37. $this->assertTrue($formatter->hasStyle('question'));
  38. $this->assertEquals(
  39. "\033[37;41msome error\033[39;49m",
  40. $formatter->format('<error>some error</error>')
  41. );
  42. $this->assertEquals(
  43. "\033[32msome info\033[39m",
  44. $formatter->format('<info>some info</info>')
  45. );
  46. $this->assertEquals(
  47. "\033[33msome comment\033[39m",
  48. $formatter->format('<comment>some comment</comment>')
  49. );
  50. $this->assertEquals(
  51. "\033[30;46msome question\033[39;49m",
  52. $formatter->format('<question>some question</question>')
  53. );
  54. }
  55. public function testNestedStyles()
  56. {
  57. $formatter = new OutputFormatter(true);
  58. $this->assertEquals(
  59. "\033[37;41msome \033[39;49m\033[32msome info\033[39m\033[37;41m error\033[39;49m",
  60. $formatter->format('<error>some <info>some info</info> error</error>')
  61. );
  62. }
  63. public function testAdjacentStyles()
  64. {
  65. $formatter = new OutputFormatter(true);
  66. $this->assertEquals(
  67. "\033[37;41msome error\033[39;49m\033[32msome info\033[39m",
  68. $formatter->format('<error>some error</error><info>some info</info>')
  69. );
  70. }
  71. public function testStyleMatchingNotGreedy()
  72. {
  73. $formatter = new OutputFormatter(true);
  74. $this->assertEquals(
  75. "(\033[32m>=2.0,<2.3\033[39m)",
  76. $formatter->format('(<info>>=2.0,<2.3</info>)')
  77. );
  78. }
  79. public function testStyleEscaping()
  80. {
  81. $formatter = new OutputFormatter(true);
  82. $this->assertEquals(
  83. "(\033[32mz>=2.0,<a2.3\033[39m)",
  84. $formatter->format('(<info>'.$formatter->escape('z>=2.0,<a2.3').'</info>)')
  85. );
  86. $this->assertEquals(
  87. "\033[32m<error>some error</error>\033[39m",
  88. $formatter->format('<info>'.$formatter->escape('<error>some error</error>').'</info>')
  89. );
  90. }
  91. public function testDeepNestedStyles()
  92. {
  93. $formatter = new OutputFormatter(true);
  94. $this->assertEquals(
  95. "\033[37;41merror\033[39;49m\033[32minfo\033[39m\033[33mcomment\033[39m\033[37;41merror\033[39;49m",
  96. $formatter->format('<error>error<info>info<comment>comment</info>error</error>')
  97. );
  98. }
  99. public function testNewStyle()
  100. {
  101. $formatter = new OutputFormatter(true);
  102. $style = new OutputFormatterStyle('blue', 'white');
  103. $formatter->setStyle('test', $style);
  104. $this->assertEquals($style, $formatter->getStyle('test'));
  105. $this->assertNotEquals($style, $formatter->getStyle('info'));
  106. $style = new OutputFormatterStyle('blue', 'white');
  107. $formatter->setStyle('b', $style);
  108. $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>'));
  109. }
  110. public function testRedefineStyle()
  111. {
  112. $formatter = new OutputFormatter(true);
  113. $style = new OutputFormatterStyle('blue', 'white');
  114. $formatter->setStyle('info', $style);
  115. $this->assertEquals("\033[34;47msome custom msg\033[39;49m", $formatter->format('<info>some custom msg</info>'));
  116. }
  117. public function testInlineStyle()
  118. {
  119. $formatter = new OutputFormatter(true);
  120. $this->assertEquals("\033[34;41msome text\033[39;49m", $formatter->format('<fg=blue;bg=red>some text</>'));
  121. $this->assertEquals("\033[34;41msome text\033[39;49m", $formatter->format('<fg=blue;bg=red>some text</fg=blue;bg=red>'));
  122. }
  123. public function testNonStyleTag()
  124. {
  125. $formatter = new OutputFormatter(true);
  126. $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>'));
  127. }
  128. public function testFormatLongString()
  129. {
  130. $formatter = new OutputFormatter(true);
  131. $long = str_repeat('\\', 14000);
  132. $this->assertEquals("\033[37;41msome error\033[39;49m".$long, $formatter->format('<error>some error</error>'.$long));
  133. }
  134. public function testFormatToStringObject()
  135. {
  136. $formatter = new OutputFormatter(false);
  137. $this->assertEquals(
  138. 'some info', $formatter->format(new TableCell())
  139. );
  140. }
  141. public function testNotDecoratedFormatter()
  142. {
  143. $formatter = new OutputFormatter(false);
  144. $this->assertTrue($formatter->hasStyle('error'));
  145. $this->assertTrue($formatter->hasStyle('info'));
  146. $this->assertTrue($formatter->hasStyle('comment'));
  147. $this->assertTrue($formatter->hasStyle('question'));
  148. $this->assertEquals(
  149. 'some error', $formatter->format('<error>some error</error>')
  150. );
  151. $this->assertEquals(
  152. 'some info', $formatter->format('<info>some info</info>')
  153. );
  154. $this->assertEquals(
  155. 'some comment', $formatter->format('<comment>some comment</comment>')
  156. );
  157. $this->assertEquals(
  158. 'some question', $formatter->format('<question>some question</question>')
  159. );
  160. $formatter->setDecorated(true);
  161. $this->assertEquals(
  162. "\033[37;41msome error\033[39;49m", $formatter->format('<error>some error</error>')
  163. );
  164. $this->assertEquals(
  165. "\033[32msome info\033[39m", $formatter->format('<info>some info</info>')
  166. );
  167. $this->assertEquals(
  168. "\033[33msome comment\033[39m", $formatter->format('<comment>some comment</comment>')
  169. );
  170. $this->assertEquals(
  171. "\033[30;46msome question\033[39;49m", $formatter->format('<question>some question</question>')
  172. );
  173. }
  174. public function testContentWithLineBreaks()
  175. {
  176. $formatter = new OutputFormatter(true);
  177. $this->assertEquals(<<<EOF
  178. \033[32m
  179. some text\033[39m
  180. EOF
  181. , $formatter->format(<<<EOF
  182. <info>
  183. some text</info>
  184. EOF
  185. ));
  186. $this->assertEquals(<<<EOF
  187. \033[32msome text
  188. \033[39m
  189. EOF
  190. , $formatter->format(<<<EOF
  191. <info>some text
  192. </info>
  193. EOF
  194. ));
  195. $this->assertEquals(<<<EOF
  196. \033[32m
  197. some text
  198. \033[39m
  199. EOF
  200. , $formatter->format(<<<EOF
  201. <info>
  202. some text
  203. </info>
  204. EOF
  205. ));
  206. $this->assertEquals(<<<EOF
  207. \033[32m
  208. some text
  209. more text
  210. \033[39m
  211. EOF
  212. , $formatter->format(<<<EOF
  213. <info>
  214. some text
  215. more text
  216. </info>
  217. EOF
  218. ));
  219. }
  220. }
  221. class TableCell
  222. {
  223. public function __toString()
  224. {
  225. return '<info>some info</info>';
  226. }
  227. }