namespace Symfony\Component\Console\Tests\Helper;
use Symfony\Component\Console\Formatter\OutputFormatter;
-use Symfony\Component\Console\Helper\QuestionHelper;
-use Symfony\Component\Console\Helper\HelperSet;
use Symfony\Component\Console\Helper\FormatterHelper;
+use Symfony\Component\Console\Helper\HelperSet;
+use Symfony\Component\Console\Helper\QuestionHelper;
use Symfony\Component\Console\Output\StreamOutput;
use Symfony\Component\Console\Question\ChoiceQuestion;
use Symfony\Component\Console\Question\ConfirmationQuestion;
$this->assertEquals('Superman', $questionHelper->ask($this->createStreamableInputInterfaceMock($inputStream, true), $this->createOutputInterface(), $question));
}
+ public function testAskChoiceNonInteractive()
+ {
+ $questionHelper = new QuestionHelper();
+
+ $helperSet = new HelperSet(array(new FormatterHelper()));
+ $questionHelper->setHelperSet($helperSet);
+ $inputStream = $this->getInputStream("\n1\n 1 \nFabien\n1\nFabien\n1\n0,2\n 0 , 2 \n\n\n");
+
+ $heroes = array('Superman', 'Batman', 'Spiderman');
+
+ $question = new ChoiceQuestion('What is your favorite superhero?', $heroes, '0');
+
+ $this->assertSame('Superman', $questionHelper->ask($this->createStreamableInputInterfaceMock($inputStream, false), $this->createOutputInterface(), $question));
+
+ $question = new ChoiceQuestion('What is your favorite superhero?', $heroes, 'Batman');
+ $this->assertSame('Batman', $questionHelper->ask($this->createStreamableInputInterfaceMock($inputStream, false), $this->createOutputInterface(), $question));
+
+ $question = new ChoiceQuestion('What is your favorite superhero?', $heroes, null);
+ $this->assertNull($questionHelper->ask($this->createStreamableInputInterfaceMock($inputStream, false), $this->createOutputInterface(), $question));
+
+ $question = new ChoiceQuestion('What is your favorite superhero?', $heroes, '0');
+ $question->setValidator(null);
+ $this->assertSame('Superman', $questionHelper->ask($this->createStreamableInputInterfaceMock($inputStream, false), $this->createOutputInterface(), $question));
+
+ try {
+ $question = new ChoiceQuestion('What is your favorite superhero?', $heroes, null);
+ $questionHelper->ask($this->createStreamableInputInterfaceMock($inputStream, false), $this->createOutputInterface(), $question);
+ } catch (\InvalidArgumentException $e) {
+ $this->assertSame('Value "" is invalid', $e->getMessage());
+ }
+
+ $question = new ChoiceQuestion('Who are your favorite superheros?', $heroes, '0, 1');
+ $question->setMultiselect(true);
+ $this->assertSame(array('Superman', 'Batman'), $questionHelper->ask($this->createStreamableInputInterfaceMock($inputStream, false), $this->createOutputInterface(), $question));
+
+ $question = new ChoiceQuestion('Who are your favorite superheros?', $heroes, '0, 1');
+ $question->setMultiselect(true);
+ $question->setValidator(null);
+ $this->assertSame(array('Superman', 'Batman'), $questionHelper->ask($this->createStreamableInputInterfaceMock($inputStream, false), $this->createOutputInterface(), $question));
+
+ $question = new ChoiceQuestion('Who are your favorite superheros?', $heroes, '0, Batman');
+ $question->setMultiselect(true);
+ $this->assertSame(array('Superman', 'Batman'), $questionHelper->ask($this->createStreamableInputInterfaceMock($inputStream, false), $this->createOutputInterface(), $question));
+
+ $question = new ChoiceQuestion('Who are your favorite superheros?', $heroes, null);
+ $question->setMultiselect(true);
+ $this->assertNull($questionHelper->ask($this->createStreamableInputInterfaceMock($inputStream, false), $this->createOutputInterface(), $question));
+
+ try {
+ $question = new ChoiceQuestion('Who are your favorite superheros?', $heroes, '');
+ $question->setMultiselect(true);
+ $questionHelper->ask($this->createStreamableInputInterfaceMock($inputStream, false), $this->createOutputInterface(), $question);
+ } catch (\InvalidArgumentException $e) {
+ $this->assertSame('Value "" is invalid', $e->getMessage());
+ }
+ }
+
public function testAsk()
{
$dialog = new QuestionHelper();
public function testAskHiddenResponse()
{
- if ('\\' === DIRECTORY_SEPARATOR) {
+ if ('\\' === \DIRECTORY_SEPARATOR) {
$this->markTestSkipped('This test is not supported on Windows');
}
$error = 'This is not a color!';
$validator = function ($color) use ($error) {
- if (!in_array($color, array('white', 'black'))) {
+ if (!\in_array($color, array('white', 'black'))) {
throw new \InvalidArgumentException($error);
}
*/
public function testLegacyAskHiddenResponse()
{
- if ('\\' === DIRECTORY_SEPARATOR) {
+ if ('\\' === \DIRECTORY_SEPARATOR) {
$this->markTestSkipped('This test is not supported on Windows');
}
$error = 'This is not a color!';
$validator = function ($color) use ($error) {
- if (!in_array($color, array('white', 'black'))) {
+ if (!\in_array($color, array('white', 'black'))) {
throw new \InvalidArgumentException($error);
}