菜谱项目

QuestionHelperTest.php 37KB

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