ApplicationTester.php 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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\Output\ConsoleOutput;
  14. use Symfony\Component\Console\Output\StreamOutput;
  15. /**
  16. * Eases the testing of console applications.
  17. *
  18. * When testing an application, don't forget to disable the auto exit flag:
  19. *
  20. * $application = new Application();
  21. * $application->setAutoExit(false);
  22. *
  23. * @author Fabien Potencier <fabien@symfony.com>
  24. */
  25. class ApplicationTester
  26. {
  27. use TesterTrait;
  28. private $application;
  29. private $input;
  30. private $statusCode;
  31. private $captureStreamsIndependently = false;
  32. public function __construct(Application $application)
  33. {
  34. $this->application = $application;
  35. }
  36. /**
  37. * Executes the application.
  38. *
  39. * Available options:
  40. *
  41. * * interactive: Sets the input interactive flag
  42. * * decorated: Sets the output decorated flag
  43. * * verbosity: Sets the output verbosity flag
  44. * * capture_stderr_separately: Make output of stdOut and stdErr separately available
  45. *
  46. * @param array $input An array of arguments and options
  47. * @param array $options An array of options
  48. *
  49. * @return int The command exit code
  50. */
  51. public function run(array $input, $options = array())
  52. {
  53. $this->input = new ArrayInput($input);
  54. if (isset($options['interactive'])) {
  55. $this->input->setInteractive($options['interactive']);
  56. }
  57. $shellInteractive = getenv('SHELL_INTERACTIVE');
  58. if ($this->inputs) {
  59. $this->input->setStream(self::createStream($this->inputs));
  60. putenv('SHELL_INTERACTIVE=1');
  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. $this->statusCode = $this->application->run($this->input, $this->output);
  90. putenv($shellInteractive ? "SHELL_INTERACTIVE=$shellInteractive" : 'SHELL_INTERACTIVE');
  91. return $this->statusCode;
  92. }
  93. /**
  94. * Gets the output written to STDERR by the application.
  95. *
  96. * @param bool $normalize Whether to normalize end of lines to \n or not
  97. *
  98. * @return string
  99. */
  100. public function getErrorOutput($normalize = false)
  101. {
  102. if (!$this->captureStreamsIndependently) {
  103. throw new \LogicException('The error output is not available when the tester is run without "capture_stderr_separately" option set.');
  104. }
  105. rewind($this->output->getErrorOutput()->getStream());
  106. $display = stream_get_contents($this->output->getErrorOutput()->getStream());
  107. if ($normalize) {
  108. $display = str_replace(PHP_EOL, "\n", $display);
  109. }
  110. return $display;
  111. }
  112. }