TesterTrait.php 5.7 KB

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