OutputFormatterTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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('<info>some info</info>', $formatter->format('\\<info>some info\\</info>'));
  26. $this->assertEquals('\\<info>some info\\</info>', OutputFormatter::escape('<info>some info</info>'));
  27. $this->assertEquals(
  28. "\033[33mSymfony\\Component\\Console does work very well!\033[39m",
  29. $formatter->format('<comment>Symfony\Component\Console does work very well!</comment>')
  30. );
  31. }
  32. public function testBundledStyles()
  33. {
  34. $formatter = new OutputFormatter(true);
  35. $this->assertTrue($formatter->hasStyle('error'));
  36. $this->assertTrue($formatter->hasStyle('info'));
  37. $this->assertTrue($formatter->hasStyle('comment'));
  38. $this->assertTrue($formatter->hasStyle('question'));
  39. $this->assertEquals(
  40. "\033[37;41msome error\033[39;49m",
  41. $formatter->format('<error>some error</error>')
  42. );
  43. $this->assertEquals(
  44. "\033[32msome info\033[39m",
  45. $formatter->format('<info>some info</info>')
  46. );
  47. $this->assertEquals(
  48. "\033[33msome comment\033[39m",
  49. $formatter->format('<comment>some comment</comment>')
  50. );
  51. $this->assertEquals(
  52. "\033[30;46msome question\033[39;49m",
  53. $formatter->format('<question>some question</question>')
  54. );
  55. }
  56. public function testNestedStyles()
  57. {
  58. $formatter = new OutputFormatter(true);
  59. $this->assertEquals(
  60. "\033[37;41msome \033[39;49m\033[32msome info\033[39m\033[37;41m error\033[39;49m",
  61. $formatter->format('<error>some <info>some info</info> error</error>')
  62. );
  63. }
  64. public function testAdjacentStyles()
  65. {
  66. $formatter = new OutputFormatter(true);
  67. $this->assertEquals(
  68. "\033[37;41msome error\033[39;49m\033[32msome info\033[39m",
  69. $formatter->format('<error>some error</error><info>some info</info>')
  70. );
  71. }
  72. public function testStyleMatchingNotGreedy()
  73. {
  74. $formatter = new OutputFormatter(true);
  75. $this->assertEquals(
  76. "(\033[32m>=2.0,<2.3\033[39m)",
  77. $formatter->format('(<info>>=2.0,<2.3</info>)')
  78. );
  79. }
  80. public function testStyleEscaping()
  81. {
  82. $formatter = new OutputFormatter(true);
  83. $this->assertEquals(
  84. "(\033[32mz>=2.0,<<<a2.3\\\033[39m)",
  85. $formatter->format('(<info>'.$formatter->escape('z>=2.0,<\\<<a2.3\\').'</info>)')
  86. );
  87. $this->assertEquals(
  88. "\033[32m<error>some error</error>\033[39m",
  89. $formatter->format('<info>'.$formatter->escape('<error>some error</error>').'</info>')
  90. );
  91. }
  92. public function testDeepNestedStyles()
  93. {
  94. $formatter = new OutputFormatter(true);
  95. $this->assertEquals(
  96. "\033[37;41merror\033[39;49m\033[32minfo\033[39m\033[33mcomment\033[39m\033[37;41merror\033[39;49m",
  97. $formatter->format('<error>error<info>info<comment>comment</info>error</error>')
  98. );
  99. }
  100. public function testNewStyle()
  101. {
  102. $formatter = new OutputFormatter(true);
  103. $style = new OutputFormatterStyle('blue', 'white');
  104. $formatter->setStyle('test', $style);
  105. $this->assertEquals($style, $formatter->getStyle('test'));
  106. $this->assertNotEquals($style, $formatter->getStyle('info'));
  107. $style = new OutputFormatterStyle('blue', 'white');
  108. $formatter->setStyle('b', $style);
  109. $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>'));
  110. }
  111. public function testRedefineStyle()
  112. {
  113. $formatter = new OutputFormatter(true);
  114. $style = new OutputFormatterStyle('blue', 'white');
  115. $formatter->setStyle('info', $style);
  116. $this->assertEquals("\033[34;47msome custom msg\033[39;49m", $formatter->format('<info>some custom msg</info>'));
  117. }
  118. public function testInlineStyle()
  119. {
  120. $formatter = new OutputFormatter(true);
  121. $this->assertEquals("\033[34;41msome text\033[39;49m", $formatter->format('<fg=blue;bg=red>some text</>'));
  122. $this->assertEquals("\033[34;41msome text\033[39;49m", $formatter->format('<fg=blue;bg=red>some text</fg=blue;bg=red>'));
  123. }
  124. /**
  125. * @param string $tag
  126. * @param string|null $expected
  127. * @param string|null $input
  128. *
  129. * @dataProvider provideInlineStyleOptionsCases
  130. */
  131. public function testInlineStyleOptions($tag, $expected = null, $input = null)
  132. {
  133. $styleString = substr($tag, 1, -1);
  134. $formatter = new OutputFormatter(true);
  135. $method = new \ReflectionMethod($formatter, 'createStyleFromString');
  136. $method->setAccessible(true);
  137. $result = $method->invoke($formatter, $styleString);
  138. if (null === $expected) {
  139. $this->assertFalse($result);
  140. $expected = $tag.$input.'</'.$styleString.'>';
  141. $this->assertSame($expected, $formatter->format($expected));
  142. } else {
  143. /* @var OutputFormatterStyle $result */
  144. $this->assertInstanceOf(OutputFormatterStyle::class, $result);
  145. $this->assertSame($expected, $formatter->format($tag.$input.'</>'));
  146. $this->assertSame($expected, $formatter->format($tag.$input.'</'.$styleString.'>'));
  147. }
  148. }
  149. public function provideInlineStyleOptionsCases()
  150. {
  151. return array(
  152. array('<unknown=_unknown_>'),
  153. array('<unknown=_unknown_;a=1;b>'),
  154. array('<fg=green;>', "\033[32m[test]\033[39m", '[test]'),
  155. array('<fg=green;bg=blue;>', "\033[32;44ma\033[39;49m", 'a'),
  156. array('<fg=green;options=bold>', "\033[32;1mb\033[39;22m", 'b'),
  157. array('<fg=green;options=reverse;>', "\033[32;7m<a>\033[39;27m", '<a>'),
  158. array('<fg=green;options=bold,underscore>', "\033[32;1;4mz\033[39;22;24m", 'z'),
  159. array('<fg=green;options=bold,underscore,reverse;>', "\033[32;1;4;7md\033[39;22;24;27m", 'd'),
  160. );
  161. }
  162. /**
  163. * @group legacy
  164. * @dataProvider provideInlineStyleTagsWithUnknownOptions
  165. * @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)".
  166. */
  167. public function testInlineStyleOptionsUnknownAreDeprecated($tag, $option)
  168. {
  169. $formatter = new OutputFormatter(true);
  170. $formatter->format($tag);
  171. }
  172. public function provideInlineStyleTagsWithUnknownOptions()
  173. {
  174. return array(
  175. array('<options=abc;>', 'abc'),
  176. array('<options=abc,def;>', 'abc'),
  177. array('<fg=green;options=xyz;>', 'xyz'),
  178. array('<fg=green;options=efg,abc>', 'efg'),
  179. );
  180. }
  181. public function testNonStyleTag()
  182. {
  183. $formatter = new OutputFormatter(true);
  184. $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>'));
  185. }
  186. public function testFormatLongString()
  187. {
  188. $formatter = new OutputFormatter(true);
  189. $long = str_repeat('\\', 14000);
  190. $this->assertEquals("\033[37;41msome error\033[39;49m".$long, $formatter->format('<error>some error</error>'.$long));
  191. }
  192. public function testFormatToStringObject()
  193. {
  194. $formatter = new OutputFormatter(false);
  195. $this->assertEquals(
  196. 'some info', $formatter->format(new TableCell())
  197. );
  198. }
  199. public function testNotDecoratedFormatter()
  200. {
  201. $formatter = new OutputFormatter(false);
  202. $this->assertTrue($formatter->hasStyle('error'));
  203. $this->assertTrue($formatter->hasStyle('info'));
  204. $this->assertTrue($formatter->hasStyle('comment'));
  205. $this->assertTrue($formatter->hasStyle('question'));
  206. $this->assertEquals(
  207. 'some error', $formatter->format('<error>some error</error>')
  208. );
  209. $this->assertEquals(
  210. 'some info', $formatter->format('<info>some info</info>')
  211. );
  212. $this->assertEquals(
  213. 'some comment', $formatter->format('<comment>some comment</comment>')
  214. );
  215. $this->assertEquals(
  216. 'some question', $formatter->format('<question>some question</question>')
  217. );
  218. $this->assertEquals(
  219. 'some text with inline style', $formatter->format('<fg=red>some text with inline style</>')
  220. );
  221. $formatter->setDecorated(true);
  222. $this->assertEquals(
  223. "\033[37;41msome error\033[39;49m", $formatter->format('<error>some error</error>')
  224. );
  225. $this->assertEquals(
  226. "\033[32msome info\033[39m", $formatter->format('<info>some info</info>')
  227. );
  228. $this->assertEquals(
  229. "\033[33msome comment\033[39m", $formatter->format('<comment>some comment</comment>')
  230. );
  231. $this->assertEquals(
  232. "\033[30;46msome question\033[39;49m", $formatter->format('<question>some question</question>')
  233. );
  234. $this->assertEquals(
  235. "\033[31msome text with inline style\033[39m", $formatter->format('<fg=red>some text with inline style</>')
  236. );
  237. }
  238. public function testContentWithLineBreaks()
  239. {
  240. $formatter = new OutputFormatter(true);
  241. $this->assertEquals(<<<EOF
  242. \033[32m
  243. some text\033[39m
  244. EOF
  245. , $formatter->format(<<<'EOF'
  246. <info>
  247. some text</info>
  248. EOF
  249. ));
  250. $this->assertEquals(<<<EOF
  251. \033[32msome text
  252. \033[39m
  253. EOF
  254. , $formatter->format(<<<'EOF'
  255. <info>some text
  256. </info>
  257. EOF
  258. ));
  259. $this->assertEquals(<<<EOF
  260. \033[32m
  261. some text
  262. \033[39m
  263. EOF
  264. , $formatter->format(<<<'EOF'
  265. <info>
  266. some text
  267. </info>
  268. EOF
  269. ));
  270. $this->assertEquals(<<<EOF
  271. \033[32m
  272. some text
  273. more text
  274. \033[39m
  275. EOF
  276. , $formatter->format(<<<'EOF'
  277. <info>
  278. some text
  279. more text
  280. </info>
  281. EOF
  282. ));
  283. }
  284. }
  285. class TableCell
  286. {
  287. public function __toString()
  288. {
  289. return '<info>some info</info>';
  290. }
  291. }