暫無描述

CommandTester.php 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. use Symfony\Component\Console\Input\InputInterface;
  15. use Symfony\Component\Console\Output\OutputInterface;
  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 = array();
  28. private $statusCode;
  29. /**
  30. * Constructor.
  31. *
  32. * @param Command $command A Command instance to test
  33. */
  34. public function __construct(Command $command)
  35. {
  36. $this->command = $command;
  37. }
  38. /**
  39. * Executes the command.
  40. *
  41. * Available execution options:
  42. *
  43. * * interactive: Sets the input interactive flag
  44. * * decorated: Sets the output decorated flag
  45. * * verbosity: Sets the output verbosity flag
  46. *
  47. * @param array $input An array of command arguments and options
  48. * @param array $options An array of execution options
  49. *
  50. * @return int The command exit code
  51. */
  52. public function execute(array $input, array $options = array())
  53. {
  54. // set the command name automatically if the application requires
  55. // this argument and no command name was passed
  56. if (!isset($input['command'])
  57. && (null !== $application = $this->command->getApplication())
  58. && $application->getDefinition()->hasArgument('command')
  59. ) {
  60. $input = array_merge(array('command' => $this->command->getName()), $input);
  61. }
  62. $this->input = new ArrayInput($input);
  63. if ($this->inputs) {
  64. $this->input->setStream(self::createStream($this->inputs));
  65. }
  66. if (isset($options['interactive'])) {
  67. $this->input->setInteractive($options['interactive']);
  68. }
  69. $this->output = new StreamOutput(fopen('php://memory', 'w', false));
  70. if (isset($options['decorated'])) {
  71. $this->output->setDecorated($options['decorated']);
  72. }
  73. if (isset($options['verbosity'])) {
  74. $this->output->setVerbosity($options['verbosity']);
  75. }
  76. return $this->statusCode = $this->command->run($this->input, $this->output);
  77. }
  78. /**
  79. * Gets the display returned by the last execution of the command.
  80. *
  81. * @param bool $normalize Whether to normalize end of lines to \n or not
  82. *
  83. * @return string The display
  84. */
  85. public function getDisplay($normalize = false)
  86. {
  87. rewind($this->output->getStream());
  88. $display = stream_get_contents($this->output->getStream());
  89. if ($normalize) {
  90. $display = str_replace(PHP_EOL, "\n", $display);
  91. }
  92. return $display;
  93. }
  94. /**
  95. * Gets the input instance used by the last execution of the command.
  96. *
  97. * @return InputInterface The current input instance
  98. */
  99. public function getInput()
  100. {
  101. return $this->input;
  102. }
  103. /**
  104. * Gets the output instance used by the last execution of the command.
  105. *
  106. * @return OutputInterface The current output instance
  107. */
  108. public function getOutput()
  109. {
  110. return $this->output;
  111. }
  112. /**
  113. * Gets the status code returned by the last execution of the application.
  114. *
  115. * @return int The status code
  116. */
  117. public function getStatusCode()
  118. {
  119. return $this->statusCode;
  120. }
  121. /**
  122. * Sets the user inputs.
  123. *
  124. * @param array An array of strings representing each input
  125. * passed to the command input stream.
  126. *
  127. * @return CommandTester
  128. */
  129. public function setInputs(array $inputs)
  130. {
  131. $this->inputs = $inputs;
  132. return $this;
  133. }
  134. private static function createStream(array $inputs)
  135. {
  136. $stream = fopen('php://memory', 'r+', false);
  137. fwrite($stream, implode(PHP_EOL, $inputs));
  138. rewind($stream);
  139. return $stream;
  140. }
  141. }