Skip to content

Commit db9ccab

Browse files
author
Robin Chalas
committed
[Console] Fix clearing sections containing questions
1 parent d7d1b29 commit db9ccab

File tree

3 files changed

+42
-6
lines changed

3 files changed

+42
-6
lines changed

Helper/QuestionHelper.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Symfony\Component\Console\Input\InputInterface;
1818
use Symfony\Component\Console\Input\StreamableInputInterface;
1919
use Symfony\Component\Console\Output\ConsoleOutputInterface;
20+
use Symfony\Component\Console\Output\ConsoleSectionOutput;
2021
use Symfony\Component\Console\Output\OutputInterface;
2122
use Symfony\Component\Console\Question\ChoiceQuestion;
2223
use Symfony\Component\Console\Question\Question;
@@ -123,6 +124,10 @@ private function doAsk(OutputInterface $output, Question $question)
123124
$ret = trim($this->autocomplete($output, $question, $inputStream, \is_array($autocomplete) ? $autocomplete : iterator_to_array($autocomplete, false)));
124125
}
125126

127+
if ($output instanceof ConsoleSectionOutput) {
128+
$output->addContent($ret);
129+
}
130+
126131
$ret = \strlen($ret) > 0 ? $ret : $question->getDefault();
127132

128133
if ($normalizer = $question->getNormalizer()) {

Output/ConsoleSectionOutput.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,18 @@ public function getContent(): string
7777
return implode('', $this->content);
7878
}
7979

80+
/**
81+
* @internal
82+
*/
83+
public function addContent(string $input)
84+
{
85+
foreach (explode(PHP_EOL, $input) as $lineContent) {
86+
$this->lines += ceil($this->getDisplayLength($lineContent) / $this->terminal->getWidth()) ?: 1;
87+
$this->content[] = $lineContent;
88+
$this->content[] = PHP_EOL;
89+
}
90+
}
91+
8092
/**
8193
* {@inheritdoc}
8294
*/
@@ -88,11 +100,7 @@ protected function doWrite($message, $newline)
88100

89101
$erasedContent = $this->popStreamContentUntilCurrentSection();
90102

91-
foreach (explode(PHP_EOL, $message) as $lineContent) {
92-
$this->lines += ceil($this->getDisplayLength($lineContent) / $this->terminal->getWidth()) ?: 1;
93-
$this->content[] = $lineContent;
94-
$this->content[] = PHP_EOL;
95-
}
103+
$this->addContent($message);
96104

97105
parent::doWrite($message, true);
98106
parent::doWrite($erasedContent, false);

Tests/Output/ConsoleSectionOutputTest.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,20 @@
1313

1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\Console\Formatter\OutputFormatter;
16+
use Symfony\Component\Console\Helper\QuestionHelper;
17+
use Symfony\Component\Console\Input\StreamableInputInterface;
1618
use Symfony\Component\Console\Output\ConsoleSectionOutput;
1719
use Symfony\Component\Console\Output\OutputInterface;
1820
use Symfony\Component\Console\Output\StreamOutput;
21+
use Symfony\Component\Console\Question\Question;
1922

2023
class ConsoleSectionOutputTest extends TestCase
2124
{
2225
private $stream;
2326

2427
protected function setUp()
2528
{
26-
$this->stream = fopen('php://memory', 'r+', false);
29+
$this->stream = fopen('php://memory', 'r+b', false);
2730
}
2831

2932
protected function tearDown()
@@ -137,4 +140,24 @@ public function testMultipleSectionsOutput()
137140
rewind($output->getStream());
138141
$this->assertEquals('Foo'.PHP_EOL.'Bar'.PHP_EOL."\x1b[2A\x1b[0JBar".PHP_EOL."\x1b[1A\x1b[0JBaz".PHP_EOL.'Bar'.PHP_EOL."\x1b[1A\x1b[0JFoobar".PHP_EOL, stream_get_contents($output->getStream()));
139142
}
143+
144+
public function testClearSectionContainingQuestion()
145+
{
146+
$inputStream = fopen('php://memory', 'r+b', false);
147+
fwrite($inputStream, "Batman & Robin\n");
148+
rewind($inputStream);
149+
150+
$input = $this->getMockBuilder(StreamableInputInterface::class)->getMock();
151+
$input->expects($this->once())->method('isInteractive')->willReturn(true);
152+
$input->expects($this->once())->method('getStream')->willReturn($inputStream);
153+
154+
$sections = array();
155+
$output = new ConsoleSectionOutput($this->stream, $sections, OutputInterface::VERBOSITY_NORMAL, true, new OutputFormatter());
156+
157+
(new QuestionHelper())->ask($input, $output, new Question('What\'s your favorite super hero?'));
158+
$output->clear();
159+
160+
rewind($output->getStream());
161+
$this->assertSame('What\'s your favorite super hero?'.PHP_EOL."\x1b[2A\x1b[0J", stream_get_contents($output->getStream()));
162+
}
140163
}

0 commit comments

Comments
 (0)