You are here

public function QuestionHelperTest::testAskChoice in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 vendor/symfony/console/Tests/Helper/QuestionHelperTest.php \Symfony\Component\Console\Tests\Helper\QuestionHelperTest::testAskChoice()

File

vendor/symfony/console/Tests/Helper/QuestionHelperTest.php, line 27

Class

QuestionHelperTest
@group tty

Namespace

Symfony\Component\Console\Tests\Helper

Code

public function testAskChoice() {
  $questionHelper = new QuestionHelper();
  $helperSet = new HelperSet(array(
    new FormatterHelper(),
  ));
  $questionHelper
    ->setHelperSet($helperSet);
  $heroes = array(
    'Superman',
    'Batman',
    'Spiderman',
  );
  $questionHelper
    ->setInputStream($this
    ->getInputStream("\n1\n  1  \nFabien\n1\nFabien\n1\n0,2\n 0 , 2  \n\n\n"));
  $question = new ChoiceQuestion('What is your favorite superhero?', $heroes, '2');
  $question
    ->setMaxAttempts(1);

  // first answer is an empty answer, we're supposed to receive the default value
  $this
    ->assertEquals('Spiderman', $questionHelper
    ->ask($this
    ->createInputInterfaceMock(), $this
    ->createOutputInterface(), $question));
  $question = new ChoiceQuestion('What is your favorite superhero?', $heroes);
  $question
    ->setMaxAttempts(1);
  $this
    ->assertEquals('Batman', $questionHelper
    ->ask($this
    ->createInputInterfaceMock(), $this
    ->createOutputInterface(), $question));
  $this
    ->assertEquals('Batman', $questionHelper
    ->ask($this
    ->createInputInterfaceMock(), $this
    ->createOutputInterface(), $question));
  $question = new ChoiceQuestion('What is your favorite superhero?', $heroes);
  $question
    ->setErrorMessage('Input "%s" is not a superhero!');
  $question
    ->setMaxAttempts(2);
  $this
    ->assertEquals('Batman', $questionHelper
    ->ask($this
    ->createInputInterfaceMock(), $output = $this
    ->createOutputInterface(), $question));
  rewind($output
    ->getStream());
  $stream = stream_get_contents($output
    ->getStream());
  $this
    ->assertContains('Input "Fabien" is not a superhero!', $stream);
  try {
    $question = new ChoiceQuestion('What is your favorite superhero?', $heroes, '1');
    $question
      ->setMaxAttempts(1);
    $questionHelper
      ->ask($this
      ->createInputInterfaceMock(), $output = $this
      ->createOutputInterface(), $question);
    $this
      ->fail();
  } catch (\InvalidArgumentException $e) {
    $this
      ->assertEquals('Value "Fabien" is invalid', $e
      ->getMessage());
  }
  $question = new ChoiceQuestion('What is your favorite superhero?', $heroes, null);
  $question
    ->setMaxAttempts(1);
  $question
    ->setMultiselect(true);
  $this
    ->assertEquals(array(
    'Batman',
  ), $questionHelper
    ->ask($this
    ->createInputInterfaceMock(), $this
    ->createOutputInterface(), $question));
  $this
    ->assertEquals(array(
    'Superman',
    'Spiderman',
  ), $questionHelper
    ->ask($this
    ->createInputInterfaceMock(), $this
    ->createOutputInterface(), $question));
  $this
    ->assertEquals(array(
    'Superman',
    'Spiderman',
  ), $questionHelper
    ->ask($this
    ->createInputInterfaceMock(), $this
    ->createOutputInterface(), $question));
  $question = new ChoiceQuestion('What is your favorite superhero?', $heroes, '0,1');
  $question
    ->setMaxAttempts(1);
  $question
    ->setMultiselect(true);
  $this
    ->assertEquals(array(
    'Superman',
    'Batman',
  ), $questionHelper
    ->ask($this
    ->createInputInterfaceMock(), $this
    ->createOutputInterface(), $question));
  $question = new ChoiceQuestion('What is your favorite superhero?', $heroes, ' 0 , 1 ');
  $question
    ->setMaxAttempts(1);
  $question
    ->setMultiselect(true);
  $this
    ->assertEquals(array(
    'Superman',
    'Batman',
  ), $questionHelper
    ->ask($this
    ->createInputInterfaceMock(), $this
    ->createOutputInterface(), $question));
}