命令测试详细输出,无法将选项-v / - versbose传递给test

时间:2017-06-15 14:29:34

标签: php symfony console phpunit

制作控制台命令:

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);
}

第二个断言不起作用。看不到这些信息。我做错了什么?

PS $output->writeln($consultation->getId(), OutputInterface::VERBOSITY_VERBOSE);如果没有--verbose标记

,此行不会显示此信息

1 个答案:

答案 0 :(得分:2)

相反:

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

有必要使用:

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