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

Skip to content

Commit 4e1c9c3

Browse files
committed
refactor
1 parent 85d7fc8 commit 4e1c9c3

File tree

2 files changed

+31
-35
lines changed

2 files changed

+31
-35
lines changed

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

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -35,31 +35,25 @@ class ApplicationTester
3535
private $input;
3636

3737
/**
38-
* @var OutputInterface|ConsoleOutputInterface
38+
* @var OutputInterface
3939
*/
4040
private $output;
41-
private $captureOutputStreamsIndependent;
41+
private $captureOutputStreamsIndependent = false;
4242

43-
/**
44-
* Constructor.
45-
*
46-
* @param Application $application Application to test.
47-
* @param bool $captureOutputStreamsIndependent
48-
*/
49-
public function __construct(Application $application, $captureOutputStreamsIndependent = false)
43+
public function __construct(Application $application)
5044
{
5145
$this->application = $application;
52-
$this->captureOutputStreamsIndependent = $captureOutputStreamsIndependent;
5346
}
5447

5548
/**
5649
* Executes the application.
5750
*
5851
* Available options:
5952
*
60-
* * interactive: Sets the input interactive flag
61-
* * decorated: Sets the output decorated flag
62-
* * verbosity: Sets the output verbosity flag
53+
* * interactive: Sets the input interactive flag
54+
* * decorated: Sets the output decorated flag
55+
* * verbosity: Sets the output verbosity flag
56+
* * capture_stderr_separately: Make output of stdOut and stdErr separately available
6357
*
6458
* @param array $input An array of arguments and options
6559
* @param array $options An array of options
@@ -73,6 +67,7 @@ public function run(array $input, $options = array())
7367
$this->input->setInteractive($options['interactive']);
7468
}
7569

70+
$this->captureOutputStreamsIndependent = array_key_exists('capture_stderr_separately', $options) && $options['capture_stderr_separately'];
7671
if (!$this->captureOutputStreamsIndependent) {
7772
$this->output = new StreamOutput(fopen('php://memory', 'w', false));
7873
if (isset($options['decorated'])) {
@@ -136,7 +131,7 @@ public function getDisplay($normalize = false)
136131
public function getErrorOutput($normalize = false)
137132
{
138133
if (!$this->captureOutputStreamsIndependent) {
139-
throw new \LogicException('Error output is not available when tester is not configured to capture the streams independent.');
134+
throw new \LogicException('Error output not available when tester run without "capture_stderr_separately" option set.');
140135
}
141136

142137
rewind($this->output->getErrorOutput()->getStream());
@@ -163,7 +158,7 @@ public function getInput()
163158
/**
164159
* Gets the output instance used by the last execution of the application.
165160
*
166-
* @return OutputInterface|ConsoleOutputInterface The current output instance
161+
* @return OutputInterface The current output instance
167162
*/
168163
public function getOutput()
169164
{

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

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

413413
$application->setCatchExceptions(true);
414-
$tester->run(array('command' => 'foo'), array('decorated' => false));
414+
$tester->run(array('command' => 'foo'), array('decorated' => false, 'capture_stderr_separately' => true));
415415
$this->assertStringEqualsFile(self::$fixturesPath.'/application_renderexception1.txt', $tester->getErrorOutput(true), '->setCatchExceptions() sets the catch exception flag');
416+
$this->assertSame('', $tester->getDisplay(true));
416417

417418
$application->setCatchExceptions(false);
418419
try {
@@ -459,33 +460,33 @@ public function testRenderException()
459460
$application->expects($this->any())
460461
->method('getTerminalWidth')
461462
->will($this->returnValue(120));
462-
$tester = new ApplicationTester($application, true);
463+
$tester = new ApplicationTester($application);
463464

464-
$tester->run(array('command' => 'foo'), array('decorated' => false));
465+
$tester->run(array('command' => 'foo'), array('decorated' => false, 'capture_stderr_separately' => true));
465466
$this->assertStringEqualsFile(self::$fixturesPath.'/application_renderexception1.txt', $tester->getErrorOutput(true), '->renderException() renders a pretty exception');
466467

467-
$tester->run(array('command' => 'foo'), array('decorated' => false, 'verbosity' => Output::VERBOSITY_VERBOSE));
468+
$tester->run(array('command' => 'foo'), array('decorated' => false, 'verbosity' => Output::VERBOSITY_VERBOSE, 'capture_stderr_separately' => true));
468469
$this->assertContains('Exception trace', $tester->getErrorOutput(), '->renderException() renders a pretty exception with a stack trace when verbosity is verbose');
469470

470-
$tester->run(array('command' => 'list', '--foo' => true), array('decorated' => false));
471+
$tester->run(array('command' => 'list', '--foo' => true), array('decorated' => false, 'capture_stderr_separately' => true));
471472
$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');
472473

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

478-
$tester->run(array('command' => 'foo3:bar'), array('decorated' => true));
479+
$tester->run(array('command' => 'foo3:bar'), array('decorated' => true, 'capture_stderr_separately' => true));
479480
$this->assertStringEqualsFile(self::$fixturesPath.'/application_renderexception3decorated.txt', $tester->getErrorOutput(true), '->renderException() renders a pretty exceptions with previous exceptions');
480481

481482
$application = $this->getMock('Symfony\Component\Console\Application', array('getTerminalWidth'));
482483
$application->setAutoExit(false);
483484
$application->expects($this->any())
484485
->method('getTerminalWidth')
485486
->will($this->returnValue(32));
486-
$tester = new ApplicationTester($application, true);
487+
$tester = new ApplicationTester($application);
487488

488-
$tester->run(array('command' => 'foo'), array('decorated' => false));
489+
$tester->run(array('command' => 'foo'), array('decorated' => false, 'capture_stderr_separately' => true));
489490
$this->assertStringEqualsFile(self::$fixturesPath.'/application_renderexception4.txt', $tester->getErrorOutput(true), '->renderException() wraps messages when they are bigger than the terminal');
490491
}
491492

@@ -502,12 +503,12 @@ public function testRenderExceptionWithDoubleWidthCharacters()
502503
$application->register('foo')->setCode(function () {
503504
throw new \Exception('エラーメッセージ');
504505
});
505-
$tester = new ApplicationTester($application, true);
506+
$tester = new ApplicationTester($application);
506507

507-
$tester->run(array('command' => 'foo'), array('decorated' => false));
508+
$tester->run(array('command' => 'foo'), array('decorated' => false, 'capture_stderr_separately' => true));
508509
$this->assertStringEqualsFile(self::$fixturesPath.'/application_renderexception_doublewidth1.txt', $tester->getErrorOutput(true), '->renderException() renders a pretty exceptions with previous exceptions');
509510

510-
$tester->run(array('command' => 'foo'), array('decorated' => true));
511+
$tester->run(array('command' => 'foo'), array('decorated' => true, 'capture_stderr_separately' => true));
511512
$this->assertStringEqualsFile(self::$fixturesPath.'/application_renderexception_doublewidth1decorated.txt', $tester->getErrorOutput(true), '->renderException() renders a pretty exceptions with previous exceptions');
512513

513514
$application = $this->getMock('Symfony\Component\Console\Application', array('getTerminalWidth'));
@@ -518,8 +519,8 @@ public function testRenderExceptionWithDoubleWidthCharacters()
518519
$application->register('foo')->setCode(function () {
519520
throw new \Exception('コマンドの実行中にエラーが発生しました。');
520521
});
521-
$tester = new ApplicationTester($application, true);
522-
$tester->run(array('command' => 'foo'), array('decorated' => false));
522+
$tester = new ApplicationTester($application);
523+
$tester->run(array('command' => 'foo'), array('decorated' => false, 'capture_stderr_separately' => true));
523524
$this->assertStringEqualsFile(self::$fixturesPath.'/application_renderexception_doublewidth2.txt', $tester->getErrorOutput(true), '->renderException() wraps messages when they are bigger than the terminal');
524525
}
525526

@@ -622,8 +623,8 @@ public function testRunReturnsIntegerExitCode()
622623
$application = $this->getMock('Symfony\Component\Console\Application', array('doRun'));
623624
$application->setAutoExit(false);
624625
$application->expects($this->once())
625-
->method('doRun')
626-
->will($this->throwException($exception));
626+
->method('doRun')
627+
->will($this->throwException($exception));
627628

628629
$exitCode = $application->run(new ArrayInput(array()), new NullOutput());
629630

@@ -637,8 +638,8 @@ public function testRunReturnsExitCodeOneForExceptionCodeZero()
637638
$application = $this->getMock('Symfony\Component\Console\Application', array('doRun'));
638639
$application->setAutoExit(false);
639640
$application->expects($this->once())
640-
->method('doRun')
641-
->will($this->throwException($exception));
641+
->method('doRun')
642+
->will($this->throwException($exception));
642643

643644
$exitCode = $application->run(new ArrayInput(array()), new NullOutput());
644645

0 commit comments

Comments
 (0)