10000 [Console] default to stderr in the console helpers by alcohol · Pull Request #15794 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Console] default to stderr in the console helpers #15794

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
add stderr tests
  • Loading branch information
alcohol committed Sep 21, 2015
commit b6e0724dd1a31aeaf69e24b323babfff072bbbc2
69 changes: 68 additions & 1 deletion src/Symfony/Component/Console/Tests/Helper/DialogHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Symfony\Component\Console\Helper\DialogHelper;
use Symfony\Component\Console\Helper\HelperSet;
use Symfony\Component\Console\Helper\FormatterHelper;
use Symfony\Component\Console\Output\ConsoleOutput;
use Symfony\Component\Console\Output\StreamOutput;

class DialogHelperTest extends \PHPUnit_Framework_TestCase
Expand Down Expand Up @@ -50,6 +51,22 @@ public function testSelect()
$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()
{
$dialog = new DialogHelper();
Expand All @@ -63,6 +80,22 @@ public function testAsk()
$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()
{
if (!$this->hasSttyAvailable()) {
Expand Down Expand Up @@ -110,6 +143,25 @@ public function testAskHiddenResponse()
$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()
{
$dialog = new DialogHelper();
Expand Down Expand Up @@ -149,10 +201,12 @@ public function testAskAndValidate()

$dialog->setInputStream($this->getInputStream("green\nyellow\norange\n"));
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();
} catch (\InvalidArgumentException $e) {
$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()));
}
}

Expand All @@ -170,6 +224,19 @@ protected function getOutputStream()
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()
{
exec('stty 2>&1', $output, $exitcode);
Expand Down
0