TYPO3  7.6
QuestionHelperTest.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11 
12 namespace Symfony\Component\Console\Tests\Helper;
13 
21 
25 class QuestionHelperTest extends \PHPUnit_Framework_TestCase
26 {
27  public function testAskChoice()
28  {
29  $questionHelper = new QuestionHelper();
30 
31  $helperSet = new HelperSet(array(new FormatterHelper()));
32  $questionHelper->setHelperSet($helperSet);
33 
34  $heroes = array('Superman', 'Batman', 'Spiderman');
35 
36  $questionHelper->setInputStream($this->getInputStream("\n1\n 1 \nFabien\n1\nFabien\n1\n0,2\n 0 , 2 \n\n\n"));
37 
38  $question = new ChoiceQuestion('What is your favorite superhero?', $heroes, '2');
39  $question->setMaxAttempts(1);
40  // first answer is an empty answer, we're supposed to receive the default value
41  $this->assertEquals('Spiderman', $questionHelper->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
42 
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));
47 
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));
52 
53  rewind($output->getStream());
54  $stream = stream_get_contents($output->getStream());
55  $this->assertContains('Input "Fabien" is not a superhero!', $stream);
56 
57  try {
58  $question = new ChoiceQuestion('What is your favorite superhero?', $heroes, '1');
59  $question->setMaxAttempts(1);
60  $questionHelper->ask($this->createInputInterfaceMock(), $output = $this->createOutputInterface(), $question);
61  $this->fail();
62  } catch (\InvalidArgumentException $e) {
63  $this->assertEquals('Value "Fabien" is invalid', $e->getMessage());
64  }
65 
66  $question = new ChoiceQuestion('What is your favorite superhero?', $heroes, null);
67  $question->setMaxAttempts(1);
68  $question->setMultiselect(true);
69 
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));
73 
74  $question = new ChoiceQuestion('What is your favorite superhero?', $heroes, '0,1');
75  $question->setMaxAttempts(1);
76  $question->setMultiselect(true);
77 
78  $this->assertEquals(array('Superman', 'Batman'), $questionHelper->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
79 
80  $question = new ChoiceQuestion('What is your favorite superhero?', $heroes, ' 0 , 1 ');
81  $question->setMaxAttempts(1);
82  $question->setMultiselect(true);
83 
84  $this->assertEquals(array('Superman', 'Batman'), $questionHelper->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
85  }
86 
87  public function testAsk()
88  {
89  $dialog = new QuestionHelper();
90 
91  $dialog->setInputStream($this->getInputStream("\n8AM\n"));
92 
93  $question = new Question('What time is it?', '2PM');
94  $this->assertEquals('2PM', $dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
95 
96  $question = new Question('What time is it?', '2PM');
97  $this->assertEquals('8AM', $dialog->ask($this->createInputInterfaceMock(), $output = $this->createOutputInterface(), $question));
98 
99  rewind($output->getStream());
100  $this->assertEquals('What time is it?', stream_get_contents($output->getStream()));
101  }
102 
103  public function testAskWithAutocomplete()
104  {
105  if (!$this->hasSttyAvailable()) {
106  $this->markTestSkipped('`stty` is required to test autocomplete functionality');
107  }
108 
109  // Acm<NEWLINE>
110  // Ac<BACKSPACE><BACKSPACE>s<TAB>Test<NEWLINE>
111  // <NEWLINE>
112  // <UP ARROW><UP ARROW><NEWLINE>
113  // <UP ARROW><UP ARROW><UP ARROW><UP ARROW><UP ARROW><TAB>Test<NEWLINE>
114  // <DOWN ARROW><NEWLINE>
115  // S<BACKSPACE><BACKSPACE><DOWN ARROW><DOWN ARROW><NEWLINE>
116  // F00<BACKSPACE><BACKSPACE>oo<TAB><NEWLINE>
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");
118 
119  $dialog = new QuestionHelper();
120  $dialog->setInputStream($inputStream);
121  $helperSet = new HelperSet(array(new FormatterHelper()));
122  $dialog->setHelperSet($helperSet);
123 
124  $question = new Question('Please select a bundle', 'FrameworkBundle');
125  $question->setAutocompleterValues(array('AcmeDemoBundle', 'AsseticBundle', 'SecurityBundle', 'FooBundle'));
126 
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));
135  }
136 
137  public function testAskHiddenResponse()
138  {
139  if ('\\' === DIRECTORY_SEPARATOR) {
140  $this->markTestSkipped('This test is not supported on Windows');
141  }
142 
143  $dialog = new QuestionHelper();
144  $dialog->setInputStream($this->getInputStream("8AM\n"));
145 
146  $question = new Question('What time is it?');
147  $question->setHidden(true);
148 
149  $this->assertEquals('8AM', $dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
150  }
151 
155  public function testAskConfirmation($question, $expected, $default = true)
156  {
157  $dialog = new QuestionHelper();
158 
159  $dialog->setInputStream($this->getInputStream($question."\n"));
160  $question = new ConfirmationQuestion('Do you like French fries?', $default);
161  $this->assertEquals($expected, $dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question), 'confirmation question should '.($expected ? 'pass' : 'cancel'));
162  }
163 
164  public function getAskConfirmationData()
165  {
166  return array(
167  array('', true),
168  array('', false, false),
169  array('y', true),
170  array('yes', true),
171  array('n', false),
172  array('no', false),
173  );
174  }
175 
177  {
178  $dialog = new QuestionHelper();
179 
180  $dialog->setInputStream($this->getInputStream("j\ny\n"));
181  $question = new ConfirmationQuestion('Do you like French fries?', false, '/^(j|y)/i');
182  $this->assertTrue($dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
183  $question = new ConfirmationQuestion('Do you like French fries?', false, '/^(j|y)/i');
184  $this->assertTrue($dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
185  }
186 
187  public function testAskAndValidate()
188  {
189  $dialog = new QuestionHelper();
190  $helperSet = new HelperSet(array(new FormatterHelper()));
191  $dialog->setHelperSet($helperSet);
192 
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);
197  }
198 
199  return $color;
200  };
201 
202  $question = new Question('What color was the white horse of Henry IV?', 'white');
203  $question->setValidator($validator);
204  $question->setMaxAttempts(2);
205 
206  $dialog->setInputStream($this->getInputStream("\nblack\n"));
207  $this->assertEquals('white', $dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
208  $this->assertEquals('black', $dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
209 
210  $dialog->setInputStream($this->getInputStream("green\nyellow\norange\n"));
211  try {
212  $dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question);
213  $this->fail();
214  } catch (\InvalidArgumentException $e) {
215  $this->assertEquals($error, $e->getMessage());
216  }
217  }
218 
222  public function testSelectChoiceFromSimpleChoices($providedAnswer, $expectedValue)
223  {
224  $possibleChoices = array(
225  'My environment 1',
226  'My environment 2',
227  'My environment 3',
228  );
229 
230  $dialog = new QuestionHelper();
231  $dialog->setInputStream($this->getInputStream($providedAnswer."\n"));
232  $helperSet = new HelperSet(array(new FormatterHelper()));
233  $dialog->setHelperSet($helperSet);
234 
235  $question = new ChoiceQuestion('Please select the environment to load', $possibleChoices);
236  $question->setMaxAttempts(1);
237  $answer = $dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question);
238 
239  $this->assertSame($expectedValue, $answer);
240  }
241 
242  public function simpleAnswerProvider()
243  {
244  return array(
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'),
251  );
252  }
253 
257  public function testChoiceFromChoicelistWithMixedKeys($providedAnswer, $expectedValue)
258  {
259  $possibleChoices = array(
260  '0' => 'No environment',
261  '1' => 'My environment 1',
262  'env_2' => 'My environment 2',
263  3 => 'My environment 3',
264  );
265 
266  $dialog = new QuestionHelper();
267  $dialog->setInputStream($this->getInputStream($providedAnswer."\n"));
268  $helperSet = new HelperSet(array(new FormatterHelper()));
269  $dialog->setHelperSet($helperSet);
270 
271  $question = new ChoiceQuestion('Please select the environment to load', $possibleChoices);
272  $question->setMaxAttempts(1);
273  $answer = $dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question);
274 
275  $this->assertSame($expectedValue, $answer);
276  }
277 
279  {
280  return array(
281  array('0', '0'),
282  array('No environment', '0'),
283  array('1', '1'),
284  array('env_2', 'env_2'),
285  array(3, '3'),
286  array('My environment 1', '1'),
287  );
288  }
289 
293  public function testSelectChoiceFromChoiceList($providedAnswer, $expectedValue)
294  {
295  $possibleChoices = array(
296  'env_1' => 'My environment 1',
297  'env_2' => 'My environment',
298  'env_3' => 'My environment',
299  );
300 
301  $dialog = new QuestionHelper();
302  $dialog->setInputStream($this->getInputStream($providedAnswer."\n"));
303  $helperSet = new HelperSet(array(new FormatterHelper()));
304  $dialog->setHelperSet($helperSet);
305 
306  $question = new ChoiceQuestion('Please select the environment to load', $possibleChoices);
307  $question->setMaxAttempts(1);
308  $answer = $dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question);
309 
310  $this->assertSame($expectedValue, $answer);
311  }
312 
318  {
319  $possibleChoices = array(
320  'env_1' => 'My first environment',
321  'env_2' => 'My environment',
322  'env_3' => 'My environment',
323  );
324 
325  $dialog = new QuestionHelper();
326  $dialog->setInputStream($this->getInputStream("My environment\n"));
327  $helperSet = new HelperSet(array(new FormatterHelper()));
328  $dialog->setHelperSet($helperSet);
329 
330  $question = new ChoiceQuestion('Please select the environment to load', $possibleChoices);
331  $question->setMaxAttempts(1);
332 
333  $dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question);
334  }
335 
336  public function answerProvider()
337  {
338  return array(
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'),
343  );
344  }
345 
346  public function testNoInteraction()
347  {
348  $dialog = new QuestionHelper();
349  $question = new Question('Do you have a job?', 'not yet');
350  $this->assertEquals('not yet', $dialog->ask($this->createInputInterfaceMock(false), $this->createOutputInterface(), $question));
351  }
352 
353  protected function getInputStream($input)
354  {
355  $stream = fopen('php://memory', 'r+', false);
356  fwrite($stream, $input);
357  rewind($stream);
358 
359  return $stream;
360  }
361 
362  protected function createOutputInterface()
363  {
364  return new StreamOutput(fopen('php://memory', 'r+', false));
365  }
366 
367  protected function createInputInterfaceMock($interactive = true)
368  {
369  $mock = $this->getMock('Symfony\Component\Console\Input\InputInterface');
370  $mock->expects($this->any())
371  ->method('isInteractive')
372  ->will($this->returnValue($interactive));
373 
374  return $mock;
375  }
376 
377  private function hasSttyAvailable()
378  {
379  exec('stty 2>&1', $output, $exitcode);
380 
381  return $exitcode === 0;
382  }
383 }