2017-06-15 67 views
1

作出的控制檯命令:命令測試詳細的輸出,無法通過-v選項/ - versbose到測試

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

而是顯示這樣的信息:

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

有必要使用:

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

那是因爲 - verbose不是命令的參數,而是應用程序。就像其他信息一樣,還有更多像--version IIRC。 – hakre