TesterTrait.php 5.9 KB

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