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