Commit 5d3684fd by Paul Klimov

Unit test for MongoDB Cache advanced.

parent 9ca62131
......@@ -11,7 +11,23 @@ use Yii;
use yii\base\InvalidConfigException;
/**
* Class Cache
* Cache implements a cache application component by storing cached data in a MongoDB.
*
* By default, Cache stores session data in a MongoDB collection named 'cache' inside the default database.
* This collection is better to be pre-created with fields 'id' and 'expire' indexed.
* The table name can be changed by setting [[cacheCollection]].
*
* Please refer to [[\yii\caching\Cache]] for common cache operations that are supported by Cache.
*
* The following example shows how you can configure the application to use Cache:
*
* ~~~
* 'cache' => [
* 'class' => 'yii\mongodb\Cache',
* // 'db' => 'mymongodb',
* // 'cacheCollection' => 'my_cache',
* ]
* ~~~
*
* @author Paul Klimov <klimov.paul@gmail.com>
* @since 2.0
......@@ -26,6 +42,7 @@ class Cache extends \yii\caching\Cache
public $db = 'mongodb';
/**
* @var string|array the name of the MongoDB collection that stores the cache data.
* This collection is better to be pre-created with fields 'id' and 'expire' indexed.
*/
public $cacheCollection = 'cache';
/**
......
......@@ -28,6 +28,7 @@ class CacheTest extends MongoDbTestCase
'class' => Cache::className(),
'db' => $this->getConnection(),
'cacheCollection' => static::$cacheCollection,
'gcProbability' => 0,
]);
}
......@@ -40,5 +41,95 @@ class CacheTest extends MongoDbTestCase
$key = 'test_key';
$value = 'test_value';
$this->assertTrue($cache->set($key, $value), 'Unable to set value!');
$this->assertEquals($value, $cache->get($key), 'Unable to set value correctly!');
$newValue = 'test_new_value';
$this->assertTrue($cache->set($key, $newValue), 'Unable to update value!');
$this->assertEquals($newValue, $cache->get($key), 'Unable to update value correctly!');
}
public function testAdd()
{
$cache = $this->createCache();
$key = 'test_key';
$value = 'test_value';
$this->assertTrue($cache->add($key, $value), 'Unable to add value!');
$this->assertEquals($value, $cache->get($key), 'Unable to add value correctly!');
$newValue = 'test_new_value';
$this->assertTrue($cache->add($key, $newValue), 'Unable to re-add value!');
$this->assertEquals($value, $cache->get($key), 'Original value is lost!');
}
/**
* @depends testSet
*/
public function testDelete()
{
$cache = $this->createCache();
$key = 'test_key';
$value = 'test_value';
$cache->set($key, $value);
$this->assertTrue($cache->delete($key), 'Unable to delete key!');
$this->assertEquals(false, $cache->get($key), 'Value is not deleted!');
}
/**
* @depends testSet
*/
public function testFlush()
{
$cache = $this->createCache();
$cache->set('key1', 'value1');
$cache->set('key2', 'value2');
$this->assertTrue($cache->flush(), 'Unable to flush cache!');
$collection = $cache->db->getCollection($cache->cacheCollection);
$rows = $this->findAll($collection);
$this->assertCount(0, $rows, 'Unable to flush records!');
}
/**
* @depends testSet
*/
public function testGc()
{
$cache = $this->createCache();
$cache->set('key1', 'value1');
$cache->set('key2', 'value2');
$collection = $cache->db->getCollection($cache->cacheCollection);
list($row) = $this->findAll($collection);
$collection->update(['_id' => $row['_id']], ['expire' => time() - 10]);
$cache->gc(true);
$rows = $this->findAll($collection);
$this->assertCount(1, $rows, 'Unable to collect garbage!');
}
/**
* @depends testSet
*/
public function testGetExpired()
{
$cache = $this->createCache();
$key = 'test_key';
$value = 'test_value';
$cache->set($key, $value);
$collection = $cache->db->getCollection($cache->cacheCollection);
list($row) = $this->findAll($collection);
$collection->update(['_id' => $row['_id']], ['expire' => time() - 10]);
$this->assertEquals(false, $cache->get($key), 'Expired key value returned!');
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment