OutputTest.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. class OutputTest extends \PHPUnit_Framework_TestCase
  13. {
  14. public function testConstructor()
  15. {
  16. $output = new TestOutput(Output::VERBOSITY_QUIET, true);
  17. $this->assertEquals(Output::VERBOSITY_QUIET, $output->getVerbosity(), '__construct() takes the verbosity as its first argument');
  18. $this->assertTrue($output->isDecorated(), '__construct() takes the decorated flag as its second argument');
  19. }
  20. public function testSetIsDecorated()
  21. {
  22. $output = new TestOutput();
  23. $output->setDecorated(true);
  24. $this->assertTrue($output->isDecorated(), 'setDecorated() sets the decorated flag');
  25. }
  26. public function testSetGetVerbosity()
  27. {
  28. $output = new TestOutput();
  29. $output->setVerbosity(Output::VERBOSITY_QUIET);
  30. $this->assertEquals(Output::VERBOSITY_QUIET, $output->getVerbosity(), '->setVerbosity() sets the verbosity');
  31. }
  32. public function testSetStyle()
  33. {
  34. Output::setStyle('FOO', array('bg' => 'red', 'fg' => 'yellow', 'blink' => true));
  35. $r = new \ReflectionClass('Symfony\Component\Console\Output\Output');
  36. $p = $r->getProperty('styles');
  37. $p->setAccessible(true);
  38. $styles = $p->getValue(null);
  39. $this->assertEquals(array('bg' => 'red', 'fg' => 'yellow', 'blink' => true), $styles['foo'], '::setStyle() sets a new style');
  40. }
  41. public function testWrite()
  42. {
  43. $output = new TestOutput(Output::VERBOSITY_QUIET);
  44. $output->writeln('foo');
  45. $this->assertEquals('', $output->output, '->writeln() outputs nothing if verbosity is set to VERBOSITY_QUIET');
  46. $output = new TestOutput();
  47. $output->writeln(array('foo', 'bar'));
  48. $this->assertEquals("foo\nbar\n", $output->output, '->writeln() can take an array of messages to output');
  49. $output = new TestOutput();
  50. $output->writeln('<info>foo</info>', Output::OUTPUT_RAW);
  51. $this->assertEquals("<info>foo</info>\n", $output->output, '->writeln() outputs the raw message if OUTPUT_RAW is specified');
  52. $output = new TestOutput();
  53. $output->writeln('<info>foo</info>', Output::OUTPUT_PLAIN);
  54. $this->assertEquals("foo\n", $output->output, '->writeln() strips decoration tags if OUTPUT_PLAIN is specified');
  55. $output = new TestOutput();
  56. $output->setDecorated(false);
  57. $output->writeln('<info>foo</info>');
  58. $this->assertEquals("foo\n", $output->output, '->writeln() strips decoration tags if decoration is set to false');
  59. $output = new TestOutput();
  60. $output->setDecorated(true);
  61. $output->writeln('<foo>foo</foo>');
  62. $this->assertEquals("\033[33;41;5mfoo\033[0m\n", $output->output, '->writeln() decorates the output');
  63. try {
  64. $output->writeln('<foo>foo</foo>', 24);
  65. $this->fail('->writeln() throws an \InvalidArgumentException when the type does not exist');
  66. } catch (\Exception $e) {
  67. $this->assertInstanceOf('\InvalidArgumentException', $e, '->writeln() throws an \InvalidArgumentException when the type does not exist');
  68. $this->assertEquals('Unknown output type given (24)', $e->getMessage());
  69. }
  70. $output->clear();
  71. $output->write('<bar>foo</bar>');
  72. $this->assertEquals('<bar>foo</bar>', $output->output, '->write() do nothing when a style does not exist');
  73. $output->clear();
  74. $output->writeln('<bar>foo</bar>');
  75. $this->assertEquals("<bar>foo</bar>\n", $output->output, '->writeln() do nothing when a style does not exist');
  76. }
  77. }
  78. class TestOutput extends Output
  79. {
  80. public $output = '';
  81. public function clear()
  82. {
  83. $this->output = '';
  84. }
  85. public function doWrite($message, $newline)
  86. {
  87. $this->output .= $message.($newline ? "\n" : '');
  88. }
  89. }