Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/Symfony/Component/Console/Helper/QuestionHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public function doAsk(OutputInterface $output, Question $question)
$ret = trim($ret);
}
} else {
$ret = trim($this->autocomplete($output, $question, $inputStream));
$ret = trim($this->autocomplete($output, $question, $inputStream, is_array($autocomplete) ? $autocomplete : iterator_to_array($autocomplete, false)));
Copy link
Contributor Author

@ro0NL ro0NL Nov 11, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

passing false to iterator_to_array fixes

1) Symfony\Component\Console\Tests\Helper\QuestionHelperTest::testTraversableAutocomplete
Undefined offset: 3

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

in case of assoc array keys (which we dont really care about)

of course using iterator_to_array fixes the offset access in the first place ;)

}

$ret = strlen($ret) > 0 ? $ret : $question->getDefault();
Expand Down Expand Up @@ -190,12 +190,12 @@ protected function writeError(OutputInterface $output, \Exception $error)
* @param OutputInterface $output
* @param Question $question
* @param resource $inputStream
* @param array $autocomplete
*
* @return string
*/
private function autocomplete(OutputInterface $output, Question $question, $inputStream)
private function autocomplete(OutputInterface $output, Question $question, $inputStream, array $autocomplete)
{
$autocomplete = $question->getAutocompleterValues();
$ret = '';

$i = 0;
Expand Down
6 changes: 2 additions & 4 deletions src/Symfony/Component/Console/Question/Question.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,8 @@ public function setAutocompleterValues($values)
$values = $this->isAssoc($values) ? array_merge(array_keys($values), array_values($values)) : array_values($values);
}

if (null !== $values && !is_array($values)) {
if (!$values instanceof \Traversable || !$values instanceof \Countable) {
throw new \InvalidArgumentException('Autocompleter values can be either an array, `null` or an object implementing both `Countable` and `Traversable` interfaces.');
}
if (null !== $values && !is_array($values) && !$values instanceof \Traversable) {
throw new \InvalidArgumentException('Autocompleter values can be either an array, `null` or a `Traversable` object.');
}

if ($this->hidden) {
Expand Down
49 changes: 49 additions & 0 deletions src/Symfony/Component/Console/Tests/Helper/QuestionHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,40 @@ public function testEmptyChoices()
new ChoiceQuestion('Question', array(), 'irrelevant');
}

public function testTraversableAutocomplete()
{
if (!$this->hasSttyAvailable()) {
$this->markTestSkipped('`stty` is required to test autocomplete functionality');
}

// Acm<NEWLINE>
// Ac<BACKSPACE><BACKSPACE>s<TAB>Test<NEWLINE>
// <NEWLINE>
// <UP ARROW><UP ARROW><NEWLINE>
// <UP ARROW><UP ARROW><UP ARROW><UP ARROW><UP ARROW><TAB>Test<NEWLINE>
// <DOWN ARROW><NEWLINE>
// S<BACKSPACE><BACKSPACE><DOWN ARROW><DOWN ARROW><NEWLINE>
// F00<BACKSPACE><BACKSPACE>oo<TAB><NEWLINE>
$inputStream = $this->getInputStream("Acm\nAc\177\177s\tTest\n\n\033[A\033[A\n\033[A\033[A\033[A\033[A\033[A\tTest\n\033[B\nS\177\177\033[B\033[B\nF00\177\177oo\t\n");

$dialog = new QuestionHelper();
$dialog->setInputStream($inputStream);
$helperSet = new HelperSet(array(new FormatterHelper()));
$dialog->setHelperSet($helperSet);

$question = new Question('Please select a bundle', 'FrameworkBundle');
$question->setAutocompleterValues(new AutocompleteValues(array('irrelevant' => 'AcmeDemoBundle', 'AsseticBundle', 'SecurityBundle', 'FooBundle')));

$this->assertEquals('AcmeDemoBundle', $dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
$this->assertEquals('AsseticBundleTest', $dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
$this->assertEquals('FrameworkBundle', $dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
$this->assertEquals('SecurityBundle', $dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
$this->assertEquals('FooBundleTest', $dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
$this->assertEquals('AcmeDemoBundle', $dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
$this->assertEquals('AsseticBundle', $dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
$this->assertEquals('FooBundle', $dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
}

protected function getInputStream($input)
{
$stream = fopen('php://memory', 'r+', false);
Expand Down Expand Up @@ -545,3 +579,18 @@ private function hasSttyAvailable()
return 0 === $exitcode;
}
}

class AutocompleteValues implements \IteratorAggregate
{
private $values;

public function __construct(array $values)
{
$this->values = $values;
}

public function getIterator()
{
return new \ArrayIterator($this->values);
}
}