TesterTrait.php 5.5 KB

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