OutputTest.php 6.3 KB

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