OutputFormatterTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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\Tests\Component\Console\Formatter;
  11. use Symfony\Component\Console\Formatter\OutputFormatter;
  12. use Symfony\Component\Console\Formatter\OutputFormatterStyle;
  13. class FormatterStyleTest extends \PHPUnit_Framework_TestCase
  14. {
  15. public function testBundledStyles()
  16. {
  17. $formatter = new OutputFormatter(true);
  18. $this->assertTrue($formatter->hasStyle('error'));
  19. $this->assertTrue($formatter->hasStyle('info'));
  20. $this->assertTrue($formatter->hasStyle('comment'));
  21. $this->assertTrue($formatter->hasStyle('question'));
  22. $this->assertEquals(
  23. "\033[37;41msome error\033[0m", $formatter->format('<error>some error</error>')
  24. );
  25. $this->assertEquals(
  26. "\033[32msome info\033[0m", $formatter->format('<info>some info</info>')
  27. );
  28. $this->assertEquals(
  29. "\033[33msome comment\033[0m", $formatter->format('<comment>some comment</comment>')
  30. );
  31. $this->assertEquals(
  32. "\033[30;46msome question\033[0m", $formatter->format('<question>some question</question>')
  33. );
  34. }
  35. public function testNestedStyles()
  36. {
  37. $formatter = new OutputFormatter(true);
  38. $this->assertEquals(
  39. "\033[37;41msome \033[32msome info\033[0m error\033[0m", $formatter->format('<error>some <info>some info</info> error</error>')
  40. );
  41. }
  42. public function testNewStyle()
  43. {
  44. $formatter = new OutputFormatter(true);
  45. $style = $this->getMockBuilder('Symfony\Component\Console\Formatter\OutputFormatterStyleInterface')->getMock();
  46. $formatter->setStyle('test', $style);
  47. $this->assertEquals($style, $formatter->getStyle('test'));
  48. $this->assertNotEquals($style, $formatter->getStyle('info'));
  49. $style
  50. ->expects($this->once())
  51. ->method('apply')
  52. ->will($this->returnValue('[STYLE_BEG]some custom msg[STYLE_END]'));
  53. $this->assertEquals("[STYLE_BEG]some custom msg[STYLE_END]", $formatter->format('<test>some custom msg</test>'));
  54. }
  55. public function testRedefineStyle()
  56. {
  57. $formatter = new OutputFormatter(true);
  58. $style = $this->getMockBuilder('Symfony\Component\Console\Formatter\OutputFormatterStyleInterface')
  59. ->getMock();
  60. $formatter->setStyle('info', $style);
  61. $style
  62. ->expects($this->once())
  63. ->method('apply')
  64. ->will($this->returnValue('[STYLE_BEG]some custom msg[STYLE_END]'));
  65. $this->assertEquals(
  66. "[STYLE_BEG]some custom msg[STYLE_END]", $formatter->format('<info>some custom msg</info>')
  67. );
  68. }
  69. public function testInlineStyle()
  70. {
  71. $formatter = new OutputFormatter(true);
  72. $this->assertEquals("\033[34;41msome text\033[0m", $formatter->format('<fg=blue;bg=red>some text</>'));
  73. $this->assertEquals("\033[34;41msome text\033[0m", $formatter->format('<fg=blue;bg=red>some text</fg=blue;bg=red>'));
  74. }
  75. public function testNotDecoratedFormatter()
  76. {
  77. $formatter = new OutputFormatter(false);
  78. $this->assertTrue($formatter->hasStyle('error'));
  79. $this->assertTrue($formatter->hasStyle('info'));
  80. $this->assertTrue($formatter->hasStyle('comment'));
  81. $this->assertTrue($formatter->hasStyle('question'));
  82. $this->assertEquals(
  83. "some error", $formatter->format('<error>some error</error>')
  84. );
  85. $this->assertEquals(
  86. "some info", $formatter->format('<info>some info</info>')
  87. );
  88. $this->assertEquals(
  89. "some comment", $formatter->format('<comment>some comment</comment>')
  90. );
  91. $this->assertEquals(
  92. "some question", $formatter->format('<question>some question</question>')
  93. );
  94. $formatter->setDecorated(true);
  95. $this->assertEquals(
  96. "\033[37;41msome error\033[0m", $formatter->format('<error>some error</error>')
  97. );
  98. $this->assertEquals(
  99. "\033[32msome info\033[0m", $formatter->format('<info>some info</info>')
  100. );
  101. $this->assertEquals(
  102. "\033[33msome comment\033[0m", $formatter->format('<comment>some comment</comment>')
  103. );
  104. $this->assertEquals(
  105. "\033[30;46msome question\033[0m", $formatter->format('<question>some question</question>')
  106. );
  107. }
  108. }