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 { $this->assertEquals('white', $dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question)); $this->fail(); } catch (\InvalidArgumentException $e) { $this->assertEquals($error, $e->getMessage()); } }