3 namespace Symfony\Component\Console\Tests\Helper;
5 use Symfony\Component\Console\Helper\FormatterHelper;
6 use Symfony\Component\Console\Helper\HelperSet;
7 use Symfony\Component\Console\Helper\SymfonyQuestionHelper;
8 use Symfony\Component\Console\Output\StreamOutput;
9 use Symfony\Component\Console\Question\ChoiceQuestion;
10 use Symfony\Component\Console\Question\Question;
15 class SymfonyQuestionHelperTest extends AbstractQuestionHelperTest
17 public function testAskChoice()
19 $questionHelper = new SymfonyQuestionHelper();
21 $helperSet = new HelperSet(array(new FormatterHelper()));
22 $questionHelper->setHelperSet($helperSet);
24 $heroes = array('Superman', 'Batman', 'Spiderman');
26 $inputStream = $this->getInputStream("\n1\n 1 \nFabien\n1\nFabien\n1\n0,2\n 0 , 2 \n\n\n");
28 $question = new ChoiceQuestion('What is your favorite superhero?', $heroes, '2');
29 $question->setMaxAttempts(1);
30 // first answer is an empty answer, we're supposed to receive the default value
31 $this->assertEquals('Spiderman', $questionHelper->ask($this->createStreamableInputInterfaceMock($inputStream), $output = $this->createOutputInterface(), $question));
32 $this->assertOutputContains('What is your favorite superhero? [Spiderman]', $output);
34 $question = new ChoiceQuestion('What is your favorite superhero?', $heroes);
35 $question->setMaxAttempts(1);
36 $this->assertEquals('Batman', $questionHelper->ask($this->createStreamableInputInterfaceMock($inputStream), $this->createOutputInterface(), $question));
37 $this->assertEquals('Batman', $questionHelper->ask($this->createStreamableInputInterfaceMock($inputStream), $this->createOutputInterface(), $question));
39 $question = new ChoiceQuestion('What is your favorite superhero?', $heroes);
40 $question->setErrorMessage('Input "%s" is not a superhero!');
41 $question->setMaxAttempts(2);
42 $this->assertEquals('Batman', $questionHelper->ask($this->createStreamableInputInterfaceMock($inputStream), $output = $this->createOutputInterface(), $question));
43 $this->assertOutputContains('Input "Fabien" is not a superhero!', $output);
46 $question = new ChoiceQuestion('What is your favorite superhero?', $heroes, '1');
47 $question->setMaxAttempts(1);
48 $questionHelper->ask($this->createStreamableInputInterfaceMock($inputStream), $output = $this->createOutputInterface(), $question);
50 } catch (\InvalidArgumentException $e) {
51 $this->assertEquals('Value "Fabien" is invalid', $e->getMessage());
54 $question = new ChoiceQuestion('What is your favorite superhero?', $heroes, null);
55 $question->setMaxAttempts(1);
56 $question->setMultiselect(true);
58 $this->assertEquals(array('Batman'), $questionHelper->ask($this->createStreamableInputInterfaceMock($inputStream), $this->createOutputInterface(), $question));
59 $this->assertEquals(array('Superman', 'Spiderman'), $questionHelper->ask($this->createStreamableInputInterfaceMock($inputStream), $this->createOutputInterface(), $question));
60 $this->assertEquals(array('Superman', 'Spiderman'), $questionHelper->ask($this->createStreamableInputInterfaceMock($inputStream), $this->createOutputInterface(), $question));
62 $question = new ChoiceQuestion('What is your favorite superhero?', $heroes, '0,1');
63 $question->setMaxAttempts(1);
64 $question->setMultiselect(true);
66 $this->assertEquals(array('Superman', 'Batman'), $questionHelper->ask($this->createStreamableInputInterfaceMock($inputStream), $output = $this->createOutputInterface(), $question));
67 $this->assertOutputContains('What is your favorite superhero? [Superman, Batman]', $output);
69 $question = new ChoiceQuestion('What is your favorite superhero?', $heroes, ' 0 , 1 ');
70 $question->setMaxAttempts(1);
71 $question->setMultiselect(true);
73 $this->assertEquals(array('Superman', 'Batman'), $questionHelper->ask($this->createStreamableInputInterfaceMock($inputStream), $output = $this->createOutputInterface(), $question));
74 $this->assertOutputContains('What is your favorite superhero? [Superman, Batman]', $output);
77 public function testAskChoiceWithChoiceValueAsDefault()
79 $questionHelper = new SymfonyQuestionHelper();
80 $helperSet = new HelperSet(array(new FormatterHelper()));
81 $questionHelper->setHelperSet($helperSet);
82 $question = new ChoiceQuestion('What is your favorite superhero?', array('Superman', 'Batman', 'Spiderman'), 'Batman');
83 $question->setMaxAttempts(1);
85 $this->assertSame('Batman', $questionHelper->ask($this->createStreamableInputInterfaceMock($this->getInputStream("Batman\n")), $output = $this->createOutputInterface(), $question));
86 $this->assertOutputContains('What is your favorite superhero? [Batman]', $output);
89 public function testAskReturnsNullIfValidatorAllowsIt()
91 $questionHelper = new SymfonyQuestionHelper();
92 $question = new Question('What is your favorite superhero?');
93 $question->setValidator(function ($value) { return $value; });
94 $input = $this->createStreamableInputInterfaceMock($this->getInputStream("\n"));
95 $this->assertNull($questionHelper->ask($input, $this->createOutputInterface(), $question));
98 public function testAskEscapeDefaultValue()
100 $helper = new SymfonyQuestionHelper();
101 $input = $this->createStreamableInputInterfaceMock($this->getInputStream('\\'));
102 $helper->ask($input, $output = $this->createOutputInterface(), new Question('Can I have a backslash?', '\\'));
104 $this->assertOutputContains('Can I have a backslash? [\]', $output);
107 public function testAskEscapeAndFormatLabel()
109 $helper = new SymfonyQuestionHelper();
110 $input = $this->createStreamableInputInterfaceMock($this->getInputStream('Foo\\Bar'));
111 $helper->ask($input, $output = $this->createOutputInterface(), new Question('Do you want to use Foo\\Bar <comment>or</comment> Foo\\Baz\\?', 'Foo\\Baz'));
113 $this->assertOutputContains('Do you want to use Foo\\Bar or Foo\\Baz\\? [Foo\\Baz]:', $output);
116 public function testLabelTrailingBackslash()
118 $helper = new SymfonyQuestionHelper();
119 $input = $this->createStreamableInputInterfaceMock($this->getInputStream('sure'));
120 $helper->ask($input, $output = $this->createOutputInterface(), new Question('Question with a trailing \\'));
122 $this->assertOutputContains('Question with a trailing \\', $output);
126 * @expectedException \Symfony\Component\Console\Exception\RuntimeException
127 * @expectedExceptionMessage Aborted
129 public function testAskThrowsExceptionOnMissingInput()
131 $dialog = new SymfonyQuestionHelper();
132 $dialog->ask($this->createStreamableInputInterfaceMock($this->getInputStream('')), $this->createOutputInterface(), new Question('What\'s your name?'));
135 protected function getInputStream($input)
137 $stream = fopen('php://memory', 'r+', false);
138 fwrite($stream, $input);
144 protected function createOutputInterface()
146 $output = new StreamOutput(fopen('php://memory', 'r+', false));
147 $output->setDecorated(false);
152 protected function createInputInterfaceMock($interactive = true)
154 $mock = $this->getMockBuilder('Symfony\Component\Console\Input\InputInterface')->getMock();
155 $mock->expects($this->any())
156 ->method('isInteractive')
157 ->will($this->returnValue($interactive));
162 private function assertOutputContains($expected, StreamOutput $output)
164 rewind($output->getStream());
165 $stream = stream_get_contents($output->getStream());
166 $this->assertContains($expected, $stream);