我使用 symfony3.2 创建一些基本命令来定期生成一些新闻通讯,当我想使用 phpunit 5.5.4 测试我的 symfony 命令时,我正在处理一些问题。它从一开始就失败了:
/**
* @param InputInterface $input
* @param OutputInterface $output
*/
protected function execute(InputInterface $input, OutputInterface $output){
$output->writeln("<info>Script start</info>");
//...
$output->writeln("<info>done</info>");
}
Run Code Online (Sandbox Code Playgroud)
通过这个单元测试:
use MyBundle\Command\MyCommand;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\Console\Tester\CommandTester;
class MyCommandTest extends KernelTestCase
{
public function testExecute(){
$kernel = static::createKernel();
$kernel->boot();
$application = new Application($kernel);
$application->add(new MyCommand());
$command = $application->find('generate:newsletter');
$commandTester = new CommandTester($command);
$commandTester->execute(array(
'command' => $command->getName()
));
$output = $commandTester->getDisplay();
$this->assertContains('done',$output);
}
}
Run Code Online (Sandbox Code Playgroud)
我一步一步地遵循这个步骤,但就我而言,我得到:
Error: Call to …Run Code Online (Sandbox Code Playgroud)