View source
<?php
namespace Doctrine\Tests\Common\Cache;
use Doctrine\Common\Cache\Cache;
use ArrayObject;
abstract class CacheTest extends \Doctrine\Tests\DoctrineTestCase {
public function testBasicCrudOperations($value) {
$cache = $this
->_getCacheDriver();
$this
->assertTrue($cache
->save('key', 'value'));
$this
->assertTrue($cache
->contains('key'));
$this
->assertEquals('value', $cache
->fetch('key'));
$this
->assertTrue($cache
->save('key', 'value-changed'));
$this
->assertTrue($cache
->contains('key'));
$this
->assertEquals('value-changed', $cache
->fetch('key'));
$this
->assertTrue($cache
->delete('key'));
$this
->assertFalse($cache
->contains('key'));
}
public function testFetchMulti() {
$cache = $this
->_getCacheDriver();
$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 testFetchMultiWillFilterNonRequestedKeys() {
$cache = $this
->getMockForAbstractClass('Doctrine\\Common\\Cache\\CacheProvider', array(), '', true, true, true, array(
'doFetchMultiple',
));
$cache
->expects($this
->once())
->method('doFetchMultiple')
->will($this
->returnValue(array(
'[foo][]' => 'bar',
'[bar][]' => 'baz',
'[baz][]' => 'tab',
)));
$this
->assertEquals(array(
'foo' => 'bar',
'bar' => 'baz',
), $cache
->fetchMultiple(array(
'foo',
'bar',
)));
}
public function provideCrudValues() {
return array(
'array' => array(
array(
'one',
2,
3.0,
),
),
'string' => array(
'value',
),
'integer' => array(
1,
),
'float' => array(
1.5,
),
'object' => array(
new ArrayObject(),
),
'null' => array(
null,
),
);
}
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));
$this
->assertTrue($cache1
->contains('key1'));
$this
->assertTrue($cache1
->contains('key2'));
$this
->assertTrue($cache2
->contains('key1'));
$this
->assertTrue($cache2
->contains('key2'));
$this
->assertTrue($cache1
->deleteAll());
$this
->assertFalse($cache1
->contains('key1'));
$this
->assertFalse($cache1
->contains('key2'));
$this
->assertTrue($cache2
->contains('key1'));
$this
->assertTrue($cache2
->contains('key2'));
$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();
$cache1
->deleteAll();
$this
->assertTrue($cache1
->save('key1', 1));
$this
->assertTrue($cache2
->save('key2', 2));
$this
->assertTrue($cache1
->contains('key1'));
$this
->assertTrue($cache1
->contains('key2'));
$this
->assertTrue($cache2
->contains('key1'));
$this
->assertTrue($cache2
->contains('key2'));
$this
->assertTrue($cache1
->flushAll());
$this
->assertFalse($cache1
->contains('key1'));
$this
->assertFalse($cache1
->contains('key2'));
$this
->assertFalse($cache2
->contains('key1'));
$this
->assertFalse($cache2
->contains('key2'));
$this
->assertTrue($cache1
->save('key1', 1));
$this
->assertTrue($cache1
->contains('key1'));
$this
->assertTrue($cache2
->contains('key1'));
$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'));
}
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();
$result = $cache
->fetch('nonexistent_key');
$this
->assertFalse($result);
$this
->assertNotNull($result);
}
public function testFalseCastedValues($value) {
$cache = $this
->_getCacheDriver();
$this
->assertTrue($cache
->save('key', $value));
$this
->assertTrue($cache
->contains('key'));
$this
->assertEquals($value, $cache
->fetch('key'));
}
public function falseCastedValuesProvider() {
return array(
array(
false,
),
array(
null,
),
array(
array(),
),
array(
'0',
),
array(
0,
),
array(
0.0,
),
array(
'',
),
);
}
protected function isSharedStorage() {
return true;
}
protected abstract function _getCacheDriver();
}