OutputTest.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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\Output;
  11. use Symfony\Component\Console\Output\Output;
  12. use Symfony\Component\Console\Formatter\OutputFormatterStyle;
  13. class OutputTest extends \PHPUnit_Framework_TestCase
  14. {
  15. public function testConstructor()
  16. {
  17. $output = new TestOutput(Output::VERBOSITY_QUIET, true);
  18. $this->assertEquals(Output::VERBOSITY_QUIET, $output->getVerbosity(), '__construct() takes the verbosity as its first argument');
  19. $this->assertTrue($output->isDecorated(), '__construct() takes the decorated flag as its second argument');
  20. }
  21. public function testSetIsDecorated()
  22. {
  23. $output = new TestOutput();
  24. $output->setDecorated(true);
  25. $this->assertTrue($output->isDecorated(), 'setDecorated() sets the decorated flag');
  26. }
  27. public function testSetGetVerbosity()
  28. {
  29. $output = new TestOutput();
  30. $output->setVerbosity(Output::VERBOSITY_QUIET);
  31. $this->assertEquals(Output::VERBOSITY_QUIET, $output->getVerbosity(), '->setVerbosity() sets the verbosity');
  32. $this->assertTrue($output->isQuiet());
  33. $this->assertFalse($output->isVerbose());
  34. $this->assertFalse($output->isVeryVerbose());
  35. $this->assertFalse($output->isDebug());
  36. $output->setVerbosity(Output::VERBOSITY_NORMAL);
  37. $this->assertFalse($output->isQuiet());
  38. $this->assertFalse($output->isVerbose());
  39. $this->assertFalse($output->isVeryVerbose());
  40. $this->assertFalse($output->isDebug());
  41. $output->setVerbosity(Output::VERBOSITY_VERBOSE);
  42. $this->assertFalse($output->isQuiet());
  43. $this->assertTrue($output->isVerbose());
  44. $this->assertFalse($output->isVeryVerbose());
  45. $this->assertFalse($output->isDebug());
  46. $output->setVerbosity(Output::VERBOSITY_VERY_VERBOSE);
  47. $this->assertFalse($output->isQuiet());
  48. $this->assertTrue($output->isVerbose());
  49. $this->assertTrue($output->isVeryVerbose());
  50. $this->assertFalse($output->isDebug());
  51. $output->setVerbosity(Output::VERBOSITY_DEBUG);
  52. $this->assertFalse($output->isQuiet());
  53. $this->assertTrue($output->isVerbose());
  54. $this->assertTrue($output->isVeryVerbose());
  55. $this->assertTrue($output->isDebug());
  56. }
  57. public function testWriteWithVerbosityQuiet()
  58. {
  59. $output = new TestOutput(Output::VERBOSITY_QUIET);
  60. $output->writeln('foo');
  61. $this->assertEquals('', $output->output, '->writeln() outputs nothing if verbosity is set to VERBOSITY_QUIET');
  62. }
  63. public function testWriteAnArrayOfMessages()
  64. {
  65. $output = new TestOutput();
  66. $output->writeln(array('foo', 'bar'));
  67. $this->assertEquals("foo\nbar\n", $output->output, '->writeln() can take an array of messages to output');
  68. }
  69. /**
  70. * @dataProvider provideWriteArguments
  71. */
  72. public function testWriteRawMessage($message, $type, $expectedOutput)
  73. {
  74. $output = new TestOutput();
  75. $output->writeln($message, $type);
  76. $this->assertEquals($expectedOutput, $output->output);
  77. }
  78. public function provideWriteArguments()
  79. {
  80. return array(
  81. array('<info>foo</info>', Output::OUTPUT_RAW, "<info>foo</info>\n"),
  82. array('<info>foo</info>', Output::OUTPUT_PLAIN, "foo\n"),
  83. );
  84. }
  85. public function testWriteWithDecorationTurnedOff()
  86. {
  87. $output = new TestOutput();
  88. $output->setDecorated(false);
  89. $output->writeln('<info>foo</info>');
  90. $this->assertEquals("foo\n", $output->output, '->writeln() strips decoration tags if decoration is set to false');
  91. }
  92. public function testWriteDecoratedMessage()
  93. {
  94. $fooStyle = new OutputFormatterStyle('yellow', 'red', array('blink'));
  95. $output = new TestOutput();
  96. $output->getFormatter()->setStyle('FOO', $fooStyle);
  97. $output->setDecorated(true);
  98. $output->writeln('<foo>foo</foo>');
  99. $this->assertEquals("\033[33;41;5mfoo\033[39;49;25m\n", $output->output, '->writeln() decorates the output');
  100. }
  101. /**
  102. * @expectedException \InvalidArgumentException
  103. * @expectedExceptionMessage Unknown output type given (24)
  104. */
  105. public function testWriteWithInvalidOutputType()
  106. {
  107. $output = new TestOutput();
  108. $output->writeln('<foo>foo</foo>', 24);
  109. }
  110. public function testWriteWithInvalidStyle()
  111. {
  112. $output = new TestOutput();
  113. $output->clear();
  114. $output->write('<bar>foo</bar>');
  115. $this->assertEquals('<bar>foo</bar>', $output->output, '->write() do nothing when a style does not exist');
  116. $output->clear();
  117. $output->writeln('<bar>foo</bar>');
  118. $this->assertEquals("<bar>foo</bar>\n", $output->output, '->writeln() do nothing when a style does not exist');
  119. }
  120. }
  121. class TestOutput extends Output
  122. {
  123. public $output = '';
  124. public function clear()
  125. {
  126. $this->output = '';
  127. }
  128. protected function doWrite($message, $newline)
  129. {
  130. $this->output .= $message.($newline ? "\n" : '');
  131. }
  132. }