OutputTest.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. require_once __DIR__.'/../../../../bootstrap.php';
  10. use Symfony\Components\Console\Output\Output;
  11. $t = new LimeTest(13);
  12. class TestOutput extends Output
  13. {
  14. public $output = '';
  15. static public function getStyle($name)
  16. {
  17. return static::$styles[$name];
  18. }
  19. public function doWrite($message)
  20. {
  21. $this->output .= $message."\n";
  22. }
  23. }
  24. // __construct()
  25. $t->diag('__construct()');
  26. $output = new TestOutput(Output::VERBOSITY_QUIET, true);
  27. $t->is($output->getVerbosity(), Output::VERBOSITY_QUIET, '__construct() takes the verbosity as its first argument');
  28. $t->is($output->isDecorated(), true, '__construct() takes the decorated flag as its second argument');
  29. // ->setDecorated() ->isDecorated()
  30. $t->diag('->setDecorated() ->isDecorated()');
  31. $output = new TestOutput();
  32. $output->setDecorated(true);
  33. $t->is($output->isDecorated(), true, 'setDecorated() sets the decorated flag');
  34. // ->setVerbosity() ->getVerbosity()
  35. $t->diag('->setVerbosity() ->getVerbosity()');
  36. $output->setVerbosity(Output::VERBOSITY_QUIET);
  37. $t->is($output->getVerbosity(), Output::VERBOSITY_QUIET, '->setVerbosity() sets the verbosity');
  38. // ::setStyle()
  39. $t->diag('::setStyle()');
  40. Output::setStyle('FOO', array('bg' => 'red', 'fg' => 'yellow', 'blink' => true));
  41. $t->is(TestOutput::getStyle('foo'), array('bg' => 'red', 'fg' => 'yellow', 'blink' => true), '::setStyle() sets a new style');
  42. // ->write()
  43. $t->diag('->write()');
  44. $output = new TestOutput(Output::VERBOSITY_QUIET);
  45. $output->write('foo');
  46. $t->is($output->output, '', '->write() outputs nothing if verbosity is set to VERBOSITY_QUIET');
  47. $output = new TestOutput();
  48. $output->write(array('foo', 'bar'));
  49. $t->is($output->output, "foo\nbar\n", '->write() can take an array of messages to output');
  50. $output = new TestOutput();
  51. $output->write('<info>foo</info>', Output::OUTPUT_RAW);
  52. $t->is($output->output, "<info>foo</info>\n", '->write() outputs the raw message if OUTPUT_RAW is specified');
  53. $output = new TestOutput();
  54. $output->write('<info>foo</info>', Output::OUTPUT_PLAIN);
  55. $t->is($output->output, "foo\n", '->write() strips decoration tags if OUTPUT_PLAIN is specified');
  56. $output = new TestOutput();
  57. $output->setDecorated(false);
  58. $output->write('<info>foo</info>');
  59. $t->is($output->output, "foo\n", '->write() strips decoration tags if decoration is set to false');
  60. $output = new TestOutput();
  61. $output->setDecorated(true);
  62. $output->write('<foo>foo</foo>');
  63. $t->is($output->output, "\033[33;41;5mfoo\033[0m\n", '->write() decorates the output');
  64. try
  65. {
  66. $output->write('<foo>foo</foo>', 24);
  67. $t->fail('->write() throws an \InvalidArgumentException when the type does not exist');
  68. }
  69. catch (\InvalidArgumentException $e)
  70. {
  71. $t->pass('->write() throws an \InvalidArgumentException when the type does not exist');
  72. }
  73. try
  74. {
  75. $output->write('<bar>foo</bar>');
  76. $t->fail('->write() throws an \InvalidArgumentException when a style does not exist');
  77. }
  78. catch (\InvalidArgumentException $e)
  79. {
  80. $t->pass('->write() throws an \InvalidArgumentException when a style does not exist');
  81. }