QuestionHelperTest.php 41 KB

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