OutputTest.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. }
  33. public function testWriteWithVerbosityQuiet()
  34. {
  35. $output = new TestOutput(Output::VERBOSITY_QUIET);
  36. $output->writeln('foo');
  37. $this->assertEquals('', $output->output, '->writeln() outputs nothing if verbosity is set to VERBOSITY_QUIET');
  38. }
  39. public function testWriteAnArrayOfMessages()
  40. {
  41. $output = new TestOutput();
  42. $output->writeln(array('foo', 'bar'));
  43. $this->assertEquals("foo\nbar\n", $output->output, '->writeln() can take an array of messages to output');
  44. }
  45. /**
  46. * @dataProvider provideWriteArguments
  47. */
  48. public function testWriteRawMessage($message, $type, $expectedOutput)
  49. {
  50. $output = new TestOutput();
  51. $output->writeln($message, $type);
  52. $this->assertEquals($expectedOutput, $output->output);
  53. }
  54. public function provideWriteArguments()
  55. {
  56. return array(
  57. array('<info>foo</info>', Output::OUTPUT_RAW, "<info>foo</info>\n"),
  58. array('<info>foo</info>', Output::OUTPUT_PLAIN, "foo\n"),
  59. );
  60. }
  61. public function testWriteWithDecorationTurnedOff()
  62. {
  63. $output = new TestOutput();
  64. $output->setDecorated(false);
  65. $output->writeln('<info>foo</info>');
  66. $this->assertEquals("foo\n", $output->output, '->writeln() strips decoration tags if decoration is set to false');
  67. }
  68. public function testWriteDecoratedMessage()
  69. {
  70. $fooStyle = new OutputFormatterStyle('yellow', 'red', array('blink'));
  71. $output = new TestOutput();
  72. $output->getFormatter()->setStyle('FOO', $fooStyle);
  73. $output->setDecorated(true);
  74. $output->writeln('<foo>foo</foo>');
  75. $this->assertEquals("\033[33;41;5mfoo\033[0m\n", $output->output, '->writeln() decorates the output');
  76. }
  77. /**
  78. * @expectedException \InvalidArgumentException
  79. * @expectedExceptionMessage Unknown output type given (24)
  80. */
  81. public function testWriteWithInvalidOutputType()
  82. {
  83. $output = new TestOutput();
  84. $output->writeln('<foo>foo</foo>', 24);
  85. }
  86. public function testWriteWithInvalidStyle()
  87. {
  88. $output = new TestOutput();
  89. $output->clear();
  90. $output->write('<bar>foo</bar>');
  91. $this->assertEquals('<bar>foo</bar>', $output->output, '->write() do nothing when a style does not exist');
  92. $output->clear();
  93. $output->writeln('<bar>foo</bar>');
  94. $this->assertEquals("<bar>foo</bar>\n", $output->output, '->writeln() do nothing when a style does not exist');
  95. }
  96. }
  97. class TestOutput extends Output
  98. {
  99. public $output = '';
  100. public function clear()
  101. {
  102. $this->output = '';
  103. }
  104. protected function doWrite($message, $newline)
  105. {
  106. $this->output .= $message.($newline ? "\n" : '');
  107. }
  108. }