bug #26875 [Console] Don't go past exact matches when autocompleting (nicolas-grekas)

This PR was merged into the 2.7 branch.

Discussion
----------

[Console] Don't go past exact matches when autocompleting

| Q             | A
| ------------- | ---
| Branch?       | 2.7
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #21789
| License       | MIT
| Doc PR        | -

Commits
-------

adba79a [Console] Don't go past exact matches when autocompleting
This commit is contained in:
Robin Chalas 2018-04-10 11:58:27 +02:00
commit 10674688da
2 changed files with 25 additions and 1 deletions

View File

@ -284,7 +284,7 @@ class QuestionHelper extends Helper
foreach ($autocomplete as $value) {
// If typed characters match the beginning chunk of value (e.g. [AcmeDe]moBundle)
if (0 === strpos($value, $ret) && $i !== strlen($value)) {
if (0 === strpos($value, $ret)) {
$matches[$numMatches++] = $value;
}
}

View File

@ -160,6 +160,30 @@ class QuestionHelperTest extends TestCase
$this->assertEquals('AsseticBundle', $dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
}
public function testAskWithAutocompleteWithExactMatch()
{
if (!$this->hasSttyAvailable()) {
$this->markTestSkipped('`stty` is required to test autocomplete functionality');
}
$inputStream = $this->getInputStream("b\n");
$possibleChoices = array(
'a' => 'berlin',
'b' => 'copenhagen',
'c' => 'amsterdam',
);
$dialog = new QuestionHelper();
$dialog->setInputStream($inputStream);
$dialog->setHelperSet(new HelperSet(array(new FormatterHelper())));
$question = new ChoiceQuestion('Please select a city', $possibleChoices);
$question->setMaxAttempts(1);
$this->assertSame('b', $dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
}
public function testAutocompleteWithTrailingBackslash()
{
if (!$this->hasSttyAvailable()) {