SymfonyQuestionHelperTest.php 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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\Exception\RuntimeException;
  12. use Symfony\Component\Console\Helper\FormatterHelper;
  13. use Symfony\Component\Console\Helper\HelperSet;
  14. use Symfony\Component\Console\Helper\SymfonyQuestionHelper;
  15. use Symfony\Component\Console\Input\InputInterface;
  16. use Symfony\Component\Console\Output\StreamOutput;
  17. use Symfony\Component\Console\Question\ChoiceQuestion;
  18. use Symfony\Component\Console\Question\Question;
  19. /**
  20. * @group tty
  21. */
  22. class SymfonyQuestionHelperTest extends AbstractQuestionHelperTestCase
  23. {
  24. public function testAskChoice()
  25. {
  26. $questionHelper = new SymfonyQuestionHelper();
  27. $helperSet = new HelperSet([new FormatterHelper()]);
  28. $questionHelper->setHelperSet($helperSet);
  29. $heroes = ['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), $output = $this->createOutputInterface(), $question));
  35. $this->assertOutputContains('What is your favorite superhero? [Spiderman]', $output);
  36. $question = new ChoiceQuestion('What is your favorite superhero?', $heroes);
  37. $question->setMaxAttempts(1);
  38. $this->assertEquals('Batman', $questionHelper->ask($this->createStreamableInputInterfaceMock($inputStream), $this->createOutputInterface(), $question));
  39. $this->assertEquals('Batman', $questionHelper->ask($this->createStreamableInputInterfaceMock($inputStream), $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->createStreamableInputInterfaceMock($inputStream), $output = $this->createOutputInterface(), $question));
  44. $this->assertOutputContains('Input "Fabien" is not a superhero!', $output);
  45. try {
  46. $question = new ChoiceQuestion('What is your favorite superhero?', $heroes, '1');
  47. $question->setMaxAttempts(1);
  48. $questionHelper->ask($this->createStreamableInputInterfaceMock($inputStream), $output = $this->createOutputInterface(), $question);
  49. $this->fail();
  50. } catch (\InvalidArgumentException $e) {
  51. $this->assertEquals('Value "Fabien" is invalid', $e->getMessage());
  52. }
  53. $question = new ChoiceQuestion('What is your favorite superhero?', $heroes, null);
  54. $question->setMaxAttempts(1);
  55. $question->setMultiselect(true);
  56. $this->assertEquals(['Batman'], $questionHelper->ask($this->createStreamableInputInterfaceMock($inputStream), $this->createOutputInterface(), $question));
  57. $this->assertEquals(['Superman', 'Spiderman'], $questionHelper->ask($this->createStreamableInputInterfaceMock($inputStream), $this->createOutputInterface(), $question));
  58. $this->assertEquals(['Superman', 'Spiderman'], $questionHelper->ask($this->createStreamableInputInterfaceMock($inputStream), $this->createOutputInterface(), $question));
  59. $question = new ChoiceQuestion('What is your favorite superhero?', $heroes, '0,1');
  60. $question->setMaxAttempts(1);
  61. $question->setMultiselect(true);
  62. $this->assertEquals(['Superman', 'Batman'], $questionHelper->ask($this->createStreamableInputInterfaceMock($inputStream), $output = $this->createOutputInterface(), $question));
  63. $this->assertOutputContains('What is your favorite superhero? [Superman, Batman]', $output);
  64. $question = new ChoiceQuestion('What is your favorite superhero?', $heroes, ' 0 , 1 ');
  65. $question->setMaxAttempts(1);
  66. $question->setMultiselect(true);
  67. $this->assertEquals(['Superman', 'Batman'], $questionHelper->ask($this->createStreamableInputInterfaceMock($inputStream), $output = $this->createOutputInterface(), $question));
  68. $this->assertOutputContains('What is your favorite superhero? [Superman, Batman]', $output);
  69. }
  70. public function testAskChoiceWithChoiceValueAsDefault()
  71. {
  72. $questionHelper = new SymfonyQuestionHelper();
  73. $helperSet = new HelperSet([new FormatterHelper()]);
  74. $questionHelper->setHelperSet($helperSet);
  75. $question = new ChoiceQuestion('What is your favorite superhero?', ['Superman', 'Batman', 'Spiderman'], 'Batman');
  76. $question->setMaxAttempts(1);
  77. $this->assertSame('Batman', $questionHelper->ask($this->createStreamableInputInterfaceMock($this->getInputStream("Batman\n")), $output = $this->createOutputInterface(), $question));
  78. $this->assertOutputContains('What is your favorite superhero? [Batman]', $output);
  79. }
  80. public function testAskReturnsNullIfValidatorAllowsIt()
  81. {
  82. $questionHelper = new SymfonyQuestionHelper();
  83. $question = new Question('What is your favorite superhero?');
  84. $question->setValidator(function ($value) { return $value; });
  85. $input = $this->createStreamableInputInterfaceMock($this->getInputStream("\n"));
  86. $this->assertNull($questionHelper->ask($input, $this->createOutputInterface(), $question));
  87. }
  88. public function testAskEscapeDefaultValue()
  89. {
  90. $helper = new SymfonyQuestionHelper();
  91. $input = $this->createStreamableInputInterfaceMock($this->getInputStream('\\'));
  92. $helper->ask($input, $output = $this->createOutputInterface(), new Question('Can I have a backslash?', '\\'));
  93. $this->assertOutputContains('Can I have a backslash? [\]', $output);
  94. }
  95. public function testAskEscapeAndFormatLabel()
  96. {
  97. $helper = new SymfonyQuestionHelper();
  98. $input = $this->createStreamableInputInterfaceMock($this->getInputStream('Foo\\Bar'));
  99. $helper->ask($input, $output = $this->createOutputInterface(), new Question('Do you want to use Foo\\Bar <comment>or</comment> Foo\\Baz\\?', 'Foo\\Baz'));
  100. $this->assertOutputContains('Do you want to use Foo\\Bar or Foo\\Baz\\? [Foo\\Baz]:', $output);
  101. }
  102. public function testLabelTrailingBackslash()
  103. {
  104. $helper = new SymfonyQuestionHelper();
  105. $input = $this->createStreamableInputInterfaceMock($this->getInputStream('sure'));
  106. $helper->ask($input, $output = $this->createOutputInterface(), new Question('Question with a trailing \\'));
  107. $this->assertOutputContains('Question with a trailing \\', $output);
  108. }
  109. public function testAskThrowsExceptionOnMissingInput()
  110. {
  111. $this->expectException(RuntimeException::class);
  112. $this->expectExceptionMessage('Aborted.');
  113. $dialog = new SymfonyQuestionHelper();
  114. $dialog->ask($this->createStreamableInputInterfaceMock($this->getInputStream('')), $this->createOutputInterface(), new Question('What\'s your name?'));
  115. }
  116. public function testChoiceQuestionPadding()
  117. {
  118. $choiceQuestion = new ChoiceQuestion('qqq', [
  119. 'foo' => 'foo',
  120. 'żółw' => 'bar',
  121. 'łabądź' => 'baz',
  122. ]);
  123. (new SymfonyQuestionHelper())->ask(
  124. $this->createStreamableInputInterfaceMock($this->getInputStream("foo\n")),
  125. $output = $this->createOutputInterface(),
  126. $choiceQuestion
  127. );
  128. $this->assertOutputContains(<<<EOT
  129. qqq:
  130. [foo ] foo
  131. [żółw ] bar
  132. [łabądź] baz
  133. >
  134. EOT
  135. , $output, true);
  136. }
  137. public function testChoiceQuestionCustomPrompt()
  138. {
  139. $choiceQuestion = new ChoiceQuestion('qqq', ['foo']);
  140. $choiceQuestion->setPrompt(' >ccc> ');
  141. (new SymfonyQuestionHelper())->ask(
  142. $this->createStreamableInputInterfaceMock($this->getInputStream("foo\n")),
  143. $output = $this->createOutputInterface(),
  144. $choiceQuestion
  145. );
  146. $this->assertOutputContains(<<<EOT
  147. qqq:
  148. [0] foo
  149. >ccc>
  150. EOT
  151. , $output, true);
  152. }
  153. protected function getInputStream($input)
  154. {
  155. $stream = fopen('php://memory', 'r+', false);
  156. fwrite($stream, $input);
  157. rewind($stream);
  158. return $stream;
  159. }
  160. protected function createOutputInterface()
  161. {
  162. $output = new StreamOutput(fopen('php://memory', 'r+', false));
  163. $output->setDecorated(false);
  164. return $output;
  165. }
  166. protected function createInputInterfaceMock($interactive = true)
  167. {
  168. $mock = $this->createMock(InputInterface::class);
  169. $mock->expects($this->any())
  170. ->method('isInteractive')
  171. ->willReturn($interactive);
  172. return $mock;
  173. }
  174. private function assertOutputContains($expected, StreamOutput $output, $normalize = false)
  175. {
  176. rewind($output->getStream());
  177. $stream = stream_get_contents($output->getStream());
  178. if ($normalize) {
  179. $stream = str_replace(\PHP_EOL, "\n", $stream);
  180. }
  181. $this->assertStringContainsString($expected, $stream);
  182. }
  183. public function testAskMultilineQuestionIncludesHelpText()
  184. {
  185. $expected = 'Write an essay (press Ctrl+D to continue)';
  186. if ('Windows' === \PHP_OS_FAMILY) {
  187. $expected = 'Write an essay (press Ctrl+Z then Enter to continue)';
  188. }
  189. $question = new Question('Write an essay');
  190. $question->setMultiline(true);
  191. $helper = new SymfonyQuestionHelper();
  192. $input = $this->createStreamableInputInterfaceMock($this->getInputStream('\\'));
  193. $helper->ask($input, $output = $this->createOutputInterface(), $question);
  194. $this->assertOutputContains($expected, $output);
  195. }
  196. }