comparison vendor/symfony/console/Tester/ApplicationTester.php @ 0:4c8ae668cc8c

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