8000 [Console] Add another test and some tweaks · symfony/symfony@df84a50 · GitHub
[go: up one dir, main page]

Skip to content

Commit df84a50

Browse files
committed
[Console] Add another test and some tweaks
1 parent 9fbfe1c commit df84a50

File tree

2 files changed

+31
-32
lines changed

2 files changed

+31
-32
lines changed

src/Symfony/Component/Console/Helper/QuestionHelper.php

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -303,10 +303,10 @@ private function autocomplete(OutputInterface $output, Question $question, $inpu
303303
if ($numMatches > 0 && -1 !== $ofs) {
304304
$ret = $matches[$ofs];
305305
// Echo out remaining chars for current match
306-
$remainingCharacters = substr($ret, strlen(trim($this->mostRecentlyEnteredValue($fullChoice))));
306+
$remainingCharacters = substr($ret, \strlen(trim($this->mostRecentlyEnteredValue($fullChoice))));
307307
$output->write($remainingCharacters);
308308
$fullChoice .= $remainingCharacters;
309-
$i = strlen($fullChoice);
309+
$i = \strlen($fullChoice);
310310
}
311311

312312
if ("\n" === $c) {
@@ -330,9 +330,8 @@ private function autocomplete(OutputInterface $output, Question $question, $inpu
330330

331331
$tempRet = $ret;
332332

333-
if ($question instanceof ChoiceQuestion && $question->isMultiselect())
334-
{
335-
$tempRet = $this->mostRecentlyEnteredValue($ret);
333+
if ($question instanceof ChoiceQuestion && $question->isMultiselect()) {
334+
$tempRet = $this->mostRecentlyEnteredValue($fullChoice);
336335
}
337336

338337
$numMatches = 0;
@@ -353,7 +352,7 @@ private function autocomplete(OutputInterface $output, Question $question, $inpu
353352
// Save cursor position
354353
$output->write("\0337");
355354
// Write highlighted text, complete the partially entered response
356-
$charactersEntered = strlen(trim($this->mostRecentlyEnteredValue($ret)));
355+
$charactersEntered = \strlen(trim($this->mostRecentlyEnteredValue($fullChoice)));
357356
$output->write('<hl>'.OutputFormatter::escapeTrailingBackslash(substr($matches[$ofs], $charactersEntered)).'</hl>');
358357
// Restore cursor position
359358
$output->write("\0338");
@@ -366,27 +365,28 @@ private function autocomplete(OutputInterface $output, Question $question, $inpu
366365
return $fullChoice;
367366
}
368367

369-
/**
370-
* Determine the most recent value the user entered
371-
* @param $ret
372-
* @return string
373-
*/
374-
private function mostRecentlyEnteredValue($ret)
375-
{
376-
$tempRet = $ret;
377-
378-
if (strpos($ret, ',') !== false)
379-
{
380-
$choices = explode(',', $ret);
381-
$lastChoice = trim($choices[count($choices) - 1]);
382-
if (strlen($lastChoice) > 0)
383-
{
384-
$tempRet = $lastChoice;
385-
}
386-
}
387-
388-
return $tempRet;
389-
}
368+
/**
369+
* Determine the most recent value the user entered.
370+
*
371+
* @param $entered
372+
*
373+
* @return string
374+
*/
375+
private function mostRecentlyEnteredValue($entered)
376+
{
377+
$tempEntered = $entered;
378+
379+
if (false !== strpos($entered, ',')) {
380+
$choices = explode(',', $entered);
381+
$lastChoice = trim($choices[\count($choices) - 1]);
382+
383+
if (\strlen($lastChoice) > 0) {
384+
$tempEntered = $lastChoice;
385+
}
386+
}
387+
388+
return $tempEntered;
389+
}
390390

391391
/**
392392
* Gets a hidden response from user.

src/Symfony/Component/Console/Tests/Helper/QuestionHelperTest.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1020,15 +1020,13 @@ public function testTraversableAutocomplete()
10201020

10211021
public function testTraversableMultiselectAutocomplete()
10221022
{
1023-
// Test cases:
1024-
// 1) default; 2) Tab single; 3) Traverse and tab multiple; 4) Backspace and traverse multiple; 5) Backspace all
1025-
10261023
// <NEWLINE>
10271024
// F<TAB><NEWLINE>
10281025
// A<3x UP ARROW><TAB>,F<TAB><NEWLINE>
10291026
// F00<BACKSPACE><BACKSPACE>o<TAB>,A<DOWN ARROW>,<SPACE>SecurityBundle<NEWLINE>
1030-
// Acme<TAB> ,<SPACE>As<TAB><29x BACKSPACE>S<TAB><NEWLINE>
1031-
$inputStream = $this->getInputStream("\nF\t\nA\033[A\033[A\033[A\t,F\t\nF00\177\177o\t,A\033[B\t, SecurityBundle\nAcme\t, As\t\177\177\177\177\177\177\177\177\177\177\177\177\177\177\177\177\177\177\177\177\177\177\177\177\177\177\177\177\177S\t\n");
1027+
// Acme<TAB>,<SPACE>As<TAB><29x BACKSPACE>S<TAB><NEWLINE>
1028+
// Ac<TAB>,As<TAB><3x BACKSPACE>d<TAB><NEWLINE>
1029+
$inputStream = $this->getInputStream("\nF\t\nA\033[A\033[A\033[A\t,F\t\nF00\177\177o\t,A\033[B\t, SecurityBundle\nAcme\t, As\t\177\177\177\177\177\177\177\177\177\177\177\177\177\177\177\177\177\177\177\177\177\177\177\177\177\177\177\177\177S\t\nAc\t,As\t\177\177\177d\t\n");
10321030

10331031
$dialog = new QuestionHelper();
10341032
$helperSet = new HelperSet([new FormatterHelper()]);
@@ -1048,6 +1046,7 @@ public function testTraversableMultiselectAutocomplete()
10481046
$this->assertEquals(['AsseticBundle', 'FooBundle'], $dialog->ask($this->createStreamableInputInterfaceMock($inputStream), $this->createOutputInterface(), $question));
10491047
$this->assertEquals(['FooBundle', 'AsseticBundle', 'SecurityBundle'], $dialog->ask($this->createStreamableInputInterfaceMock($inputStream), $this->createOutputInterface(), $question));
10501048
$this->assertEquals(['SecurityBundle'], $dialog->ask($this->createStreamableInputInterfaceMock($inputStream), $this->createOutputInterface(), $question));
1049+
$this->assertEquals(['AcmeDemoBundle', 'AsseticBundle'], $dialog->ask($this->createStreamableInputInterfaceMock($inputStream), $this->createOutputInterface(), $question));
10511050
}
10521051

10531052
protected function getInputStream($input)

0 commit comments

Comments
 (0)
0