diff --git a/src/Composer/IO/ConsoleIO.php b/src/Composer/IO/ConsoleIO.php index f23e75c84..918845828 100644 --- a/src/Composer/IO/ConsoleIO.php +++ b/src/Composer/IO/ConsoleIO.php @@ -279,4 +279,12 @@ class ConsoleIO extends BaseIO 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); + } } diff --git a/src/Composer/IO/IOInterface.php b/src/Composer/IO/IOInterface.php index ff20a591d..389694dbc 100644 --- a/src/Composer/IO/IOInterface.php +++ b/src/Composer/IO/IOInterface.php @@ -149,6 +149,22 @@ interface IOInterface */ 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. * diff --git a/src/Composer/IO/NullIO.php b/src/Composer/IO/NullIO.php index 587168677..474ebf2ae 100644 --- a/src/Composer/IO/NullIO.php +++ b/src/Composer/IO/NullIO.php @@ -118,4 +118,12 @@ class NullIO extends BaseIO { return null; } + + /** + * {@inheritDoc} + */ + public function select($question, $choices, $default = null, $attempts = false, $errorMessage = 'Value "%s" is invalid', $multiselect = false) + { + return $default; + } } diff --git a/tests/Composer/Test/IO/ConsoleIOTest.php b/tests/Composer/Test/IO/ConsoleIOTest.php index ca7d420c9..4ca424ea9 100644 --- a/tests/Composer/Test/IO/ConsoleIOTest.php +++ b/tests/Composer/Test/IO/ConsoleIOTest.php @@ -222,6 +222,31 @@ class ConsoleIOTest extends TestCase $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() { $inputMock = $this->getMock('Symfony\Component\Console\Input\InputInterface'); diff --git a/tests/Composer/Test/IO/NullIOTest.php b/tests/Composer/Test/IO/NullIOTest.php index feb586f95..6902f08bd 100644 --- a/tests/Composer/Test/IO/NullIOTest.php +++ b/tests/Composer/Test/IO/NullIOTest.php @@ -67,4 +67,11 @@ class NullIOTest extends TestCase $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)); + } }