In my php file I am using Zend\Cache\Storage\StorageInterface. I have bind this interface with Zend\filesystem cache. I have to unit test the main php file. Problem is StorageInterface does not define flush method. Filesystem have flush method from different interface.
In my unit testing I am trying to mock the StorageInterface but problem is it gives an error stating that flush is not defined. How can I stub the flush method?
Binding:
$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 (only showing relevant code
use Zend\Cache\Storage\StorageInterface;
public function __destruct()
{
$this->cache->flush();
}
unit test file
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;
}
Error: Trying to configure method "flush" which cannot be configured because it does not exist, has not been specified, is final, or is static