我正在尝试使用phpunit测试我的Symfony2控制台命令。
我正在关注有关此主题的Symfony2食谱文章: http://symfony.com/doc/current/components/console/helpers/questionhelper.html#testing-a-command-that-expects-input
但是,如果我没有提供输入(测试失败),那么phpunit只是坐在那里等待输入。这是一个例子:
// MyCommand.php
class MyCommand extends Command {
// ... configure()
protected function execute(InputInterface $input, OutputInterface $output) {
$qh = $this->getHelper('question');
$q1 = new ConfirmationQuestion('First question, yes or no?', false);
$qh->ask($input, $output, $q);
$q2 = new ConfirmationQuestion('Second question, yes or no?', false);
$qh->ask($input, $output, $q);
}
}
// MyCommandTest.php
class MyCommandTest extends \PHPUnit_Framework_TestCase {
// ... getInputstream()
public function testExecute() {
$app = new Application();
$app->add(new MyCommand());
$cmd = $app->find('askquestions');
$cmdTester = new CommandTester($cmd);
$helper = $cmd->getHelper('question');
$helper->setInputStream($this->getInputStream('y\\n')); // this should be yy\\n
$cmdTester->execute([
'command' => $cmd->getName(),
]);
}
}
请注意我有目的地使我的测试不正确,它只提供问题1的答案。因为我写了测试,我已经添加了q2但我忘了修改我的测试。作为一个优秀的程序员,虽然我运行phpunit看看是否有问题,但phpunit挂起,因为它期望从q2输入!
如何制作,以便我的测试会忽略任何进一步的输入请求,如果遇到输入请求失败,并继续进行其他测试?