SymfonyQuestionHelperTest.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. namespace Symfony\Component\Console\Tests\Helper;
  3. use PHPUnit\Framework\TestCase;
  4. use Symfony\Component\Console\Helper\FormatterHelper;
  5. use Symfony\Component\Console\Helper\HelperSet;
  6. use Symfony\Component\Console\Helper\SymfonyQuestionHelper;
  7. use Symfony\Component\Console\Output\StreamOutput;
  8. use Symfony\Component\Console\Question\Question;
  9. use Symfony\Component\Console\Question\ChoiceQuestion;
  10. /**
  11. * @group tty
  12. */
  13. class SymfonyQuestionHelperTest extends TestCase
  14. {
  15. public function testAskChoice()
  16. {
  17. $questionHelper = new SymfonyQuestionHelper();
  18. $helperSet = new HelperSet(array(new FormatterHelper()));
  19. $questionHelper->setHelperSet($helperSet);
  20. $heroes = array('Superman', 'Batman', 'Spiderman');
  21. $questionHelper->setInputStream($this->getInputStream("\n1\n 1 \nFabien\n1\nFabien\n1\n0,2\n 0 , 2 \n\n\n"));
  22. $question = new ChoiceQuestion('What is your favorite superhero?', $heroes, '2');
  23. $question->setMaxAttempts(1);
  24. // first answer is an empty answer, we're supposed to receive the default value
  25. $this->assertEquals('Spiderman', $questionHelper->ask($this->createInputInterfaceMock(), $output = $this->createOutputInterface(), $question));
  26. $this->assertOutputContains('What is your favorite superhero? [Spiderman]', $output);
  27. $question = new ChoiceQuestion('What is your favorite superhero?', $heroes);
  28. $question->setMaxAttempts(1);
  29. $this->assertEquals('Batman', $questionHelper->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
  30. $this->assertEquals('Batman', $questionHelper->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
  31. $question = new ChoiceQuestion('What is your favorite superhero?', $heroes);
  32. $question->setErrorMessage('Input "%s" is not a superhero!');
  33. $question->setMaxAttempts(2);
  34. $this->assertEquals('Batman', $questionHelper->ask($this->createInputInterfaceMock(), $output = $this->createOutputInterface(), $question));
  35. $this->assertOutputContains('Input "Fabien" is not a superhero!', $output);
  36. try {
  37. $question = new ChoiceQuestion('What is your favorite superhero?', $heroes, '1');
  38. $question->setMaxAttempts(1);
  39. $questionHelper->ask($this->createInputInterfaceMock(), $output = $this->createOutputInterface(), $question);
  40. $this->fail();
  41. } catch (\InvalidArgumentException $e) {
  42. $this->assertEquals('Value "Fabien" is invalid', $e->getMessage());
  43. }
  44. $question = new ChoiceQuestion('What is your favorite superhero?', $heroes, null);
  45. $question->setMaxAttempts(1);
  46. $question->setMultiselect(true);
  47. $this->assertEquals(array('Batman'), $questionHelper->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
  48. $this->assertEquals(array('Superman', 'Spiderman'), $questionHelper->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
  49. $this->assertEquals(array('Superman', 'Spiderman'), $questionHelper->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
  50. $question = new ChoiceQuestion('What is your favorite superhero?', $heroes, '0,1');
  51. $question->setMaxAttempts(1);
  52. $question->setMultiselect(true);
  53. $this->assertEquals(array('Superman', 'Batman'), $questionHelper->ask($this->createInputInterfaceMock(), $output = $this->createOutputInterface(), $question));
  54. $this->assertOutputContains('What is your favorite superhero? [Superman, Batman]', $output);
  55. $question = new ChoiceQuestion('What is your favorite superhero?', $heroes, ' 0 , 1 ');
  56. $question->setMaxAttempts(1);
  57. $question->setMultiselect(true);
  58. $this->assertEquals(array('Superman', 'Batman'), $questionHelper->ask($this->createInputInterfaceMock(), $output = $this->createOutputInterface(), $question));
  59. $this->assertOutputContains('What is your favorite superhero? [Superman, Batman]', $output);
  60. }
  61. public function testAskReturnsNullIfValidatorAllowsIt()
  62. {
  63. $questionHelper = new SymfonyQuestionHelper();
  64. $questionHelper->setInputStream($this->getInputStream("\n"));
  65. $question = new Question('What is your favorite superhero?');
  66. $question->setValidator(function ($value) { return $value; });
  67. $this->assertNull($questionHelper->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
  68. }
  69. public function testAskEscapeDefaultValue()
  70. {
  71. $helper = new SymfonyQuestionHelper();
  72. $helper->setInputStream($this->getInputStream('\\'));
  73. $helper->ask($this->createInputInterfaceMock(), $output = $this->createOutputInterface(), new Question('Can I have a backslash?', '\\'));
  74. $this->assertOutputContains('Can I have a backslash? [\]', $output);
  75. }
  76. public function testAskEscapeAndFormatLabel()
  77. {
  78. $helper = new SymfonyQuestionHelper();
  79. $helper->setInputStream($this->getInputStream('Foo\\Bar'));
  80. $helper->ask($this->createInputInterfaceMock(), $output = $this->createOutputInterface(), new Question('Do you want to use Foo\\Bar <comment>or</comment> Foo\\Baz\\?', 'Foo\\Baz'));
  81. $this->assertOutputContains('Do you want to use Foo\\Bar or Foo\\Baz\\? [Foo\\Baz]:', $output);
  82. }
  83. public function testLabelTrailingBackslash()
  84. {
  85. $helper = new SymfonyQuestionHelper();
  86. $helper->setInputStream($this->getInputStream('sure'));
  87. $helper->ask($this->createInputInterfaceMock(), $output = $this->createOutputInterface(), new Question('Question with a trailing \\'));
  88. $this->assertOutputContains('Question with a trailing \\', $output);
  89. }
  90. /**
  91. * @expectedException \Symfony\Component\Console\Exception\RuntimeException
  92. * @expectedExceptionMessage Aborted
  93. */
  94. public function testAskThrowsExceptionOnMissingInput()
  95. {
  96. $dialog = new SymfonyQuestionHelper();
  97. $dialog->setInputStream($this->getInputStream(''));
  98. $dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), new Question('What\'s your name?'));
  99. }
  100. protected function getInputStream($input)
  101. {
  102. $stream = fopen('php://memory', 'r+', false);
  103. fwrite($stream, $input);
  104. rewind($stream);
  105. return $stream;
  106. }
  107. protected function createOutputInterface()
  108. {
  109. $output = new StreamOutput(fopen('php://memory', 'r+', false));
  110. $output->setDecorated(false);
  111. return $output;
  112. }
  113. protected function createInputInterfaceMock($interactive = true)
  114. {
  115. $mock = $this->getMockBuilder('Symfony\Component\Console\Input\InputInterface')->getMock();
  116. $mock->expects($this->any())
  117. ->method('isInteractive')
  118. ->will($this->returnValue($interactive));
  119. return $mock;
  120. }
  121. private function assertOutputContains($expected, StreamOutput $output)
  122. {
  123. rewind($output->getStream());
  124. $stream = stream_get_contents($output->getStream());
  125. $this->assertContains($expected, $stream);
  126. }
  127. }