Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f3fbc7a

Browse files
committed
Use reflection
1 parent bbde83b commit f3fbc7a

File tree

4 files changed

+82
-81
lines changed

4 files changed

+82
-81
lines changed

src/Symfony/Component/Console/Output/ConsoleOutput.php

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,14 @@
1616
/**
1717
* ConsoleOutput is the default class for all CLI output. It uses STDOUT and STDERR.
1818
*
19-
* This class is a convenient wrapper around `StreamOutput`.
19+
* This class is a convenient wrapper around `StreamOutput` for both STDOUT and STDERR.
2020
*
2121
* $output = new ConsoleOutput();
2222
*
2323
* This is equivalent to:
2424
*
2525
* $output = new StreamOutput(fopen('php://stdout', 'w'));
26+
* $stdErr = new StreamOutput(fopen('php://stderr', 'w'));
2627
*
2728
* @author Fabien Potencier <[email protected]>
2829
*/
@@ -96,7 +97,8 @@ public function setErrorOutput(OutputInterface $error)
9697
}
9798

9899
/**
99-
* Returns true if current environment supports writing console output to STDOUT.
100+
* Returns true if current environment supports writing console output to
101+
* STDOUT.
100102
*
101103
* @return bool
102104
*/
@@ -106,7 +108,8 @@ protected function hasStdoutSupport()
106108
}
107109

108110
/**
109-
* Returns true if current environment supports writing console output to STDERR.
111+
* Returns true if current environment supports writing console output to
112+
* STDERR.
110113
*
111114
* @return bool
112115
*/
@@ -116,43 +119,39 @@ protected function hasStderrSupport()
116119
}
117120

118121
/**
119-
* @return resource
122+
* Checks if current executing environment is IBM iSeries (OS400), which
123+
* doesn't properly convert character-encodings between ASCII to EBCDIC.
124+
*
125+
* @return bool
120126
*/
121-
protected function openOutputStream()
127+
private function isRunningOS400()
122128
{
123-
$outputStream = $this->hasStdoutSupport() ? 'php://stdout' : 'php://output';
129+
$checks = array(
130+
function_exists('php_uname') ? php_uname('s') : '',
131+
getenv('OSTYPE'),
132+
PHP_OS,
133+
);
124134

125-
<<<<<<< HEAD
126135
return false !== stripos(implode(';', $checks), 'OS400');
127-
=======
128-
return @fopen($outputStream, 'w') ?: fopen('php://output', 'w');
129-
>>>>>>> build up
130136
}
131137

132138
/**
133139
* @return resource
134140
*/
135-
protected function openErrorStream()
141+
private function openOutputStream()
136142
{
137-
$errorStream = $this->hasStderrSupport() ? 'php://stderr' : 'php://output';
143+
if (!$this->hasStdoutSupport()) {
144+
return fopen('php://output', 'w');
145+
}
138146

139-
return fopen($errorStream, 'w');
147+
return @fopen('php://stdout', 'w') ?: fopen('php://output', 'w');
140148
}
141149

142150
/**
143-
* Checks if current executing environment is IBM iSeries (OS400), which
144-
* doesn't properly convert character-encodings between ASCII to EBCDIC.
145-
*
146-
* @return bool
151+
* @return resource
147152
*/
148-
private function isRunningOS400()
153+
private function openErrorStream()
149154
{
150-
$checks = array(
151-
function_exists('php_uname') ? php_uname('s') : '',
152-
getenv('OSTYPE'),
153-
PHP_OS,
154-
);
155-
156-
return false !== stristr(implode(';', $checks), 'OS400');
155+
return fopen($this->hasStderrSupport() ? 'php://stderr' : 'php://output', 'w');
157156
}
158157
}

src/Symfony/Component/Console/Tester/ApplicationTester.php

Lines changed: 52 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
use Symfony\Component\Console\Input\ArrayInput;
1616
use Symfony\Component\Console\Input\InputInterface;
1717
use Symfony\Component\Console\Output\ConsoleOutput;
18+
use Symfony\Component\Console\Output\ConsoleOutputInterface;
1819
use Symfony\Component\Console\Output\OutputInterface;
20+
use Symfony\Component\Console\Output\StreamOutput;
1921

