PageRenderTime 51ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/symfony/console/Symfony/Component/Console/Tests/Helper/QuestionHelperTest.php

https://gitlab.com/xolotsoft/pumasruiz
PHP | 238 lines | 183 code | 35 blank | 20 comment | 2 complexity | 5bf6684baf68b469c5f5a6e3345b8458 MD5 | raw file
  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\Helper\QuestionHelper;
  12. use Symfony\Component\Console\Helper\HelperSet;
  13. use Symfony\Component\Console\Helper\FormatterHelper;
  14. use Symfony\Component\Console\Output\StreamOutput;
  15. use Symfony\Component\Console\Question\ChoiceQuestion;
  16. use Symfony\Component\Console\Question\ConfirmationQuestion;
  17. use Symfony\Component\Console\Question\Question;
  18. /**
  19. * @group tty
  20. */
  21. class QuestionHelperTest extends \PHPUnit_Framework_TestCase
  22. {
  23. public function testAskChoice()
  24. {
  25. $questionHelper = new QuestionHelper();
  26. $helperSet = new HelperSet(array(new FormatterHelper()));
  27. $questionHelper->setHelperSet($helperSet);
  28. $heroes = array('Superman', 'Batman', 'Spiderman');
  29. $questionHelper->setInputStream($this->getInputStream("\n1\n 1 \nFabien\n1\nFabien\n1\n0,2\n 0 , 2 \n\n\n"));
  30. $question = new ChoiceQuestion('What is your favorite superhero?', $heroes, '2');
  31. // first answer is an empty answer, we're supposed to receive the default value
  32. $this->assertEquals('Spiderman', $questionHelper->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
  33. $question = new ChoiceQuestion('What is your favorite superhero?', $heroes);
  34. $this->assertEquals('Batman', $questionHelper->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
  35. $this->assertEquals('Batman', $questionHelper->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
  36. $question = new ChoiceQuestion('What is your favorite superhero?', $heroes);
  37. $question->setErrorMessage('Input "%s" is not a superhero!');
  38. $this->assertEquals('Batman', $questionHelper->ask($this->createInputInterfaceMock(), $output = $this->createOutputInterface(), $question));
  39. rewind($output->getStream());
  40. $stream = stream_get_contents($output->getStream());
  41. $this->assertContains('Input "Fabien" is not a superhero!', $stream);
  42. try {
  43. $question = new ChoiceQuestion('What is your favorite superhero?', $heroes, '1');
  44. $question->setMaxAttempts(1);
  45. $questionHelper->ask($this->createInputInterfaceMock(), $output = $this->createOutputInterface(), $question);
  46. $this->fail();
  47. } catch (\InvalidArgumentException $e) {
  48. $this->assertEquals('Value "Fabien" is invalid', $e->getMessage());
  49. }
  50. $question = new ChoiceQuestion('What is your favorite superhero?', $heroes, null);
  51. $question->setMultiselect(true);
  52. $this->assertEquals(array('Batman'), $questionHelper->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
  53. $this->assertEquals(array('Superman', 'Spiderman'), $questionHelper->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
  54. $this->assertEquals(array('Superman', 'Spiderman'), $questionHelper->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
  55. $question = new ChoiceQuestion('What is your favorite superhero?', $heroes, '0,1');
  56. $question->setMultiselect(true);
  57. $this->assertEquals(array('Superman', 'Batman'), $questionHelper->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
  58. $question = new ChoiceQuestion('What is your favorite superhero?', $heroes, ' 0 , 1 ');
  59. $question->setMultiselect(true);
  60. $this->assertEquals(array('Superman', 'Batman'), $questionHelper->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
  61. }
  62. public function testAsk()
  63. {
  64. $dialog = new QuestionHelper();
  65. $dialog->setInputStream($this->getInputStream("\n8AM\n"));
  66. $question = new Question('What time is it?', '2PM');
  67. $this->assertEquals('2PM', $dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
  68. $question = new Question('What time is it?', '2PM');
  69. $this->assertEquals('8AM', $dialog->ask($this->createInputInterfaceMock(), $output = $this->createOutputInterface(), $question));
  70. rewind($output->getStream());
  71. $this->assertEquals('What time is it?', stream_get_contents($output->getStream()));
  72. }
  73. public function testAskWithAutocomplete()
  74. {
  75. if (!$this->hasSttyAvailable()) {
  76. $this->markTestSkipped('`stty` is required to test autocomplete functionality');
  77. }
  78. // Acm<NEWLINE>
  79. // Ac<BACKSPACE><BACKSPACE>s<TAB>Test<NEWLINE>
  80. // <NEWLINE>
  81. // <UP ARROW><UP ARROW><NEWLINE>
  82. // <UP ARROW><UP ARROW><UP ARROW><UP ARROW><UP ARROW><TAB>Test<NEWLINE>
  83. // <DOWN ARROW><NEWLINE>
  84. // S<BACKSPACE><BACKSPACE><DOWN ARROW><DOWN ARROW><NEWLINE>
  85. // F00<BACKSPACE><BACKSPACE>oo<TAB><NEWLINE>
  86. $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");
  87. $dialog = new QuestionHelper();
  88. $dialog->setInputStream($inputStream);
  89. $helperSet = new HelperSet(array(new FormatterHelper()));
  90. $dialog->setHelperSet($helperSet);
  91. $question = new Question('Please select a bundle', 'FrameworkBundle');
  92. $question->setAutocompleterValues(array('AcmeDemoBundle', 'AsseticBundle', 'SecurityBundle', 'FooBundle'));
  93. $this->assertEquals('AcmeDemoBundle', $dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
  94. $this->assertEquals('AsseticBundleTest', $dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
  95. $this->assertEquals('FrameworkBundle', $dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
  96. $this->assertEquals('SecurityBundle', $dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
  97. $this->assertEquals('FooBundleTest', $dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
  98. $this->assertEquals('AcmeDemoBundle', $dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
  99. $this->assertEquals('AsseticBundle', $dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
  100. $this->assertEquals('FooBundle', $dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
  101. }
  102. public function testAskHiddenResponse()
  103. {
  104. if ('\\' === DIRECTORY_SEPARATOR) {
  105. $this->markTestSkipped('This test is not supported on Windows');
  106. }
  107. $dialog = new QuestionHelper();
  108. $dialog->setInputStream($this->getInputStream("8AM\n"));
  109. $question = new Question('What time is it?');
  110. $question->setHidden(true);
  111. $this->assertEquals('8AM', $dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
  112. }
  113. public function testAskConfirmation()
  114. {
  115. $dialog = new QuestionHelper();
  116. $dialog->setInputStream($this->getInputStream("\n\n"));
  117. $question = new ConfirmationQuestion('Do you like French fries?');
  118. $this->assertTrue($dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
  119. $question = new ConfirmationQuestion('Do you like French fries?', false);
  120. $this->assertFalse($dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
  121. $dialog->setInputStream($this->getInputStream("y\nyes\n"));
  122. $question = new ConfirmationQuestion('Do you like French fries?', false);
  123. $this->assertTrue($dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
  124. $question = new ConfirmationQuestion('Do you like French fries?', false);
  125. $this->assertTrue($dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
  126. $dialog->setInputStream($this->getInputStream("n\nno\n"));
  127. $question = new ConfirmationQuestion('Do you like French fries?', true);
  128. $this->assertFalse($dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
  129. $question = new ConfirmationQuestion('Do you like French fries?', true);
  130. $this->assertFalse($dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
  131. }
  132. public function testAskAndValidate()
  133. {
  134. $dialog = new QuestionHelper();
  135. $helperSet = new HelperSet(array(new FormatterHelper()));
  136. $dialog->setHelperSet($helperSet);
  137. $error = 'This is not a color!';
  138. $validator = function ($color) use ($error) {
  139. if (!in_array($color, array('white', 'black'))) {
  140. throw new \InvalidArgumentException($error);
  141. }
  142. return $color;
  143. };
  144. $question = new Question('What color was the white horse of Henry IV?', 'white');
  145. $question->setValidator($validator);
  146. $question->setMaxAttempts(2);
  147. $dialog->setInputStream($this->getInputStream("\nblack\n"));
  148. $this->assertEquals('white', $dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
  149. $this->assertEquals('black', $dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
  150. $dialog->setInputStream($this->getInputStream("green\nyellow\norange\n"));
  151. try {
  152. $this->assertEquals('white', $dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
  153. $this->fail();
  154. } catch (\InvalidArgumentException $e) {
  155. $this->assertEquals($error, $e->getMessage());
  156. }
  157. }
  158. public function testNoInteraction()
  159. {
  160. $dialog = new QuestionHelper();
  161. $question = new Question('Do you have a job?', 'not yet');
  162. $this->assertEquals('not yet', $dialog->ask($this->createInputInterfaceMock(false), $this->createOutputInterface(), $question));
  163. }
  164. protected function getInputStream($input)
  165. {
  166. $stream = fopen('php://memory', 'r+', false);
  167. fputs($stream, $input);
  168. rewind($stream);
  169. return $stream;
  170. }
  171. protected function createOutputInterface()
  172. {
  173. return new StreamOutput(fopen('php://memory', 'r+', false));
  174. }
  175. protected function createInputInterfaceMock($interactive = true)
  176. {
  177. $mock = $this->getMock('Symfony\Component\Console\Input\InputInterface');
  178. $mock->expects($this->any())
  179. ->method('isInteractive')
  180. ->will($this->returnValue($interactive));
  181. return $mock;
  182. }
  183. private function hasSttyAvailable()
  184. {
  185. exec('stty 2>&1', $output, $exitcode);
  186. return $exitcode === 0;
  187. }
  188. }