1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
|
4
|
|
|
namespace RoaveTest\DoctrineSimpleCache; |
5
|
|
|
|
6
|
|
|
use Cache\IntegrationTests\SimpleCacheTest; |
7
|
|
|
use Doctrine\Common\Cache\ArrayCache; |
8
|
|
|
use Roave\DoctrineSimpleCache\CacheException; |
9
|
|
|
use Roave\DoctrineSimpleCache\SimpleCacheAdapter; |
10
|
|
|
use RoaveTestAsset\DoctrineSimpleCache\FullyImplementedCache; |
11
|
|
|
use RoaveTestAsset\DoctrineSimpleCache\NotClearableCache; |
12
|
|
|
use RoaveTestAsset\DoctrineSimpleCache\NotMultiGettableCache; |
13
|
|
|
use RoaveTestAsset\DoctrineSimpleCache\NotMultiPuttableCache; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @covers \Roave\DoctrineSimpleCache\SimpleCacheAdapter |
17
|
|
|
*/ |
18
|
|
|
final class SimpleCacheAdapterTest extends SimpleCacheTest |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @return \Psr\SimpleCache\CacheInterface that is used in the tests |
22
|
|
|
*/ |
23
|
|
|
public function createSimpleCache() |
24
|
|
|
{ |
25
|
|
|
$doctrineCache = new ArrayCache(); |
26
|
|
|
|
27
|
|
|
return new SimpleCacheAdapter($doctrineCache); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
protected function setUp() |
31
|
|
|
{ |
32
|
|
|
parent::setUp(); |
33
|
|
|
|
34
|
|
|
// @todo: Let's make these tests passed! |
35
|
|
|
$this->skippedTests['testSetMultipleWithGenerator'] = true; |
36
|
|
|
$this->skippedTests['testGetMultipleWithGenerator'] = true; |
37
|
|
|
$this->skippedTests['testGetInvalidKeys'] = true; |
38
|
|
|
$this->skippedTests['testGetMultipleInvalidKeys'] = true; |
39
|
|
|
$this->skippedTests['testGetMultipleNoIterable'] = true; |
40
|
|
|
$this->skippedTests['testSetInvalidKeys'] = true; |
41
|
|
|
$this->skippedTests['testSetMultipleInvalidKeys'] = true; |
42
|
|
|
$this->skippedTests['testSetMultipleNoIterable'] = true; |
43
|
|
|
$this->skippedTests['testHasInvalidKeys'] = true; |
44
|
|
|
$this->skippedTests['testDeleteInvalidKeys'] = true; |
45
|
|
|
$this->skippedTests['testDeleteMultipleInvalidKeys'] = true; |
46
|
|
|
$this->skippedTests['testDeleteMultipleNoIterable'] = true; |
47
|
|
|
$this->skippedTests['testObjectDoesNotChangeInCache'] = true; |
48
|
|
|
$this->skippedTests['testDataTypeBoolean'] = true; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function testConstructorThrowsExceptionWhenNotMultiPuttableCacheIsUsed() |
52
|
|
|
{ |
53
|
|
|
/** @var NotMultiPuttableCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */ |
54
|
|
|
$doctrineCache = $this->createMock(NotMultiPuttableCache::class); |
55
|
|
|
|
56
|
|
|
$this->expectException(CacheException::class); |
57
|
|
|
new SimpleCacheAdapter($doctrineCache); |
|
|
|
|
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function testConstructorThrowsExceptionWhenNotClearableCacheIsUsed() |
61
|
|
|
{ |
62
|
|
|
/** @var NotClearableCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */ |
63
|
|
|
$doctrineCache = $this->createMock(NotClearableCache::class); |
64
|
|
|
|
65
|
|
|
$this->expectException(CacheException::class); |
66
|
|
|
new SimpleCacheAdapter($doctrineCache); |
|
|
|
|
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function testConstructorThrowsExceptionWhenNotMultiGettableCacheIsUsed() |
70
|
|
|
{ |
71
|
|
|
/** @var NotMultiGettableCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */ |
72
|
|
|
$doctrineCache = $this->createMock(NotMultiGettableCache::class); |
73
|
|
|
|
74
|
|
|
$this->expectException(CacheException::class); |
75
|
|
|
new SimpleCacheAdapter($doctrineCache); |
|
|
|
|
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function testGetProxiesToDoctrineFetch() |
79
|
|
|
{ |
80
|
|
|
$key = uniqid('key', true); |
81
|
|
|
$value = uniqid('value', true); |
82
|
|
|
|
83
|
|
|
/** @var FullyImplementedCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */ |
84
|
|
|
$doctrineCache = $this->createMock(FullyImplementedCache::class); |
85
|
|
|
$doctrineCache->expects(self::once())->method('fetch')->with($key)->willReturn($value); |
|
|
|
|
86
|
|
|
|
87
|
|
|
$psrCache = new SimpleCacheAdapter($doctrineCache); |
|
|
|
|
88
|
|
|
self::assertSame($value, $psrCache->get($key)); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function testGetWithNotExistingKey() |
92
|
|
|
{ |
93
|
|
|
$key = uniqid('key', true); |
94
|
|
|
$value = uniqid('value', true); |
95
|
|
|
|
96
|
|
|
$psrCache = new SimpleCacheAdapter(new ArrayCache()); |
97
|
|
|
$psrCache->set($key, $value); |
98
|
|
|
|
99
|
|
|
$default = uniqid('default', true); |
100
|
|
|
self::assertSame($value, $psrCache->get($key, $default)); |
101
|
|
|
|
102
|
|
|
$anotherKey = uniqid('key', true); |
103
|
|
|
self::assertSame($default, $psrCache->get($anotherKey, $default)); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function testSetProxiesToDoctrineSave() |
107
|
|
|
{ |
108
|
|
|
$key = uniqid('key', true); |
109
|
|
|
$value = uniqid('value', true); |
110
|
|
|
$ttl = random_int(1000, 9999); |
111
|
|
|
|
112
|
|
|
/** @var FullyImplementedCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */ |
113
|
|
|
$doctrineCache = $this->createMock(FullyImplementedCache::class); |
114
|
|
|
$doctrineCache->expects(self::once())->method('save')->with($key, $value, $ttl)->willReturn(true); |
|
|
|
|
115
|
|
|
|
116
|
|
|
$psrCache = new SimpleCacheAdapter($doctrineCache); |
|
|
|
|
117
|
|
|
self::assertTrue($psrCache->set($key, $value, $ttl)); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
View Code Duplication |
public function testDeleteProxiesToDoctrineDelete() |
|
|
|
|
121
|
|
|
{ |
122
|
|
|
$key = uniqid('key', true); |
123
|
|
|
|
124
|
|
|
/** @var FullyImplementedCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */ |
125
|
|
|
$doctrineCache = $this->createMock(FullyImplementedCache::class); |
126
|
|
|
$doctrineCache->expects(self::once())->method('delete')->with($key)->willReturn(true); |
|
|
|
|
127
|
|
|
|
128
|
|
|
$psrCache = new SimpleCacheAdapter($doctrineCache); |
|
|
|
|
129
|
|
|
self::assertTrue($psrCache->delete($key)); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
public function testClearProxiesToDeleteAll() |
133
|
|
|
{ |
134
|
|
|
/** @var FullyImplementedCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */ |
135
|
|
|
$doctrineCache = $this->createMock(FullyImplementedCache::class); |
136
|
|
|
$doctrineCache->expects(self::once())->method('deleteAll')->with()->willReturn(true); |
|
|
|
|
137
|
|
|
|
138
|
|
|
$psrCache = new SimpleCacheAdapter($doctrineCache); |
|
|
|
|
139
|
|
|
self::assertTrue($psrCache->clear()); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
public function testGetMultipleProxiesToFetchMultiple() |
143
|
|
|
{ |
144
|
|
|
$values = [ |
145
|
|
|
uniqid('key1', true) => uniqid('value1', true), |
146
|
|
|
uniqid('key2', true) => uniqid('value2', true), |
147
|
|
|
]; |
148
|
|
|
$keys = array_keys($values); |
149
|
|
|
|
150
|
|
|
/** @var FullyImplementedCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */ |
151
|
|
|
$doctrineCache = $this->createMock(FullyImplementedCache::class); |
152
|
|
|
$doctrineCache->expects(self::once())->method('fetchMultiple')->with($keys)->willReturn($values); |
|
|
|
|
153
|
|
|
|
154
|
|
|
$psrCache = new SimpleCacheAdapter($doctrineCache); |
|
|
|
|
155
|
|
|
self::assertSame($values, $psrCache->getMultiple($keys)); |
|
|
|
|
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
public function testGetMultipleWithPartialKeys() |
159
|
|
|
{ |
160
|
|
|
$values = [ |
161
|
|
|
uniqid('key1', true) => uniqid('value1', true), |
162
|
|
|
uniqid('key2', true) => uniqid('value2', true), |
163
|
|
|
]; |
164
|
|
|
$keys = array_keys($values); |
165
|
|
|
|
166
|
|
|
$psrCache = new SimpleCacheAdapter(new ArrayCache()); |
167
|
|
|
$psrCache->setMultiple($values); |
|
|
|
|
168
|
|
|
|
169
|
|
|
$default = uniqid('default', true); |
170
|
|
|
$invalid_key = uniqid('key3', true); |
171
|
|
|
$keys[] = $invalid_key; |
172
|
|
|
$values[$invalid_key] = $default; |
173
|
|
|
|
174
|
|
|
self::assertSame($values, $psrCache->getMultiple($keys, $default)); |
|
|
|
|
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
public function testSetMultipleProxiesToSaveMultiple() |
178
|
|
|
{ |
179
|
|
|
$values = [ |
180
|
|
|
uniqid('key1', true) => uniqid('value1', true), |
181
|
|
|
uniqid('key2', true) => uniqid('value2', true), |
182
|
|
|
]; |
183
|
|
|
|
184
|
|
|
/** @var FullyImplementedCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */ |
185
|
|
|
$doctrineCache = $this->createMock(FullyImplementedCache::class); |
186
|
|
|
$doctrineCache->expects(self::once())->method('saveMultiple')->with($values)->willReturn(true); |
|
|
|
|
187
|
|
|
|
188
|
|
|
$psrCache = new SimpleCacheAdapter($doctrineCache); |
|
|
|
|
189
|
|
|
self::assertTrue($psrCache->setMultiple($values)); |
|
|
|
|
190
|
|
|
} |
191
|
|
|
|
192
|
|
View Code Duplication |
public function testDeleteMultipleReturnsTrueWhenAllDeletesSucceed() |
|
|
|
|
193
|
|
|
{ |
194
|
|
|
$keys = [ |
195
|
|
|
uniqid('key1', true), |
196
|
|
|
uniqid('key2', true), |
197
|
|
|
]; |
198
|
|
|
|
199
|
|
|
/** @var FullyImplementedCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */ |
200
|
|
|
$doctrineCache = $this->createMock(FullyImplementedCache::class); |
201
|
|
|
$doctrineCache->expects(self::at(0))->method('delete')->with($keys[0])->willReturn(true); |
|
|
|
|
202
|
|
|
$doctrineCache->expects(self::at(1))->method('delete')->with($keys[1])->willReturn(true); |
203
|
|
|
|
204
|
|
|
$psrCache = new SimpleCacheAdapter($doctrineCache); |
|
|
|
|
205
|
|
|
self::assertTrue($psrCache->deleteMultiple($keys)); |
|
|
|
|
206
|
|
|
} |
207
|
|
|
|
208
|
|
View Code Duplication |
public function testDeleteMultipleReturnsFalseWhenOneDeleteFails() |
|
|
|
|
209
|
|
|
{ |
210
|
|
|
$keys = [ |
211
|
|
|
uniqid('key1', true), |
212
|
|
|
uniqid('key2', true), |
213
|
|
|
]; |
214
|
|
|
|
215
|
|
|
/** @var FullyImplementedCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */ |
216
|
|
|
$doctrineCache = $this->createMock(FullyImplementedCache::class); |
217
|
|
|
$doctrineCache->expects(self::at(0))->method('delete')->with($keys[0])->willReturn(false); |
|
|
|
|
218
|
|
|
$doctrineCache->expects(self::at(1))->method('delete')->with($keys[1])->willReturn(true); |
219
|
|
|
|
220
|
|
|
$psrCache = new SimpleCacheAdapter($doctrineCache); |
|
|
|
|
221
|
|
|
self::assertFalse($psrCache->deleteMultiple($keys)); |
|
|
|
|
222
|
|
|
} |
223
|
|
|
|
224
|
|
View Code Duplication |
public function testHasProxiesToDoctrineContains() |
|
|
|
|
225
|
|
|
{ |
226
|
|
|
$key = uniqid('key', true); |
227
|
|
|
|
228
|
|
|
/** @var FullyImplementedCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */ |
229
|
|
|
$doctrineCache = $this->createMock(FullyImplementedCache::class); |
230
|
|
|
$doctrineCache->expects(self::once())->method('contains')->with($key)->willReturn(true); |
|
|
|
|
231
|
|
|
|
232
|
|
|
$psrCache = new SimpleCacheAdapter($doctrineCache); |
|
|
|
|
233
|
|
|
self::assertTrue($psrCache->has($key)); |
234
|
|
|
} |
235
|
|
|
} |
236
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.