Skip to content

[Console] Ease writing to stderr using SymfonyStyle #20586

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
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
10 changes: 10 additions & 0 deletions src/Symfony/Component/Console/Style/OutputStyle.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Symfony\Component\Console\Formatter\OutputFormatterInterface;
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Output\ConsoleOutputInterface;

/**
* Decorates output to add console style guide helpers.
Expand Down Expand Up @@ -145,4 +146,13 @@ public function isDebug()
{
return $this->output->isDebug();
}

protected function getErrorOutput()
{
if (!$this->output instanceof ConsoleOutputInterface) {
return $this->output;
}

return $this->output->getErrorOutput();
}
}
10 changes: 10 additions & 0 deletions src/Symfony/Component/Console/Style/SymfonyStyle.php
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,16 @@ public function newLine($count = 1)
$this->bufferedOutput->write(str_repeat("\n", $count));
}

/**
* Returns a new instance which makes use of stderr if available.
*
* @return self
*/
public function getErrorStyle()
{
return new self($this->input, $this->getErrorOutput());
}

/**
* @return ProgressBar
*/
Expand Down
42 changes: 42 additions & 0 deletions src/Symfony/Component/Console/Tests/Style/SymfonyStyleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
use PHPUnit_Framework_TestCase;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Tester\CommandTester;
use Symfony\Component\Console\Formatter\OutputFormatter;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Output\ConsoleOutputInterface;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;

class SymfonyStyleTest extends PHPUnit_Framework_TestCase
{
Expand Down Expand Up @@ -70,4 +75,41 @@ public function inputCommandToOutputFilesProvider()

return array_map(null, glob($baseDir.'/command/command_*.php'), glob($baseDir.'/output/output_*.txt'));
}

public function testGetErrorStyle()
{
$input = $this->getMockBuilder(InputInterface::class)->getMock();

$errorOutput = $this->getMockBuilder(OutputInterface::class)->getMock();
$errorOutput
->method('getFormatter')
->willReturn(new OutputFormatter());
$errorOutput
->expects($this->once())
->method('write');

$output = $this->getMockBuilder(ConsoleOutputInterface::class)->getMock();
$output
->method('getFormatter')
->willReturn(new OutputFormatter());
$output
->expects($this->once())
->method('getErrorOutput')
->willReturn($errorOutput);

$io = new SymfonyStyle($input, $output);
$io->getErrorStyle()->write('');
}

public function testGetErrorStyleUsesTheCurrentOutputIfNoErrorOutputIsAvailable()
{
$output = $this->getMockBuilder(OutputInterface::class)->getMock();
$output
->method('getFormatter')
->willReturn(new OutputFormatter());

$style = new SymfonyStyle($this->getMockBuilder(InputInterface::class)->getMock(), $output);

$this->assertInstanceOf(SymfonyStyle::class, $style->getErrorStyle());
}
}