OutputTest.php 4.0 KB

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