响应隐藏时,控制台命令测试永远不会完

时间:2014-12-27 19:17:07

标签: php symfony phpunit

在Symfony 2.6中对请求密码的控制台命令进行测试,如果将响应设置为隐藏$question->setHidden(true);,则测试永远不会完成(并且永不超时)注释掉该行允许测试完成成功。 [DialogHelper及其隐藏响应的等效方法也是如此,而不是像QuestionHelper所示。]

编辑:

以上描述涉及交互式控制台命令的测试。使用下面的测试测试命令本身会导致RuntimeException: Aborted $input->setArgument('username', $helper->ask($input, $output, $question));

命令

use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\Question;

class CreateUserCommand extends ContainerAwareCommand
{
...
    protected function interact(InputInterface $input, OutputInterface $output)
    {
        $helper = $this->getHelper('question');

        $question = new Question('Please enter a password: ');
        $question->setValidator(function ($answer) {
            if (empty($answer)) {
                throw new \RuntimeException(
                'A password is required'
                );
            }
            return $answer;
        });
        $question->setHidden(true);
        $question->setMaxAttempts(5);

        $input->setArgument('password', $helper->ask($input, $output, $question));
    ...
    }
...
}

交互式测试

public function testExecute()
{
    $kernel = $this->createKernel();
    $kernel->boot();

    $application = new Application($kernel);
    $application->add(new CreateUserCommand());

    $command = $application->find('truckee:user:create');
    $commandTester = new CommandTester($command);

    $dialog = $command->getHelper('question');
    $dialog->setInputStream($this->getInputStream("bborko\n"
                    . "Benny\n "
                    . "Borko\n "
                    . "bborko@bogus.info\n "
                    . "123Abcd\n "
                    . "sandbox\n"
    ));
    $commandTester->execute(array('command' => $command->getName()));

    $this->assertRegExp('/.../', $commandTester->getDisplay());
}

控制台测试

    public function testCreateAdmin()
    {
        $kernel = $this->createKernel();
        $kernel->boot();

        $application = new Application($kernel);
        $application->add(new CreateUserCommand());

        $command = $application->find('truckee:user:create');
        $commandTester = new CommandTester($command);
        $commandTester->execute(
                array(
                    'username' => 'bborko',
                    'firstname' => 'Benny',
                    'lastname' => 'Borko',
                    'email' => 'bborko@bogus.info',
                    'password' => '123Abcd',
                    'type' => 'admin',
                )
        );
        $this->assertRegExp('/.../', $commandTester->getDisplay());
}

1 个答案:

答案 0 :(得分:0)

我再次了解到Windows并不是Symfony最好的开发地点。今天我找到了

  

$ this-> markTestSkipped(' Windows'不支持此测试);

在Symfony \ Component \ Console \ Tests \ Helper \ QuestionHelperTest public function testAskHiddenResponse()中。

如果您想了解哪些测试不受支持,请搜索文本' Windows'在vendor\symfony目录中(例如,使用像Netbeans这样的IDE)。