2022
/**
2123
* Eases the testing of console applications.
@@ -33,28 +35,31 @@ class ApplicationTester
3335
private $input;
3436

3537
/**
36-
* @var TestConsoleOutput
38+
* @var OutputInterface|ConsoleOutputInterface
3739
*/
3840
private $output;
41+
private $captureOutputStreamsIndependent;
3942

4043
/**
4144
* Constructor.
4245
*
43-
* @param Application $application Application to test.
46+
* @param Application $application Application to test.
47+
* @param bool $captureOutputStreamsIndependent
4448
*/
45-
public function __construct(Application $application)
49+
public function __construct(Application $application, $captureOutputStreamsIndependent = false)
4650
{
4751
$this->application = $application;
52+
$this->captureOutputStreamsIndependent = $captureOutputStreamsIndependent;
4853
}
4954

5055
/**
5156
* Executes the application.
5257
*
5358
* Available options:
5459
*
55-
* * interactive: Sets the input interactive flag
56-
* * decorated: Sets the output decorated flag
57-
* * verbosity: Sets the output verbosity flag
60+
* * interactive: Sets the input interactive flag
61+
* * decorated: Sets the output decorated flag
62+
* * verbosity: Sets the output verbosity flag
5863
*
5964
* @param array $input An array of arguments and options
6065
* @param array $options An array of options
@@ -68,10 +73,35 @@ public function run(array $input, $options = array())
6873
$this->input->setInteractive($options['interactive']);
6974
}
7075

71-
$this->output = new TestConsoleOutput(
72-
isset($options['verbosity']) ? $options['verbosity'] : ConsoleOutput::VERBOSITY_NORMAL,
73-
isset($options['decorated']) ? $options['decorated'] : null
74-
);
76+
if (!$this->captureOutputStreamsIndependent) {
77+
$this->output = new StreamOutput(fopen('php://memory', 'w', false));
78+
if (isset($options['decorated'])) {
79+
$this->output->setDecorated($options['decorated']);
80+
}
81+
if (isset($options['verbosity'])) {
82+
$this->output->setVerbosity($options['verbosity']);
83+
}
84+
} else {
85+
$this->output = new ConsoleOutput(
86+
isset($options['verbosity']) ? $options['verbosity'] : ConsoleOutput::VERBOSITY_NORMAL,
87+
isset($options['decorated']) ? $options['decorated'] : null
88+
);
89+
90+
$errorOutput = new StreamOutput(fopen('php://memory', 'w', false));
91+
$errorOutput->setFormatter($this->output->getFormatter());
92+
$errorOutput->setVerbosity($this->output->getVerbosity());
93+
$errorOutput->setDecorated($this->output->isDecorated());
94+
95+
$reflectedOutput = new \ReflectionObject($this->output);
96+
$strErrProperty = $reflectedOutput->getProperty('stderr');
97+
$strErrProperty->setAccessible(true);
98+
$strErrProperty->setValue($this->output, $errorOutput);
99+
100+
$reflectedParent = $reflectedOutput->getParentClass();
101+
$streamProperty = $reflectedParent->getProperty('stream');
102+
$streamProperty->setAccessible(true);
103+
$streamProperty->setValue($this->output, fopen('php://memory', 'w', false));
104+
}
75105

76106
return $this->application->run($this->input, $this->output);
77107
}
@@ -96,8 +126,19 @@ public function getDisplay($normalize = false)
96126
return $display;
97127
}
98128

129+
/**
130+
* Gets the output written to STDERR by the application.
131+
*
132+
* @param bool $normalize Whether to normalize end of lines to \n or not
133+
*
134+
* @return string
135+
*/
99136
public function getErrorOutput($normalize = false)
100137
{
138+
if (!$this->captureOutputStreamsIndependent) {
139+
throw new \LogicException('Error output is not available when tester is not configured to capture the streams independent.');
140+
}
141+
101142
rewind($this->output->getErrorOutput()->getStream());
102143

103144
$display = stream_get_contents($this->output->getErrorOutput()->getStream());
@@ -122,7 +163,7 @@ public function getInput()
122163
/**
123164
* Gets the output instance used by the last execution of the application.
124165
*
125-
* @return OutputInterface The current output instance
166+
* @return OutputInterface|ConsoleOutputInterface The current output instance
126167
*/
127168
public function getOutput()
128169
{

src/Symfony/Component/Console/Tester/TestConsoleOutput.php

Lines changed: 0 additions & 38 deletions
This file was deleted.

src/Symfony/Component/Console/Tests/ApplicationTest.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -408,11 +408,10 @@ public function testSetCatchExceptions()
408408
$application->expects($this->any())
409409
->method('getTerminalWidth')
410410
->will($this->returnValue(120));
411-
$tester = new ApplicationTester($application);
411+
$tester = new ApplicationTester($application, true);
412412

413413
$application->setCatchExceptions(true);
414414
$tester->run(array('command' => 'foo'), array('decorated' => false));
415-
416415
$this->assertStringEqualsFile(self::$fixturesPath.'/application_renderexception1.txt', $tester->getErrorOutput(true), '->setCatchExceptions() sets the catch exception flag');
417416

418417
$application->setCatchExceptions(false);
@@ -460,7 +459,7 @@ public function testRenderException()
460459
$application->expects($this->any())
461460
->method('getTerminalWidth')
462461
->will($this->returnValue(120));
463-
$tester = new ApplicationTester($application);
462+
$tester = new ApplicationTester($application, true);
464463

465464
$tester->run(array('command' => 'foo'), array('decorated' => false));
466465
$this->assertStringEqualsFile(self::$fixturesPath.'/application_renderexception1.txt', $tester->getErrorOutput(true), '->renderException() renders a pretty exception');
@@ -472,7 +471,7 @@ public function testRenderException()
472471
$this->assertStringEqualsFile(self::$fixturesPath.'/application_renderexception2.txt', $tester->getErrorOutput(true), '->renderException() renders the command synopsis when an exception occurs in the context of a command');
473472

474473
$application->add(new \Foo3Command());
475-
$tester = new ApplicationTester($application);
474+
$tester = new ApplicationTester($application, true);
476475
$tester->run(array('command' => 'foo3:bar'), array('decorated' => false));
477476
$this->assertStringEqualsFile(self::$fixturesPath.'/application_renderexception3.txt', $tester->getErrorOutput(true), '->renderException() renders a pretty exceptions with previous exceptions');
478477

@@ -484,7 +483,7 @@ public function testRenderException()
484483
$application->expects($this->any())
485484
->method('getTerminalWidth')
486485
->will($this->returnValue(32));
487-
$tester = new ApplicationTester($application);
486+
$tester = new ApplicationTester($application, true);
488487

489488
$tester->run(array('command' => 'foo'), array('decorated' => false));
490489
$this->assertStringEqualsFile(self::$fixturesPath.'/application_renderexception4.txt', $tester->getErrorOutput(true), '->renderException() wraps messages when they are bigger than the terminal');
@@ -503,7 +502,7 @@ public function testRenderExceptionWithDoubleWidthCharacters()
503502
$application->register('foo')->setCode(function () {
504503
throw new \Exception('エラーメッセージ');
505504
});
506-
$tester = new ApplicationTester($application);
505+
$tester = new ApplicationTester($application, true);
507506

508507
$tester->run(array('command' => 'foo'), array('decorated' => false));
509508
$this->assertStringEqualsFile(self::$fixturesPath.'/application_renderexception_doublewidth1.txt', $tester->getErrorOutput(true), '->renderException() renders a pretty exceptions with previous exceptions');
@@ -519,7 +518,7 @@ public function testRenderExceptionWithDoubleWidthCharacters()
519518
$application->register('foo')->setCode(function () {
520519
throw new \Exception('コマンドの実行中にエラーが発生しました。');
521520
});
522-
$tester = new ApplicationTester($application);
521+
$tester = new ApplicationTester($application, true);
523522
$tester->run(array('command' => 'foo'), array('decorated' => false));
524523
$this->assertStringEqualsFile(self::$fixturesPath.'/application_renderexception_doublewidth2.txt', $tester->getErrorOutput(true), '->renderException() wraps messages when they are bigger than the terminal');
525524
}

0 commit comments

Comments
 (0)