TesterTrait.php 5.2 KB

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