Ei kuvausta

ApplicationTester.php 5.5KB

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