8000 feature #20586 [Console] Ease writing to stderr using SymfonyStyle (c… · symfony/symfony@e4c95ed · GitHub
[go: up one dir, main page]

Skip to content

Commit e4c95ed

Browse files
committed
feature #20586 [Console] Ease writing to stderr using SymfonyStyle (chalasr)
This PR was merged into the 3.3-dev branch. Discussion ---------- [Console] Ease writing to stderr using SymfonyStyle | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #15986 | License | MIT | Doc PR | n/a Commits ------- 5b0504c [Console] Ease writing to stderr using styles
2 parents 36aedd8 + 5b0504c commit e4c95ed

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
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;
1718

1819
/**
1920
* Decorates output to add console style guide helpers.
@@ -145,4 +146,13 @@ public function isDebug()
145146
{
146147
return $this->output->isDebug();
147148
}
149+
150+
protected function getErrorOutput()
151+
{
152+
if (!$this->output instanceof ConsoleOutputInterface) {
153+
return $this->output;
154+
}
155+
156+
return $this->output->getErrorOutput();
157+
}
148158
}

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,16 @@ public function newLine($count = 1)
337337
$this->bufferedOutput->write(str_repeat("\n", $count));
338338
}
339339

340+
/**
341+
* Returns a new instance which makes use of stderr if available.
342+
*
343+
* @return self
344+
*/
345+
public function getErrorStyle()
346+
{
347+
return new self($this->input, $this->getErrorOutput());
348+
}
349+
< 10000 /td>
340350
/**
341351
* @return ProgressBar
342352
*/

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

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@
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\OutputInterface;
19+
use Symfony\Component\Console\Output\ConsoleOutputInterface;
20+
use Symfony\Component\Console\Input\InputInterface;
21+
use Symfony\Component\Console\Style\SymfonyStyle;
1722

1823
class SymfonyStyleTest extends PHPUnit_Framework_TestCase
1924
{
@@ -70,4 +75,41 @@ public function inputCommandToOutputFilesProvider()
7075

7176
return array_map(null, glob($baseDir.'/command/command_*.php'), glob($baseDir.'/output/output_*.txt'));
7277
}
78+
79+
public function testGetErrorStyle()
80+
{
81+
$input = $this->getMockBuilder(InputInterface::class)->getMock();
82+
83+
$errorOutput = $this->getMockBuilder(OutputInterface::class)->getMock();
84+
$errorOutput
85+
->method('getFormatter')
86+
->willReturn(new OutputFormatter());
87+
$errorOutput
88+
->expects($this->once())
89+
->method('write');
90+
91+
$output = $this->getMockBuilder(ConsoleOutputInterface::class)->getMock();
92+
$output
93+
->method('getFormatter')
94+
->willReturn(new OutputFormatter());
95+
$output
96+
->expects($this->once())
97+
->method('getErrorOutput')
98+
->willReturn($errorOutput);
99+
100+
$io = new SymfonyStyle($input, $output);
101+
$io->getErrorStyle()->write('');
102+
}
103+
104+
public function testGetErrorStyleUsesTheCurrentOutputIfNoErrorOutputIsAvailable()
105+
{
106+
$output = $this->getMockBuilder(OutputInterface::class)->getMock();
107+
$output
108+
->method('getFormatter')
109+
->willReturn(new OutputFormatter());
110+
111+
$style = new SymfonyStyle($this->getMockBuilder(InputInterface::class)->getMock(), $output);
112+
113+
$this->assertInstanceOf(SymfonyStyle::class, $style->getErrorStyle());
114+
}
73115
}

0 commit comments

Comments
 (0)
0