OutputTest.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. namespace Symfony\Tests\Component\Console\Output;
  10. use Symfony\Component\Console\Output\Output;
  11. class OutputTest extends \PHPUnit_Framework_TestCase
  12. {
  13. public function testConstructor()
  14. {
  15. $output = new TestOutput(Output::VERBOSITY_QUIET, true);
  16. $this->assertEquals(Output::VERBOSITY_QUIET, $output->getVerbosity(), '__construct() takes the verbosity as its first argument');
  17. $this->assertTrue($output->isDecorated(), '__construct() takes the decorated flag as its second argument');
  18. }
  19. public function testSetIsDecorated()
  20. {
  21. $output = new TestOutput();
  22. $output->setDecorated(true);
  23. $this->assertTrue($output->isDecorated(), 'setDecorated() sets the decorated flag');
  24. }
  25. public function testSetGetVerbosity()
  26. {
  27. $output = new TestOutput();
  28. $output->setVerbosity(Output::VERBOSITY_QUIET);
  29. $this->assertEquals(Output::VERBOSITY_QUIET, $output->getVerbosity(), '->setVerbosity() sets the verbosity');
  30. }
  31. public function testSetStyle()
  32. {
  33. Output::setStyle('FOO', array('bg' => 'red', 'fg' => 'yellow', 'blink' => true));
  34. $this->assertEquals(array('bg' => 'red', 'fg' => 'yellow', 'blink' => true), TestOutput::getStyle('foo'), '::setStyle() sets a new style');
  35. }
  36. public function testWrite()
  37. {
  38. $output = new TestOutput(Output::VERBOSITY_QUIET);
  39. $output->writeln('foo');
  40. $this->assertEquals('', $output->output, '->writeln() outputs nothing if verbosity is set to VERBOSITY_QUIET');
  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. $output = new TestOutput();
  45. $output->writeln('<info>foo</info>', Output::OUTPUT_RAW);
  46. $this->assertEquals("<info>foo</info>\n", $output->output, '->writeln() outputs the raw message if OUTPUT_RAW is specified');
  47. $output = new TestOutput();
  48. $output->writeln('<info>foo</info>', Output::OUTPUT_PLAIN);
  49. $this->assertEquals("foo\n", $output->output, '->writeln() strips decoration tags if OUTPUT_PLAIN is specified');
  50. $output = new TestOutput();
  51. $output->setDecorated(false);
  52. $output->writeln('<info>foo</info>');
  53. $this->assertEquals("foo\n", $output->output, '->writeln() strips decoration tags if decoration is set to false');
  54. $output = new TestOutput();
  55. $output->setDecorated(true);
  56. $output->writeln('<foo>foo</foo>');
  57. $this->assertEquals("\033[33;41;5mfoo\033[0m\n", $output->output, '->writeln() decorates the output');
  58. try {
  59. $output->writeln('<foo>foo</foo>', 24);
  60. $this->fail('->writeln() throws an \InvalidArgumentException when the type does not exist');
  61. } catch (\Exception $e) {
  62. $this->assertInstanceOf('\InvalidArgumentException', $e, '->writeln() throws an \InvalidArgumentException when the type does not exist');
  63. $this->assertEquals('Unknown output type given (24)', $e->getMessage());
  64. }
  65. try {
  66. $output->writeln('<bar>foo</bar>');
  67. $this->fail('->writeln() throws an \InvalidArgumentException when a style does not exist');
  68. } catch (\Exception $e) {
  69. $this->assertInstanceOf('\InvalidArgumentException', $e, '->writeln() throws an \InvalidArgumentException when a style does not exist');
  70. $this->assertEquals('Unknown style "bar".', $e->getMessage());
  71. }
  72. }
  73. }
  74. class TestOutput extends Output
  75. {
  76. public $output = '';
  77. static public function getStyle($name)
  78. {
  79. return static::$styles[$name];
  80. }
  81. public function doWrite($message, $newline)
  82. {
  83. $this->output .= $message.($newline ? "\n" : '');
  84. }
  85. }