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

时间:2019-06-26 07:42:45

标签: php phpunit

在我的php文件中,我正在使用Zend\Cache\Storage\StorageInterface。我已将此接口与Zend \ filesystem缓存绑定。我必须对主要的php文件进行单元测试。问题是StorageInterface没有定义刷新方法。文件系统具有来自不同接口的刷新方法。

在单元测试中,我试图模拟StorageInterface,但问题是它给出了一个错误,指出未定义冲洗。如何存入冲洗方法?

绑定:

$container->bindIf(
            "Zend\\Cache\\Storage\\StorageInterface",
            function () {
                $directoryPath = "/tmp/zend_report_cache";
                if (!file_exists($directoryPath) && !mkdir($directoryPath)) {
                    throw new Exception("Failed to create directory [directoryPath=$directoryPath]");
                }
                return \Zend\Cache\StorageFactory::factory(array(
                    'adapter' => array(
                        'name' => 'filesystem',
                        'options' => array(
                            'cache_dir' => $directoryPath
                        ),
                    ),
                    'plugins' => array('serializer'),
                ));
            }
        );

Main.php(仅显示相关代码

use Zend\Cache\Storage\StorageInterface;

public function __destruct()
{
    $this->cache->flush();
}

单元测试文件

private function getCacheMock()
    {
$cacheMock = $this->setMethods(['execute'])->createMock(StorageInterface::class);
        $cacheMock->expects($this->at(0))->method('setItem')->with(CsvReport::HEADER_CACHE_KEY, []);
        $cacheMock->expects($this->at(1))->method('getItem')->with(CsvReport::HEADER_CACHE_KEY)->willReturn([]);
        $cacheMock->expects($this->at(2))->method('setItem')->with(CsvReport::HEADER_CACHE_KEY, ['key1', 'key2', 'key3']);
        $cacheMock->expects($this->at(3))->method('setItem')->with(0, $this->getMockRow(0));
        $cacheMock->expects($this->at(4))->method('getItem')->with(CsvReport::HEADER_CACHE_KEY)->willReturn(['key1', 'key2', 'key3']);
        $cacheMock->expects($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->expects($this->at(7))->method('getItem')->with(CsvReport::HEADER_CACHE_KEY)->willReturn(['key1', 'key2', 'key3', 'key4']);
        $cacheMock->expects($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));
        return $cacheMock;
    }

错误:尝试配置方法“ flush”,该方法无法配置,因为它不存在,尚未指定,是最终的还是静态的

0 个答案:

没有答案
相关问题