diff --git a/src/Symfony/Component/Console/Style/OutputStyle.php b/src/Symfony/Component/Console/Style/OutputStyle.php index de7be1e08b3f3..1274a98d13b6b 100644 --- a/src/Symfony/Component/Console/Style/OutputStyle.php +++ b/src/Symfony/Component/Console/Style/OutputStyle.php @@ -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. @@ -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(); + } } diff --git a/src/Symfony/Component/Console/Style/SymfonyStyle.php b/src/Symfony/Component/Console/Style/SymfonyStyle.php index 3c9617fbd25f0..d9d9794cf2c1e 100644 --- a/src/Symfony/Component/Console/Style/SymfonyStyle.php +++ b/src/Symfony/Component/Console/Style/SymfonyStyle.php @@ -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 */ diff --git a/src/Symfony/Component/Console/Tests/Style/SymfonyStyleTest.php b/src/Symfony/Component/Console/Tests/Style/SymfonyStyleTest.php index 3338f4b055cc4..2210c38ca3828 100644 --- a/src/Symfony/Component/Console/Tests/Style/SymfonyStyleTest.php +++ b/src/Symfony/Component/Console/Tests/Style/SymfonyStyleTest.php @@ -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 { @@ -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()); + } }