/** * Returns or initializes the symfony/console QuestionHelper * * @return QuestionHelper */ protected function getQuestionHelper() { if ($this->questionHelper === null) { $this->questionHelper = new QuestionHelper(); $helperSet = new HelperSet(array(new FormatterHelper())); $this->questionHelper->setHelperSet($helperSet); } return $this->questionHelper; }
/** * @expectedException \InvalidArgumentException * @expectedExceptionMessage The provided answer is ambiguous. Value should be one of env_2 or env_3. */ public function testAmbiguousChoiceFromChoicelist() { $possibleChoices = array('env_1' => 'My first environment', 'env_2' => 'My environment', 'env_3' => 'My environment'); $dialog = new QuestionHelper(); $dialog->setInputStream($this->getInputStream("My environment\n")); $helperSet = new HelperSet(array(new FormatterHelper())); $dialog->setHelperSet($helperSet); $question = new ChoiceQuestion('Please select the environment to load', $possibleChoices); $question->setMaxAttempts(1); $dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question); }
public function testAskAndValidate() { $dialog = new QuestionHelper(); $helperSet = new HelperSet(array(new FormatterHelper())); $dialog->setHelperSet($helperSet); $error = 'This is not a color!'; $validator = function ($color) use($error) { if (!in_array($color, array('white', 'black'))) { throw new \InvalidArgumentException($error); } return $color; }; $question = new Question('What color was the white horse of Henry IV?', 'white'); $question->setValidator($validator); $question->setMaxAttempts(2); $dialog->setInputStream($this->getInputStream("\nblack\n")); $this->assertEquals('white', $dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question)); $this->assertEquals('black', $dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question)); $dialog->setInputStream($this->getInputStream("green\nyellow\norange\n")); try { $dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question); $this->fail(); } catch (\InvalidArgumentException $e) { $this->assertEquals($error, $e->getMessage()); } }
/** * @requires function mb_strwidth */ public function testChoiceOutputFormattingQuestionForUtf8Keys() { $question = 'Lorem ipsum?'; $possibleChoices = array('foo' => 'foo', 'żółw' => 'bar', 'łabądź' => 'baz'); $outputShown = array($question, ' [<info>foo </info>] foo', ' [<info>żółw </info>] bar', ' [<info>łabądź</info>] baz'); $output = $this->getMock('\\Symfony\\Component\\Console\\Output\\OutputInterface'); $output->method('getFormatter')->willReturn(new OutputFormatter()); $dialog = new QuestionHelper(); $dialog->setInputStream($this->getInputStream("\n")); $helperSet = new HelperSet(array(new FormatterHelper())); $dialog->setHelperSet($helperSet); $output->expects($this->once())->method('writeln')->with($this->equalTo($outputShown)); $question = new ChoiceQuestion($question, $possibleChoices, 'foo'); $dialog->ask($this->createInputInterfaceMock(), $output, $question); }
public function testAmbiguousChoiceFromChoicelist() { $possibleChoices = array('env_1' => 'My environment 1', 'env_2' => 'My environment', 'env_3' => 'My environment'); $dialog = new QuestionHelper(); $dialog->setInputStream($this->getInputStream("My environment\n")); $helperSet = new HelperSet(array(new FormatterHelper())); $dialog->setHelperSet($helperSet); $question = new ChoiceQuestion('Please select the environment to load', $possibleChoices); try { $dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question); } catch (\InvalidArgumentException $e) { $this->assertEquals('The provided answer is ambiguous. Value should be one of env_2 or env_3.', $e->getMessage()); } }
/** * @return QuestionHelper */ protected function getQuestionHelper() { $questionHelper = new QuestionHelper(); $helperSet = new HelperSet([new FormatterHelper()]); $questionHelper->setHelperSet($helperSet); return $questionHelper; }