Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 552c419

Browse filesBrowse files
committed
[Console] Ease writing to stderr using styles
1 parent 6f6100a commit 552c419
Copy full SHA for 552c419

File tree

3 files changed

+66
-0
lines changed
Filter options

3 files changed

+66
-0
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Console/Style/OutputStyle.php
+11Lines 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+
return $this->output;
155+
}
156+
157+
return $this->output->getErrorOutput();
158+
}
148159
}

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Console/Style/SymfonyStyle.php
+11Lines changed: 11 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,16 @@ 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 if available.
343+
*
344+
* @return self
345+
*/
346+
public function getErrorStyle()
347+
{
348+
return new self($this->input, $this->getErrorOutput());
349+
}
350+
340351
/**
341352
* @return ProgressBar
342353
*/

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Console/Tests/Style/SymfonyStyleTest.php
+44Lines changed: 44 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,41 @@ 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+
public function testGetErrorStyleUsesTheCurrentOutputIfNoErrorOutputIsAvailable()
107+
{
108+
$output = $this->getMockBuilder(OutputInterface::class)->getMock();
109+
$output
110+
->method('getFormatter')
111+
->willReturn(new OutputFormatter());
112+
113+
$style = new SymfonyStyle($this->getMockBuilder(InputInterface::class)->getMock(), $output);
114+
115+
$this->assertInstanceOf(SymfonyStyle::class, $style->getErrorStyle());
116+
}
73117
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.