douhu4091 2015-05-11 18:23
浏览 22

测试期望输入的Symfony2控制台命令

I am trying to test my Symfony2 Console command using phpunit.

I'm following the Symfony2 cookbook article about this topic: http://symfony.com/doc/current/components/console/helpers/questionhelper.html#testing-a-command-that-expects-input

However, if I fail to provide an input (a test fail), then phpunit simply sit there doing nothing waiting for input. Here's an example:

// 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\
')); // this should be yy\


    $cmdTester->execute([
       'command' => $cmd->getName(),
    ]);
  }
}

Please notice that I have purposefully made my test incorrect, it is only supplying an answer to question 1. Since I wrote that test, I have since added q2 but I forgot to modify my tests. Being a good programmer though I run phpunit to see if there are problems, but phpunit hangs as it expects input from q2!

How do I make it so my test will disregard any further requests for input, fail if it encounters one, and keep going with other tests?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
    • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
    • ¥16 mybatis的代理对象无法通过@Autowired装填
    • ¥15 可见光定位matlab仿真
    • ¥15 arduino 四自由度机械臂
    • ¥15 wordpress 产品图片 GIF 没法显示
    • ¥15 求三国群英传pl国战时间的修改方法
    • ¥15 matlab代码代写,需写出详细代码,代价私
    • ¥15 ROS系统搭建请教(跨境电商用途)
    • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。