我正在编写一个使用Symfony2库的控制台组件,这些库使用另一个也是用symfony2控制台组件编写的应用程序。
我想模拟其他应用程序控制台组件,我该如何实现这一目标?我正在构建的应用程序只是消耗来自另一个应用程序的现有命令:
基本上,您如何为以下代码编写单元测试:
protected function execute(InputInterface $input, OutputInterface $output)
{
$command = $this->getApplication()->find('demo:greet');
$arguments = array(
'command' => 'demo:greet',
'name' => 'Fabien',
'--yell' => true,
);
$input = new ArrayInput($arguments);
$returnCode = $command->run($input, $output);
// ...
}
答案 0 :(得分:0)
另一个应用程序或相同的应用程序?
$commandMock = $this->getMock('Symfony\Component\Console\Command\Command');
$commandMock->expects($this->once())->method('run')->with(...)->will($this->returnValue(1));
$applicationMock = $this->getMockBuilder('Symfony\Component\Console\Application')
->disableConstructor()->getMock();
$applicationMock->method('find')
->with($this->equalTo('demo:greet'))
->will($this->returnValue($commandMock));
您只需模拟命令并模拟应用程序以返回模拟命令。