SymfonyQuestionHelperTest.php 7.9 KB

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