Merge branch '2.3' into 2.7
* 2.3: [Console] default to stderr in the console helpers
This commit is contained in:
commit
e187f8bdb1
@ -11,6 +11,7 @@
|
|||||||
|
|
||||||
namespace Symfony\Component\Console\Helper;
|
namespace Symfony\Component\Console\Helper;
|
||||||
|
|
||||||
|
use Symfony\Component\Console\Output\ConsoleOutputInterface;
|
||||||
use Symfony\Component\Console\Output\OutputInterface;
|
use Symfony\Component\Console\Output\OutputInterface;
|
||||||
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
|
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
|
||||||
|
|
||||||
@ -52,6 +53,10 @@ class DialogHelper extends InputAwareHelper
|
|||||||
*/
|
*/
|
||||||
public function select(OutputInterface $output, $question, $choices, $default = null, $attempts = false, $errorMessage = 'Value "%s" is invalid', $multiselect = false)
|
public function select(OutputInterface $output, $question, $choices, $default = null, $attempts = false, $errorMessage = 'Value "%s" is invalid', $multiselect = false)
|
||||||
{
|
{
|
||||||
|
if ($output instanceof ConsoleOutputInterface) {
|
||||||
|
$output = $output->getErrorOutput();
|
||||||
|
}
|
||||||
|
|
||||||
$width = max(array_map('strlen', array_keys($choices)));
|
$width = max(array_map('strlen', array_keys($choices)));
|
||||||
|
|
||||||
$messages = (array) $question;
|
$messages = (array) $question;
|
||||||
@ -112,6 +117,10 @@ class DialogHelper extends InputAwareHelper
|
|||||||
return $default;
|
return $default;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ($output instanceof ConsoleOutputInterface) {
|
||||||
|
$output = $output->getErrorOutput();
|
||||||
|
}
|
||||||
|
|
||||||
$output->write($question);
|
$output->write($question);
|
||||||
|
|
||||||
$inputStream = $this->inputStream ?: STDIN;
|
$inputStream = $this->inputStream ?: STDIN;
|
||||||
@ -269,6 +278,10 @@ class DialogHelper extends InputAwareHelper
|
|||||||
*/
|
*/
|
||||||
public function askHiddenResponse(OutputInterface $output, $question, $fallback = true)
|
public function askHiddenResponse(OutputInterface $output, $question, $fallback = true)
|
||||||
{
|
{
|
||||||
|
if ($output instanceof ConsoleOutputInterface) {
|
||||||
|
$output = $output->getErrorOutput();
|
||||||
|
}
|
||||||
|
|
||||||
if ('\\' === DIRECTORY_SEPARATOR) {
|
if ('\\' === DIRECTORY_SEPARATOR) {
|
||||||
$exe = __DIR__.'/../Resources/bin/hiddeninput.exe';
|
$exe = __DIR__.'/../Resources/bin/hiddeninput.exe';
|
||||||
|
|
||||||
@ -466,6 +479,10 @@ class DialogHelper extends InputAwareHelper
|
|||||||
*/
|
*/
|
||||||
private function validateAttempts($interviewer, OutputInterface $output, $validator, $attempts)
|
private function validateAttempts($interviewer, OutputInterface $output, $validator, $attempts)
|
||||||
{
|
{
|
||||||
|
if ($output instanceof ConsoleOutputInterface) {
|
||||||
|
$output = $output->getErrorOutput();
|
||||||
|
}
|
||||||
|
|
||||||
$e = null;
|
$e = null;
|
||||||
while (false === $attempts || $attempts--) {
|
while (false === $attempts || $attempts--) {
|
||||||
if (null !== $e) {
|
if (null !== $e) {
|
||||||
|
@ -12,6 +12,7 @@
|
|||||||
namespace Symfony\Component\Console\Helper;
|
namespace Symfony\Component\Console\Helper;
|
||||||
|
|
||||||
use Symfony\Component\Console\Output\NullOutput;
|
use Symfony\Component\Console\Output\NullOutput;
|
||||||
|
use Symfony\Component\Console\Output\ConsoleOutputInterface;
|
||||||
use Symfony\Component\Console\Output\OutputInterface;
|
use Symfony\Component\Console\Output\OutputInterface;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -193,6 +194,10 @@ class ProgressHelper extends Helper
|
|||||||
*/
|
*/
|
||||||
public function start(OutputInterface $output, $max = null)
|
public function start(OutputInterface $output, $max = null)
|
||||||
{
|
{
|
||||||
|
if ($output instanceof ConsoleOutputInterface) {
|
||||||
|
$output = $output->getErrorOutput();
|
||||||
|
}
|
||||||
|
|
||||||
$this->startTime = time();
|
$this->startTime = time();
|
||||||
$this->current = 0;
|
$this->current = 0;
|
||||||
$this->max = (int) $max;
|
$this->max = (int) $max;
|
||||||
|
@ -15,6 +15,7 @@ use Symfony\Component\Console\Input\ArrayInput;
|
|||||||
use Symfony\Component\Console\Helper\DialogHelper;
|
use Symfony\Component\Console\Helper\DialogHelper;
|
||||||
use Symfony\Component\Console\Helper\HelperSet;
|
use Symfony\Component\Console\Helper\HelperSet;
|
||||||
use Symfony\Component\Console\Helper\FormatterHelper;
|
use Symfony\Component\Console\Helper\FormatterHelper;
|
||||||
|
use Symfony\Component\Console\Output\ConsoleOutput;
|
||||||
use Symfony\Component\Console\Output\StreamOutput;
|
use Symfony\Component\Console\Output\StreamOutput;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -54,6 +55,22 @@ class LegacyDialogHelperTest extends \PHPUnit_Framework_TestCase
|
|||||||
$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));
|
$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));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testSelectOnErrorOutput()
|
||||||
|
{
|
||||||
|
$dialog = new DialogHelper();
|
||||||
|
|
||||||
|
$helperSet = new HelperSet(array(new FormatterHelper()));
|
||||||
|
$dialog->setHelperSet($helperSet);
|
||||||
|
|
||||||
|
$heroes = array('Superman', 'Batman', 'Spiderman');
|
||||||
|
|
||||||
|
$dialog->setInputStream($this->getInputStream("Stdout\n1\n"));
|
||||||
|
$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));
|
||||||
|
|
||||||
|
rewind($output->getErrorOutput()->getStream());
|
||||||
|
$this->assertContains('Input "Stdout" is not a superhero!', stream_get_contents($output->getErrorOutput()->getStream()));
|
||||||
|
}
|
||||||
|
|
||||||
public function testAsk()
|
public function testAsk()
|
||||||
{
|
{
|
||||||
$dialog = new DialogHelper();
|
$dialog = new DialogHelper();
|
||||||
@ -67,6 +84,22 @@ class LegacyDialogHelperTest extends \PHPUnit_Framework_TestCase
|
|||||||
$this->assertEquals('What time is it?', stream_get_contents($output->getStream()));
|
$this->assertEquals('What time is it?', stream_get_contents($output->getStream()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testAskOnErrorOutput()
|
||||||
|
{
|
||||||
|
if (!$this->hasSttyAvailable()) {
|
||||||
|
$this->markTestSkipped('`stderr` is required to test stderr output functionality');
|
||||||
|
}
|
||||||
|
|
||||||
|
$dialog = new DialogHelper();
|
||||||
|
|
||||||
|
$dialog->setInputStream($this->getInputStream("not stdout\n"));
|
||||||
|
|
||||||
|
$this->assertEquals('not stdout', $dialog->ask($output = $this->getConsoleOutput($this->getOutputStream()), 'Where should output go?', 'stderr'));
|
||||||
|
|
||||||
|
rewind($output->getErrorOutput()->getStream());
|
||||||
|
$this->assertEquals('Where should output go?', stream_get_contents($output->getErrorOutput()->getStream()));
|
||||||
|
}
|
||||||
|
|
||||||
public function testAskWithAutocomplete()
|
public function testAskWithAutocomplete()
|
||||||
{
|
{
|
||||||
if (!$this->hasSttyAvailable()) {
|
if (!$this->hasSttyAvailable()) {
|
||||||
@ -114,6 +147,25 @@ class LegacyDialogHelperTest extends \PHPUnit_Framework_TestCase
|
|||||||
$this->assertEquals('8AM', $dialog->askHiddenResponse($this->getOutputStream(), 'What time is it?'));
|
$this->assertEquals('8AM', $dialog->askHiddenResponse($this->getOutputStream(), 'What time is it?'));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @group tty
|
||||||
|
*/
|
||||||
|
public function testAskHiddenResponseOnErrorOutput()
|
||||||
|
{
|
||||||
|
if ('\\' === DIRECTORY_SEPARATOR) {
|
||||||
|
$this->markTestSkipped('This test is not supported on Windows');
|
||||||
|
}
|
||||||
|
|
||||||
|
$dialog = new DialogHelper();
|
||||||
|
|
||||||
|
$dialog->setInputStream($this->getInputStream("8AM\n"));
|
||||||
|
|
||||||
|
$this->assertEquals('8AM', $dialog->askHiddenResponse($output = $this->getConsoleOutput($this->getOutputStream()), 'What time is it?'));
|
||||||
|
|
||||||
|
rewind($output->getErrorOutput()->getStream());
|
||||||
|
$this->assertContains('What time is it?', stream_get_contents($output->getErrorOutput()->getStream()));
|
||||||
|
}
|
||||||
|
|
||||||
public function testAskConfirmation()
|
public function testAskConfirmation()
|
||||||
{
|
{
|
||||||
$dialog = new DialogHelper();
|
$dialog = new DialogHelper();
|
||||||
@ -153,10 +205,12 @@ class LegacyDialogHelperTest extends \PHPUnit_Framework_TestCase
|
|||||||
|
|
||||||
$dialog->setInputStream($this->getInputStream("green\nyellow\norange\n"));
|
$dialog->setInputStream($this->getInputStream("green\nyellow\norange\n"));
|
||||||
try {
|
try {
|
||||||
$this->assertEquals('white', $dialog->askAndValidate($this->getOutputStream(), $question, $validator, 2, 'white'));
|
$this->assertEquals('white', $dialog->askAndValidate($output = $this->getConsoleOutput($this->getOutputStream()), $question, $validator, 2, 'white'));
|
||||||
$this->fail();
|
$this->fail();
|
||||||
} catch (\InvalidArgumentException $e) {
|
} catch (\InvalidArgumentException $e) {
|
||||||
$this->assertEquals($error, $e->getMessage());
|
$this->assertEquals($error, $e->getMessage());
|
||||||
|
rewind($output->getErrorOutput()->getStream());
|
||||||
|
$this->assertContains('What color was the white horse of Henry IV?', stream_get_contents($output->getErrorOutput()->getStream()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -186,6 +240,19 @@ class LegacyDialogHelperTest extends \PHPUnit_Framework_TestCase
|
|||||||
return new StreamOutput(fopen('php://memory', 'r+', false));
|
return new StreamOutput(fopen('php://memory', 'r+', false));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected function getConsoleOutput($stderr)
|
||||||
|
{
|
||||||
|
$output = new ConsoleOutput();
|
||||||
|
$output->setErrorOutput($stderr);
|
||||||
|
|
||||||
|
return $output;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function hasStderrSupport()
|
||||||
|
{
|
||||||
|
return false === $this->isRunningOS400();
|
||||||
|
}
|
||||||
|
|
||||||
private function hasSttyAvailable()
|
private function hasSttyAvailable()
|
||||||
{
|
{
|
||||||
exec('stty 2>&1', $output, $exitcode);
|
exec('stty 2>&1', $output, $exitcode);
|
||||||
|
Reference in New Issue
Block a user