OutputTest.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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\Formatter\OutputFormatterStyle;
  13. use Symfony\Component\Console\Output\Output;
  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(['foo', 'bar']);
  68. $this->assertEquals("foo\nbar\n", $output->output, '->writeln() can take an array of messages to output');
  69. }
  70. public function testWriteAnIterableOfMessages()
  71. {
  72. $output = new TestOutput();
  73. $output->writeln($this->generateMessages());
  74. $this->assertEquals("foo\nbar\n", $output->output, '->writeln() can take an iterable of messages to output');
  75. }
  76. private function generateMessages(): iterable
  77. {
  78. yield 'foo';
  79. yield 'bar';
  80. }
  81. /**
  82. * @dataProvider provideWriteArguments
  83. */
  84. public function testWriteRawMessage($message, $type, $expectedOutput)
  85. {
  86. $output = new TestOutput();
  87. $output->writeln($message, $type);
  88. $this->assertEquals($expectedOutput, $output->output);
  89. }
  90. public function provideWriteArguments()
  91. {
  92. return [
  93. ['<info>foo</info>', Output::OUTPUT_RAW, "<info>foo</info>\n"],
  94. ['<info>foo</info>', Output::OUTPUT_PLAIN, "foo\n"],
  95. ];
  96. }
  97. public function testWriteWithDecorationTurnedOff()
  98. {
  99. $output = new TestOutput();
  100. $output->setDecorated(false);
  101. $output->writeln('<info>foo</info>');
  102. $this->assertEquals("foo\n", $output->output, '->writeln() strips decoration tags if decoration is set to false');
  103. }
  104. public function testWriteDecoratedMessage()
  105. {
  106. $fooStyle = new OutputFormatterStyle('yellow', 'red', ['blink']);
  107. $output = new TestOutput();
  108. $output->getFormatter()->setStyle('FOO', $fooStyle);
  109. $output->setDecorated(true);
  110. $output->writeln('<foo>foo</foo>');
  111. $this->assertEquals("\033[33;41;5mfoo\033[39;49;25m\n", $output->output, '->writeln() decorates the output');
  112. }
  113. public function testWriteWithInvalidStyle()
  114. {
  115. $output = new TestOutput();
  116. $output->clear();
  117. $output->write('<bar>foo</bar>');
  118. $this->assertEquals('<bar>foo</bar>', $output->output, '->write() do nothing when a style does not exist');
  119. $output->clear();
  120. $output->writeln('<bar>foo</bar>');
  121. $this->assertEquals("<bar>foo</bar>\n", $output->output, '->writeln() do nothing when a style does not exist');
  122. }
  123. /**
  124. * @dataProvider verbosityProvider
  125. */
  126. public function testWriteWithVerbosityOption($verbosity, $expected, $msg)
  127. {
  128. $output = new TestOutput();
  129. $output->setVerbosity($verbosity);
  130. $output->clear();
  131. $output->write('1', false);
  132. $output->write('2', false, Output::VERBOSITY_QUIET);
  133. $output->write('3', false, Output::VERBOSITY_NORMAL);
  134. $output->write('4', false, Output::VERBOSITY_VERBOSE);
  135. $output->write('5', false, Output::VERBOSITY_VERY_VERBOSE);
  136. $output->write('6', false, Output::VERBOSITY_DEBUG);
  137. $this->assertEquals($expected, $output->output, $msg);
  138. }
  139. public function verbosityProvider()
  140. {
  141. return [
  142. [Output::VERBOSITY_QUIET, '2', '->write() in QUIET mode only outputs when an explicit QUIET verbosity is passed'],
  143. [Output::VERBOSITY_NORMAL, '123', '->write() in NORMAL mode outputs anything below an explicit VERBOSE verbosity'],
  144. [Output::VERBOSITY_VERBOSE, '1234', '->write() in VERBOSE mode outputs anything below an explicit VERY_VERBOSE verbosity'],
  145. [Output::VERBOSITY_VERY_VERBOSE, '12345', '->write() in VERY_VERBOSE mode outputs anything below an explicit DEBUG verbosity'],
  146. [Output::VERBOSITY_DEBUG, '123456', '->write() in DEBUG mode outputs everything'],
  147. ];
  148. }
  149. }
  150. class TestOutput extends Output
  151. {
  152. public $output = '';
  153. public function clear()
  154. {
  155. $this->output = '';
  156. }
  157. protected function doWrite($message, $newline)
  158. {
  159. $this->output .= $message.($newline ? "\n" : '');
  160. }
  161. }