Php 如何模拟接口中未定义的方法?

Php 如何模拟接口中未定义的方法?,php,phpunit,Php,Phpunit,在我的php文件中,我使用的是Zend\Cache\Storage\StorageInterface。我已经用Zend\filesystemcache绑定了这个接口。我必须对主php文件进行单元测试。问题是存储接口未定义刷新方法文件系统具有来自不同接口的刷新方法 在我的单元测试中,我试图模拟StorageInterface,但问题是它给出了一个错误,指出未定义刷新。我怎样才能将flush方法存根 绑定: $container->bindIf( “Zend\\Cache\\Storage\\Sto

在我的php文件中,我使用的是
Zend\Cache\Storage\StorageInterface
。我已经用
Zend\filesystem
cache绑定了这个接口。我必须对主php文件进行单元测试。问题是存储接口未定义刷新方法文件系统具有来自不同接口的刷新方法

在我的单元测试中,我试图模拟StorageInterface,但问题是它给出了一个错误,指出未定义刷新。我怎样才能将flush方法存根

绑定:

$container->bindIf(
“Zend\\Cache\\Storage\\StorageInterface”,
函数(){
$directoryPath=“/tmp/zend_report_cache”;
如果(!file_存在($directoryPath)&&!mkdir($directoryPath)){
抛出新异常(“未能创建目录[directoryPath=$directoryPath]”;
}
return\Zend\Cache\StorageFactory::factory(数组(
“适配器”=>阵列(
“名称”=>“文件系统”,
“选项”=>数组(
'cache_dir'=>$directoryPath
),
),
'plugins'=>array('serializer'),
));
}
);
Main.php(仅显示相关代码

使用Zend\Cache\Storage\StorageInterface;
公共函数_udestruct()
{
$this->cache->flush();
}
单元测试文件

私有函数getCacheMock()
{
$cacheMock=$this->setMethods(['execute'])->createMock(StorageInterface::class);
$cacheMock->expects($this->at(0))->method('setItem')->with(CsvReport::HEADER_CACHE_KEY,[]);
$cacheMock->expected($this->at(1))->method('getItem')->with(CsvReport::HEADER\u CACHE\u KEY)->将返回([]);
$cacheMock->expected($this->at(2))->method('setItem')->with(CsvReport::HEADER_CACHE_KEY,['key1','key2','key3']);
$cacheMock->expects($this->at(3))->method('setItem')->与(0,$this->getMockRow(0));
$cacheMock->expected($this->at(4))->method('getItem')->with(CsvReport::HEADER_CACHE_KEY)->将返回(['key1'、'key2'、'key3');
$cacheMock->expected($this->at(5))->method('setItem')->with(CsvReport::HEADER_CACHE_KEY,['key1','key2','key3','key4']);
$cacheMock->expects($this->at(6))->method('setItem')->with(1,$this->getMockRow(1));
$cacheMock->expected($this->at(7))->method('getItem')->with(CsvReport::HEADER\u CACHE\u KEY)->将返回(['key1'、'key2'、'key3'、'key4');
$cacheMock->expected($this->at(8))->method('setItem')->with(CsvReport::HEADER_CACHE_KEY,['key1','key2','key3','key4','key5']);
$cacheMock->expects($this->at(9))->method('setItem')->with(2,$this->getMockRow(2));
返回$cacheMock;
}

错误:尝试配置方法“刷新”无法配置,因为它不存在、未指定、是最终的或是静态的

您始终可以定义一个测试存根并使用或模拟它,而不是您的接口。如果该方法未定义,则Phpunit模拟生成器无法模拟/配置它。但是正如Robbie Averill所评论的,您可以将存根写入自己的界面但是您也可以只模拟一个现有的类来实现您想要测试的接口,并且只使用flush方法。