SymfonyQuestionHelperTest.php 7.3 KB

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