3 namespace Symfony\Component\Console\Tests\Helper;
5 use PHPUnit\Framework\TestCase;
6 use Symfony\Component\Console\Helper\FormatterHelper;
7 use Symfony\Component\Console\Helper\HelperSet;
8 use Symfony\Component\Console\Helper\SymfonyQuestionHelper;
9 use Symfony\Component\Console\Output\StreamOutput;
10 use Symfony\Component\Console\Question\Question;
11 use Symfony\Component\Console\Question\ChoiceQuestion;
16 class SymfonyQuestionHelperTest extends TestCase
18 public function testAskChoice()
20 $questionHelper = new SymfonyQuestionHelper();
22 $helperSet = new HelperSet(array(new FormatterHelper()));
23 $questionHelper->setHelperSet($helperSet);
25 $heroes = array('Superman', 'Batman', 'Spiderman');
27 $questionHelper->setInputStream($this->getInputStream("\n1\n 1 \nFabien\n1\nFabien\n1\n0,2\n 0 , 2 \n\n\n"));
29 $question = new ChoiceQuestion('What is your favorite superhero?', $heroes, '2');
30 $question->setMaxAttempts(1);
31 // first answer is an empty answer, we're supposed to receive the default value
32 $this->assertEquals('Spiderman', $questionHelper->ask($this->createInputInterfaceMock(), $output = $this->createOutputInterface(), $question));
33 $this->assertOutputContains('What is your favorite superhero? [Spiderman]', $output);
35 $question = new ChoiceQuestion('What is your favorite superhero?', $heroes);
36 $question->setMaxAttempts(1);
37 $this->assertEquals('Batman', $questionHelper->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
38 $this->assertEquals('Batman', $questionHelper->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
40 $question = new ChoiceQuestion('What is your favorite superhero?', $heroes);
41 $question->setErrorMessage('Input "%s" is not a superhero!');
42 $question->setMaxAttempts(2);
43 $this->assertEquals('Batman', $questionHelper->ask($this->createInputInterfaceMock(), $output = $this->createOutputInterface(), $question));
44 $this->assertOutputContains('Input "Fabien" is not a superhero!', $output);
47 $question = new ChoiceQuestion('What is your favorite superhero?', $heroes, '1');
48 $question->setMaxAttempts(1);
49 $questionHelper->ask($this->createInputInterfaceMock(), $output = $this->createOutputInterface(), $question);
51 } catch (\InvalidArgumentException $e) {
52 $this->assertEquals('Value "Fabien" is invalid', $e->getMessage());
55 $question = new ChoiceQuestion('What is your favorite superhero?', $heroes, null);
56 $question->setMaxAttempts(1);
57 $question->setMultiselect(true);
59 $this->assertEquals(array('Batman'), $questionHelper->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
60 $this->assertEquals(array('Superman', 'Spiderman'), $questionHelper->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
61 $this->assertEquals(array('Superman', 'Spiderman'), $questionHelper->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
63 $question = new ChoiceQuestion('What is your favorite superhero?', $heroes, '0,1');
64 $question->setMaxAttempts(1);
65 $question->setMultiselect(true);
67 $this->assertEquals(array('Superman', 'Batman'), $questionHelper->ask($this->createInputInterfaceMock(), $output = $this->createOutputInterface(), $question));
68 $this->assertOutputContains('What is your favorite superhero? [Superman, Batman]', $output);
70 $question = new ChoiceQuestion('What is your favorite superhero?', $heroes, ' 0 , 1 ');
71 $question->setMaxAttempts(1);
72 $question->setMultiselect(true);
74 $this->assertEquals(array('Superman', 'Batman'), $questionHelper->ask($this->createInputInterfaceMock(), $output = $this->createOutputInterface(), $question));
75 $this->assertOutputContains('What is your favorite superhero? [Superman, Batman]', $output);
78 public function testAskReturnsNullIfValidatorAllowsIt()
80 $questionHelper = new SymfonyQuestionHelper();
81 $questionHelper->setInputStream($this->getInputStream("\n"));
82 $question = new Question('What is your favorite superhero?');
83 $question->setValidator(function ($value) { return $value; });
84 $this->assertNull($questionHelper->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
87 public function testAskEscapeDefaultValue()
89 $helper = new SymfonyQuestionHelper();
90 $helper->setInputStream($this->getInputStream('\\'));
91 $helper->ask($this->createInputInterfaceMock(), $output = $this->createOutputInterface(), new Question('Can I have a backslash?', '\\'));
93 $this->assertOutputContains('Can I have a backslash? [\]', $output);
96 public function testAskEscapeAndFormatLabel()
98 $helper = new SymfonyQuestionHelper();
99 $helper->setInputStream($this->getInputStream('Foo\\Bar'));
100 $helper->ask($this->createInputInterfaceMock(), $output = $this->createOutputInterface(), new Question('Do you want to use Foo\\Bar <comment>or</comment> Foo\\Baz\\?', 'Foo\\Baz'));
102 $this->assertOutputContains('Do you want to use Foo\\Bar or Foo\\Baz\\? [Foo\\Baz]:', $output);
105 public function testLabelTrailingBackslash()
107 $helper = new SymfonyQuestionHelper();
108 $helper->setInputStream($this->getInputStream('sure'));
109 $helper->ask($this->createInputInterfaceMock(), $output = $this->createOutputInterface(), new Question('Question with a trailing \\'));
111 $this->assertOutputContains('Question with a trailing \\', $output);
115 * @expectedException \Symfony\Component\Console\Exception\RuntimeException
116 * @expectedExceptionMessage Aborted
118 public function testAskThrowsExceptionOnMissingInput()
120 $dialog = new SymfonyQuestionHelper();
122 $dialog->setInputStream($this->getInputStream(''));
123 $dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), new Question('What\'s your name?'));
126 protected function getInputStream($input)
128 $stream = fopen('php://memory', 'r+', false);
129 fwrite($stream, $input);
135 protected function createOutputInterface()
137 $output = new StreamOutput(fopen('php://memory', 'r+', false));
138 $output->setDecorated(false);
143 protected function createInputInterfaceMock($interactive = true)
145 $mock = $this->getMockBuilder('Symfony\Component\Console\Input\InputInterface')->getMock();
146 $mock->expects($this->any())
147 ->method('isInteractive')
148 ->will($this->returnValue($interactive));
153 private function assertOutputContains($expected, StreamOutput $output)
155 rewind($output->getStream());
156 $stream = stream_get_contents($output->getStream());
157 $this->assertContains($expected, $stream);