Skip to content

Commit e187f8b

Browse files
committed
Merge branch '2.3' into 2.7
* 2.3: [Console] default to stderr in the console helpers
2 parents d66f0eb + ce60be5 commit e187f8b

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

@@ -52,6 +53,10 @@ public function __construct($triggerDeprecationError = true)
5253
*/
5354
public function select(OutputInterface $output, $question, $choices, $default = null, $attempts = false, $errorMessage = 'Value "%s" is invalid', $multiselect = false)
5455
{
56+
if ($output instanceof ConsoleOutputInterface) {
57+
$output = $output->getErrorOutput();
58+
}
59+
5560
$width = max(array_map('strlen', array_keys($choices)));
5661

5762
$messages = (array) $question;
@@ -112,6 +117,10 @@ public function ask(OutputInterface $output, $question, $default = null, array $
112117
return $default;
113118
}
114119

120+
if ($output instanceof ConsoleOutputInterface) {
121+
$output = $output->getErrorOutput();
122+
}
123+
115124
$output->write($question);
116125

117126
$inputStream = $this->inputStream ?: STDIN;
@@ -269,6 +278,10 @@ public function askConfirmation(OutputInterface $output, $question, $default = t
269278
*/
270279
public function askHiddenResponse(OutputInterface $output, $question, $fallback = true)
271280
{
281+
if ($output instanceof ConsoleOutputInterface) {
282+
$output = $output->getErrorOutput();
283+
}
284+
272285
if ('\\' === DIRECTORY_SEPARATOR) {
273286
$exe = __DIR__.'/../Resources/bin/hiddeninput.exe';
274287

@@ -466,6 +479,10 @@ private function hasSttyAvailable()
466479
*/
467480
private function validateAttempts($interviewer, OutputInterface $output, $validator, $attempts)
468481
{
482+
if ($output instanceof ConsoleOutputInterface) {
483+
$output = $output->getErrorOutput();
484+
}
485+
469486
$e = null;
470487
while (false === $attempts || $attempts--) {
471488
if (null !== $e) {

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Console\Helper;
1313

1414
use Symfony\Component\Console\Output\NullOutput;
15+
use Symfony\Component\Console\Output\ConsoleOutputInterface;
1516
use Symfony\Component\Console\Output\OutputInterface;
1617

1718
/**
@@ -193,6 +194,10 @@ public function setRedrawFrequency($freq)
193194
*/
194195
public function start(OutputInterface $output, $max = null)
195196
{
197+
if ($output instanceof ConsoleOutputInterface) {
198+
$output = $output->getErrorOutput();
199+
}
200+
196201
$this->startTime = time();
197202
$this->current = 0;
198203
$this->max = (int) $max;

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

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

2021
/**
@@ -54,6 +55,22 @@ public function testSelect()
5455
$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));
5556
}
5657

58+
public function testSelectOnErrorOutput()
59+
{
60+
$dialog = new DialogHelper();
61+
62+
$helperSet = new HelperSet(array(new FormatterHelper()));
63+
$dialog->setHelperSet($helperSet);
64+
65+
$heroes = array('Superman', 'Batman', 'Spiderman');
66+
67+
$dialog->setInputStream($this->getInputStream("Stdout\n1\n"));
68+
$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));
69+
70+
rewind($output->getErrorOutput()->getStream());
71+
$this->assertContains('Input "Stdout" is not a superhero!', stream_get_contents($output->getErrorOutput()->getStream()));
72+
}
73+
5774
public function testAsk()
5875
{
5976
$dialog = new DialogHelper();
@@ -67,6 +84,22 @@ public function testAsk()
6784
$this->assertEquals('What time is it?', stream_get_contents($output->getStream()));
6885
}
6986

87+
public function testAskOnErrorOutput()
88+
{
89+
if (!$this->hasSttyAvailable()) {
90+
$this->markTestSkipped('`stderr` is required to test stderr output functionality');
91+
}
92+
93+
$dialog = new DialogHelper();
94+
95+
$dialog->setInputStream($this->getInputStream("not stdout\n"));
96+
97+
$this->assertEquals('not stdout', $dialog->ask($output = $this->getConsoleOutput($this->getOutputStream()), 'Where should output go?', 'stderr'));
98+
99+
rewind($output->getErrorOutput()->getStream());
100+
$this->assertEquals('Where should output go?', stream_get_contents($output->getErrorOutput()->getStream()));
101+
}
102+
70103
public function testAskWithAutocomplete()
71104
{
72105
if (!$this->hasSttyAvailable()) {
@@ -114,6 +147,25 @@ public function testAskHiddenResponse()
114147
$this->assertEquals('8AM', $dialog->askHiddenResponse($this->getOutputStream(), 'What time is it?'));
115148
}
116149

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

154206
$dialog->setInputStream($this->getInputStream("green\nyellow\norange\n"));
155207
try {
156-
$this->assertEquals('white', $dialog->askAndValidate($this->getOutputStream(), $question, $validator, 2, 'white'));
208+
$this->assertEquals('white', $dialog->askAndValidate($output = $this->getConsoleOutput($this->getOutputStream()), $question, $validator, 2, 'white'));
157209
$this->fail();
158210
} catch (\InvalidArgumentException $e) {
159211
$this->assertEquals($error, $e->getMessage());
212+
rewind($output->getErrorOutput()->getStream());
213+
$this->assertContains('What color was the white horse of Henry IV?', stream_get_contents($output->getErrorOutput()->getStream()));
160214
}
161215
}
162216

@@ -186,6 +240,19 @@ protected function getOutputStream()
186240
return new StreamOutput(fopen('php://memory', 'r+', false));
187241
}
188242

243+
protected function getConsoleOutput($stderr)
244+
{
245+
$output = new ConsoleOutput();
246+
$output->setErrorOutput($stderr);
247+
248+
return $output;
249+
}
250+
251+
private function hasStderrSupport()
252+
{
253+
return false === $this->isRunningOS400();
254+
}
255+
189256
private function hasSttyAvailable()
190257
{
191258
exec('stty 2>&1', $output, $exitcode);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy