OutputTest.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. public function testWriteWithInvalidStyle()
  102. {
  103. $output = new TestOutput();
  104. $output->clear();
  105. $output->write('<bar>foo</bar>');
  106. $this->assertEquals('<bar>foo</bar>', $output->output, '->write() do nothing when a style does not exist');
  107. $output->clear();
  108. $output->writeln('<bar>foo</bar>');
  109. $this->assertEquals("<bar>foo</bar>\n", $output->output, '->writeln() do nothing when a style does not exist');
  110. }
  111. /**
  112. * @dataProvider verbosityProvider
  113. */
  114. public function testWriteWithVerbosityOption($verbosity, $expected, $msg)
  115. {
  116. $output = new TestOutput();
  117. $output->setVerbosity($verbosity);
  118. $output->clear();
  119. $output->write('1', false);
  120. $output->write('2', false, Output::VERBOSITY_QUIET);
  121. $output->write('3', false, Output::VERBOSITY_NORMAL);
  122. $output->write('4', false, Output::VERBOSITY_VERBOSE);
  123. $output->write('5', false, Output::VERBOSITY_VERY_VERBOSE);
  124. $output->write('6', false, Output::VERBOSITY_DEBUG);
  125. $this->assertEquals($expected, $output->output, $msg);
  126. }
  127. public function verbosityProvider()
  128. {
  129. return array(
  130. array(Output::VERBOSITY_QUIET, '2', '->write() in QUIET mode only outputs when an explicit QUIET verbosity is passed'),
  131. array(Output::VERBOSITY_NORMAL, '123', '->write() in NORMAL mode outputs anything below an explicit VERBOSE verbosity'),
  132. array(Output::VERBOSITY_VERBOSE, '1234', '->write() in VERBOSE mode outputs anything below an explicit VERY_VERBOSE verbosity'),
  133. array(Output::VERBOSITY_VERY_VERBOSE, '12345', '->write() in VERY_VERBOSE mode outputs anything below an explicit DEBUG verbosity'),
  134. array(Output::VERBOSITY_DEBUG, '123456', '->write() in DEBUG mode outputs everything'),
  135. );
  136. }
  137. }
  138. class TestOutput extends Output
  139. {
  140. public $output = '';
  141. public function clear()
  142. {
  143. $this->output = '';
  144. }
  145. protected function doWrite($message, $newline)
  146. {
  147. $this->output .= $message.($newline ? "\n" : '');
  148. }
  149. }