CommandTester.php 2.8 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\Component\Console\Tester;
  11. use Symfony\Component\Console\Command\Command;
  12. use Symfony\Component\Console\Input\ArrayInput;
  13. use Symfony\Component\Console\Output\StreamOutput;
  14. /**
  15. * Eases the testing of console commands.
  16. *
  17. * @author Fabien Potencier <fabien@symfony.com>
  18. */
  19. class CommandTester
  20. {
  21. private $command;
  22. private $input;
  23. private $output;
  24. /**
  25. * Constructor.
  26. *
  27. * @param Command $command A Command instance to test.
  28. */
  29. public function __construct(Command $command)
  30. {
  31. $this->command = $command;
  32. }
  33. /**
  34. * Executes the command.
  35. *
  36. * Available options:
  37. *
  38. * * interactive: Sets the input interactive flag
  39. * * decorated: Sets the output decorated flag
  40. * * verbosity: Sets the output verbosity flag
  41. *
  42. * @param array $input An array of arguments and options
  43. * @param array $options An array of options
  44. *
  45. * @return integer The command exit code
  46. */
  47. public function execute(array $input, array $options = array())
  48. {
  49. $this->input = new ArrayInput($input);
  50. if (isset($options['interactive'])) {
  51. $this->input->setInteractive($options['interactive']);
  52. }
  53. $this->output = new StreamOutput(fopen('php://memory', 'w', false));
  54. if (isset($options['decorated'])) {
  55. $this->output->setDecorated($options['decorated']);
  56. }
  57. if (isset($options['verbosity'])) {
  58. $this->output->setVerbosity($options['verbosity']);
  59. }
  60. return $this->command->run($this->input, $this->output);
  61. }
  62. /**
  63. * Gets the display returned by the last execution of the command.
  64. *
  65. * @param Boolean $normalize Whether to normalize end of lines to \n or not
  66. *
  67. * @return string The display
  68. */
  69. public function getDisplay($normalize = false)
  70. {
  71. rewind($this->output->getStream());
  72. $display = stream_get_contents($this->output->getStream());
  73. if ($normalize) {
  74. $display = str_replace(PHP_EOL, "\n", $display);
  75. }
  76. return $display;
  77. }
  78. /**
  79. * Gets the input instance used by the last execution of the command.
  80. *
  81. * @return InputInterface The current input instance
  82. */
  83. public function getInput()
  84. {
  85. return $this->input;
  86. }
  87. /**
  88. * Gets the output instance used by the last execution of the command.
  89. *
  90. * @return OutputInterface The current output instance
  91. */
  92. public function getOutput()
  93. {
  94. return $this->output;
  95. }
  96. }