1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
|
<?php
namespace Doctrine\Tests\Common\Cache;
use Doctrine\Common\Cache\CacheProvider;
use Doctrine\Tests\DoctrineTestCase;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit_Framework_MockObject_MockObject;
use function assert;
class CacheProviderTest extends DoctrineTestCase
{
#[Group('nophpunit11')]
public function testFetchMultiWillFilterNonRequestedKeys(): void
{
$cache = $this->getMockForAbstractClass(
CacheProvider::class,
[],
'',
true,
true,
true,
['doFetchMultiple']
);
assert($cache instanceof CacheProvider || $cache instanceof PHPUnit_Framework_MockObject_MockObject);
$cache
->expects($this->once())
->method('doFetchMultiple')
->will($this->returnValue([
'[foo][1]' => 'bar',
'[bar][1]' => 'baz',
'[baz][1]' => 'tab',
]));
self::assertEquals(
['foo' => 'bar', 'bar' => 'baz'],
$cache->fetchMultiple(['foo', 'bar'])
);
}
#[Group('nophpunit11')]
public function testFailedDeleteAllDoesNotChangeNamespaceVersion(): void
{
$cache = $this->getMockForAbstractClass(
CacheProvider::class,
[],
'',
true,
true,
true,
['doFetch', 'doSave', 'doContains']
);
assert($cache instanceof CacheProvider || $cache instanceof PHPUnit_Framework_MockObject_MockObject);
$cache
->expects($this->once())
->method('doFetch')
->with('DoctrineNamespaceCacheKey[]')
->will($this->returnValue(false));
// doSave is only called once from deleteAll as we do not need to persist the default version in getNamespaceVersion()
$cache
->expects($this->once())
->method('doSave')
->with('DoctrineNamespaceCacheKey[]')
->will($this->returnValue(false));
// After a failed deleteAll() the local namespace version is not increased (still 1). Otherwise all data written afterwards
// would be lost outside the current instance.
$cache
->expects($this->once())
->method('doContains')
->with('[key][1]')
->will($this->returnValue(true));
self::assertFalse($cache->deleteAll(), 'deleteAll() returns false when saving the namespace version fails');
$cache->contains('key');
}
#[Group('nophpunit11')]
public function testSaveMultipleNoFail(): void
{
$cache = $this->getMockForAbstractClass(
CacheProvider::class,
[],
'',
true,
true,
true,
['doSave']
);
assert($cache instanceof CacheProvider || $cache instanceof PHPUnit_Framework_MockObject_MockObject);
$cache
->expects($this->at(1))
->method('doSave')
->with('[kerr][1]', 'verr', 0)
->will($this->returnValue(false));
$cache
->expects($this->at(2))
->method('doSave')
->with('[kok][1]', 'vok', 0)
->will($this->returnValue(true));
$cache->saveMultiple([
'kerr' => 'verr',
'kok' => 'vok',
]);
}
#[Group('nophpunit11')]
public function testDeleteMultipleNoFail(): void
{
$cache = $this
->getMockBuilder(CacheProvider::class)
->setMethods(['doDelete'])
->getMockForAbstractClass();
assert($cache instanceof CacheProvider || $cache instanceof PHPUnit_Framework_MockObject_MockObject);
$cache
->expects($this->at(1))
->method('doDelete')
->with('[kerr][1]')
->will($this->returnValue(false));
$cache
->expects($this->at(2))
->method('doDelete')
->with('[kok][1]')
->will($this->returnValue(true));
$cache->deleteMultiple(['kerr', 'kok']);
}
#[Group('nophpunit11')]
public function testInvalidNamespaceVersionCacheEntry(): void
{
$cache = $this->getMockForAbstractClass(CacheProvider::class);
assert($cache instanceof CacheProvider || $cache instanceof PHPUnit_Framework_MockObject_MockObject);
$cache->expects($this->once())
->method('doFetch')
->with('DoctrineNamespaceCacheKey[]')
->willReturn('corruptedStringKey');
$cache->expects($this->once())
->method('doSave')
->with('DoctrineNamespaceCacheKey[]', 2, 0)
->willReturn(true);
self::assertTrue($cache->deleteAll());
}
}
|