diff --git a/framework/caching/DbCache.php b/framework/caching/DbCache.php index 75f13b0..b74c5e0 100644 --- a/framework/caching/DbCache.php +++ b/framework/caching/DbCache.php @@ -104,7 +104,7 @@ class DbCache extends Cache $query = new Query; $query->select(array('data')) ->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(); if ($db->enableQueryCache) { // temporarily disable and re-enable query caching @@ -131,7 +131,7 @@ class DbCache extends Cache $query->select(array('id', 'data')) ->from($this->cacheTableName) ->where(array('id' => $keys)) - ->andWhere("expire = 0 OR expire > " . time() . ")"); + ->andWhere('(expire = 0 OR expire > ' . time() . ')'); $db = $this->getDb(); if ($db->enableQueryCache) { diff --git a/tests/unit/.gitignore b/tests/unit/.gitignore new file mode 100644 index 0000000..34651d7 --- /dev/null +++ b/tests/unit/.gitignore @@ -0,0 +1 @@ +runtime/cache/* \ No newline at end of file diff --git a/tests/unit/framework/caching/ApcCacheTest.php b/tests/unit/framework/caching/ApcCacheTest.php new file mode 100644 index 0000000..74ede2a --- /dev/null +++ b/tests/unit/framework/caching/ApcCacheTest.php @@ -0,0 +1,27 @@ +markTestSkipped("APC not installed. Skipping."); + } + + if($this->_cacheInstance === null) { + $this->_cacheInstance = new ApcCache(); + } + return $this->_cacheInstance; + } +} \ No newline at end of file diff --git a/tests/unit/framework/caching/CacheTest.php b/tests/unit/framework/caching/CacheTest.php new file mode 100644 index 0000000..ad2fcf5 --- /dev/null +++ b/tests/unit/framework/caching/CacheTest.php @@ -0,0 +1,82 @@ +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')); + } +} diff --git a/tests/unit/framework/caching/DbCacheTest.php b/tests/unit/framework/caching/DbCacheTest.php new file mode 100644 index 0000000..3977ee8 --- /dev/null +++ b/tests/unit/framework/caching/DbCacheTest.php @@ -0,0 +1,70 @@ +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; + } +} \ No newline at end of file diff --git a/tests/unit/framework/caching/FileCacheTest.php b/tests/unit/framework/caching/FileCacheTest.php new file mode 100644 index 0000000..1f6debd --- /dev/null +++ b/tests/unit/framework/caching/FileCacheTest.php @@ -0,0 +1,25 @@ +_cacheInstance === null) { + $this->_cacheInstance = new FileCache(array( + 'cachePath' => '@yiiunit/runtime/cache', + )); + } + return $this->_cacheInstance; + } +} \ No newline at end of file diff --git a/tests/unit/framework/caching/MemCacheTest.php b/tests/unit/framework/caching/MemCacheTest.php new file mode 100644 index 0000000..e4804d9 --- /dev/null +++ b/tests/unit/framework/caching/MemCacheTest.php @@ -0,0 +1,27 @@ +markTestSkipped("memcache not installed. Skipping."); + } + + if($this->_cacheInstance === null) { + $this->_cacheInstance = new MemCache(); + } + return $this->_cacheInstance; + } +} \ No newline at end of file diff --git a/tests/unit/framework/caching/MemCachedTest.php b/tests/unit/framework/caching/MemCachedTest.php new file mode 100644 index 0000000..59396df --- /dev/null +++ b/tests/unit/framework/caching/MemCachedTest.php @@ -0,0 +1,29 @@ +markTestSkipped("memcached not installed. Skipping."); + } + + if($this->_cacheInstance === null) { + $this->_cacheInstance = new MemCache(array( + 'useMemcached' => true, + )); + } + return $this->_cacheInstance; + } +} \ No newline at end of file diff --git a/tests/unit/framework/caching/WinCacheTest.php b/tests/unit/framework/caching/WinCacheTest.php new file mode 100644 index 0000000..b78d57b --- /dev/null +++ b/tests/unit/framework/caching/WinCacheTest.php @@ -0,0 +1,31 @@ +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; + } +} \ No newline at end of file diff --git a/tests/unit/framework/caching/XCacheTest.php b/tests/unit/framework/caching/XCacheTest.php new file mode 100644 index 0000000..e1ed844 --- /dev/null +++ b/tests/unit/framework/caching/XCacheTest.php @@ -0,0 +1,27 @@ +markTestSkipped("XCache not installed. Skipping."); + } + + if($this->_cacheInstance === null) { + $this->_cacheInstance = new XCache(); + } + return $this->_cacheInstance; + } +} \ No newline at end of file diff --git a/tests/unit/framework/caching/ZendDataCacheTest.php b/tests/unit/framework/caching/ZendDataCacheTest.php new file mode 100644 index 0000000..91dfbb5 --- /dev/null +++ b/tests/unit/framework/caching/ZendDataCacheTest.php @@ -0,0 +1,27 @@ +markTestSkipped("Zend Data cache not installed. Skipping."); + } + + if($this->_cacheInstance === null) { + $this->_cacheInstance = new ZendDataCache(); + } + return $this->_cacheInstance; + } +} \ No newline at end of file