mirror of
https://github.com/yiisoft/yii2.git
synced 2025-08-26 14:26:54 +08:00
added tests for cache
This commit is contained in:
@ -104,7 +104,7 @@ class DbCache extends Cache
|
|||||||
$query = new Query;
|
$query = new Query;
|
||||||
$query->select(array('data'))
|
$query->select(array('data'))
|
||||||
->from($this->cacheTableName)
|
->from($this->cacheTableName)
|
||||||
->where('id = :id AND (expire = 0 OR expire > :time)', array(':id' => $key, ':time' => time()));
|
->where('id = :id AND (expire = 0 OR expire >' . time() . ')', array(':id' => $key));
|
||||||
$db = $this->getDb();
|
$db = $this->getDb();
|
||||||
if ($db->enableQueryCache) {
|
if ($db->enableQueryCache) {
|
||||||
// temporarily disable and re-enable query caching
|
// temporarily disable and re-enable query caching
|
||||||
@ -131,7 +131,7 @@ class DbCache extends Cache
|
|||||||
$query->select(array('id', 'data'))
|
$query->select(array('id', 'data'))
|
||||||
->from($this->cacheTableName)
|
->from($this->cacheTableName)
|
||||||
->where(array('id' => $keys))
|
->where(array('id' => $keys))
|
||||||
->andWhere("expire = 0 OR expire > " . time() . ")");
|
->andWhere('(expire = 0 OR expire > ' . time() . ')');
|
||||||
|
|
||||||
$db = $this->getDb();
|
$db = $this->getDb();
|
||||||
if ($db->enableQueryCache) {
|
if ($db->enableQueryCache) {
|
||||||
|
1
tests/unit/.gitignore
vendored
Normal file
1
tests/unit/.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
runtime/cache/*
|
27
tests/unit/framework/caching/ApcCacheTest.php
Normal file
27
tests/unit/framework/caching/ApcCacheTest.php
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
<?php
|
||||||
|
namespace yiiunit\framework\caching;
|
||||||
|
use yii\caching\ApcCache;
|
||||||
|
use yiiunit\TestCase;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class for testing APC cache backend
|
||||||
|
*/
|
||||||
|
class ApcCacheTest extends CacheTest
|
||||||
|
{
|
||||||
|
private $_cacheInstance = null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return ApcCache
|
||||||
|
*/
|
||||||
|
protected function getCacheInstance()
|
||||||
|
{
|
||||||
|
if(!extension_loaded("apc")) {
|
||||||
|
$this->markTestSkipped("APC not installed. Skipping.");
|
||||||
|
}
|
||||||
|
|
||||||
|
if($this->_cacheInstance === null) {
|
||||||
|
$this->_cacheInstance = new ApcCache();
|
||||||
|
}
|
||||||
|
return $this->_cacheInstance;
|
||||||
|
}
|
||||||
|
}
|
82
tests/unit/framework/caching/CacheTest.php
Normal file
82
tests/unit/framework/caching/CacheTest.php
Normal file
@ -0,0 +1,82 @@
|
|||||||
|
<?php
|
||||||
|
namespace yiiunit\framework\caching;
|
||||||
|
use yiiunit\TestCase;
|
||||||
|
use yii\caching\Cache;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Base class for testing cache backends
|
||||||
|
*/
|
||||||
|
abstract class CacheTest extends TestCase
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @return Cache
|
||||||
|
*/
|
||||||
|
abstract protected function getCacheInstance();
|
||||||
|
|
||||||
|
public function testSet()
|
||||||
|
{
|
||||||
|
$cache = $this->getCacheInstance();
|
||||||
|
$cache->set('string_test', 'string_test');
|
||||||
|
$cache->set('number_test', 42);
|
||||||
|
$cache->set('array_test', array('array_test' => 'array_test'));
|
||||||
|
$cache['arrayaccess_test'] = new \stdClass();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testGet()
|
||||||
|
{
|
||||||
|
$cache = $this->getCacheInstance();
|
||||||
|
$this->assertEquals('string_test', $cache->get('string_test'));
|
||||||
|
|
||||||
|
$this->assertEquals(42, $cache->get('number_test'));
|
||||||
|
|
||||||
|
$array = $cache->get('array_test');
|
||||||
|
$this->assertArrayHasKey('array_test', $array);
|
||||||
|
$this->assertEquals('array_test', $array['array_test']);
|
||||||
|
|
||||||
|
$this->assertInstanceOf('stdClass', $cache['arrayaccess_test']);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testMget()
|
||||||
|
{
|
||||||
|
$cache = $this->getCacheInstance();
|
||||||
|
$this->assertEquals(array('string_test' => 'string_test', 'number_test' => 42), $cache->mget(array('string_test', 'number_test')));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testExpire()
|
||||||
|
{
|
||||||
|
$cache = $this->getCacheInstance();
|
||||||
|
$cache->set('expire_test', 'expire_test', 2);
|
||||||
|
sleep(1);
|
||||||
|
$this->assertEquals('expire_test', $cache->get('expire_test'));
|
||||||
|
sleep(2);
|
||||||
|
$this->assertEquals(false, $cache->get('expire_test'));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testAdd()
|
||||||
|
{
|
||||||
|
$cache = $this->getCacheInstance();
|
||||||
|
|
||||||
|
// should not change existing keys
|
||||||
|
$cache->add('number_test', 13);
|
||||||
|
$this->assertEquals(42, $cache->get('number_test'));
|
||||||
|
|
||||||
|
// should store data is it's not there yet
|
||||||
|
$cache->add('add_test', 13);
|
||||||
|
$this->assertEquals(13, $cache->get('add_test'));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testDelete()
|
||||||
|
{
|
||||||
|
$cache = $this->getCacheInstance();
|
||||||
|
|
||||||
|
$cache->delete('number_test');
|
||||||
|
$this->assertEquals(null, $cache->get('number_test'));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testFlush()
|
||||||
|
{
|
||||||
|
$cache = $this->getCacheInstance();
|
||||||
|
$cache->flush();
|
||||||
|
$this->assertEquals(null, $cache->get('add_test'));
|
||||||
|
}
|
||||||
|
}
|
70
tests/unit/framework/caching/DbCacheTest.php
Normal file
70
tests/unit/framework/caching/DbCacheTest.php
Normal file
@ -0,0 +1,70 @@
|
|||||||
|
<?php
|
||||||
|
namespace yiiunit\framework\caching;
|
||||||
|
use yii\caching\DbCache;
|
||||||
|
use yiiunit\TestCase;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class for testing file cache backend
|
||||||
|
*/
|
||||||
|
class DbCacheTest extends CacheTest
|
||||||
|
{
|
||||||
|
private $_cacheInstance;
|
||||||
|
private $_connection;
|
||||||
|
|
||||||
|
function __construct()
|
||||||
|
{
|
||||||
|
if (!extension_loaded('pdo') || !extension_loaded('pdo_mysql')) {
|
||||||
|
$this->markTestSkipped('pdo and pdo_mysql extensions are required.');
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->getConnection()->createCommand("
|
||||||
|
CREATE TABLE IF NOT EXISTS tbl_cache (
|
||||||
|
id char(128) NOT NULL,
|
||||||
|
expire int(11) DEFAULT NULL,
|
||||||
|
data LONGBLOB,
|
||||||
|
PRIMARY KEY (id),
|
||||||
|
KEY expire (expire)
|
||||||
|
);
|
||||||
|
")->execute();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param bool $reset whether to clean up the test database
|
||||||
|
* @return \yii\db\Connection
|
||||||
|
*/
|
||||||
|
function getConnection($reset = true)
|
||||||
|
{
|
||||||
|
if($this->_connection === null) {
|
||||||
|
$params = $this->getParam('mysql');
|
||||||
|
$db = new \yii\db\Connection;
|
||||||
|
$db->dsn = $params['dsn'];
|
||||||
|
$db->username = $params['username'];
|
||||||
|
$db->password = $params['password'];
|
||||||
|
if ($reset) {
|
||||||
|
$db->open();
|
||||||
|
$lines = explode(';', file_get_contents($params['fixture']));
|
||||||
|
foreach ($lines as $line) {
|
||||||
|
if (trim($line) !== '') {
|
||||||
|
$db->pdo->exec($line);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$this->_connection = $db;
|
||||||
|
}
|
||||||
|
return $this->_connection;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return DbCache
|
||||||
|
*/
|
||||||
|
protected function getCacheInstance()
|
||||||
|
{
|
||||||
|
if($this->_cacheInstance === null) {
|
||||||
|
$this->_cacheInstance = new DbCache(array(
|
||||||
|
'db' => $this->getConnection(),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
return $this->_cacheInstance;
|
||||||
|
}
|
||||||
|
}
|
25
tests/unit/framework/caching/FileCacheTest.php
Normal file
25
tests/unit/framework/caching/FileCacheTest.php
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
<?php
|
||||||
|
namespace yiiunit\framework\caching;
|
||||||
|
use yii\caching\FileCache;
|
||||||
|
use yiiunit\TestCase;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class for testing file cache backend
|
||||||
|
*/
|
||||||
|
class FileCacheTest extends CacheTest
|
||||||
|
{
|
||||||
|
private $_cacheInstance = null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return FileCache
|
||||||
|
*/
|
||||||
|
protected function getCacheInstance()
|
||||||
|
{
|
||||||
|
if($this->_cacheInstance === null) {
|
||||||
|
$this->_cacheInstance = new FileCache(array(
|
||||||
|
'cachePath' => '@yiiunit/runtime/cache',
|
||||||
|
));
|
||||||
|
}
|
||||||
|
return $this->_cacheInstance;
|
||||||
|
}
|
||||||
|
}
|
27
tests/unit/framework/caching/MemCacheTest.php
Normal file
27
tests/unit/framework/caching/MemCacheTest.php
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
<?php
|
||||||
|
namespace yiiunit\framework\caching;
|
||||||
|
use yii\caching\MemCache;
|
||||||
|
use yiiunit\TestCase;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class for testing memcache cache backend
|
||||||
|
*/
|
||||||
|
class MemCacheTest extends CacheTest
|
||||||
|
{
|
||||||
|
private $_cacheInstance = null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return MemCache
|
||||||
|
*/
|
||||||
|
protected function getCacheInstance()
|
||||||
|
{
|
||||||
|
if(!extension_loaded("memcache")) {
|
||||||
|
$this->markTestSkipped("memcache not installed. Skipping.");
|
||||||
|
}
|
||||||
|
|
||||||
|
if($this->_cacheInstance === null) {
|
||||||
|
$this->_cacheInstance = new MemCache();
|
||||||
|
}
|
||||||
|
return $this->_cacheInstance;
|
||||||
|
}
|
||||||
|
}
|
29
tests/unit/framework/caching/MemCachedTest.php
Normal file
29
tests/unit/framework/caching/MemCachedTest.php
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
<?php
|
||||||
|
namespace yiiunit\framework\caching;
|
||||||
|
use yii\caching\MemCache;
|
||||||
|
use yiiunit\TestCase;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class for testing memcache cache backend
|
||||||
|
*/
|
||||||
|
class MemCachedTest extends CacheTest
|
||||||
|
{
|
||||||
|
private $_cacheInstance = null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return MemCache
|
||||||
|
*/
|
||||||
|
protected function getCacheInstance()
|
||||||
|
{
|
||||||
|
if(!extension_loaded("memcached")) {
|
||||||
|
$this->markTestSkipped("memcached not installed. Skipping.");
|
||||||
|
}
|
||||||
|
|
||||||
|
if($this->_cacheInstance === null) {
|
||||||
|
$this->_cacheInstance = new MemCache(array(
|
||||||
|
'useMemcached' => true,
|
||||||
|
));
|
||||||
|
}
|
||||||
|
return $this->_cacheInstance;
|
||||||
|
}
|
||||||
|
}
|
31
tests/unit/framework/caching/WinCacheTest.php
Normal file
31
tests/unit/framework/caching/WinCacheTest.php
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
<?php
|
||||||
|
namespace yiiunit\framework\caching;
|
||||||
|
use yii\caching\FileCache;
|
||||||
|
use yiiunit\TestCase;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class for testing wincache backend
|
||||||
|
*/
|
||||||
|
class WinCacheTest extends CacheTest
|
||||||
|
{
|
||||||
|
private $_cacheInstance = null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return WinCache
|
||||||
|
*/
|
||||||
|
protected function getCacheInstance()
|
||||||
|
{
|
||||||
|
if(!extension_loaded('wincache')) {
|
||||||
|
$this->markTestSkipped("Wincache not installed. Skipping.");
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!ini_get('wincache.ucenabled')) {
|
||||||
|
$this->markTestSkipped("Wincache user cache disabled. Skipping.");
|
||||||
|
}
|
||||||
|
|
||||||
|
if($this->_cacheInstance === null) {
|
||||||
|
$this->_cacheInstance = new WinCache();
|
||||||
|
}
|
||||||
|
return $this->_cacheInstance;
|
||||||
|
}
|
||||||
|
}
|
27
tests/unit/framework/caching/XCacheTest.php
Normal file
27
tests/unit/framework/caching/XCacheTest.php
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
<?php
|
||||||
|
namespace yiiunit\framework\caching;
|
||||||
|
use yii\caching\XCache;
|
||||||
|
use yiiunit\TestCase;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class for testing xcache backend
|
||||||
|
*/
|
||||||
|
class XCacheTest extends CacheTest
|
||||||
|
{
|
||||||
|
private $_cacheInstance = null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return XCache
|
||||||
|
*/
|
||||||
|
protected function getCacheInstance()
|
||||||
|
{
|
||||||
|
if(!function_exists("xcache_isset")) {
|
||||||
|
$this->markTestSkipped("XCache not installed. Skipping.");
|
||||||
|
}
|
||||||
|
|
||||||
|
if($this->_cacheInstance === null) {
|
||||||
|
$this->_cacheInstance = new XCache();
|
||||||
|
}
|
||||||
|
return $this->_cacheInstance;
|
||||||
|
}
|
||||||
|
}
|
27
tests/unit/framework/caching/ZendDataCacheTest.php
Normal file
27
tests/unit/framework/caching/ZendDataCacheTest.php
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
<?php
|
||||||
|
namespace yiiunit\framework\caching;
|
||||||
|
use yii\caching\ZendDataCache;
|
||||||
|
use yiiunit\TestCase;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class for testing Zend cache backend
|
||||||
|
*/
|
||||||
|
class ZendDataCacheTest extends CacheTest
|
||||||
|
{
|
||||||
|
private $_cacheInstance = null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return ZendDataCache
|
||||||
|
*/
|
||||||
|
protected function getCacheInstance()
|
||||||
|
{
|
||||||
|
if(!function_exists("zend_shm_cache_store")) {
|
||||||
|
$this->markTestSkipped("Zend Data cache not installed. Skipping.");
|
||||||
|
}
|
||||||
|
|
||||||
|
if($this->_cacheInstance === null) {
|
||||||
|
$this->_cacheInstance = new ZendDataCache();
|
||||||
|
}
|
||||||
|
return $this->_cacheInstance;
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user