TesterTrait.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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 PHPUnit\Framework\Assert;
  12. use Symfony\Component\Console\Input\InputInterface;
  13. use Symfony\Component\Console\Output\ConsoleOutput;
  14. use Symfony\Component\Console\Output\OutputInterface;
  15. use Symfony\Component\Console\Output\StreamOutput;
  16. use Symfony\Component\Console\Tester\Constraint\CommandIsSuccessful;
  17. /**
  18. * @author Amrouche Hamza <hamza.simperfit@gmail.com>
  19. */
  20. trait TesterTrait
  21. {
  22. /**
  23. * @var \Symfony\Component\Console\Output\StreamOutput
  24. */
  25. private $output;
  26. /**
  27. * @var mixed[]
  28. */
  29. private $inputs = [];
  30. /**
  31. * @var bool
  32. */
  33. private $captureStreamsIndependently = false;
  34. /**
  35. * @var \Symfony\Component\Console\Input\InputInterface
  36. */
  37. private $input;
  38. /**
  39. * @var int
  40. */
  41. private $statusCode;
  42. /**
  43. * Gets the display returned by the last execution of the command or application.
  44. *
  45. * @throws \RuntimeException If it's called before the execute method
  46. * @param bool $normalize
  47. */
  48. public function getDisplay($normalize = false)
  49. {
  50. if (!isset($this->output)) {
  51. throw new \RuntimeException('Output not initialized, did you execute the command before requesting the display?');
  52. }
  53. rewind($this->output->getStream());
  54. $display = stream_get_contents($this->output->getStream());
  55. if ($normalize) {
  56. $display = str_replace(\PHP_EOL, "\n", $display);
  57. }
  58. return $display;
  59. }
  60. /**
  61. * Gets the output written to STDERR by the application.
  62. *
  63. * @param bool $normalize Whether to normalize end of lines to \n or not
  64. */
  65. public function getErrorOutput($normalize = false)
  66. {
  67. if (!$this->captureStreamsIndependently) {
  68. throw new \LogicException('The error output is not available when the tester is run without "capture_stderr_separately" option set.');
  69. }
  70. rewind($this->output->getErrorOutput()->getStream());
  71. $display = stream_get_contents($this->output->getErrorOutput()->getStream());
  72. if ($normalize) {
  73. $display = str_replace(\PHP_EOL, "\n", $display);
  74. }
  75. return $display;
  76. }
  77. /**
  78. * Gets the input instance used by the last execution of the command or application.
  79. */
  80. public function getInput()
  81. {
  82. return $this->input;
  83. }
  84. /**
  85. * Gets the output instance used by the last execution of the command or application.
  86. */
  87. public function getOutput()
  88. {
  89. return $this->output;
  90. }
  91. /**
  92. * Gets the status code returned by the last execution of the command or application.
  93. *
  94. * @throws \RuntimeException If it's called before the execute method
  95. */
  96. public function getStatusCode()
  97. {
  98. if (!isset($this->statusCode)) {
  99. throw new \RuntimeException('Status code not initialized, did you execute the command before requesting the status code?');
  100. }
  101. return $this->statusCode;
  102. }
  103. /**
  104. * @param string $message
  105. */
  106. public function assertCommandIsSuccessful($message = '')
  107. {
  108. Assert::assertThat($this->statusCode, new CommandIsSuccessful(), $message);
  109. }
  110. /**
  111. * Sets the user inputs.
  112. *
  113. * @param array $inputs An array of strings representing each input
  114. * passed to the command input stream
  115. *
  116. * @return $this
  117. */
  118. public function setInputs($inputs)
  119. {
  120. $this->inputs = $inputs;
  121. return $this;
  122. }
  123. /**
  124. * Initializes the output property.
  125. *
  126. * Available options:
  127. *
  128. * * decorated: Sets the output decorated flag
  129. * * verbosity: Sets the output verbosity flag
  130. * * capture_stderr_separately: Make output of stdOut and stdErr separately available
  131. * @param mixed[] $options
  132. */
  133. private function initOutput($options)
  134. {
  135. $this->captureStreamsIndependently = \array_key_exists('capture_stderr_separately', $options) && $options['capture_stderr_separately'];
  136. if (!$this->captureStreamsIndependently) {
  137. $this->output = new StreamOutput(fopen('php://memory', 'w', false));
  138. if (isset($options['decorated'])) {
  139. $this->output->setDecorated($options['decorated']);
  140. }
  141. if (isset($options['verbosity'])) {
  142. $this->output->setVerbosity($options['verbosity']);
  143. }
  144. } else {
  145. $this->output = new ConsoleOutput(
  146. $options['verbosity'] ?? ConsoleOutput::VERBOSITY_NORMAL,
  147. $options['decorated'] ?? null
  148. );
  149. $errorOutput = new StreamOutput(fopen('php://memory', 'w', false));
  150. $errorOutput->setFormatter($this->output->getFormatter());
  151. $errorOutput->setVerbosity($this->output->getVerbosity());
  152. $errorOutput->setDecorated($this->output->isDecorated());
  153. $reflectedOutput = new \ReflectionObject($this->output);
  154. $strErrProperty = $reflectedOutput->getProperty('stderr');
  155. $strErrProperty->setValue($this->output, $errorOutput);
  156. $reflectedParent = $reflectedOutput->getParentClass();
  157. $streamProperty = $reflectedParent->getProperty('stream');
  158. $streamProperty->setValue($this->output, fopen('php://memory', 'w', false));
  159. }
  160. }
  161. /**
  162. * @return resource
  163. * @param mixed[] $inputs
  164. */
  165. private static function createStream($inputs)
  166. {
  167. $stream = fopen('php://memory', 'r+', false);
  168. foreach ($inputs as $input) {
  169. fwrite($stream, $input.\PHP_EOL);
  170. }
  171. rewind($stream);
  172. return $stream;
  173. }
  174. }