CommandTester.php 3.5 KB

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