3 namespace Doctrine\Tests\Common\Cache;
5 use Doctrine\Common\Cache\Cache;
8 abstract class CacheTest extends \Doctrine\Tests\DoctrineTestCase
11 * @dataProvider provideDataToCache
13 public function testSetContainsFetchDelete($value)
15 $cache = $this->_getCacheDriver();
17 // Test saving a value, checking if it exists, and fetching it back
18 $this->assertTrue($cache->save('key', $value));
19 $this->assertTrue($cache->contains('key'));
20 if (is_object($value)) {
21 $this->assertEquals($value, $cache->fetch('key'), 'Objects retrieved from the cache must be equal but not necessarily the same reference');
23 $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');
26 // Test deleting a value
27 $this->assertTrue($cache->delete('key'));
28 $this->assertFalse($cache->contains('key'));
29 $this->assertFalse($cache->fetch('key'));
33 * @dataProvider provideDataToCache
35 public function testUpdateExistingEntry($value)
37 $cache = $this->_getCacheDriver();
39 $this->assertTrue($cache->save('key', 'old-value'));
40 $this->assertTrue($cache->contains('key'));
42 $this->assertTrue($cache->save('key', $value));
43 $this->assertTrue($cache->contains('key'));
44 if (is_object($value)) {
45 $this->assertEquals($value, $cache->fetch('key'), 'Objects retrieved from the cache must be equal but not necessarily the same reference');
47 $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');
51 public function testCacheKeyIsCaseSensitive()
53 $cache = $this->_getCacheDriver();
55 $this->assertTrue($cache->save('key', 'value'));
56 $this->assertTrue($cache->contains('key'));
57 $this->assertSame('value', $cache->fetch('key'));
59 $this->assertFalse($cache->contains('KEY'));
60 $this->assertFalse($cache->fetch('KEY'));
62 $cache->delete('KEY');
63 $this->assertTrue($cache->contains('key', 'Deleting cache item with different case must not affect other cache item'));
66 public function testFetchMultiple()
68 $cache = $this->_getCacheDriver();
69 $values = $this->provideDataToCache();
72 foreach ($values as $key => $value) {
73 $cache->save($key, $value[0]);
75 $saved[$key] = $value[0];
78 $keys = array_keys($saved);
82 $cache->fetchMultiple($keys),
83 'Testing fetchMultiple with different data types'
86 array_slice($saved, 0, 1),
87 $cache->fetchMultiple(array_slice($keys, 0, 1)),
88 'Testing fetchMultiple with a single key'
91 $keysWithNonExisting = array();
92 $keysWithNonExisting[] = 'non_existing1';
93 $keysWithNonExisting[] = $keys[0];
94 $keysWithNonExisting[] = 'non_existing2';
95 $keysWithNonExisting[] = $keys[1];
96 $keysWithNonExisting[] = 'non_existing3';
99 array_slice($saved, 0, 2),
100 $cache->fetchMultiple($keysWithNonExisting),
101 'Testing fetchMultiple with a subset of keys and mixed with non-existing ones'
105 public function testFetchMultipleWithNoKeys()
107 $cache = $this->_getCacheDriver();
109 $this->assertSame(array(), $cache->fetchMultiple(array()));
112 public function testSaveMultiple()
114 $cache = $this->_getCacheDriver();
117 $data = array_map(function ($value) {
119 }, $this->provideDataToCache());
121 $this->assertTrue($cache->saveMultiple($data));
123 $keys = array_keys($data);
125 $this->assertEquals($data, $cache->fetchMultiple($keys));
128 public function provideDataToCache()
130 $obj = new \stdClass();
132 $obj2 = new \stdClass();
138 'array' => array(array('one', 2, 3.01)),
139 'string' => array('value'),
140 'string_invalid_utf8' => array("\xc3\x28"),
141 'string_null_byte' => array('with'."\0".'null char'),
142 'integer' => array(1),
143 'float' => array(1.5),
144 'object' => array(new ArrayObject(array('one', 2, 3.01))),
145 'object_recursive' => array($obj),
146 'true' => array(true),
147 // the following are considered FALSE in boolean context, but caches should still recognize their existence
148 'null' => array(null),
149 'false' => array(false),
150 'array_empty' => array(array()),
151 'string_zero' => array('0'),
152 'integer_zero' => array(0),
153 'float_zero' => array(0.0),
154 'string_empty' => array(''),
158 public function testDeleteIsSuccessfulWhenKeyDoesNotExist()
160 $cache = $this->_getCacheDriver();
162 $cache->delete('key');
163 $this->assertFalse($cache->contains('key'));
164 $this->assertTrue($cache->delete('key'));
167 public function testDeleteAll()
169 $cache = $this->_getCacheDriver();
171 $this->assertTrue($cache->save('key1', 1));
172 $this->assertTrue($cache->save('key2', 2));
173 $this->assertTrue($cache->deleteAll());
174 $this->assertFalse($cache->contains('key1'));
175 $this->assertFalse($cache->contains('key2'));
179 * @dataProvider provideCacheIds
181 public function testCanHandleSpecialCacheIds($id)
183 $cache = $this->_getCacheDriver();
185 $this->assertTrue($cache->save($id, 'value'));
186 $this->assertTrue($cache->contains($id));
187 $this->assertEquals('value', $cache->fetch($id));
189 $this->assertTrue($cache->delete($id));
190 $this->assertFalse($cache->contains($id));
191 $this->assertFalse($cache->fetch($id));
194 public function testNoCacheIdCollisions()
196 $cache = $this->_getCacheDriver();
198 $ids = $this->provideCacheIds();
200 // fill cache with each id having a different value
201 foreach ($ids as $index => $id) {
202 $cache->save($id[0], $index);
205 // then check value of each cache id
206 foreach ($ids as $index => $id) {
207 $value = $cache->fetch($id[0]);
208 $this->assertNotFalse($value, sprintf('Failed to retrieve data for cache id "%s".', $id[0]));
209 if ($index !== $value) {
210 $this->fail(sprintf('Cache id "%s" collides with id "%s".', $id[0], $ids[$value][0]));
216 * Returns cache ids with special characters that should still work.
218 * For example, the characters :\/<>"*?| are not valid in Windows filenames. So they must be encoded properly.
219 * Each cache id should be considered different from the others.
223 public function provideCacheIds()
241 array('.'), // directory traversal
242 array('..'), // directory traversal
250 array(str_repeat('a', 300)), // long key
251 array(str_repeat('a', 113)),
255 public function testLifetime()
257 $cache = $this->_getCacheDriver();
258 $cache->save('expire', 'value', 1);
259 $this->assertTrue($cache->contains('expire'), 'Data should not be expired yet');
260 // @TODO should more TTL-based tests pop up, so then we should mock the `time` API instead
262 $this->assertFalse($cache->contains('expire'), 'Data should be expired');
265 public function testNoExpire()
267 $cache = $this->_getCacheDriver();
268 $cache->save('noexpire', 'value', 0);
269 // @TODO should more TTL-based tests pop up, so then we should mock the `time` API instead
271 $this->assertTrue($cache->contains('noexpire'), 'Data with lifetime of zero should not expire');
274 public function testLongLifetime()
276 $cache = $this->_getCacheDriver();
277 $cache->save('longlifetime', 'value', 30 * 24 * 3600 + 1);
278 $this->assertTrue($cache->contains('longlifetime'), 'Data with lifetime > 30 days should be accepted');
281 public function testDeleteAllAndNamespaceVersioningBetweenCaches()
283 if ( ! $this->isSharedStorage()) {
284 $this->markTestSkipped('The cache storage needs to be shared.');
287 $cache1 = $this->_getCacheDriver();
288 $cache2 = $this->_getCacheDriver();
290 $this->assertTrue($cache1->save('key1', 1));
291 $this->assertTrue($cache2->save('key2', 2));
293 /* Both providers are initialized with the same namespace version, so
294 * they can see entries set by each other.
296 $this->assertTrue($cache1->contains('key1'));
297 $this->assertTrue($cache1->contains('key2'));
298 $this->assertTrue($cache2->contains('key1'));
299 $this->assertTrue($cache2->contains('key2'));
301 /* Deleting all entries through one provider will only increment the
302 * namespace version on that object (and in the cache itself, which new
303 * instances will use to initialize). The second provider will retain
304 * its original version and still see stale data.
306 $this->assertTrue($cache1->deleteAll());
307 $this->assertFalse($cache1->contains('key1'));
308 $this->assertFalse($cache1->contains('key2'));
309 $this->assertTrue($cache2->contains('key1'));
310 $this->assertTrue($cache2->contains('key2'));
312 /* A new cache provider should not see the deleted entries, since its
313 * namespace version will be initialized.
315 $cache3 = $this->_getCacheDriver();
316 $this->assertFalse($cache3->contains('key1'));
317 $this->assertFalse($cache3->contains('key2'));
320 public function testFlushAll()
322 $cache = $this->_getCacheDriver();
324 $this->assertTrue($cache->save('key1', 1));
325 $this->assertTrue($cache->save('key2', 2));
326 $this->assertTrue($cache->flushAll());
327 $this->assertFalse($cache->contains('key1'));
328 $this->assertFalse($cache->contains('key2'));
331 public function testFlushAllAndNamespaceVersioningBetweenCaches()
333 if ( ! $this->isSharedStorage()) {
334 $this->markTestSkipped('The cache storage needs to be shared.');
337 $cache1 = $this->_getCacheDriver();
338 $cache2 = $this->_getCacheDriver();
340 /* Deleting all elements from the first provider should increment its
341 * namespace version before saving the first entry.
343 $cache1->deleteAll();
344 $this->assertTrue($cache1->save('key1', 1));
346 /* The second provider will be initialized with the same namespace
347 * version upon its first save operation.
349 $this->assertTrue($cache2->save('key2', 2));
351 /* Both providers have the same namespace version and can see entries
354 $this->assertTrue($cache1->contains('key1'));
355 $this->assertTrue($cache1->contains('key2'));
356 $this->assertTrue($cache2->contains('key1'));
357 $this->assertTrue($cache2->contains('key2'));
359 /* Flushing all entries through one cache will remove all entries from
360 * the cache but leave their namespace version as-is.
362 $this->assertTrue($cache1->flushAll());
363 $this->assertFalse($cache1->contains('key1'));
364 $this->assertFalse($cache1->contains('key2'));
365 $this->assertFalse($cache2->contains('key1'));
366 $this->assertFalse($cache2->contains('key2'));
368 /* Inserting a new entry will use the same, incremented namespace
369 * version, and it will be visible to both providers.
371 $this->assertTrue($cache1->save('key1', 1));
372 $this->assertTrue($cache1->contains('key1'));
373 $this->assertTrue($cache2->contains('key1'));
375 /* A new cache provider will be initialized with the original namespace
376 * version and not share any visibility with the first two providers.
378 $cache3 = $this->_getCacheDriver();
379 $this->assertFalse($cache3->contains('key1'));
380 $this->assertFalse($cache3->contains('key2'));
381 $this->assertTrue($cache3->save('key3', 3));
382 $this->assertTrue($cache3->contains('key3'));
385 public function testNamespace()
387 $cache = $this->_getCacheDriver();
389 $cache->setNamespace('ns1_');
391 $this->assertTrue($cache->save('key1', 1));
392 $this->assertTrue($cache->contains('key1'));
394 $cache->setNamespace('ns2_');
396 $this->assertFalse($cache->contains('key1'));
399 public function testDeleteAllNamespace()
401 $cache = $this->_getCacheDriver();
403 $cache->setNamespace('ns1');
404 $this->assertFalse($cache->contains('key1'));
405 $cache->save('key1', 'test');
406 $this->assertTrue($cache->contains('key1'));
408 $cache->setNamespace('ns2');
409 $this->assertFalse($cache->contains('key1'));
410 $cache->save('key1', 'test');
411 $this->assertTrue($cache->contains('key1'));
413 $cache->setNamespace('ns1');
414 $this->assertTrue($cache->contains('key1'));
416 $this->assertFalse($cache->contains('key1'));
418 $cache->setNamespace('ns2');
419 $this->assertTrue($cache->contains('key1'));
421 $this->assertFalse($cache->contains('key1'));
427 public function testGetStats()
429 $cache = $this->_getCacheDriver();
430 $stats = $cache->getStats();
432 $this->assertArrayHasKey(Cache::STATS_HITS, $stats);
433 $this->assertArrayHasKey(Cache::STATS_MISSES, $stats);
434 $this->assertArrayHasKey(Cache::STATS_UPTIME, $stats);
435 $this->assertArrayHasKey(Cache::STATS_MEMORY_USAGE, $stats);
436 $this->assertArrayHasKey(Cache::STATS_MEMORY_AVAILABLE, $stats);
439 public function testSaveReturnsTrueWithAndWithoutTTlSet()
441 $cache = $this->_getCacheDriver();
443 $this->assertTrue($cache->save('without_ttl', 'without_ttl'));
444 $this->assertTrue($cache->save('with_ttl', 'with_ttl', 3600));
447 public function testValueThatIsFalseBooleanIsProperlyRetrieved()
449 $cache = $this->_getCacheDriver();
452 $this->assertTrue($cache->save('key1', false));
453 $this->assertTrue($cache->contains('key1'));
454 $this->assertFalse($cache->fetch('key1'));
458 * Return whether multiple cache providers share the same storage.
460 * This is used for skipping certain tests for shared storage behavior.
464 protected function isSharedStorage()
470 * @return \Doctrine\Common\Cache\CacheProvider
472 abstract protected function _getCacheDriver();