You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
100 lines
2.7 KiB
100 lines
2.7 KiB
12 years ago
|
<?php
|
||
12 years ago
|
|
||
12 years ago
|
namespace yiiunit\framework\caching;
|
||
12 years ago
|
|
||
12 years ago
|
use yii\caching\DbCache;
|
||
|
|
||
|
/**
|
||
|
* Class for testing file cache backend
|
||
11 years ago
|
* @group db
|
||
|
* @group caching
|
||
12 years ago
|
*/
|
||
12 years ago
|
class DbCacheTest extends CacheTestCase
|
||
12 years ago
|
{
|
||
11 years ago
|
private $_cacheInstance;
|
||
|
private $_connection;
|
||
12 years ago
|
|
||
11 years ago
|
protected function setUp()
|
||
|
{
|
||
|
if (!extension_loaded('pdo') || !extension_loaded('pdo_mysql')) {
|
||
|
$this->markTestSkipped('pdo and pdo_mysql extensions are required.');
|
||
|
}
|
||
12 years ago
|
|
||
11 years ago
|
parent::setUp();
|
||
12 years ago
|
|
||
11 years ago
|
$this->getConnection()->createCommand("
|
||
11 years ago
|
CREATE TABLE IF NOT EXISTS cache (
|
||
11 years ago
|
id char(128) NOT NULL,
|
||
|
expire int(11) DEFAULT NULL,
|
||
|
data LONGBLOB,
|
||
|
PRIMARY KEY (id),
|
||
|
KEY expire (expire)
|
||
|
);
|
||
|
")->execute();
|
||
|
}
|
||
12 years ago
|
|
||
11 years ago
|
/**
|
||
|
* @param boolean $reset whether to clean up the test database
|
||
|
* @return \yii\db\Connection
|
||
|
*/
|
||
|
public function getConnection($reset = true)
|
||
|
{
|
||
|
if ($this->_connection === null) {
|
||
11 years ago
|
$databases = self::getParam('databases');
|
||
11 years ago
|
$params = $databases['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;
|
||
|
}
|
||
12 years ago
|
|
||
11 years ago
|
return $this->_connection;
|
||
|
}
|
||
12 years ago
|
|
||
11 years ago
|
/**
|
||
|
* @return DbCache
|
||
|
*/
|
||
|
protected function getCacheInstance()
|
||
|
{
|
||
|
if ($this->_cacheInstance === null) {
|
||
|
$this->_cacheInstance = new DbCache(['db' => $this->getConnection()]);
|
||
|
}
|
||
12 years ago
|
|
||
11 years ago
|
return $this->_cacheInstance;
|
||
|
}
|
||
11 years ago
|
|
||
11 years ago
|
public function testExpire()
|
||
|
{
|
||
|
$cache = $this->getCacheInstance();
|
||
11 years ago
|
|
||
11 years ago
|
static::$time = \time();
|
||
|
$this->assertTrue($cache->set('expire_test', 'expire_test', 2));
|
||
|
static::$time++;
|
||
|
$this->assertEquals('expire_test', $cache->get('expire_test'));
|
||
|
static::$time++;
|
||
|
$this->assertFalse($cache->get('expire_test'));
|
||
|
}
|
||
|
|
||
|
public function testExpireAdd()
|
||
|
{
|
||
|
$cache = $this->getCacheInstance();
|
||
|
|
||
|
static::$time = \time();
|
||
|
$this->assertTrue($cache->add('expire_testa', 'expire_testa', 2));
|
||
|
static::$time++;
|
||
|
$this->assertEquals('expire_testa', $cache->get('expire_testa'));
|
||
|
static::$time++;
|
||
|
$this->assertFalse($cache->get('expire_testa'));
|
||
|
}
|
||
12 years ago
|
}
|