TesterTrait.php 5.6 KB

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