CacheTest.php
12.6 KB
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
<?php
namespace Doctrine\Tests\Common\Cache;
use Doctrine\Common\Cache\Cache;
use ArrayObject;
abstract class CacheTest extends \Doctrine\Tests\DoctrineTestCase
{
/**
* @dataProvider provideDataToCache
*/
public function testSetContainsFetchDelete($value)
{
$cache = $this->_getCacheDriver();
// Test saving a value, checking if it exists, and fetching it back
$this->assertTrue($cache->save('key', $value));
$this->assertTrue($cache->contains('key'));
if (is_object($value)) {
$this->assertEquals($value, $cache->fetch('key'), 'Objects retrieved from the cache must be equal but not necessarily the same reference');
} else {
$this->assertSame($value, $cache->fetch('key'), 'Scalar and array data retrieved from the cache must be the same as the original, e.g. same type');
}
// Test deleting a value
$this->assertTrue($cache->delete('key'));
$this->assertFalse($cache->contains('key'));
$this->assertFalse($cache->fetch('key'));
}
/**
* @dataProvider provideDataToCache
*/
public function testUpdateExistingEntry($value)
{
$cache = $this->_getCacheDriver();
$this->assertTrue($cache->save('key', 'old-value'));
$this->assertTrue($cache->contains('key'));
$this->assertTrue($cache->save('key', $value));
$this->assertTrue($cache->contains('key'));
if (is_object($value)) {
$this->assertEquals($value, $cache->fetch('key'), 'Objects retrieved from the cache must be equal but not necessarily the same reference');
} else {
$this->assertSame($value, $cache->fetch('key'), 'Scalar and array data retrieved from the cache must be the same as the original, e.g. same type');
}
}
public function testFetchMulti()
{
$cache = $this->_getCacheDriver();
$cache->deleteAll();
// Test saving some values, checking if it exists, and fetching it back with multiGet
$this->assertTrue($cache->save('key1', 'value1'));
$this->assertTrue($cache->save('key2', 'value2'));
$this->assertEquals(
array('key1' => 'value1', 'key2' => 'value2'),
$cache->fetchMultiple(array('key1', 'key2'))
);
$this->assertEquals(
array('key1' => 'value1', 'key2' => 'value2'),
$cache->fetchMultiple(array('key1', 'key3', 'key2'))
);
$this->assertEquals(
array('key1' => 'value1', 'key2' => 'value2'),
$cache->fetchMultiple(array('key1', 'key2', 'key3'))
);
}
public function testFetchMultiWithEmptyKeysArray()
{
$cache = $this->_getCacheDriver();
$this->assertEmpty(
$cache->fetchMultiple(array())
);
}
public function testFetchMultiWithFalsey()
{
$cache = $this->_getCacheDriver();
$cache->deleteAll();
$values = array(
'string' => 'str',
'integer' => 1,
'boolean' => true,
'null' => null,
'array_empty' => array(),
'integer_zero' => 0,
'string_empty' => ''
);
foreach ($values AS $key => $value) {
$cache->save($key, $value);
}
$this->assertEquals(
$values,
$cache->fetchMultiple(array_keys($values))
);
}
public function provideDataToCache()
{
return array(
'array' => array(array('one', 2, 3.01)),
'string' => array('value'),
'integer' => array(1),
'float' => array(1.5),
'object' => array(new ArrayObject()),
'true' => array(true),
// the following are considered FALSE in boolean context, but caches should still recognize their existence
'null' => array(null),
'false' => array(false),
'array_empty' => array(array()),
'string_zero' => array('0'),
'integer_zero' => array(0),
'float_zero' => array(0.0),
'string_empty' => array('')
);
}
public function testDeleteIsSuccessfulWhenKeyDoesNotExist()
{
$cache = $this->_getCacheDriver();
$this->assertFalse($cache->contains('key'));
$this->assertTrue($cache->delete('key'));
}
public function testDeleteAll()
{
$cache = $this->_getCacheDriver();
$this->assertTrue($cache->save('key1', 1));
$this->assertTrue($cache->save('key2', 2));
$this->assertTrue($cache->deleteAll());
$this->assertFalse($cache->contains('key1'));
$this->assertFalse($cache->contains('key2'));
}
public function testDeleteAllAndNamespaceVersioningBetweenCaches()
{
if ( ! $this->isSharedStorage()) {
$this->markTestSkipped('The ' . __CLASS__ .' does not use shared storage');
}
$cache1 = $this->_getCacheDriver();
$cache2 = $this->_getCacheDriver();
$this->assertTrue($cache1->save('key1', 1));
$this->assertTrue($cache2->save('key2', 2));
/* Both providers are initialized with the same namespace version, so
* they can see entries set by each other.
*/
$this->assertTrue($cache1->contains('key1'));
$this->assertTrue($cache1->contains('key2'));
$this->assertTrue($cache2->contains('key1'));
$this->assertTrue($cache2->contains('key2'));
/* Deleting all entries through one provider will only increment the
* namespace version on that object (and in the cache itself, which new
* instances will use to initialize). The second provider will retain
* its original version and still see stale data.
*/
$this->assertTrue($cache1->deleteAll());
$this->assertFalse($cache1->contains('key1'));
$this->assertFalse($cache1->contains('key2'));
$this->assertTrue($cache2->contains('key1'));
$this->assertTrue($cache2->contains('key2'));
/* A new cache provider should not see the deleted entries, since its
* namespace version will be initialized.
*/
$cache3 = $this->_getCacheDriver();
$this->assertFalse($cache3->contains('key1'));
$this->assertFalse($cache3->contains('key2'));
}
public function testFlushAll()
{
$cache = $this->_getCacheDriver();
$this->assertTrue($cache->save('key1', 1));
$this->assertTrue($cache->save('key2', 2));
$this->assertTrue($cache->flushAll());
$this->assertFalse($cache->contains('key1'));
$this->assertFalse($cache->contains('key2'));
}
public function testFlushAllAndNamespaceVersioningBetweenCaches()
{
if ( ! $this->isSharedStorage()) {
$this->markTestSkipped('The ' . __CLASS__ .' does not use shared storage');
}
$cache1 = $this->_getCacheDriver();
$cache2 = $this->_getCacheDriver();
/* Deleting all elements from the first provider should increment its
* namespace version before saving the first entry.
*/
$cache1->deleteAll();
$this->assertTrue($cache1->save('key1', 1));
/* The second provider will be initialized with the same namespace
* version upon its first save operation.
*/
$this->assertTrue($cache2->save('key2', 2));
/* Both providers have the same namespace version and can see entries
* set by each other.
*/
$this->assertTrue($cache1->contains('key1'));
$this->assertTrue($cache1->contains('key2'));
$this->assertTrue($cache2->contains('key1'));
$this->assertTrue($cache2->contains('key2'));
/* Flushing all entries through one cache will remove all entries from
* the cache but leave their namespace version as-is.
*/
$this->assertTrue($cache1->flushAll());
$this->assertFalse($cache1->contains('key1'));
$this->assertFalse($cache1->contains('key2'));
$this->assertFalse($cache2->contains('key1'));
$this->assertFalse($cache2->contains('key2'));
/* Inserting a new entry will use the same, incremented namespace
* version, and it will be visible to both providers.
*/
$this->assertTrue($cache1->save('key1', 1));
$this->assertTrue($cache1->contains('key1'));
$this->assertTrue($cache2->contains('key1'));
/* A new cache provider will be initialized with the original namespace
* version and not share any visibility with the first two providers.
*/
$cache3 = $this->_getCacheDriver();
$this->assertFalse($cache3->contains('key1'));
$this->assertFalse($cache3->contains('key2'));
$this->assertTrue($cache3->save('key3', 3));
$this->assertTrue($cache3->contains('key3'));
}
public function testNamespace()
{
$cache = $this->_getCacheDriver();
$cache->setNamespace('ns1_');
$this->assertTrue($cache->save('key1', 1));
$this->assertTrue($cache->contains('key1'));
$cache->setNamespace('ns2_');
$this->assertFalse($cache->contains('key1'));
}
public function testDeleteAllNamespace()
{
$cache = $this->_getCacheDriver();
$cache->setNamespace('ns1');
$this->assertFalse($cache->contains('key1'));
$cache->save('key1', 'test');
$this->assertTrue($cache->contains('key1'));
$cache->setNamespace('ns2');
$this->assertFalse($cache->contains('key1'));
$cache->save('key1', 'test');
$this->assertTrue($cache->contains('key1'));
$cache->setNamespace('ns1');
$this->assertTrue($cache->contains('key1'));
$cache->deleteAll();
$this->assertFalse($cache->contains('key1'));
$cache->setNamespace('ns2');
$this->assertTrue($cache->contains('key1'));
$cache->deleteAll();
$this->assertFalse($cache->contains('key1'));
}
/**
* @group DCOM-43
*/
public function testGetStats()
{
$cache = $this->_getCacheDriver();
$stats = $cache->getStats();
$this->assertArrayHasKey(Cache::STATS_HITS, $stats);
$this->assertArrayHasKey(Cache::STATS_MISSES, $stats);
$this->assertArrayHasKey(Cache::STATS_UPTIME, $stats);
$this->assertArrayHasKey(Cache::STATS_MEMORY_USAGE, $stats);
$this->assertArrayHasKey(Cache::STATS_MEMORY_AVAILABLE, $stats);
}
public function testFetchMissShouldReturnFalse()
{
$cache = $this->_getCacheDriver();
/* Ensure that caches return boolean false instead of null on a fetch
* miss to be compatible with ORM integration.
*/
$result = $cache->fetch('nonexistent_key');
$this->assertFalse($result);
$this->assertNotNull($result);
}
/**
* Check to see that objects are correctly serialized and unserialized by the cache
* provider.
*/
public function testCachedObject()
{
$cache = $this->_getCacheDriver();
$cache->deleteAll();
$obj = new \stdClass();
$obj->foo = "bar";
$obj2 = new \stdClass();
$obj2->bar = "foo";
$obj2->obj = $obj;
$obj->obj2 = $obj2;
$cache->save("obj", $obj);
$fetched = $cache->fetch("obj");
$this->assertInstanceOf("stdClass", $obj);
$this->assertInstanceOf("stdClass", $obj->obj2);
$this->assertInstanceOf("stdClass", $obj->obj2->obj);
$this->assertEquals("bar", $fetched->foo);
$this->assertEquals("foo", $fetched->obj2->bar);
}
/**
* Check to see that objects fetched via fetchMultiple are properly unserialized
*/
public function testFetchMultipleObjects()
{
$cache = $this->_getCacheDriver();
$cache->deleteAll();
$obj1 = new \stdClass();
$obj1->foo = "bar";
$cache->save("obj1", $obj1);
$obj2 = new \stdClass();
$obj2->bar = "baz";
$cache->save("obj2", $obj2);
$fetched = $cache->fetchMultiple(array("obj1", "obj2"));
$this->assertInstanceOf("stdClass", $fetched["obj1"]);
$this->assertInstanceOf("stdClass", $fetched["obj2"]);
$this->assertEquals("bar", $fetched["obj1"]->foo);
$this->assertEquals("baz", $fetched["obj2"]->bar);
}
/**
* Return whether multiple cache providers share the same storage.
*
* This is used for skipping certain tests for shared storage behavior.
*
* @return boolean
*/
protected function isSharedStorage()
{
return true;
}
/**
* @return \Doctrine\Common\Cache\CacheProvider
*/
abstract protected function _getCacheDriver();
}