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

Skip to content

Commit 3d4e95e

Browse files
alcoholfabpot
authored andcommitted
[Console] default to stderr in the console helpers
1 parent bd415c6 commit 3d4e95e

File tree

3 files changed

+90
-1
lines changed

3 files changed

+90
-1
lines changed

src/Symfony/Component/Console/Helper/DialogHelper.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\Console\Helper;
1313

14+
use Symfony\Component\Console\Output\ConsoleOutputInterface;
1415
use Symfony\Component\Console\Output\OutputInterface;
1516
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
1617

@@ -42,6 +43,10 @@ class DialogHelper extends Helper
4243
*/
4344
public function select(OutputInterface $output, $question, $choices, $default = null, $attempts = false, $errorMessage = 'Value "%s" is invalid', $multiselect = false)
4445
{
46+
if ($output instanceof ConsoleOutputInterface) {
47+
$output = $output->getErrorOutput();
48+
}
49+
4550
$width = max(array_map('strlen', array_keys($choices)));
4651

4752
$messages = (array) $question;
@@ -98,6 +103,10 @@ public function select(OutputInterface $output, $question, $choices, $default =
98103
*/
99104
public function ask(OutputInterface $output, $question, $default = null, array $autocomplete = null)
100105
{
106+
if ($output instanceof ConsoleOutputInterface) {
107+
$output = $output->getErrorOutput();
108+
}
109+
101110
$output->write($question);
102111

103112
$inputStream = $this->inputStream ?: STDIN;
@@ -255,6 +264,10 @@ public function askConfirmation(OutputInterface $output, $question, $default = t
255264
*/
256265
public function askHiddenResponse(OutputInterface $output, $question, $fallback = true)
257266
{
267+
if ($output instanceof ConsoleOutputInterface) {
268+
$output = $output->getErrorOutput();
269+
}
270+
258271
if ('\\' === DIRECTORY_SEPARATOR) {
259272
$exe = __DIR__.'/../Resources/bin/hiddeninput.exe';
260273

@@ -452,6 +465,10 @@ private function hasSttyAvailable()
452465
*/
453466
private function validateAttempts($interviewer, OutputInterface $output, $validator, $attempts)
454467
{
468+
if ($output instanceof ConsoleOutputInterface) {
469+
$output = $output->getErrorOutput();
470+
}
471+
455472
$e = null;
456473
while (false === $attempts || $attempts--) {
457474
if (null !== $e) {

src/Symfony/Component/Console/Helper/ProgressHelper.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\Console\Helper;
1313

14+
use Symfony\Component\Console\Output\ConsoleOutputInterface;
1415
use Symfony\Component\Console\Output\OutputInterface;
1516

1617
/**
@@ -182,6 +183,10 @@ public function setRedrawFrequency($freq)
182183
*/
183184
public function start(OutputInterface $output, $max = null)
184185
{
186+
if ($output instanceof ConsoleOutputInterface) {
187+
$output = $output->getErrorOutput();
188+
}
189+
185190
$this->startTime = time();
186191
$this->current = 0;
187192
$this->max = (int) $max;

src/Symfony/Component/Console/Tests/Helper/DialogHelperTest.php

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Component\Console\Helper\DialogHelper;
1515
use Symfony\Component\Console\Helper\HelperSet;
1616
use Symfony\Component\Console\Helper\FormatterHelper;
17+
use Symfony\Component\Console\Output\ConsoleOutput;
1718
use Symfony\Component\Console\Output\StreamOutput;
1819

1920
class DialogHelperTest extends \PHPUnit_Framework_TestCase
@@ -50,6 +51,22 @@ public function testSelect()
5051
$this->assertEquals(array('0', '1'), $dialog->select($this->getOutputStream(), 'What is your favorite superhero?', $heroes, ' 0 , 1 ', false, 'Input "%s" is not a superhero!', true));
5152
}
5253

54+
public function testSelectOnErrorOutput()
55+
{
56+
$dialog = new DialogHelper();
57+
58+
$helperSet = new HelperSet(array(new FormatterHelper()));
59+
$dialog->setHelperSet($helperSet);
60+
61+
$heroes = array('Superman', 'Batman', 'Spiderman');
62+
63+
$dialog->setInputStream($this->getInputStream("Stdout\n1\n"));
64+
$this->assertEquals('1', $dialog->select($output = $this->getConsoleOutput($this->getOutputStream()), 'What is your favorite superhero?', $heroes, null, false, 'Input "%s" is not a superhero!', false));
65+
66+
rewind($output->getErrorOutput()->getStream());
67+
$this->assertContains('Input "Stdout" is not a superhero!', stream_get_contents($output->getErrorOutput()->getStream()));
68+
}
69+
5370
public function testAsk()
5471
{
5572
$dialog = new DialogHelper();
@@ -63,6 +80,22 @@ public function testAsk()
6380
$this->assertEquals('What time is it?', stream_get_contents($output->getStream()));
6481
}
6582

83+
public function testAskOnErrorOutput()
84+
{
85+
if (!$this->hasSttyAvailable()) {
86+
$this->markTestSkipped('`stderr` is required to test stderr output functionality');
87+
}
88+
89+
$dialog = new DialogHelper();
90+
91+
$dialog->setInputStream($this->getInputStream("not stdout\n"));
92+
93+
$this->assertEquals('not stdout', $dialog->ask($output = $this->getConsoleOutput($this->getOutputStream()), 'Where should output go?', 'stderr'));
94+
95+
rewind($output->getErrorOutput()->getStream());
96+
$this->assertEquals('Where should output go?', stream_get_contents($output->getErrorOutput()->getStream()));
97+
}
98+
6699
public function testAskWithAutocomplete()
67100
{
68101
if (!$this->hasSttyAvailable()) {
@@ -110,6 +143,25 @@ public function testAskHiddenResponse()
110143
$this->assertEquals('8AM', $dialog->askHiddenResponse($this->getOutputStream(), 'What time is it?'));
111144
}
112145

146+
/**
147+
* @group tty
148+
*/
149+
public function testAskHiddenResponseOnErrorOutput()
150+
{
151+
if ('\\' === DIRECTORY_SEPARATOR) {
152+
$this->markTestSkipped('This test is not supported on Windows');
153+
}
154+
155+
$dialog = new DialogHelper();
156+
157+
$dialog->setInputStream($this->getInputStream("8AM\n"));
158+
159+
$this->assertEquals('8AM', $dialog->askHiddenResponse($output = $this->getConsoleOutput($this->getOutputStream()), 'What time is it?'));
160+
161+
rewind($output->getErrorOutput()->getStream());
162+
$this->assertContains('What time is it?', stream_get_contents($output->getErrorOutput()->getStream()));
163+
}
164+
113165
public function testAskConfirmation()
114166
{
115167
$dialog = new DialogHelper();
@@ -149,10 +201,12 @@ public function testAskAndValidate()
149201

150202
$dialog->setInputStream($this->getInputStream("green\nyellow\norange\n"));
151203
try {
152-
$this->assertEquals('white', $dialog->askAndValidate($this->getOutputStream(), $question, $validator, 2, 'white'));
204+
$this->assertEquals('white', $dialog->askAndValidate($output = $this->getConsoleOutput($this->getOutputStream()), $question, $validator, 2, 'white'));
153205
$this->fail();
154206
} catch (\InvalidArgumentException $e) {
155207
$this->assertEquals($error, $e->getMessage());
208+
rewind($output->getErrorOutput()->getStream());
209+
$this->assertContains('What color was the white horse of Henry IV?', stream_get_contents($output->getErrorOutput()->getStream()));
156210
}
157211
}
158212

@@ -170,6 +224,19 @@ protected function getOutputStream()
170224
return new StreamOutput(fopen('php://memory', 'r+', false));
171225
}
172226

227+
protected function getConsoleOutput($stderr)
228+
{
229+
$output = new ConsoleOutput();
230+
$output->setErrorOutput($stderr);
231+
232+
return $output;
233+
}
234+
235+
private function hasStderrSupport()
236+
{
237+
return false === $this->isRunningOS400();
238+
}
239+
173240
private function hasSttyAvailable()
174241
{
175242
exec('stty 2>&1', $output, $exitcode);

0 commit comments

Comments
 (0)