8000 [Console] Ease writing to stderr using styles · symfony/symfony@43b7085 · GitHub
[go: up one dir, main page]

Skip to content

Commit 43b7085

Browse files
committed
[Console] Ease writing to stderr using styles
1 parent ad64c8a commit 43b7085

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed

src/Symfony/Component/Console/Style/OutputStyle.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
use Symfony\Component\Console\Formatter\OutputFormatterInterface;
1515
use Symfony\Component\Console\Helper\ProgressBar;
1616
use Symfony\Component\Console\Output\OutputInterface;
17+
use Symfony\Component\Console\Output\ConsoleOutputInterface;
18+
use Symfony\Component\Console\Exception\LogicException;
1719

1820
/**
1921
* Decorates output to add console style guide helpers.
@@ -145,4 +147,13 @@ public function isDebug()
145147
{
146148
return $this->output->isDebug();
147149
}
150+
151+
protected function getErrorOutput()
152+
{
153+
if (!$this->output instanceof ConsoleOutputInterface) {
154+
throw new LogicException(sprintf('The output must be an instance of %s for using getErrorOutput().', ConsoleOutputInterface::class));
155+
}
156+
157+
return $this->output->getErrorOutput();
158+
}
148159
}

src/Symfony/Component/Console/Style/SymfonyStyle.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\Console\Style;
1313

14+
use Symfony\Component\Console\Exception\LogicException;
1415
use Symfony\Component\Console\Exception\RuntimeException;
1516
use Symfony\Component\Console\Formatter\OutputFormatter;
1617
use Symfony\Component\Console\Helper\Helper;
@@ -337,6 +338,18 @@ public function newLine($count = 1)
337338
$this->bufferedOutput->write(str_repeat("\n", $count));
338339
}
339340

341+
/**
342+
* Returns a new instance which makes use of stderr.
343+
*
344+
* @return self
345+
*
346+
* @throws LogicException If stderr is not accessible
347+
*/
348+
public function getErrorStyle()
349+
{
350+
return new self($this->input, $this->getErrorOutput());
351+
}
352+
340353
/**
341354
* @return ProgressBar
342355
*/

src/Symfony/Component/Console/Tests/Style/SymfonyStyleTest.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@
1414
use PHPUnit_Framework_TestCase;
1515
use Symfony\Component\Console\Command\Command;
1616
use Symfony\Component\Console\Tester\CommandTester;
17+
use Symfony\Component\Console\Formatter\OutputFormatter;
18+
use Symfony\Component\Console\Output\NullOutput;
19+
use Symfony\Component\Console\Output\StreamOutput;
20+
use Symfony\Component\Console\Output\OutputInterface;
21+
use Symfony\Component\Console\Output\ConsoleOutputInterface;
22+
use Symfony\Component\Console\Input\InputInterface;
23+
use Symfony\Component\Console\Style\SymfonyStyle;
1724

1825
class SymfonyStyleTest extends PHPUnit_Framework_TestCase
1926
{
@@ -70,4 +77,38 @@ public function inputCommandToOutputFilesProvider()
7077

7178
return array_map(null, glob($baseDir.'/command/command_*.php'), glob($baseDir.'/output/output_*.txt'));
7279
}
80+
81+
public function testGetErrorStyle()
82+
{
83+
$input = $this->getMockBuilder(InputInterface::class)->getMock();
84+
85+
$errorOutput = $this->getMockBuilder(OutputInterface::class)->getMock();
86+
$errorOutput
87+
->method('getFormatter')
88+
->willReturn(new OutputFormatter());
89+
$errorOutput
90+
->expects($this->once())
91+
->method('write');
92+
93+
$output = $this->getMockBuilder(ConsoleOutputInterface::class)->getMock();
94+
$output
95+
->method('getFormatter')
96+
->willReturn(new OutputFormatter());
97+
$output
98+
->expects($this->once())
99+
->method('getErrorOutput')
100+
->willReturn($errorOutput);
101+
102+
$io = new SymfonyStyle($input, $output);
103+
$io->getErrorStyle()->write('');
104+
}
105+
106+
/**
107+
* @expectedException Symfony\Component\Console\Exception\LogicException
108+
* @expectedExceptionMessage The output must be an instance of Symfony\Component\Console\Output\ConsoleOutputInterface for using getErrorOutput().
109+
*/
110+
public function testGetErrorStyleThrowsExceptionOnInvalidOutput()
111+
{
112+
(new SymfonyStyle($this->getMockBuilder(InputInterface::class)->getMock(), new NullOutput()))->getErrorStyle();
113+
}
73114
}

0 commit comments

Comments
 (0)
0