-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Console][2.3] ApplicationTester - test stdout and stderr #17255
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ | |
use Symfony\Component\Console\Application; | ||
use Symfony\Component\Console\Input\ArrayInput; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\ConsoleOutput; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Symfony\Component\Console\Output\StreamOutput; | ||
|
||
|
@@ -31,13 +32,13 @@ class ApplicationTester | |
{ | ||
private $application; | ||
private $input; | ||
private $output; | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param Application $application An Application instance to test. | ||
* @var OutputInterface | ||
*/ | ||
private $output; | ||
private $captureOutputStreamsIndependent = false; | ||
|
||
public function __construct(Application $application) | ||
{ | ||
$this->application = $application; | ||
|
@@ -48,9 +49,10 @@ public function __construct(Application $application) | |
* | ||
* Available options: | ||
* | ||
* * interactive: Sets the input interactive flag | ||
* * decorated: Sets the output decorated flag | ||
* * verbosity: Sets the output verbosity flag | ||
* * interactive: Sets the input interactive flag | ||
* * decorated: Sets the output decorated flag | ||
* * verbosity: Sets the output verbosity flag | ||
* * capture_stderr_separately: Make output of stdOut and stdErr separately available | ||
* | ||
* @param array $input An array of arguments and options | ||
* @param array $options An array of options | ||
|
@@ -64,12 +66,35 @@ public function run(array $input, $options = array()) | |
$this->input->setInteractive($options['interactive']); | ||
} | ||
|
||
$this->output = new StreamOutput(fopen('php://memory', 'w', false)); | ||
if (isset($options['decorated'])) { | ||
$this->output->setDecorated($options['decorated']); | ||
} | ||
if (isset($options['verbosity'])) { | ||
$this->output->setVerbosity($options['verbosity']); | ||
$this->captureOutputStreamsIndependent = array_key_exists('capture_stderr_separately', $options) && $options['capture_stderr_separately']; | ||
if (!$this->captureOutputStreamsIndependent) { | ||
$this->output = new StreamOutput(fopen('php://memory', 'w', false)); | ||
if (isset($options['decorated'])) { | ||
$this->output->setDecorated($options['decorated']); | ||
} | ||
if (isset($options['verbosity'])) { | ||
$this->output->setVerbosity($options['verbosity']); | ||
} | ||
} else { | ||
$this->output = new ConsoleOutput( | ||
isset($options['verbosity']) ? $options['verbosity'] : ConsoleOutput::VERBOSITY_NORMAL, | ||
isset($options['decorated']) ? $options['decorated'] : null | ||
); | ||
|
||
$errorOutput = new StreamOutput(fopen('php://memory', 'w', false)); | ||
$errorOutput->setFormatter($this->output->getFormatter()); | ||
$errorOutput->setVerbosity($this->output->getVerbosity()); | ||
$errorOutput->setDecorated($this->output->isDecorated()); | ||
|
||
$reflectedOutput = new \ReflectionObject($this->output); | ||
$strErrProperty = $reflectedOutput->getProperty('stderr'); | ||
$strErrProperty->setAccessible(true); | ||
$strErrProperty->setValue($this->output, $errorOutput); | ||
|
||
$reflectedParent = $reflectedOutput->getParentClass(); | ||
$streamProperty = $reflectedParent->getProperty('stream'); | ||
$streamProperty->setAccessible(true); | ||
$streamProperty->setValue($this->output, fopen('php://memory', 'w', false)); | ||
} | ||
|
||
return $this->application->run($this->input, $this->output); | ||
|
@@ -95,6 +120,30 @@ public function getDisplay($normalize = false) | |
return $display; | ||
} | ||
|
||
/** | ||
* Gets the output written to STDERR by the application. | ||
* | ||
* @param bool $normalize Whether to normalize end of lines to \n or not | ||
* | ||
* @return string | ||
*/ | ||
public function getErrorOutput($normalize = false) | ||
{ | ||
if (!$this->captureOutputStreamsIndependent) { | ||
throw new \LogicException('Error output not available when tester run without "capture_stderr_separately" option set.'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 updated, I've rename the variable (#17255 (comment)) as well |
||
} | ||
|
||
rewind($this->output->getErrorOutput()->getStream()); | ||
|
||
$display = stream_get_contents($this->output->getErrorOutput()->getStream()); | ||
|
||
if ($normalize) { | ||
$display = str_replace(PHP_EOL, "\n", $display); | ||
} | ||
|
||
return $display; | ||
} | ||
|
||
/** | ||
* Gets the input instance used by the last execution of the application. | ||
* | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
$captureStreamsIndependently
?