Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/229.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在Symfony2中,如何模拟正在使用另一个ConsoleComponent的Console组件_Php_Unit Testing_Symfony_Mocking - Fatal编程技术网

Php 在Symfony2中,如何模拟正在使用另一个ConsoleComponent的Console组件

Php 在Symfony2中,如何模拟正在使用另一个ConsoleComponent的Console组件,php,unit-testing,symfony,mocking,Php,Unit Testing,Symfony,Mocking,我正在使用Symfony2库编写一个控制台组件,该库使用另一个同样使用Symfony2控制台组件编写的应用程序 我想模拟其他应用程序控制台组件,如何实现这一点?我正在构建的应用程序只是使用另一个应用程序中的现有命令: 基本上,如何为下面的代码编写单元测试: protected function execute(InputInterface $input, OutputInterface $output) { $command = $this->getApplication()-&g

我正在使用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);

    // ...
}

另一个应用程序还是同一个应用程序

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

您只需模拟命令并模拟应用程序以返回模拟命令。

我正在模拟另一个应用程序,特别是这里的这个应用程序。在symfony2组件下,他们在最底部有一个示例,“调用现有命令”,这正是我正在做的。当我运行您提供的代码时,我得到“Mock_Command_86c6fb40”中定义的命令不能有空名称。”。唯一的区别是在我的代码中,第2行用“…”->($this->anythis())-…”,我可以看到->方法('run'),这是方法名'run',那么问题出在哪里?