【问题标题】:Command testing verbose output, can not pass option -v/--versbose into test命令测试详细输出,无法将选项 -v/--versbose 传递给测试
【发布时间】:2017-06-15 14:29:33
【问题描述】:

制作控制台命令:

protected function execute(InputInterface $input, OutputInterface $output)
{
    $consultations = $this->getContainer()->get('consultation.service')->setConsultationsCompleted();

    if ($consultations) {
        $output->writeln("=== Consultations has been completed ===");
    } else {
        $output->writeln("=== There are no overdue consulations ===");
    }

    foreach ($consultations as $consultation) {
        $output->writeln($consultation->getId(), OutputInterface::VERBOSITY_VERBOSE);
    }
}

并测试:

public function testExecute()
{
    $this->application->add(new CompleteConsultationsCommand());

    $consultations = $this->em->getRepository('ODConsultationBundle:Consultation')->findPastConsultations();

    $command = $this->application->find('consultations:complete');
    $commandTester = new CommandTester($command);
    $commandTester->execute([
        'command'  => $command->getName(),
        '--verbose' => true
    ]);

    $output = $commandTester->getDisplay();
    $this->assertContains('Consultations has been completed', $output);
    $this->assertContains($consultations[0]->getId(), $output);
}

第二个断言不起作用。看不到资料。我做错了什么?

附言 $output->writeln($consultation->getId(), OutputInterface::VERBOSITY_VERBOSE); 没有--verbose 标志,此行不显示此信息

【问题讨论】:

    标签: php symfony console phpunit


    【解决方案1】:

    改为:

    $commandTester->execute([
        'command'  => $command->getName(),
        '--verbose' => true
    ]);
    

    必须使用:

    $commandTester->execute([
        'command'  => $command->getName()
    ], [
        'verbosity' => OutputInterface::VERBOSITY_VERBOSE
    ]);
    

    【讨论】:

    • 那是因为 --verbose 不是命令的参数而是应用程序的参数。就像附加信息一样,还有更多类似 --version IIRC。
    猜你喜欢
    • 2015-04-27
    • 1970-01-01
    • 2017-03-02
    • 1970-01-01
    • 1970-01-01
    • 2019-06-29
    • 1970-01-01
    • 2020-09-06
    • 1970-01-01
    相关资源
    最近更新 更多