QuestionHelperTest.php 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Console\Tests\Helper;
  11. use Symfony\Component\Console\Formatter\OutputFormatter;
  12. use Symfony\Component\Console\Helper\QuestionHelper;
  13. use Symfony\Component\Console\Helper\HelperSet;
  14. use Symfony\Component\Console\Helper\FormatterHelper;
  15. use Symfony\Component\Console\Output\StreamOutput;
  16. use Symfony\Component\Console\Question\ChoiceQuestion;
  17. use Symfony\Component\Console\Question\ConfirmationQuestion;
  18. use Symfony\Component\Console\Question\Question;
  19. /**
  20. * @group tty
  21. */
  22. class QuestionHelperTest extends AbstractQuestionHelperTest
  23. {
  24. public function testAskChoice()
  25. {
  26. $questionHelper = new QuestionHelper();
  27. $helperSet = new HelperSet(array(new FormatterHelper()));
  28. $questionHelper->setHelperSet($helperSet);
  29. $heroes = array('Superman', 'Batman', 'Spiderman');
  30. $inputStream = $this->getInputStream("\n1\n 1 \nFabien\n1\nFabien\n1\n0,2\n 0 , 2 \n\n\n");
  31. $question = new ChoiceQuestion('What is your favorite superhero?', $heroes, '2');
  32. $question->setMaxAttempts(1);
  33. // first answer is an empty answer, we're supposed to receive the default value
  34. $this->assertEquals('Spiderman', $questionHelper->ask($this->createStreamableInputInterfaceMock($inputStream), $this->createOutputInterface(), $question));
  35. $question = new ChoiceQuestion('What is your favorite superhero?', $heroes);
  36. $question->setMaxAttempts(1);
  37. $this->assertEquals('Batman', $questionHelper->ask($this->createStreamableInputInterfaceMock($inputStream), $this->createOutputInterface(), $question));
  38. $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. rewind($output->getStream());
  44. $stream = stream_get_contents($output->getStream());
  45. $this->assertContains('Input "Fabien" is not a superhero!', $stream);
  46. try {
  47. $question = new ChoiceQuestion('What is your favorite superhero?', $heroes, '1');
  48. $question->setMaxAttempts(1);
  49. $questionHelper->ask($this->createStreamableInputInterfaceMock($inputStream), $output = $this->createOutputInterface(), $question);
  50. $this->fail();
  51. } catch (\InvalidArgumentException $e) {
  52. $this->assertEquals('Value "Fabien" is invalid', $e->getMessage());
  53. }
  54. $question = new ChoiceQuestion('What is your favorite superhero?', $heroes, null);
  55. $question->setMaxAttempts(1);
  56. $question->setMultiselect(true);
  57. $this->assertEquals(array('Batman'), $questionHelper->ask($this->createStreamableInputInterfaceMock($inputStream), $this->createOutputInterface(), $question));
  58. $this->assertEquals(array('Superman', 'Spiderman'), $questionHelper->ask($this->createStreamableInputInterfaceMock($inputStream), $this->createOutputInterface(), $question));
  59. $this->assertEquals(array('Superman', 'Spiderman'), $questionHelper->ask($this->createStreamableInputInterfaceMock($inputStream), $this->createOutputInterface(), $question));
  60. $question = new ChoiceQuestion('What is your favorite superhero?', $heroes, '0,1');
  61. $question->setMaxAttempts(1);
  62. $question->setMultiselect(true);
  63. $this->assertEquals(array('Superman', 'Batman'), $questionHelper->ask($this->createStreamableInputInterfaceMock($inputStream), $this->createOutputInterface(), $question));
  64. $question = new ChoiceQuestion('What is your favorite superhero?', $heroes, ' 0 , 1 ');
  65. $question->setMaxAttempts(1);
  66. $question->setMultiselect(true);
  67. $this->assertEquals(array('Superman', 'Batman'), $questionHelper->ask($this->createStreamableInputInterfaceMock($inputStream), $this->createOutputInterface(), $question));
  68. }
  69. public function testAsk()
  70. {
  71. $dialog = new QuestionHelper();
  72. $inputStream = $this->getInputStream("\n8AM\n");
  73. $question = new Question('What time is it?', '2PM');
  74. $this->assertEquals('2PM', $dialog->ask($this->createStreamableInputInterfaceMock($inputStream), $this->createOutputInterface(), $question));
  75. $question = new Question('What time is it?', '2PM');
  76. $this->assertEquals('8AM', $dialog->ask($this->createStreamableInputInterfaceMock($inputStream), $output = $this->createOutputInterface(), $question));
  77. rewind($output->getStream());
  78. $this->assertEquals('What time is it?', stream_get_contents($output->getStream()));
  79. }
  80. public function testAskWithAutocomplete()
  81. {
  82. if (!$this->hasSttyAvailable()) {
  83. $this->markTestSkipped('`stty` is required to test autocomplete functionality');
  84. }
  85. // Acm<NEWLINE>
  86. // Ac<BACKSPACE><BACKSPACE>s<TAB>Test<NEWLINE>
  87. // <NEWLINE>
  88. // <UP ARROW><UP ARROW><NEWLINE>
  89. // <UP ARROW><UP ARROW><UP ARROW><UP ARROW><UP ARROW><TAB>Test<NEWLINE>
  90. // <DOWN ARROW><NEWLINE>
  91. // S<BACKSPACE><BACKSPACE><DOWN ARROW><DOWN ARROW><NEWLINE>
  92. // F00<BACKSPACE><BACKSPACE>oo<TAB><NEWLINE>
  93. $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");
  94. $dialog = new QuestionHelper();
  95. $helperSet = new HelperSet(array(new FormatterHelper()));
  96. $dialog->setHelperSet($helperSet);
  97. $question = new Question('Please select a bundle', 'FrameworkBundle');
  98. $question->setAutocompleterValues(array('AcmeDemoBundle', 'AsseticBundle', 'SecurityBundle', 'FooBundle'));
  99. $this->assertEquals('AcmeDemoBundle', $dialog->ask($this->createStreamableInputInterfaceMock($inputStream), $this->createOutputInterface(), $question));
  100. $this->assertEquals('AsseticBundleTest', $dialog->ask($this->createStreamableInputInterfaceMock($inputStream), $this->createOutputInterface(), $question));
  101. $this->assertEquals('FrameworkBundle', $dialog->ask($this->createStreamableInputInterfaceMock($inputStream), $this->createOutputInterface(), $question));
  102. $this->assertEquals('SecurityBundle', $dialog->ask($this->createStreamableInputInterfaceMock($inputStream), $this->createOutputInterface(), $question));
  103. $this->assertEquals('FooBundleTest', $dialog->ask($this->createStreamableInputInterfaceMock($inputStream), $this->createOutputInterface(), $question));
  104. $this->assertEquals('AcmeDemoBundle', $dialog->ask($this->createStreamableInputInterfaceMock($inputStream), $this->createOutputInterface(), $question));
  105. $this->assertEquals('AsseticBundle', $dialog->ask($this->createStreamableInputInterfaceMock($inputStream), $this->createOutputInterface(), $question));
  106. $this->assertEquals('FooBundle', $dialog->ask($this->createStreamableInputInterfaceMock($inputStream), $this->createOutputInterface(), $question));
  107. }
  108. public function testAskWithAutocompleteWithNonSequentialKeys()
  109. {
  110. if (!$this->hasSttyAvailable()) {
  111. $this->markTestSkipped('`stty` is required to test autocomplete functionality');
  112. }
  113. // <UP ARROW><UP ARROW><NEWLINE><DOWN ARROW><DOWN ARROW><NEWLINE>
  114. $inputStream = $this->getInputStream("\033[A\033[A\n\033[B\033[B\n");
  115. $dialog = new QuestionHelper();
  116. $dialog->setHelperSet(new HelperSet(array(new FormatterHelper())));
  117. $question = new ChoiceQuestion('Please select a bundle', array(1 => 'AcmeDemoBundle', 4 => 'AsseticBundle'));
  118. $question->setMaxAttempts(1);
  119. $this->assertEquals('AcmeDemoBundle', $dialog->ask($this->createStreamableInputInterfaceMock($inputStream), $this->createOutputInterface(), $question));
  120. $this->assertEquals('AsseticBundle', $dialog->ask($this->createStreamableInputInterfaceMock($inputStream), $this->createOutputInterface(), $question));
  121. }
  122. public function testAskHiddenResponse()
  123. {
  124. if ('\\' === DIRECTORY_SEPARATOR) {
  125. $this->markTestSkipped('This test is not supported on Windows');
  126. }
  127. $dialog = new QuestionHelper();
  128. $question = new Question('What time is it?');
  129. $question->setHidden(true);
  130. $this->assertEquals('8AM', $dialog->ask($this->createStreamableInputInterfaceMock($this->getInputStream("8AM\n")), $this->createOutputInterface(), $question));
  131. }
  132. /**
  133. * @dataProvider getAskConfirmationData
  134. */
  135. public function testAskConfirmation($question, $expected, $default = true)
  136. {
  137. $dialog = new QuestionHelper();
  138. $inputStream = $this->getInputStream($question."\n");
  139. $question = new ConfirmationQuestion('Do you like French fries?', $default);
  140. $this->assertEquals($expected, $dialog->ask($this->createStreamableInputInterfaceMock($inputStream), $this->createOutputInterface(), $question), 'confirmation question should '.($expected ? 'pass' : 'cancel'));
  141. }
  142. public function getAskConfirmationData()
  143. {
  144. return array(
  145. array('', true),
  146. array('', false, false),
  147. array('y', true),
  148. array('yes', true),
  149. array('n', false),
  150. array('no', false),
  151. );
  152. }
  153. public function testAskConfirmationWithCustomTrueAnswer()
  154. {
  155. $dialog = new QuestionHelper();
  156. $inputStream = $this->getInputStream("j\ny\n");
  157. $question = new ConfirmationQuestion('Do you like French fries?', false, '/^(j|y)/i');
  158. $this->assertTrue($dialog->ask($this->createStreamableInputInterfaceMock($inputStream), $this->createOutputInterface(), $question));
  159. $question = new ConfirmationQuestion('Do you like French fries?', false, '/^(j|y)/i');
  160. $this->assertTrue($dialog->ask($this->createStreamableInputInterfaceMock($inputStream), $this->createOutputInterface(), $question));
  161. }
  162. public function testAskAndValidate()
  163. {
  164. $dialog = new QuestionHelper();
  165. $helperSet = new HelperSet(array(new FormatterHelper()));
  166. $dialog->setHelperSet($helperSet);
  167. $error = 'This is not a color!';
  168. $validator = function ($color) use ($error) {
  169. if (!in_array($color, array('white', 'black'))) {
  170. throw new \InvalidArgumentException($error);
  171. }
  172. return $color;
  173. };
  174. $question = new Question('What color was the white horse of Henry IV?', 'white');
  175. $question->setValidator($validator);
  176. $question->setMaxAttempts(2);
  177. $inputStream = $this->getInputStream("\nblack\n");
  178. $this->assertEquals('white', $dialog->ask($this->createStreamableInputInterfaceMock($inputStream), $this->createOutputInterface(), $question));
  179. $this->assertEquals('black', $dialog->ask($this->createStreamableInputInterfaceMock($inputStream), $this->createOutputInterface(), $question));
  180. try {
  181. $dialog->ask($this->createStreamableInputInterfaceMock($this->getInputStream("green\nyellow\norange\n")), $this->createOutputInterface(), $question);
  182. $this->fail();
  183. } catch (\InvalidArgumentException $e) {
  184. $this->assertEquals($error, $e->getMessage());
  185. }
  186. }
  187. /**
  188. * @dataProvider simpleAnswerProvider
  189. */
  190. public function testSelectChoiceFromSimpleChoices($providedAnswer, $expectedValue)
  191. {
  192. $possibleChoices = array(
  193. 'My environment 1',
  194. 'My environment 2',
  195. 'My environment 3',
  196. );
  197. $dialog = new QuestionHelper();
  198. $helperSet = new HelperSet(array(new FormatterHelper()));
  199. $dialog->setHelperSet($helperSet);
  200. $question = new ChoiceQuestion('Please select the environment to load', $possibleChoices);
  201. $question->setMaxAttempts(1);
  202. $answer = $dialog->ask($this->createStreamableInputInterfaceMock($this->getInputStream($providedAnswer."\n")), $this->createOutputInterface(), $question);
  203. $this->assertSame($expectedValue, $answer);
  204. }
  205. public function simpleAnswerProvider()
  206. {
  207. return array(
  208. array(0, 'My environment 1'),
  209. array(1, 'My environment 2'),
  210. array(2, 'My environment 3'),
  211. array('My environment 1', 'My environment 1'),
  212. array('My environment 2', 'My environment 2'),
  213. array('My environment 3', 'My environment 3'),
  214. );
  215. }
  216. /**
  217. * @dataProvider specialCharacterInMultipleChoice
  218. */
  219. public function testSpecialCharacterChoiceFromMultipleChoiceList($providedAnswer, $expectedValue)
  220. {
  221. $possibleChoices = array(
  222. '.',
  223. 'src',
  224. );
  225. $dialog = new QuestionHelper();
  226. $inputStream = $this->getInputStream($providedAnswer."\n");
  227. $helperSet = new HelperSet(array(new FormatterHelper()));
  228. $dialog->setHelperSet($helperSet);
  229. $question = new ChoiceQuestion('Please select the directory', $possibleChoices);
  230. $question->setMaxAttempts(1);
  231. $question->setMultiselect(true);
  232. $answer = $dialog->ask($this->createStreamableInputInterfaceMock($inputStream), $this->createOutputInterface(), $question);
  233. $this->assertSame($expectedValue, $answer);
  234. }
  235. public function specialCharacterInMultipleChoice()
  236. {
  237. return array(
  238. array('.', array('.')),
  239. array('., src', array('.', 'src')),
  240. );
  241. }
  242. /**
  243. * @dataProvider mixedKeysChoiceListAnswerProvider
  244. */
  245. public function testChoiceFromChoicelistWithMixedKeys($providedAnswer, $expectedValue)
  246. {
  247. $possibleChoices = array(
  248. '0' => 'No environment',
  249. '1' => 'My environment 1',
  250. 'env_2' => 'My environment 2',
  251. 3 => 'My environment 3',
  252. );
  253. $dialog = new QuestionHelper();
  254. $helperSet = new HelperSet(array(new FormatterHelper()));
  255. $dialog->setHelperSet($helperSet);
  256. $question = new ChoiceQuestion('Please select the environment to load', $possibleChoices);
  257. $question->setMaxAttempts(1);
  258. $answer = $dialog->ask($this->createStreamableInputInterfaceMock($this->getInputStream($providedAnswer."\n")), $this->createOutputInterface(), $question);
  259. $this->assertSame($expectedValue, $answer);
  260. }
  261. public function mixedKeysChoiceListAnswerProvider()
  262. {
  263. return array(
  264. array('0', '0'),
  265. array('No environment', '0'),
  266. array('1', '1'),
  267. array('env_2', 'env_2'),
  268. array(3, '3'),
  269. array('My environment 1', '1'),
  270. );
  271. }
  272. /**
  273. * @dataProvider answerProvider
  274. */
  275. public function testSelectChoiceFromChoiceList($providedAnswer, $expectedValue)
  276. {
  277. $possibleChoices = array(
  278. 'env_1' => 'My environment 1',
  279. 'env_2' => 'My environment',
  280. 'env_3' => 'My environment',
  281. );
  282. $dialog = new QuestionHelper();
  283. $helperSet = new HelperSet(array(new FormatterHelper()));
  284. $dialog->setHelperSet($helperSet);
  285. $question = new ChoiceQuestion('Please select the environment to load', $possibleChoices);
  286. $question->setMaxAttempts(1);
  287. $answer = $dialog->ask($this->createStreamableInputInterfaceMock($this->getInputStream($providedAnswer."\n")), $this->createOutputInterface(), $question);
  288. $this->assertSame($expectedValue, $answer);
  289. }
  290. /**
  291. * @expectedException \InvalidArgumentException
  292. * @expectedExceptionMessage The provided answer is ambiguous. Value should be one of env_2 or env_3.
  293. */
  294. public function testAmbiguousChoiceFromChoicelist()
  295. {
  296. $possibleChoices = array(
  297. 'env_1' => 'My first environment',
  298. 'env_2' => 'My environment',
  299. 'env_3' => 'My environment',
  300. );
  301. $dialog = new QuestionHelper();
  302. $helperSet = new HelperSet(array(new FormatterHelper()));
  303. $dialog->setHelperSet($helperSet);
  304. $question = new ChoiceQuestion('Please select the environment to load', $possibleChoices);
  305. $question->setMaxAttempts(1);
  306. $dialog->ask($this->createStreamableInputInterfaceMock($this->getInputStream("My environment\n")), $this->createOutputInterface(), $question);
  307. }
  308. public function answerProvider()
  309. {
  310. return array(
  311. array('env_1', 'env_1'),
  312. array('env_2', 'env_2'),
  313. array('env_3', 'env_3'),
  314. array('My environment 1', 'env_1'),
  315. );
  316. }
  317. public function testNoInteraction()
  318. {
  319. $dialog = new QuestionHelper();
  320. $question = new Question('Do you have a job?', 'not yet');
  321. $this->assertEquals('not yet', $dialog->ask($this->createStreamableInputInterfaceMock(null, false), $this->createOutputInterface(), $question));
  322. }
  323. /**
  324. * @requires function mb_strwidth
  325. */
  326. public function testChoiceOutputFormattingQuestionForUtf8Keys()
  327. {
  328. $question = 'Lorem ipsum?';
  329. $possibleChoices = array(
  330. 'foo' => 'foo',
  331. 'żółw' => 'bar',
  332. 'łabądź' => 'baz',
  333. );
  334. $outputShown = array(
  335. $question,
  336. ' [<info>foo </info>] foo',
  337. ' [<info>żółw </info>] bar',
  338. ' [<info>łabądź</info>] baz',
  339. );
  340. $output = $this->getMockBuilder('\Symfony\Component\Console\Output\OutputInterface')->getMock();
  341. $output->method('getFormatter')->willReturn(new OutputFormatter());
  342. $dialog = new QuestionHelper();
  343. $helperSet = new HelperSet(array(new FormatterHelper()));
  344. $dialog->setHelperSet($helperSet);
  345. $output->expects($this->once())->method('writeln')->with($this->equalTo($outputShown));
  346. $question = new ChoiceQuestion($question, $possibleChoices, 'foo');
  347. $dialog->ask($this->createStreamableInputInterfaceMock($this->getInputStream("\n")), $output, $question);
  348. }
  349. /**
  350. * @group legacy
  351. */
  352. public function testLegacyAskChoice()
  353. {
  354. $questionHelper = new QuestionHelper();
  355. $helperSet = new HelperSet(array(new FormatterHelper()));
  356. $questionHelper->setHelperSet($helperSet);
  357. $heroes = array('Superman', 'Batman', 'Spiderman');
  358. $questionHelper->setInputStream($this->getInputStream("\n1\n 1 \nFabien\n1\nFabien\n1\n0,2\n 0 , 2 \n\n\n"));
  359. $question = new ChoiceQuestion('What is your favorite superhero?', $heroes, '2');
  360. $question->setMaxAttempts(1);
  361. // first answer is an empty answer, we're supposed to receive the default value
  362. $this->assertEquals('Spiderman', $questionHelper->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
  363. $question = new ChoiceQuestion('What is your favorite superhero?', $heroes);
  364. $question->setMaxAttempts(1);
  365. $this->assertEquals('Batman', $questionHelper->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
  366. $this->assertEquals('Batman', $questionHelper->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
  367. $question = new ChoiceQuestion('What is your favorite superhero?', $heroes);
  368. $question->setErrorMessage('Input "%s" is not a superhero!');
  369. $question->setMaxAttempts(2);
  370. $this->assertEquals('Batman', $questionHelper->ask($this->createInputInterfaceMock(), $output = $this->createOutputInterface(), $question));
  371. rewind($output->getStream());
  372. $stream = stream_get_contents($output->getStream());
  373. $this->assertContains('Input "Fabien" is not a superhero!', $stream);
  374. try {
  375. $question = new ChoiceQuestion('What is your favorite superhero?', $heroes, '1');
  376. $question->setMaxAttempts(1);
  377. $questionHelper->ask($this->createInputInterfaceMock(), $output = $this->createOutputInterface(), $question);
  378. $this->fail();
  379. } catch (\InvalidArgumentException $e) {
  380. $this->assertEquals('Value "Fabien" is invalid', $e->getMessage());
  381. }
  382. $question = new ChoiceQuestion('What is your favorite superhero?', $heroes, null);
  383. $question->setMaxAttempts(1);
  384. $question->setMultiselect(true);
  385. $this->assertEquals(array('Batman'), $questionHelper->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
  386. $this->assertEquals(array('Superman', 'Spiderman'), $questionHelper->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
  387. $this->assertEquals(array('Superman', 'Spiderman'), $questionHelper->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
  388. $question = new ChoiceQuestion('What is your favorite superhero?', $heroes, '0,1');
  389. $question->setMaxAttempts(1);
  390. $question->setMultiselect(true);
  391. $this->assertEquals(array('Superman', 'Batman'), $questionHelper->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
  392. $question = new ChoiceQuestion('What is your favorite superhero?', $heroes, ' 0 , 1 ');
  393. $question->setMaxAttempts(1);
  394. $question->setMultiselect(true);
  395. $this->assertEquals(array('Superman', 'Batman'), $questionHelper->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
  396. }
  397. /**
  398. * @group legacy
  399. */
  400. public function testLegacyAsk()
  401. {
  402. $dialog = new QuestionHelper();
  403. $dialog->setInputStream($this->getInputStream("\n8AM\n"));
  404. $question = new Question('What time is it?', '2PM');
  405. $this->assertEquals('2PM', $dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
  406. $question = new Question('What time is it?', '2PM');
  407. $this->assertEquals('8AM', $dialog->ask($this->createInputInterfaceMock(), $output = $this->createOutputInterface(), $question));
  408. rewind($output->getStream());
  409. $this->assertEquals('What time is it?', stream_get_contents($output->getStream()));
  410. }
  411. /**
  412. * @group legacy
  413. */
  414. public function testLegacyAskWithAutocomplete()
  415. {
  416. if (!$this->hasSttyAvailable()) {
  417. $this->markTestSkipped('`stty` is required to test autocomplete functionality');
  418. }
  419. // Acm<NEWLINE>
  420. // Ac<BACKSPACE><BACKSPACE>s<TAB>Test<NEWLINE>
  421. // <NEWLINE>
  422. // <UP ARROW><UP ARROW><NEWLINE>
  423. // <UP ARROW><UP ARROW><UP ARROW><UP ARROW><UP ARROW><TAB>Test<NEWLINE>
  424. // <DOWN ARROW><NEWLINE>
  425. // S<BACKSPACE><BACKSPACE><DOWN ARROW><DOWN ARROW><NEWLINE>
  426. // F00<BACKSPACE><BACKSPACE>oo<TAB><NEWLINE>
  427. $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");
  428. $dialog = new QuestionHelper();
  429. $dialog->setInputStream($inputStream);
  430. $helperSet = new HelperSet(array(new FormatterHelper()));
  431. $dialog->setHelperSet($helperSet);
  432. $question = new Question('Please select a bundle', 'FrameworkBundle');
  433. $question->setAutocompleterValues(array('AcmeDemoBundle', 'AsseticBundle', 'SecurityBundle', 'FooBundle'));
  434. $this->assertEquals('AcmeDemoBundle', $dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
  435. $this->assertEquals('AsseticBundleTest', $dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
  436. $this->assertEquals('FrameworkBundle', $dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
  437. $this->assertEquals('SecurityBundle', $dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
  438. $this->assertEquals('FooBundleTest', $dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
  439. $this->assertEquals('AcmeDemoBundle', $dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
  440. $this->assertEquals('AsseticBundle', $dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
  441. $this->assertEquals('FooBundle', $dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
  442. }
  443. /**
  444. * @group legacy
  445. */
  446. public function testLegacyAskWithAutocompleteWithNonSequentialKeys()
  447. {
  448. if (!$this->hasSttyAvailable()) {
  449. $this->markTestSkipped('`stty` is required to test autocomplete functionality');
  450. }
  451. // <UP ARROW><UP ARROW><NEWLINE><DOWN ARROW><DOWN ARROW><NEWLINE>
  452. $inputStream = $this->getInputStream("\033[A\033[A\n\033[B\033[B\n");
  453. $dialog = new QuestionHelper();
  454. $dialog->setInputStream($inputStream);
  455. $dialog->setHelperSet(new HelperSet(array(new FormatterHelper())));
  456. $question = new ChoiceQuestion('Please select a bundle', array(1 => 'AcmeDemoBundle', 4 => 'AsseticBundle'));
  457. $question->setMaxAttempts(1);
  458. $this->assertEquals('AcmeDemoBundle', $dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
  459. $this->assertEquals('AsseticBundle', $dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
  460. }
  461. /**
  462. * @group legacy
  463. */
  464. public function testLegacyAskHiddenResponse()
  465. {
  466. if ('\\' === DIRECTORY_SEPARATOR) {
  467. $this->markTestSkipped('This test is not supported on Windows');
  468. }
  469. $dialog = new QuestionHelper();
  470. $dialog->setInputStream($this->getInputStream("8AM\n"));
  471. $question = new Question('What time is it?');
  472. $question->setHidden(true);
  473. $this->assertEquals('8AM', $dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
  474. }
  475. /**
  476. * @group legacy
  477. * @dataProvider getAskConfirmationData
  478. */
  479. public function testLegacyAskConfirmation($question, $expected, $default = true)
  480. {
  481. $dialog = new QuestionHelper();
  482. $dialog->setInputStream($this->getInputStream($question."\n"));
  483. $question = new ConfirmationQuestion('Do you like French fries?', $default);
  484. $this->assertEquals($expected, $dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question), 'confirmation question should '.($expected ? 'pass' : 'cancel'));
  485. }
  486. /**
  487. * @group legacy
  488. */
  489. public function testLegacyAskConfirmationWithCustomTrueAnswer()
  490. {
  491. $dialog = new QuestionHelper();
  492. $dialog->setInputStream($this->getInputStream("j\ny\n"));
  493. $question = new ConfirmationQuestion('Do you like French fries?', false, '/^(j|y)/i');
  494. $this->assertTrue($dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
  495. $question = new ConfirmationQuestion('Do you like French fries?', false, '/^(j|y)/i');
  496. $this->assertTrue($dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
  497. }
  498. /**
  499. * @group legacy
  500. */
  501. public function testLegacyAskAndValidate()
  502. {
  503. $dialog = new QuestionHelper();
  504. $helperSet = new HelperSet(array(new FormatterHelper()));
  505. $dialog->setHelperSet($helperSet);
  506. $error = 'This is not a color!';
  507. $validator = function ($color) use ($error) {
  508. if (!in_array($color, array('white', 'black'))) {
  509. throw new \InvalidArgumentException($error);
  510. }
  511. return $color;
  512. };
  513. $question = new Question('What color was the white horse of Henry IV?', 'white');
  514. $question->setValidator($validator);
  515. $question->setMaxAttempts(2);
  516. $dialog->setInputStream($this->getInputStream("\nblack\n"));
  517. $this->assertEquals('white', $dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
  518. $this->assertEquals('black', $dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
  519. $dialog->setInputStream($this->getInputStream("green\nyellow\norange\n"));
  520. try {
  521. $dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question);
  522. $this->fail();
  523. } catch (\InvalidArgumentException $e) {
  524. $this->assertEquals($error, $e->getMessage());
  525. }
  526. }
  527. /**
  528. * @group legacy
  529. * @dataProvider simpleAnswerProvider
  530. */
  531. public function testLegacySelectChoiceFromSimpleChoices($providedAnswer, $expectedValue)
  532. {
  533. $possibleChoices = array(
  534. 'My environment 1',
  535. 'My environment 2',
  536. 'My environment 3',
  537. );
  538. $dialog = new QuestionHelper();
  539. $dialog->setInputStream($this->getInputStream($providedAnswer."\n"));
  540. $helperSet = new HelperSet(array(new FormatterHelper()));
  541. $dialog->setHelperSet($helperSet);
  542. $question = new ChoiceQuestion('Please select the environment to load', $possibleChoices);
  543. $question->setMaxAttempts(1);
  544. $answer = $dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question);
  545. $this->assertSame($expectedValue, $answer);
  546. }
  547. /**
  548. * @group legacy
  549. * @dataProvider mixedKeysChoiceListAnswerProvider
  550. */
  551. public function testLegacyChoiceFromChoicelistWithMixedKeys($providedAnswer, $expectedValue)
  552. {
  553. $possibleChoices = array(
  554. '0' => 'No environment',
  555. '1' => 'My environment 1',
  556. 'env_2' => 'My environment 2',
  557. 3 => 'My environment 3',
  558. );
  559. $dialog = new QuestionHelper();
  560. $dialog->setInputStream($this->getInputStream($providedAnswer."\n"));
  561. $helperSet = new HelperSet(array(new FormatterHelper()));
  562. $dialog->setHelperSet($helperSet);
  563. $question = new ChoiceQuestion('Please select the environment to load', $possibleChoices);
  564. $question->setMaxAttempts(1);
  565. $answer = $dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question);
  566. $this->assertSame($expectedValue, $answer);
  567. }
  568. /**
  569. * @group legacy
  570. * @dataProvider answerProvider
  571. */
  572. public function testLegacySelectChoiceFromChoiceList($providedAnswer, $expectedValue)
  573. {
  574. $possibleChoices = array(
  575. 'env_1' => 'My environment 1',
  576. 'env_2' => 'My environment',
  577. 'env_3' => 'My environment',
  578. );
  579. $dialog = new QuestionHelper();
  580. $dialog->setInputStream($this->getInputStream($providedAnswer."\n"));
  581. $helperSet = new HelperSet(array(new FormatterHelper()));
  582. $dialog->setHelperSet($helperSet);
  583. $question = new ChoiceQuestion('Please select the environment to load', $possibleChoices);
  584. $question->setMaxAttempts(1);
  585. $answer = $dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question);
  586. $this->assertSame($expectedValue, $answer);
  587. }
  588. /**
  589. * @group legacy
  590. * @expectedException \InvalidArgumentException
  591. * @expectedExceptionMessage The provided answer is ambiguous. Value should be one of env_2 or env_3.
  592. */
  593. public function testLegacyAmbiguousChoiceFromChoicelist()
  594. {
  595. $possibleChoices = array(
  596. 'env_1' => 'My first environment',
  597. 'env_2' => 'My environment',
  598. 'env_3' => 'My environment',
  599. );
  600. $dialog = new QuestionHelper();
  601. $dialog->setInputStream($this->getInputStream("My environment\n"));
  602. $helperSet = new HelperSet(array(new FormatterHelper()));
  603. $dialog->setHelperSet($helperSet);
  604. $question = new ChoiceQuestion('Please select the environment to load', $possibleChoices);
  605. $question->setMaxAttempts(1);
  606. $dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question);
  607. }
  608. /**
  609. * @requires function mb_strwidth
  610. * @group legacy
  611. */
  612. public function testLegacyChoiceOutputFormattingQuestionForUtf8Keys()
  613. {
  614. $question = 'Lorem ipsum?';
  615. $possibleChoices = array(
  616. 'foo' => 'foo',
  617. 'żółw' => 'bar',
  618. 'łabądź' => 'baz',
  619. );
  620. $outputShown = array(
  621. $question,
  622. ' [<info>foo </info>] foo',
  623. ' [<info>żółw </info>] bar',
  624. ' [<info>łabądź</info>] baz',
  625. );
  626. $output = $this->getMockBuilder('\Symfony\Component\Console\Output\OutputInterface')->getMock();
  627. $output->method('getFormatter')->willReturn(new OutputFormatter());
  628. $dialog = new QuestionHelper();
  629. $dialog->setInputStream($this->getInputStream("\n"));
  630. $helperSet = new HelperSet(array(new FormatterHelper()));
  631. $dialog->setHelperSet($helperSet);
  632. $output->expects($this->once())->method('writeln')->with($this->equalTo($outputShown));
  633. $question = new ChoiceQuestion($question, $possibleChoices, 'foo');
  634. $dialog->ask($this->createInputInterfaceMock(), $output, $question);
  635. }
  636. /**
  637. * @expectedException \Symfony\Component\Console\Exception\RuntimeException
  638. * @expectedExceptionMessage Aborted
  639. */
  640. public function testAskThrowsExceptionOnMissingInput()
  641. {
  642. $dialog = new QuestionHelper();
  643. $dialog->ask($this->createStreamableInputInterfaceMock($this->getInputStream('')), $this->createOutputInterface(), new Question('What\'s your name?'));
  644. }
  645. /**
  646. * @expectedException \Symfony\Component\Console\Exception\RuntimeException
  647. * @expectedExceptionMessage Aborted
  648. */
  649. public function testAskThrowsExceptionOnMissingInputWithValidator()
  650. {
  651. $dialog = new QuestionHelper();
  652. $question = new Question('What\'s your name?');
  653. $question->setValidator(function () {
  654. if (!$value) {
  655. throw new \Exception('A value is required.');
  656. }
  657. });
  658. $dialog->ask($this->createStreamableInputInterfaceMock($this->getInputStream('')), $this->createOutputInterface(), $question);
  659. }
  660. /**
  661. * @expectedException \LogicException
  662. * @expectedExceptionMessage Choice question must have at least 1 choice available.
  663. */
  664. public function testEmptyChoices()
  665. {
  666. new ChoiceQuestion('Question', array(), 'irrelevant');
  667. }
  668. protected function getInputStream($input)
  669. {
  670. $stream = fopen('php://memory', 'r+', false);
  671. fwrite($stream, $input);
  672. rewind($stream);
  673. return $stream;
  674. }
  675. protected function createOutputInterface()
  676. {
  677. return new StreamOutput(fopen('php://memory', 'r+', false));
  678. }
  679. protected function createInputInterfaceMock($interactive = true)
  680. {
  681. $mock = $this->getMockBuilder('Symfony\Component\Console\Input\InputInterface')->getMock();
  682. $mock->expects($this->any())
  683. ->method('isInteractive')
  684. ->will($this->returnValue($interactive));
  685. return $mock;
  686. }
  687. private function hasSttyAvailable()
  688. {
  689. exec('stty 2>&1', $output, $exitcode);
  690. return $exitcode === 0;
  691. }
  692. }