OutputFormatterTest.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 testNewStyle()
  36. {
  37. $formatter = new OutputFormatter(true);
  38. $style = $this->getMockBuilder('Symfony\Component\Console\Formatter\OutputFormatterStyleInterface')
  39. ->getMock();
  40. $formatter->setStyle('test', $style);
  41. $this->assertEquals($style, $formatter->getStyle('test'));
  42. $this->assertNotEquals($style, $formatter->getStyle('info'));
  43. $style
  44. ->expects($this->once())
  45. ->method('getBeginStyle')
  46. ->will($this->returnValue('[STYLE_BEG]'));
  47. $style
  48. ->expects($this->once())
  49. ->method('getEndStyle')
  50. ->will($this->returnValue('[STYLE_END]'));
  51. $this->assertEquals(
  52. "[STYLE_BEG]some custom msg[STYLE_END]", $formatter->format('<test>some custom msg</test>')
  53. );
  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('getBeginStyle')
  64. ->will($this->returnValue('[STYLE_BEG]'));
  65. $style
  66. ->expects($this->once())
  67. ->method('getEndStyle')
  68. ->will($this->returnValue('[STYLE_END]'));
  69. $this->assertEquals(
  70. "[STYLE_BEG]some custom msg[STYLE_END]", $formatter->format('<info>some custom msg</info>')
  71. );
  72. }
  73. public function testInlineStyle()
  74. {
  75. $formatter = new OutputFormatter(true);
  76. $this->assertEquals(
  77. "\033[34;41msome text\033[0m", $formatter->format('<fg=blue;bg=red>some text</>')
  78. );
  79. }
  80. public function testNotDecoratedFormatter()
  81. {
  82. $formatter = new OutputFormatter(false);
  83. $this->assertTrue($formatter->hasStyle('error'));
  84. $this->assertTrue($formatter->hasStyle('info'));
  85. $this->assertTrue($formatter->hasStyle('comment'));
  86. $this->assertTrue($formatter->hasStyle('question'));
  87. $this->assertEquals(
  88. "some error", $formatter->format('<error>some error</error>')
  89. );
  90. $this->assertEquals(
  91. "some info", $formatter->format('<info>some info</info>')
  92. );
  93. $this->assertEquals(
  94. "some comment", $formatter->format('<comment>some comment</comment>')
  95. );
  96. $this->assertEquals(
  97. "some question", $formatter->format('<question>some question</question>')
  98. );
  99. $formatter->setDecorated(true);
  100. $this->assertEquals(
  101. "\033[37;41msome error\033[0m", $formatter->format('<error>some error</error>')
  102. );
  103. $this->assertEquals(
  104. "\033[32msome info\033[0m", $formatter->format('<info>some info</info>')
  105. );
  106. $this->assertEquals(
  107. "\033[33msome comment\033[0m", $formatter->format('<comment>some comment</comment>')
  108. );
  109. $this->assertEquals(
  110. "\033[30;46msome question\033[0m", $formatter->format('<question>some question</question>')
  111. );
  112. }
  113. }