1
0
Fork 0

Merge remote-tracking branch 'fduch/console-select-support'

pull/4207/merge
Jordi Boggiano 2016-02-29 15:24:28 +00:00
commit d24ea66892
5 changed files with 64 additions and 0 deletions

View File

@ -279,4 +279,12 @@ class ConsoleIO extends BaseIO
return \Seld\CliPrompt\CliPrompt::hiddenPrompt(true); return \Seld\CliPrompt\CliPrompt::hiddenPrompt(true);
} }
/**
* {@inheritDoc}
*/
public function select($question, $choices, $default = null, $attempts = false, $errorMessage = 'Value "%s" is invalid', $multiselect = false)
{
return $this->helperSet->get('dialog')->select($this->output, $question, $choices, $default, $attempts, $errorMessage, $multiselect);
}
} }

View File

@ -149,6 +149,22 @@ interface IOInterface
*/ */
public function askAndHideAnswer($question); public function askAndHideAnswer($question);
/**
* Asks the user to select a value.
*
* @param string|array $question The question to ask
* @param array $choices List of choices to pick from
* @param bool|string $default The default answer if the user enters nothing
* @param bool|int $attempts Max number of times to ask before giving up (false by default, which means infinite)
* @param string $errorMessage Message which will be shown if invalid value from choice list would be picked
* @param bool $multiselect Select more than one value separated by comma
*
* @return int|string|array The selected value or values (the key of the choices array)
*
* @throws \InvalidArgumentException
*/
public function select($question, $choices, $default = null, $attempts = false, $errorMessage = 'Value "%s" is invalid', $multiselect = false);
/** /**
* Get all authentication information entered. * Get all authentication information entered.
* *

View File

@ -118,4 +118,12 @@ class NullIO extends BaseIO
{ {
return null; return null;
} }
/**
* {@inheritDoc}
*/
public function select($question, $choices, $default = null, $attempts = false, $errorMessage = 'Value "%s" is invalid', $multiselect = false)
{
return $default;
}
} }

View File

@ -222,6 +222,31 @@ class ConsoleIOTest extends TestCase
$consoleIO->askAndValidate('Why?', 'validator', 10, 'default'); $consoleIO->askAndValidate('Why?', 'validator', 10, 'default');
} }
public function testSelect()
{
$inputMock = $this->getMock('Symfony\Component\Console\Input\InputInterface');
$outputMock = $this->getMock('Symfony\Component\Console\Output\OutputInterface');
$dialogMock = $this->getMock('Symfony\Component\Console\Helper\DialogHelper');
$helperMock = $this->getMock('Symfony\Component\Console\Helper\HelperSet');
$dialogMock->expects($this->once())
->method('select')
->with($this->isInstanceOf('Symfony\Component\Console\Output\OutputInterface'),
$this->equalTo('Select item'),
$this->equalTo(array("item1", "item2")),
$this->equalTo(null),
$this->equalTo(false),
$this->equalTo("Error message"),
$this->equalTo(true));
$helperMock->expects($this->once())
->method('get')
->with($this->equalTo('dialog'))
->will($this->returnValue($dialogMock));
$consoleIO = new ConsoleIO($inputMock, $outputMock, $helperMock);
$consoleIO->select('Select item', array("item1", "item2"), null, false, "Error message", true);
}
public function testSetAndgetAuthentication() public function testSetAndgetAuthentication()
{ {
$inputMock = $this->getMock('Symfony\Component\Console\Input\InputInterface'); $inputMock = $this->getMock('Symfony\Component\Console\Input\InputInterface');

View File

@ -67,4 +67,11 @@ class NullIOTest extends TestCase
$this->assertEquals('foo', $io->askAndValidate('question', 'validator', false, 'foo')); $this->assertEquals('foo', $io->askAndValidate('question', 'validator', false, 'foo'));
} }
public function testSelect()
{
$io = new NullIO();
$this->assertEquals('1', $io->select('question', array('item1', 'item2'), '1', 2, 'foo', true));
}
} }