annotate vendor/symfony/console/Tester/ApplicationTester.php @ 19:fa3358dc1485 tip

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