QuestionHelperTest.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682
  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 PHPUnit\Framework\TestCase;
  12. use Symfony\Component\Console\Formatter\OutputFormatter;
  13. use Symfony\Component\Console\Helper\FormatterHelper;
  14. use Symfony\Component\Console\Helper\HelperSet;
  15. use Symfony\Component\Console\Helper\QuestionHelper;
  16. use Symfony\Component\Console\Output\StreamOutput;
  17. use Symfony\Component\Console\Question\ChoiceQuestion;
  18. use Symfony\Component\Console\Question\ConfirmationQuestion;
  19. use Symfony\Component\Console\Question\Question;
  20. /**
  21. * @group tty
  22. */
  23. class QuestionHelperTest extends TestCase
  24. {
  25. public function testAskChoice()
  26. {
  27. $questionHelper = new QuestionHelper();
  28. $helperSet = new HelperSet(array(new FormatterHelper()));
  29. $questionHelper->setHelperSet($helperSet);
  30. $heroes = array('Superman', 'Batman', 'Spiderman');
  31. $questionHelper->setInputStream($this->getInputStream("\n1\n 1 \nFabien\n1\nFabien\n1\n0,2\n 0 , 2 \n\n\n"));
  32. $question = new ChoiceQuestion('What is your favorite superhero?', $heroes, '2');
  33. $question->setMaxAttempts(1);
  34. // first answer is an empty answer, we're supposed to receive the default value
  35. $this->assertEquals('Spiderman', $questionHelper->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
  36. $question = new ChoiceQuestion('What is your favorite superhero?', $heroes);
  37. $question->setMaxAttempts(1);
  38. $this->assertEquals('Batman', $questionHelper->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
  39. $this->assertEquals('Batman', $questionHelper->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
  40. $question = new ChoiceQuestion('What is your favorite superhero?', $heroes);
  41. $question->setErrorMessage('Input "%s" is not a superhero!');
  42. $question->setMaxAttempts(2);
  43. $this->assertEquals('Batman', $questionHelper->ask($this->createInputInterfaceMock(), $output = $this->createOutputInterface(), $question));
  44. rewind($output->getStream());
  45. $stream = stream_get_contents($output->getStream());
  46. $this->assertContains('Input "Fabien" is not a superhero!', $stream);
  47. try {
  48. $question = new ChoiceQuestion('What is your favorite superhero?', $heroes, '1');
  49. $question->setMaxAttempts(1);
  50. $questionHelper->ask($this->createInputInterfaceMock(), $output = $this->createOutputInterface(), $question);
  51. $this->fail();
  52. } catch (\InvalidArgumentException $e) {
  53. $this->assertEquals('Value "Fabien" is invalid', $e->getMessage());
  54. }
  55. $question = new ChoiceQuestion('What is your favorite superhero?', $heroes, null);
  56. $question->setMaxAttempts(1);
  57. $question->setMultiselect(true);
  58. $this->assertEquals(array('Batman'), $questionHelper->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
  59. $this->assertEquals(array('Superman', 'Spiderman'), $questionHelper->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
  60. $this->assertEquals(array('Superman', 'Spiderman'), $questionHelper->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
  61. $question = new ChoiceQuestion('What is your favorite superhero?', $heroes, '0,1');
  62. $question->setMaxAttempts(1);
  63. $question->setMultiselect(true);
  64. $this->assertEquals(array('Superman', 'Batman'), $questionHelper->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
  65. $question = new ChoiceQuestion('What is your favorite superhero?', $heroes, ' 0 , 1 ');
  66. $question->setMaxAttempts(1);
  67. $question->setMultiselect(true);
  68. $this->assertEquals(array('Superman', 'Batman'), $questionHelper->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
  69. $question = new ChoiceQuestion('What is your favorite superhero?', $heroes, 0);
  70. // We are supposed to get the default value since we are not in interactive mode
  71. $this->assertEquals('Superman', $questionHelper->ask($this->createInputInterfaceMock(true), $this->createOutputInterface(), $question));
  72. }
  73. public function testAskChoiceNonInteractive()
  74. {
  75. $questionHelper = new QuestionHelper();
  76. $helperSet = new HelperSet(array(new FormatterHelper()));
  77. $questionHelper->setHelperSet($helperSet);
  78. $questionHelper->setInputStream($this->getInputStream("\n1\n 1 \nFabien\n1\nFabien\n1\n0,2\n 0 , 2 \n\n\n"));
  79. $heroes = array('Superman', 'Batman', 'Spiderman');
  80. $question = new ChoiceQuestion('What is your favorite superhero?', $heroes, '0');
  81. $this->assertSame('Superman', $questionHelper->ask($this->createInputInterfaceMock(false), $this->createOutputInterface(), $question));
  82. $question = new ChoiceQuestion('What is your favorite superhero?', $heroes, 'Batman');
  83. $this->assertSame('Batman', $questionHelper->ask($this->createInputInterfaceMock(false), $this->createOutputInterface(), $question));
  84. $question = new ChoiceQuestion('What is your favorite superhero?', $heroes, null);
  85. $this->assertNull($questionHelper->ask($this->createInputInterfaceMock(false), $this->createOutputInterface(), $question));
  86. $question = new ChoiceQuestion('What is your favorite superhero?', $heroes, '0');
  87. $question->setValidator(null);
  88. $this->assertSame('Superman', $questionHelper->ask($this->createInputInterfaceMock(false), $this->createOutputInterface(), $question));
  89. try {
  90. $question = new ChoiceQuestion('What is your favorite superhero?', $heroes, null);
  91. $questionHelper->ask($this->createInputInterfaceMock(false), $this->createOutputInterface(), $question);
  92. } catch (\InvalidArgumentException $e) {
  93. $this->assertSame('Value "" is invalid', $e->getMessage());
  94. }
  95. $question = new ChoiceQuestion('Who are your favorite superheros?', $heroes, '0, 1');
  96. $question->setMultiselect(true);
  97. $this->assertSame(array('Superman', 'Batman'), $questionHelper->ask($this->createInputInterfaceMock(false), $this->createOutputInterface(), $question));
  98. $question = new ChoiceQuestion('Who are your favorite superheros?', $heroes, '0, 1');
  99. $question->setMultiselect(true);
  100. $question->setValidator(null);
  101. $this->assertSame(array('Superman', 'Batman'), $questionHelper->ask($this->createInputInterfaceMock(false), $this->createOutputInterface(), $question));
  102. $question = new ChoiceQuestion('Who are your favorite superheros?', $heroes, '0, Batman');
  103. $question->setMultiselect(true);
  104. $this->assertSame(array('Superman', 'Batman'), $questionHelper->ask($this->createInputInterfaceMock(false), $this->createOutputInterface(), $question));
  105. $question = new ChoiceQuestion('Who are your favorite superheros?', $heroes, null);
  106. $question->setMultiselect(true);
  107. $this->assertNull($questionHelper->ask($this->createInputInterfaceMock(false), $this->createOutputInterface(), $question));
  108. try {
  109. $question = new ChoiceQuestion('Who are your favorite superheros?', $heroes, '');
  110. $question->setMultiselect(true);
  111. $questionHelper->ask($this->createInputInterfaceMock(false), $this->createOutputInterface(), $question);
  112. } catch (\InvalidArgumentException $e) {
  113. $this->assertSame('Value "" is invalid', $e->getMessage());
  114. }
  115. }
  116. public function testAsk()
  117. {
  118. $dialog = new QuestionHelper();
  119. $dialog->setInputStream($this->getInputStream("\n8AM\n"));
  120. $question = new Question('What time is it?', '2PM');
  121. $this->assertEquals('2PM', $dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
  122. $question = new Question('What time is it?', '2PM');
  123. $this->assertEquals('8AM', $dialog->ask($this->createInputInterfaceMock(), $output = $this->createOutputInterface(), $question));
  124. rewind($output->getStream());
  125. $this->assertEquals('What time is it?', stream_get_contents($output->getStream()));
  126. }
  127. public function testAskWithAutocomplete()
  128. {
  129. if (!$this->hasSttyAvailable()) {
  130. $this->markTestSkipped('`stty` is required to test autocomplete functionality');
  131. }
  132. // Acm<NEWLINE>
  133. // Ac<BACKSPACE><BACKSPACE>s<TAB>Test<NEWLINE>
  134. // <NEWLINE>
  135. // <UP ARROW><UP ARROW><NEWLINE>
  136. // <UP ARROW><UP ARROW><UP ARROW><UP ARROW><UP ARROW><TAB>Test<NEWLINE>
  137. // <DOWN ARROW><NEWLINE>
  138. // S<BACKSPACE><BACKSPACE><DOWN ARROW><DOWN ARROW><NEWLINE>
  139. // F00<BACKSPACE><BACKSPACE>oo<TAB><NEWLINE>
  140. $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");
  141. $dialog = new QuestionHelper();
  142. $dialog->setInputStream($inputStream);
  143. $helperSet = new HelperSet(array(new FormatterHelper()));
  144. $dialog->setHelperSet($helperSet);
  145. $question = new Question('Please select a bundle', 'FrameworkBundle');
  146. $question->setAutocompleterValues(array('AcmeDemoBundle', 'AsseticBundle', 'SecurityBundle', 'FooBundle'));
  147. $this->assertEquals('AcmeDemoBundle', $dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
  148. $this->assertEquals('AsseticBundleTest', $dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
  149. $this->assertEquals('FrameworkBundle', $dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
  150. $this->assertEquals('SecurityBundle', $dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
  151. $this->assertEquals('FooBundleTest', $dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
  152. $this->assertEquals('AcmeDemoBundle', $dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
  153. $this->assertEquals('AsseticBundle', $dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
  154. $this->assertEquals('FooBundle', $dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
  155. }
  156. public function testAskWithAutocompleteWithNonSequentialKeys()
  157. {
  158. if (!$this->hasSttyAvailable()) {
  159. $this->markTestSkipped('`stty` is required to test autocomplete functionality');
  160. }
  161. // <UP ARROW><UP ARROW><NEWLINE><DOWN ARROW><DOWN ARROW><NEWLINE>
  162. $inputStream = $this->getInputStream("\033[A\033[A\n\033[B\033[B\n");
  163. $dialog = new QuestionHelper();
  164. $dialog->setInputStream($inputStream);
  165. $dialog->setHelperSet(new HelperSet(array(new FormatterHelper())));
  166. $question = new ChoiceQuestion('Please select a bundle', array(1 => 'AcmeDemoBundle', 4 => 'AsseticBundle'));
  167. $question->setMaxAttempts(1);
  168. $this->assertEquals('AcmeDemoBundle', $dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
  169. $this->assertEquals('AsseticBundle', $dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
  170. }
  171. public function testAskWithAutocompleteWithExactMatch()
  172. {
  173. if (!$this->hasSttyAvailable()) {
  174. $this->markTestSkipped('`stty` is required to test autocomplete functionality');
  175. }
  176. $inputStream = $this->getInputStream("b\n");
  177. $possibleChoices = array(
  178. 'a' => 'berlin',
  179. 'b' => 'copenhagen',
  180. 'c' => 'amsterdam',
  181. );
  182. $dialog = new QuestionHelper();
  183. $dialog->setInputStream($inputStream);
  184. $dialog->setHelperSet(new HelperSet(array(new FormatterHelper())));
  185. $question = new ChoiceQuestion('Please select a city', $possibleChoices);
  186. $question->setMaxAttempts(1);
  187. $this->assertSame('b', $dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
  188. }
  189. public function testAutocompleteWithTrailingBackslash()
  190. {
  191. if (!$this->hasSttyAvailable()) {
  192. $this->markTestSkipped('`stty` is required to test autocomplete functionality');
  193. }
  194. $inputStream = $this->getInputStream('E');
  195. $dialog = new QuestionHelper();
  196. $dialog->setInputStream($inputStream);
  197. $helperSet = new HelperSet(array(new FormatterHelper()));
  198. $dialog->setHelperSet($helperSet);
  199. $question = new Question('');
  200. $expectedCompletion = 'ExampleNamespace\\';
  201. $question->setAutocompleterValues(array($expectedCompletion));
  202. $output = $this->createOutputInterface();
  203. $dialog->ask($this->createInputInterfaceMock(), $output, $question);
  204. $outputStream = $output->getStream();
  205. rewind($outputStream);
  206. $actualOutput = stream_get_contents($outputStream);
  207. // Shell control (esc) sequences are not so important: we only care that
  208. // <hl> tag is interpreted correctly and replaced
  209. $irrelevantEscSequences = array(
  210. "\0337" => '', // Save cursor position
  211. "\0338" => '', // Restore cursor position
  212. "\033[K" => '', // Clear line from cursor till the end
  213. );
  214. $importantActualOutput = strtr($actualOutput, $irrelevantEscSequences);
  215. // Remove colors (e.g. "\033[30m", "\033[31;41m")
  216. $importantActualOutput = preg_replace('/\033\[\d+(;\d+)?m/', '', $importantActualOutput);
  217. $this->assertEquals($expectedCompletion, $importantActualOutput);
  218. }
  219. public function testAskHiddenResponse()
  220. {
  221. if ('\\' === \DIRECTORY_SEPARATOR) {
  222. $this->markTestSkipped('This test is not supported on Windows');
  223. }
  224. $dialog = new QuestionHelper();
  225. $dialog->setInputStream($this->getInputStream("8AM\n"));
  226. $question = new Question('What time is it?');
  227. $question->setHidden(true);
  228. $this->assertEquals('8AM', $dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
  229. }
  230. /**
  231. * @dataProvider getAskConfirmationData
  232. */
  233. public function testAskConfirmation($question, $expected, $default = true)
  234. {
  235. $dialog = new QuestionHelper();
  236. $dialog->setInputStream($this->getInputStream($question."\n"));
  237. $question = new ConfirmationQuestion('Do you like French fries?', $default);
  238. $this->assertEquals($expected, $dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question), 'confirmation question should '.($expected ? 'pass' : 'cancel'));
  239. }
  240. public function getAskConfirmationData()
  241. {
  242. return array(
  243. array('', true),
  244. array('', false, false),
  245. array('y', true),
  246. array('yes', true),
  247. array('n', false),
  248. array('no', false),
  249. );
  250. }
  251. public function testAskConfirmationWithCustomTrueAnswer()
  252. {
  253. $dialog = new QuestionHelper();
  254. $dialog->setInputStream($this->getInputStream("j\ny\n"));
  255. $question = new ConfirmationQuestion('Do you like French fries?', false, '/^(j|y)/i');
  256. $this->assertTrue($dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
  257. $question = new ConfirmationQuestion('Do you like French fries?', false, '/^(j|y)/i');
  258. $this->assertTrue($dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
  259. }
  260. public function testAskAndValidate()
  261. {
  262. $dialog = new QuestionHelper();
  263. $helperSet = new HelperSet(array(new FormatterHelper()));
  264. $dialog->setHelperSet($helperSet);
  265. $error = 'This is not a color!';
  266. $validator = function ($color) use ($error) {
  267. if (!\in_array($color, array('white', 'black'))) {
  268. throw new \InvalidArgumentException($error);
  269. }
  270. return $color;
  271. };
  272. $question = new Question('What color was the white horse of Henry IV?', 'white');
  273. $question->setValidator($validator);
  274. $question->setMaxAttempts(2);
  275. $dialog->setInputStream($this->getInputStream("\nblack\n"));
  276. $this->assertEquals('white', $dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
  277. $this->assertEquals('black', $dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
  278. $dialog->setInputStream($this->getInputStream("green\nyellow\norange\n"));
  279. try {
  280. $dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question);
  281. $this->fail();
  282. } catch (\InvalidArgumentException $e) {
  283. $this->assertEquals($error, $e->getMessage());
  284. }
  285. }
  286. /**
  287. * @dataProvider simpleAnswerProvider
  288. */
  289. public function testSelectChoiceFromSimpleChoices($providedAnswer, $expectedValue)
  290. {
  291. $possibleChoices = array(
  292. 'My environment 1',
  293. 'My environment 2',
  294. 'My environment 3',
  295. );
  296. $dialog = new QuestionHelper();
  297. $dialog->setInputStream($this->getInputStream($providedAnswer."\n"));
  298. $helperSet = new HelperSet(array(new FormatterHelper()));
  299. $dialog->setHelperSet($helperSet);
  300. $question = new ChoiceQuestion('Please select the environment to load', $possibleChoices);
  301. $question->setMaxAttempts(1);
  302. $answer = $dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question);
  303. $this->assertSame($expectedValue, $answer);
  304. }
  305. public function simpleAnswerProvider()
  306. {
  307. return array(
  308. array(0, 'My environment 1'),
  309. array(1, 'My environment 2'),
  310. array(2, 'My environment 3'),
  311. array('My environment 1', 'My environment 1'),
  312. array('My environment 2', 'My environment 2'),
  313. array('My environment 3', 'My environment 3'),
  314. );
  315. }
  316. /**
  317. * @dataProvider specialCharacterInMultipleChoice
  318. */
  319. public function testSpecialCharacterChoiceFromMultipleChoiceList($providedAnswer, $expectedValue)
  320. {
  321. $possibleChoices = array(
  322. '.',
  323. 'src',
  324. );
  325. $dialog = new QuestionHelper();
  326. $dialog->setInputStream($this->getInputStream($providedAnswer."\n"));
  327. $helperSet = new HelperSet(array(new FormatterHelper()));
  328. $dialog->setHelperSet($helperSet);
  329. $question = new ChoiceQuestion('Please select the directory', $possibleChoices);
  330. $question->setMaxAttempts(1);
  331. $question->setMultiselect(true);
  332. $answer = $dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question);
  333. $this->assertSame($expectedValue, $answer);
  334. }
  335. public function specialCharacterInMultipleChoice()
  336. {
  337. return array(
  338. array('.', array('.')),
  339. array('., src', array('.', 'src')),
  340. );
  341. }
  342. /**
  343. * @dataProvider mixedKeysChoiceListAnswerProvider
  344. */
  345. public function testChoiceFromChoicelistWithMixedKeys($providedAnswer, $expectedValue)
  346. {
  347. $possibleChoices = array(
  348. '0' => 'No environment',
  349. '1' => 'My environment 1',
  350. 'env_2' => 'My environment 2',
  351. 3 => 'My environment 3',
  352. );
  353. $dialog = new QuestionHelper();
  354. $dialog->setInputStream($this->getInputStream($providedAnswer."\n"));
  355. $helperSet = new HelperSet(array(new FormatterHelper()));
  356. $dialog->setHelperSet($helperSet);
  357. $question = new ChoiceQuestion('Please select the environment to load', $possibleChoices);
  358. $question->setMaxAttempts(1);
  359. $answer = $dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question);
  360. $this->assertSame($expectedValue, $answer);
  361. }
  362. public function mixedKeysChoiceListAnswerProvider()
  363. {
  364. return array(
  365. array('0', '0'),
  366. array('No environment', '0'),
  367. array('1', '1'),
  368. array('env_2', 'env_2'),
  369. array(3, '3'),
  370. array('My environment 1', '1'),
  371. );
  372. }
  373. /**
  374. * @dataProvider answerProvider
  375. */
  376. public function testSelectChoiceFromChoiceList($providedAnswer, $expectedValue)
  377. {
  378. $possibleChoices = array(
  379. 'env_1' => 'My environment 1',
  380. 'env_2' => 'My environment',
  381. 'env_3' => 'My environment',
  382. );
  383. $dialog = new QuestionHelper();
  384. $dialog->setInputStream($this->getInputStream($providedAnswer."\n"));
  385. $helperSet = new HelperSet(array(new FormatterHelper()));
  386. $dialog->setHelperSet($helperSet);
  387. $question = new ChoiceQuestion('Please select the environment to load', $possibleChoices);
  388. $question->setMaxAttempts(1);
  389. $answer = $dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question);
  390. $this->assertSame($expectedValue, $answer);
  391. }
  392. /**
  393. * @expectedException \InvalidArgumentException
  394. * @expectedExceptionMessage The provided answer is ambiguous. Value should be one of env_2 or env_3.
  395. */
  396. public function testAmbiguousChoiceFromChoicelist()
  397. {
  398. $possibleChoices = array(
  399. 'env_1' => 'My first environment',
  400. 'env_2' => 'My environment',
  401. 'env_3' => 'My environment',
  402. );
  403. $dialog = new QuestionHelper();
  404. $dialog->setInputStream($this->getInputStream("My environment\n"));
  405. $helperSet = new HelperSet(array(new FormatterHelper()));
  406. $dialog->setHelperSet($helperSet);
  407. $question = new ChoiceQuestion('Please select the environment to load', $possibleChoices);
  408. $question->setMaxAttempts(1);
  409. $dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question);
  410. }
  411. public function answerProvider()
  412. {
  413. return array(
  414. array('env_1', 'env_1'),
  415. array('env_2', 'env_2'),
  416. array('env_3', 'env_3'),
  417. array('My environment 1', 'env_1'),
  418. );
  419. }
  420. public function testNoInteraction()
  421. {
  422. $dialog = new QuestionHelper();
  423. $question = new Question('Do you have a job?', 'not yet');
  424. $this->assertEquals('not yet', $dialog->ask($this->createInputInterfaceMock(false), $this->createOutputInterface(), $question));
  425. }
  426. /**
  427. * @requires function mb_strwidth
  428. */
  429. public function testChoiceOutputFormattingQuestionForUtf8Keys()
  430. {
  431. $question = 'Lorem ipsum?';
  432. $possibleChoices = array(
  433. 'foo' => 'foo',
  434. 'żółw' => 'bar',
  435. 'łabądź' => 'baz',
  436. );
  437. $outputShown = array(
  438. $question,
  439. ' [<info>foo </info>] foo',
  440. ' [<info>żółw </info>] bar',
  441. ' [<info>łabądź</info>] baz',
  442. );
  443. $output = $this->getMockBuilder('\Symfony\Component\Console\Output\OutputInterface')->getMock();
  444. $output->method('getFormatter')->willReturn(new OutputFormatter());
  445. $dialog = new QuestionHelper();
  446. $dialog->setInputStream($this->getInputStream("\n"));
  447. $helperSet = new HelperSet(array(new FormatterHelper()));
  448. $dialog->setHelperSet($helperSet);
  449. $output->expects($this->once())->method('writeln')->with($this->equalTo($outputShown));
  450. $question = new ChoiceQuestion($question, $possibleChoices, 'foo');
  451. $dialog->ask($this->createInputInterfaceMock(), $output, $question);
  452. }
  453. /**
  454. * @expectedException \Symfony\Component\Console\Exception\RuntimeException
  455. * @expectedExceptionMessage Aborted
  456. */
  457. public function testAskThrowsExceptionOnMissingInput()
  458. {
  459. $dialog = new QuestionHelper();
  460. $dialog->setInputStream($this->getInputStream(''));
  461. $dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), new Question('What\'s your name?'));
  462. }
  463. /**
  464. * @expectedException \Symfony\Component\Console\Exception\RuntimeException
  465. * @expectedExceptionMessage Aborted
  466. */
  467. public function testAskThrowsExceptionOnMissingInputWithValidator()
  468. {
  469. $dialog = new QuestionHelper();
  470. $dialog->setInputStream($this->getInputStream(''));
  471. $question = new Question('What\'s your name?');
  472. $question->setValidator(function () {
  473. if (!$value) {
  474. throw new \Exception('A value is required.');
  475. }
  476. });
  477. $dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question);
  478. }
  479. /**
  480. * @expectedException \LogicException
  481. * @expectedExceptionMessage Choice question must have at least 1 choice available.
  482. */
  483. public function testEmptyChoices()
  484. {
  485. new ChoiceQuestion('Question', array(), 'irrelevant');
  486. }
  487. public function testTraversableAutocomplete()
  488. {
  489. if (!$this->hasSttyAvailable()) {
  490. $this->markTestSkipped('`stty` is required to test autocomplete functionality');
  491. }
  492. // Acm<NEWLINE>
  493. // Ac<BACKSPACE><BACKSPACE>s<TAB>Test<NEWLINE>
  494. // <NEWLINE>
  495. // <UP ARROW><UP ARROW><NEWLINE>
  496. // <UP ARROW><UP ARROW><UP ARROW><UP ARROW><UP ARROW><TAB>Test<NEWLINE>
  497. // <DOWN ARROW><NEWLINE>
  498. // S<BACKSPACE><BACKSPACE><DOWN ARROW><DOWN ARROW><NEWLINE>
  499. // F00<BACKSPACE><BACKSPACE>oo<TAB><NEWLINE>
  500. $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");
  501. $dialog = new QuestionHelper();
  502. $dialog->setInputStream($inputStream);
  503. $helperSet = new HelperSet(array(new FormatterHelper()));
  504. $dialog->setHelperSet($helperSet);
  505. $question = new Question('Please select a bundle', 'FrameworkBundle');
  506. $question->setAutocompleterValues(new AutocompleteValues(array('irrelevant' => 'AcmeDemoBundle', 'AsseticBundle', 'SecurityBundle', 'FooBundle')));
  507. $this->assertEquals('AcmeDemoBundle', $dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
  508. $this->assertEquals('AsseticBundleTest', $dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
  509. $this->assertEquals('FrameworkBundle', $dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
  510. $this->assertEquals('SecurityBundle', $dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
  511. $this->assertEquals('FooBundleTest', $dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
  512. $this->assertEquals('AcmeDemoBundle', $dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
  513. $this->assertEquals('AsseticBundle', $dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
  514. $this->assertEquals('FooBundle', $dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
  515. }
  516. protected function getInputStream($input)
  517. {
  518. $stream = fopen('php://memory', 'r+', false);
  519. fwrite($stream, $input);
  520. rewind($stream);
  521. return $stream;
  522. }
  523. protected function createOutputInterface()
  524. {
  525. return new StreamOutput(fopen('php://memory', 'r+', false));
  526. }
  527. protected function createInputInterfaceMock($interactive = true)
  528. {
  529. $mock = $this->getMockBuilder('Symfony\Component\Console\Input\InputInterface')->getMock();
  530. $mock->expects($this->any())
  531. ->method('isInteractive')
  532. ->will($this->returnValue($interactive));
  533. return $mock;
  534. }
  535. private function hasSttyAvailable()
  536. {
  537. exec('stty 2>&1', $output, $exitcode);
  538. return 0 === $exitcode;
  539. }
  540. }
  541. class AutocompleteValues implements \IteratorAggregate
  542. {
  543. private $values;
  544. public function __construct(array $values)
  545. {
  546. $this->values = $values;
  547. }
  548. public function getIterator()
  549. {
  550. return new \ArrayIterator($this->values);
  551. }
  552. }