SymfonyQuestionHelperTest.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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\ChoiceQuestion;
  9. use Symfony\Component\Console\Question\Question;
  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 testAskChoiceWithChoiceValueAsDefault()
  62. {
  63. $questionHelper = new SymfonyQuestionHelper();
  64. $helperSet = new HelperSet(array(new FormatterHelper()));
  65. $questionHelper->setHelperSet($helperSet);
  66. $questionHelper->setInputStream($this->getInputStream("Batman\n"));
  67. $question = new ChoiceQuestion('What is your favorite superhero?', array('Superman', 'Batman', 'Spiderman'), 'Batman');
  68. $question->setMaxAttempts(1);
  69. $this->assertSame('Batman', $questionHelper->ask($this->createInputInterfaceMock(), $output = $this->createOutputInterface(), $question));
  70. $this->assertOutputContains('What is your favorite superhero? [Batman]', $output);
  71. }
  72. public function testAskReturnsNullIfValidatorAllowsIt()
  73. {
  74. $questionHelper = new SymfonyQuestionHelper();
  75. $questionHelper->setInputStream($this->getInputStream("\n"));
  76. $question = new Question('What is your favorite superhero?');
  77. $question->setValidator(function ($value) { return $value; });
  78. $this->assertNull($questionHelper->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
  79. }
  80. public function testAskEscapeDefaultValue()
  81. {
  82. $helper = new SymfonyQuestionHelper();
  83. $helper->setInputStream($this->getInputStream('\\'));
  84. $helper->ask($this->createInputInterfaceMock(), $output = $this->createOutputInterface(), new Question('Can I have a backslash?', '\\'));
  85. $this->assertOutputContains('Can I have a backslash? [\]', $output);
  86. }
  87. public function testAskEscapeAndFormatLabel()
  88. {
  89. $helper = new SymfonyQuestionHelper();
  90. $helper->setInputStream($this->getInputStream('Foo\\Bar'));
  91. $helper->ask($this->createInputInterfaceMock(), $output = $this->createOutputInterface(), new Question('Do you want to use Foo\\Bar <comment>or</comment> Foo\\Baz\\?', 'Foo\\Baz'));
  92. $this->assertOutputContains('Do you want to use Foo\\Bar or Foo\\Baz\\? [Foo\\Baz]:', $output);
  93. }
  94. public function testLabelTrailingBackslash()
  95. {
  96. $helper = new SymfonyQuestionHelper();
  97. $helper->setInputStream($this->getInputStream('sure'));
  98. $helper->ask($this->createInputInterfaceMock(), $output = $this->createOutputInterface(), new Question('Question with a trailing \\'));
  99. $this->assertOutputContains('Question with a trailing \\', $output);
  100. }
  101. /**
  102. * @expectedException \Symfony\Component\Console\Exception\RuntimeException
  103. * @expectedExceptionMessage Aborted
  104. */
  105. public function testAskThrowsExceptionOnMissingInput()
  106. {
  107. $dialog = new SymfonyQuestionHelper();
  108. $dialog->setInputStream($this->getInputStream(''));
  109. $dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), new Question('What\'s your name?'));
  110. }
  111. protected function getInputStream($input)
  112. {
  113. $stream = fopen('php://memory', 'r+', false);
  114. fwrite($stream, $input);
  115. rewind($stream);
  116. return $stream;
  117. }
  118. protected function createOutputInterface()
  119. {
  120. $output = new StreamOutput(fopen('php://memory', 'r+', false));
  121. $output->setDecorated(false);
  122. return $output;
  123. }
  124. protected function createInputInterfaceMock($interactive = true)
  125. {
  126. $mock = $this->getMockBuilder('Symfony\Component\Console\Input\InputInterface')->getMock();
  127. $mock->expects($this->any())
  128. ->method('isInteractive')
  129. ->will($this->returnValue($interactive));
  130. return $mock;
  131. }
  132. private function assertOutputContains($expected, StreamOutput $output)
  133. {
  134. rewind($output->getStream());
  135. $stream = stream_get_contents($output->getStream());
  136. $this->assertContains($expected, $stream);
  137. }
  138. }