QuestionHelperTest.php 25KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609
  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. $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. * @expectedException \Symfony\Component\Console\Exception\RuntimeException
  400. * @expectedExceptionMessage Aborted
  401. */
  402. public function testAskThrowsExceptionOnMissingInput()
  403. {
  404. $dialog = new QuestionHelper();
  405. $dialog->ask($this->createStreamableInputInterfaceMock($this->getInputStream('')), $this->createOutputInterface(), new Question('What\'s your name?'));
  406. }
  407. /**
  408. * @expectedException \Symfony\Component\Console\Exception\RuntimeException
  409. * @expectedExceptionMessage Aborted
  410. */
  411. public function testAskThrowsExceptionOnMissingInputWithValidator()
  412. {
  413. $dialog = new QuestionHelper();
  414. $question = new Question('What\'s your name?');
  415. $question->setValidator(function () {
  416. if (!$value) {
  417. throw new \Exception('A value is required.');
  418. }
  419. });
  420. $dialog->ask($this->createStreamableInputInterfaceMock($this->getInputStream('')), $this->createOutputInterface(), $question);
  421. }
  422. /**
  423. * @expectedException \LogicException
  424. * @expectedExceptionMessage Choice question must have at least 1 choice available.
  425. */
  426. public function testEmptyChoices()
  427. {
  428. new ChoiceQuestion('Question', array(), 'irrelevant');
  429. }
  430. public function testTraversableAutocomplete()
  431. {
  432. if (!$this->hasSttyAvailable()) {
  433. $this->markTestSkipped('`stty` is required to test autocomplete functionality');
  434. }
  435. // Acm<NEWLINE>
  436. // Ac<BACKSPACE><BACKSPACE>s<TAB>Test<NEWLINE>
  437. // <NEWLINE>
  438. // <UP ARROW><UP ARROW><NEWLINE>
  439. // <UP ARROW><UP ARROW><UP ARROW><UP ARROW><UP ARROW><TAB>Test<NEWLINE>
  440. // <DOWN ARROW><NEWLINE>
  441. // S<BACKSPACE><BACKSPACE><DOWN ARROW><DOWN ARROW><NEWLINE>
  442. // F00<BACKSPACE><BACKSPACE>oo<TAB><NEWLINE>
  443. $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");
  444. $dialog = new QuestionHelper();
  445. $helperSet = new HelperSet(array(new FormatterHelper()));
  446. $dialog->setHelperSet($helperSet);
  447. $question = new Question('Please select a bundle', 'FrameworkBundle');
  448. $question->setAutocompleterValues(new AutocompleteValues(array('irrelevant' => 'AcmeDemoBundle', 'AsseticBundle', 'SecurityBundle', 'FooBundle')));
  449. $this->assertEquals('AcmeDemoBundle', $dialog->ask($this->createStreamableInputInterfaceMock($inputStream), $this->createOutputInterface(), $question));
  450. $this->assertEquals('AsseticBundleTest', $dialog->ask($this->createStreamableInputInterfaceMock($inputStream), $this->createOutputInterface(), $question));
  451. $this->assertEquals('FrameworkBundle', $dialog->ask($this->createStreamableInputInterfaceMock($inputStream), $this->createOutputInterface(), $question));
  452. $this->assertEquals('SecurityBundle', $dialog->ask($this->createStreamableInputInterfaceMock($inputStream), $this->createOutputInterface(), $question));
  453. $this->assertEquals('FooBundleTest', $dialog->ask($this->createStreamableInputInterfaceMock($inputStream), $this->createOutputInterface(), $question));
  454. $this->assertEquals('AcmeDemoBundle', $dialog->ask($this->createStreamableInputInterfaceMock($inputStream), $this->createOutputInterface(), $question));
  455. $this->assertEquals('AsseticBundle', $dialog->ask($this->createStreamableInputInterfaceMock($inputStream), $this->createOutputInterface(), $question));
  456. $this->assertEquals('FooBundle', $dialog->ask($this->createStreamableInputInterfaceMock($inputStream), $this->createOutputInterface(), $question));
  457. }
  458. protected function getInputStream($input)
  459. {
  460. $stream = fopen('php://memory', 'r+', false);
  461. fwrite($stream, $input);
  462. rewind($stream);
  463. return $stream;
  464. }
  465. protected function createOutputInterface()
  466. {
  467. return new StreamOutput(fopen('php://memory', 'r+', false));
  468. }
  469. protected function createInputInterfaceMock($interactive = true)
  470. {
  471. $mock = $this->getMockBuilder('Symfony\Component\Console\Input\InputInterface')->getMock();
  472. $mock->expects($this->any())
  473. ->method('isInteractive')
  474. ->will($this->returnValue($interactive));
  475. return $mock;
  476. }
  477. private function hasSttyAvailable()
  478. {
  479. exec('stty 2>&1', $output, $exitcode);
  480. return 0 === $exitcode;
  481. }
  482. }
  483. class AutocompleteValues implements \IteratorAggregate
  484. {
  485. private $values;
  486. public function __construct(array $values)
  487. {
  488. $this->values = $values;
  489. }
  490. public function getIterator()
  491. {
  492. return new \ArrayIterator($this->values);
  493. }
  494. }