12 namespace Symfony\Component\Console\Tests\Helper;
32 $questionHelper->setHelperSet($helperSet);
34 $heroes = array(
'Superman',
'Batman',
'Spiderman');
36 $questionHelper->setInputStream($this->
getInputStream(
"\n1\n 1 \nFabien\n1\nFabien\n1\n0,2\n 0 , 2 \n\n\n"));
38 $question =
new ChoiceQuestion(
'What is your favorite superhero?', $heroes,
'2');
39 $question->setMaxAttempts(1);
41 $this->assertEquals(
'Spiderman', $questionHelper->ask($this->createInputInterfaceMock(), $this->
createOutputInterface(), $question));
43 $question =
new ChoiceQuestion(
'What is your favorite superhero?', $heroes);
44 $question->setMaxAttempts(1);
45 $this->assertEquals(
'Batman', $questionHelper->ask($this->createInputInterfaceMock(), $this->
createOutputInterface(), $question));
46 $this->assertEquals(
'Batman', $questionHelper->ask($this->createInputInterfaceMock(), $this->
createOutputInterface(), $question));
48 $question =
new ChoiceQuestion(
'What is your favorite superhero?', $heroes);
49 $question->setErrorMessage(
'Input "%s" is not a superhero!');
50 $question->setMaxAttempts(2);
51 $this->assertEquals(
'Batman', $questionHelper->ask($this->createInputInterfaceMock(), $output = $this->
createOutputInterface(), $question));
53 rewind($output->getStream());
54 $stream = stream_get_contents($output->getStream());
55 $this->assertContains(
'Input "Fabien" is not a superhero!', $stream);
58 $question =
new ChoiceQuestion(
'What is your favorite superhero?', $heroes,
'1');
59 $question->setMaxAttempts(1);
62 }
catch (\InvalidArgumentException $e) {
63 $this->assertEquals(
'Value "Fabien" is invalid', $e->getMessage());
66 $question =
new ChoiceQuestion(
'What is your favorite superhero?', $heroes, null);
67 $question->setMaxAttempts(1);
68 $question->setMultiselect(
true);
70 $this->assertEquals(array(
'Batman'), $questionHelper->ask($this->createInputInterfaceMock(), $this->
createOutputInterface(), $question));
71 $this->assertEquals(array(
'Superman',
'Spiderman'), $questionHelper->ask($this->createInputInterfaceMock(), $this->
createOutputInterface(), $question));
72 $this->assertEquals(array(
'Superman',
'Spiderman'), $questionHelper->ask($this->createInputInterfaceMock(), $this->
createOutputInterface(), $question));
74 $question =
new ChoiceQuestion(
'What is your favorite superhero?', $heroes,
'0,1');
75 $question->setMaxAttempts(1);
76 $question->setMultiselect(
true);
78 $this->assertEquals(array(
'Superman',
'Batman'), $questionHelper->ask($this->createInputInterfaceMock(), $this->
createOutputInterface(), $question));
80 $question =
new ChoiceQuestion(
'What is your favorite superhero?', $heroes,
' 0 , 1 ');
81 $question->setMaxAttempts(1);
82 $question->setMultiselect(
true);
84 $this->assertEquals(array(
'Superman',
'Batman'), $questionHelper->ask($this->createInputInterfaceMock(), $this->
createOutputInterface(), $question));
93 $question =
new Question(
'What time is it?',
'2PM');
94 $this->assertEquals(
'2PM', $dialog->ask($this->createInputInterfaceMock(), $this->
createOutputInterface(), $question));
96 $question =
new Question(
'What time is it?',
'2PM');
97 $this->assertEquals(
'8AM', $dialog->ask($this->createInputInterfaceMock(), $output = $this->
createOutputInterface(), $question));
99 rewind($output->getStream());
100 $this->assertEquals(
'What time is it?', stream_get_contents($output->getStream()));
106 $this->markTestSkipped(
'`stty` is required to test autocomplete functionality');
117 $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");
120 $dialog->setInputStream($inputStream);
122 $dialog->setHelperSet($helperSet);
124 $question =
new Question(
'Please select a bundle',
'FrameworkBundle');
125 $question->setAutocompleterValues(array(
'AcmeDemoBundle',
'AsseticBundle',
'SecurityBundle',
'FooBundle'));
127 $this->assertEquals(
'AcmeDemoBundle', $dialog->ask($this->createInputInterfaceMock(), $this->
createOutputInterface(), $question));
128 $this->assertEquals(
'AsseticBundleTest', $dialog->ask($this->createInputInterfaceMock(), $this->
createOutputInterface(), $question));
129 $this->assertEquals(
'FrameworkBundle', $dialog->ask($this->createInputInterfaceMock(), $this->
createOutputInterface(), $question));
130 $this->assertEquals(
'SecurityBundle', $dialog->ask($this->createInputInterfaceMock(), $this->
createOutputInterface(), $question));
131 $this->assertEquals(
'FooBundleTest', $dialog->ask($this->createInputInterfaceMock(), $this->
createOutputInterface(), $question));
132 $this->assertEquals(
'AcmeDemoBundle', $dialog->ask($this->createInputInterfaceMock(), $this->
createOutputInterface(), $question));
133 $this->assertEquals(
'AsseticBundle', $dialog->ask($this->createInputInterfaceMock(), $this->
createOutputInterface(), $question));
134 $this->assertEquals(
'FooBundle', $dialog->ask($this->createInputInterfaceMock(), $this->
createOutputInterface(), $question));
139 if (
'\\' === DIRECTORY_SEPARATOR) {
140 $this->markTestSkipped(
'This test is not supported on Windows');
146 $question =
new Question(
'What time is it?');
147 $question->setHidden(
true);
149 $this->assertEquals(
'8AM', $dialog->ask($this->createInputInterfaceMock(), $this->
createOutputInterface(), $question));
161 $this->assertEquals($expected, $dialog->ask($this->createInputInterfaceMock(), $this->
createOutputInterface(), $question),
'confirmation question should '.($expected ?
'pass' :
'cancel'));
168 array(
'',
false,
false),
182 $this->assertTrue($dialog->ask($this->createInputInterfaceMock(), $this->
createOutputInterface(), $question));
184 $this->assertTrue($dialog->ask($this->createInputInterfaceMock(), $this->
createOutputInterface(), $question));
191 $dialog->setHelperSet($helperSet);
193 $error =
'This is not a color!';
194 $validator =
function ($color) use ($error) {
195 if (!in_array($color, array(
'white',
'black'))) {
196 throw new \InvalidArgumentException($error);
202 $question =
new Question(
'What color was the white horse of Henry IV?',
'white');
203 $question->setValidator($validator);
204 $question->setMaxAttempts(2);
207 $this->assertEquals(
'white', $dialog->ask($this->createInputInterfaceMock(), $this->
createOutputInterface(), $question));
208 $this->assertEquals(
'black', $dialog->ask($this->createInputInterfaceMock(), $this->
createOutputInterface(), $question));
210 $dialog->setInputStream($this->
getInputStream(
"green\nyellow\norange\n"));
214 }
catch (\InvalidArgumentException $e) {
215 $this->assertEquals($error, $e->getMessage());
224 $possibleChoices = array(
231 $dialog->setInputStream($this->
getInputStream($providedAnswer.
"\n"));
233 $dialog->setHelperSet($helperSet);
235 $question =
new ChoiceQuestion(
'Please select the environment to load', $possibleChoices);
236 $question->setMaxAttempts(1);
239 $this->assertSame($expectedValue, $answer);
245 array(0,
'My environment 1'),
246 array(1,
'My environment 2'),
247 array(2,
'My environment 3'),
248 array(
'My environment 1',
'My environment 1'),
249 array(
'My environment 2',
'My environment 2'),
250 array(
'My environment 3',
'My environment 3'),
259 $possibleChoices = array(
260 '0' =>
'No environment',
261 '1' =>
'My environment 1',
262 'env_2' =>
'My environment 2',
263 3 =>
'My environment 3',
267 $dialog->setInputStream($this->
getInputStream($providedAnswer.
"\n"));
269 $dialog->setHelperSet($helperSet);
271 $question =
new ChoiceQuestion(
'Please select the environment to load', $possibleChoices);
272 $question->setMaxAttempts(1);
275 $this->assertSame($expectedValue, $answer);
282 array(
'No environment',
'0'),
284 array(
'env_2',
'env_2'),
286 array(
'My environment 1',
'1'),
295 $possibleChoices = array(
296 'env_1' =>
'My environment 1',
297 'env_2' =>
'My environment',
298 'env_3' =>
'My environment',
302 $dialog->setInputStream($this->
getInputStream($providedAnswer.
"\n"));
304 $dialog->setHelperSet($helperSet);
306 $question =
new ChoiceQuestion(
'Please select the environment to load', $possibleChoices);
307 $question->setMaxAttempts(1);
310 $this->assertSame($expectedValue, $answer);
319 $possibleChoices = array(
320 'env_1' =>
'My first environment',
321 'env_2' =>
'My environment',
322 'env_3' =>
'My environment',
326 $dialog->setInputStream($this->
getInputStream(
"My environment\n"));
328 $dialog->setHelperSet($helperSet);
330 $question =
new ChoiceQuestion(
'Please select the environment to load', $possibleChoices);
331 $question->setMaxAttempts(1);
339 array(
'env_1',
'env_1'),
340 array(
'env_2',
'env_2'),
341 array(
'env_3',
'env_3'),
342 array(
'My environment 1',
'env_1'),
349 $question =
new Question(
'Do you have a job?',
'not yet');
350 $this->assertEquals(
'not yet', $dialog->ask($this->createInputInterfaceMock(
false), $this->
createOutputInterface(), $question));
355 $stream = fopen(
'php://memory',
'r+',
false);
356 fwrite($stream, $input);
364 return new StreamOutput(fopen(
'php://memory',
'r+',
false));
369 $mock = $this->getMock(
'Symfony\Component\Console\Input\InputInterface');
370 $mock->expects($this->any())
371 ->method(
'isInteractive')
372 ->will($this->returnValue($interactive));
379 exec(
'stty 2>&1', $output, $exitcode);
381 return $exitcode === 0;