Без опису

TesterTrait.php 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. * @param bool $normalize Whether to normalize end of lines to \n or not
  28. *
  29. * @return string The display
  30. */
  31. public function getDisplay($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($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. * @return int The status code
  84. */
  85. public function getStatusCode()
  86. {
  87. return $this->statusCode;
  88. }
  89. /**
  90. * Sets the user inputs.
  91. *
  92. * @param array $inputs An array of strings representing each input
  93. * passed to the command input stream
  94. *
  95. * @return $this
  96. */
  97. public function setInputs(array $inputs)
  98. {
  99. $this->inputs = $inputs;
  100. return $this;
  101. }
  102. /**
  103. * Initializes the output property.
  104. *
  105. * Available options:
  106. *
  107. * * decorated: Sets the output decorated flag
  108. * * verbosity: Sets the output verbosity flag
  109. * * capture_stderr_separately: Make output of stdOut and stdErr separately available
  110. */
  111. private function initOutput(array $options)
  112. {
  113. $this->captureStreamsIndependently = \array_key_exists('capture_stderr_separately', $options) && $options['capture_stderr_separately'];
  114. if (!$this->captureStreamsIndependently) {
  115. $this->output = new StreamOutput(fopen('php://memory', 'w', false));
  116. if (isset($options['decorated'])) {
  117. $this->output->setDecorated($options['decorated']);
  118. }
  119. if (isset($options['verbosity'])) {
  120. $this->output->setVerbosity($options['verbosity']);
  121. }
  122. } else {
  123. $this->output = new ConsoleOutput(
  124. $options['verbosity'] ?? ConsoleOutput::VERBOSITY_NORMAL,
  125. $options['decorated'] ?? null
  126. );
  127. $errorOutput = new StreamOutput(fopen('php://memory', 'w', false));
  128. $errorOutput->setFormatter($this->output->getFormatter());
  129. $errorOutput->setVerbosity($this->output->getVerbosity());
  130. $errorOutput->setDecorated($this->output->isDecorated());
  131. $reflectedOutput = new \ReflectionObject($this->output);
  132. $strErrProperty = $reflectedOutput->getProperty('stderr');
  133. $strErrProperty->setAccessible(true);
  134. $strErrProperty->setValue($this->output, $errorOutput);
  135. $reflectedParent = $reflectedOutput->getParentClass();
  136. $streamProperty = $reflectedParent->getProperty('stream');
  137. $streamProperty->setAccessible(true);
  138. $streamProperty->setValue($this->output, fopen('php://memory', 'w', false));
  139. }
  140. }
  141. /**
  142. * @return resource
  143. */
  144. private static function createStream(array $inputs)
  145. {
  146. $stream = fopen('php://memory', 'r+', false);
  147. foreach ($inputs as $input) {
  148. fwrite($stream, $input.\PHP_EOL);
  149. }
  150. rewind($stream);
  151. return $stream;
  152. }
  153. }