OutputFormatterTest.php 4.3 KB

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