CommandTester.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. * @author Robin Chalas <robin.chalas@gmail.com>
  21. */
  22. class CommandTester
  23. {
  24. private $command;
  25. private $input;
  26. private $output;
  27. private $inputs = [];
  28. private $statusCode;
  29. public function __construct(Command $command)
  30. {
  31. $this->command = $command;
  32. }
  33. /**
  34. * Executes the command.
  35. *
  36. * Available execution 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 command arguments and options
  43. * @param array $options An array of execution options
  44. *
  45. * @return int The command exit code
  46. */
  47. public function execute(array $input, array $options = [])
  48. {
  49. // set the command name automatically if the application requires
  50. // this argument and no command name was passed
  51. if (!isset($input['command'])
  52. && (null !== $application = $this->command->getApplication())
  53. && $application->getDefinition()->hasArgument('command')
  54. ) {
  55. $input = array_merge(['command' => $this->command->getName()], $input);
  56. }
  57. $this->input = new ArrayInput($input);
  58. // Use an in-memory input stream even if no inputs are set so that QuestionHelper::ask() does not rely on the blocking STDIN.
  59. $this->input->setStream(self::createStream($this->inputs));
  60. if (isset($options['interactive'])) {
  61. $this->input->setInteractive($options['interactive']);
  62. }
  63. $this->output = new StreamOutput(fopen('php://memory', 'w', false));
  64. $this->output->setDecorated(isset($options['decorated']) ? $options['decorated'] : false);
  65. if (isset($options['verbosity'])) {
  66. $this->output->setVerbosity($options['verbosity']);
  67. }
  68. return $this->statusCode = $this->command->run($this->input, $this->output);
  69. }
  70. /**
  71. * Gets the display returned by the last execution of the command.
  72. *
  73. * @param bool $normalize Whether to normalize end of lines to \n or not
  74. *
  75. * @return string The display
  76. */
  77. public function getDisplay($normalize = false)
  78. {
  79. if (null === $this->output) {
  80. throw new \RuntimeException('Output not initialized, did you execute the command before requesting the display?');
  81. }
  82. rewind($this->output->getStream());
  83. $display = stream_get_contents($this->output->getStream());
  84. if ($normalize) {
  85. $display = str_replace(PHP_EOL, "\n", $display);
  86. }
  87. return $display;
  88. }
  89. /**
  90. * Gets the input instance used by the last execution of the command.
  91. *
  92. * @return InputInterface The current input instance
  93. */
  94. public function getInput()
  95. {
  96. return $this->input;
  97. }
  98. /**
  99. * Gets the output instance used by the last execution of the command.
  100. *
  101. * @return OutputInterface The current output instance
  102. */
  103. public function getOutput()
  104. {
  105. return $this->output;
  106. }
  107. /**
  108. * Gets the status code returned by the last execution of the application.
  109. *
  110. * @return int The status code
  111. */
  112. public function getStatusCode()
  113. {
  114. return $this->statusCode;
  115. }
  116. /**
  117. * Sets the user inputs.
  118. *
  119. * @param array $inputs An array of strings representing each input
  120. * passed to the command input stream
  121. *
  122. * @return CommandTester
  123. */
  124. public function setInputs(array $inputs)
  125. {
  126. $this->inputs = $inputs;
  127. return $this;
  128. }
  129. private static function createStream(array $inputs)
  130. {
  131. $stream = fopen('php://memory', 'r+', false);
  132. foreach ($inputs as $input) {
  133. fwrite($stream, $input.PHP_EOL);
  134. }
  135. rewind($stream);
  136. return $stream;
  137. }
  138. }