OutputTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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\Tests\Component\Console\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 testWrite()
  34. {
  35. $fooStyle = new OutputFormatterStyle('yellow', 'red', array('blink'));
  36. $output = new TestOutput(Output::VERBOSITY_QUIET);
  37. $output->writeln('foo');
  38. $this->assertEquals('', $output->output, '->writeln() outputs nothing if verbosity is set to VERBOSITY_QUIET');
  39. $output = new TestOutput();
  40. $output->writeln(array('foo', 'bar'));
  41. $this->assertEquals("foo\nbar\n", $output->output, '->writeln() can take an array of messages to output');
  42. $output = new TestOutput();
  43. $output->writeln('<info>foo</info>', Output::OUTPUT_RAW);
  44. $this->assertEquals("<info>foo</info>\n", $output->output, '->writeln() outputs the raw message if OUTPUT_RAW is specified');
  45. $output = new TestOutput();
  46. $output->writeln('<info>foo</info>', Output::OUTPUT_PLAIN);
  47. $this->assertEquals("foo\n", $output->output, '->writeln() strips decoration tags if OUTPUT_PLAIN is specified');
  48. $output = new TestOutput();
  49. $output->setDecorated(false);
  50. $output->writeln('<info>foo</info>');
  51. $this->assertEquals("foo\n", $output->output, '->writeln() strips decoration tags if decoration is set to false');
  52. $output = new TestOutput();
  53. $output->getFormatter()->setStyle('FOO', $fooStyle);
  54. $output->setDecorated(true);
  55. $output->writeln('<foo>foo</foo>');
  56. $this->assertEquals("\033[33;41;5mfoo\033[0m\n", $output->output, '->writeln() decorates the output');
  57. try {
  58. $output->writeln('<foo>foo</foo>', 24);
  59. $this->fail('->writeln() throws an \InvalidArgumentException when the type does not exist');
  60. } catch (\Exception $e) {
  61. $this->assertInstanceOf('\InvalidArgumentException', $e, '->writeln() throws an \InvalidArgumentException when the type does not exist');
  62. $this->assertEquals('Unknown output type given (24)', $e->getMessage());
  63. }
  64. $output->clear();
  65. $output->write('<bar>foo</bar>');
  66. $this->assertEquals('<bar>foo</bar>', $output->output, '->write() do nothing when a style does not exist');
  67. $output->clear();
  68. $output->writeln('<bar>foo</bar>');
  69. $this->assertEquals("<bar>foo</bar>\n", $output->output, '->writeln() do nothing when a style does not exist');
  70. }
  71. }
  72. class TestOutput extends Output
  73. {
  74. public $output = '';
  75. public function clear()
  76. {
  77. $this->output = '';
  78. }
  79. public function doWrite($message, $newline)
  80. {
  81. $this->output .= $message.($newline ? "\n" : '');
  82. }
  83. }