Cache classes tested
This commit is contained in:
85
tests/cache/CacheKeyTest.php
vendored
Normal file
85
tests/cache/CacheKeyTest.php
vendored
Normal file
@ -0,0 +1,85 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* @copyright NetMonsters <team@netmonsters.ru>
|
||||
* @link http://netmonsters.ru
|
||||
* @package Majestic
|
||||
* @subpackage UnitTests
|
||||
* @since 2011-10-14
|
||||
*
|
||||
* Unit tests for CacheKey class
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* @TODO: CacheKey->getExpire() method never used
|
||||
*/
|
||||
|
||||
require_once dirname(__FILE__) . '/../../cache/Cache.php';
|
||||
require_once dirname(__FILE__) . '/../../cache/CacheKey.php';
|
||||
|
||||
class CacheKeyTest extends PHPUnit_Framework_TestCase
|
||||
{
|
||||
|
||||
private $cache;
|
||||
|
||||
|
||||
public function testConstructor()
|
||||
{
|
||||
$mock = $this->getMock('Cache');
|
||||
|
||||
$cache = new CacheKey($mock, 'anything', array('one', 'two', 'three'),200);
|
||||
$this->assertAttributeEquals('anything_one|two|three', 'key', $cache);
|
||||
|
||||
}
|
||||
|
||||
public function testExpire()
|
||||
{
|
||||
$this->cache = new CacheKey($this->getMock('Cache'), 'key');
|
||||
$this->assertAttributeEquals(0, 'expire', $this->cache);
|
||||
$this->cache->setExpire(20);
|
||||
$this->assertAttributeEquals(20, 'expire', $this->cache);
|
||||
}
|
||||
|
||||
public function testGetExpire()
|
||||
{
|
||||
$this->cache = new CacheKey(null, 'key');
|
||||
$this->cache->setExpire(100);
|
||||
|
||||
$getExpireMethod = new ReflectionMethod('CacheKey', 'getExpire');
|
||||
$getExpireMethod->setAccessible(TRUE);
|
||||
|
||||
$this->assertEquals(100, $getExpireMethod->invoke($this->cache));
|
||||
}
|
||||
|
||||
public function testGetSet()
|
||||
{
|
||||
$mockCacher = $this->getMock('Cache');
|
||||
$mockCacher->expects($this->any())
|
||||
->method('set')
|
||||
->with('any', 'some', 0);
|
||||
|
||||
$mockCacher->expects($this->any())
|
||||
->method('get')
|
||||
->with('any')
|
||||
->will($this->returnValue('some'));
|
||||
|
||||
$this->cache = new CacheKey($mockCacher, 'any');
|
||||
$this->cache->set('some');
|
||||
$this->assertEquals('some', $this->cache->get());
|
||||
}
|
||||
|
||||
public function testDel()
|
||||
{
|
||||
$mockCacher = $this->getMock('Cacher', array('del'));
|
||||
|
||||
$mockCacher->expects($this->any())
|
||||
->method('del')
|
||||
->with('some')
|
||||
->will($this->returnValue(true));
|
||||
|
||||
$cache = new CacheKey($mockCacher, 'some');
|
||||
$this->assertTrue($cache->del());
|
||||
}
|
||||
|
||||
}
|
70
tests/cache/CacherTest.php
vendored
Normal file
70
tests/cache/CacherTest.php
vendored
Normal file
@ -0,0 +1,70 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* @copyright NetMonsters <team@netmonsters.ru>
|
||||
* @link http://netmonsters.ru
|
||||
* @package Majestic
|
||||
* @subpackage UnitTests
|
||||
* @since 2011-10-25
|
||||
*
|
||||
* Unit tests for Cacher class
|
||||
*/
|
||||
|
||||
|
||||
require_once dirname(__FILE__) . '/../../cache/Cache.php';
|
||||
require_once dirname(__FILE__) . '/../../cache/Cacher.php';
|
||||
require_once dirname(__FILE__) . '/../../Registry.php';
|
||||
require_once dirname(__FILE__) . '/../../Config.php';
|
||||
|
||||
class CacherTest extends PHPUnit_Framework_TestCase
|
||||
{
|
||||
|
||||
private $mock;
|
||||
|
||||
protected function setUp()
|
||||
{
|
||||
set_new_overload(array($this, 'newCallback'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException Exception
|
||||
* @expectedExcepptionMessage Cache driver
|
||||
*/
|
||||
public function testNotExtendsCache()
|
||||
{
|
||||
Cacher::get('Cacher');
|
||||
}
|
||||
|
||||
public function testGet()
|
||||
{
|
||||
$this->mock = $this->getMockForAbstractClass(
|
||||
'Cache', /* name of class to mock */
|
||||
array(), /* list of methods to mock */
|
||||
'CacheMock' /* name for mocked class */
|
||||
);
|
||||
|
||||
$this->assertTrue(Cacher::get('Cache') instanceof Cache);
|
||||
}
|
||||
|
||||
public function testCacheAlreadySet()
|
||||
{
|
||||
$this->assertTrue(Cacher::get('Cache') instanceof Cache);
|
||||
}
|
||||
|
||||
protected function newCallback($className)
|
||||
{
|
||||
switch ($className) {
|
||||
case 'Cache':
|
||||
return 'CacheMock';
|
||||
default:
|
||||
return $className;
|
||||
}
|
||||
}
|
||||
|
||||
public function tearDown()
|
||||
{
|
||||
unset_new_overload();
|
||||
}
|
||||
|
||||
}
|
||||
|
204
tests/cache/MemcacheCacheTest.php
vendored
Normal file
204
tests/cache/MemcacheCacheTest.php
vendored
Normal file
@ -0,0 +1,204 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* @copyright NetMonsters <team@netmonsters.ru>
|
||||
* @link http://netmonsters.ru
|
||||
* @package Majestic
|
||||
* @subpackage UnitTests
|
||||
* @since 2011-10-25
|
||||
*
|
||||
* Unit tests for MemcacheCache class
|
||||
*/
|
||||
|
||||
|
||||
require_once dirname(__FILE__) . '/../../cache/Cache.php';
|
||||
require_once dirname(__FILE__) . '/../../cache/MemcacheCache.php';
|
||||
|
||||
class MemcacheCacheTest extends PHPUnit_Framework_TestCase
|
||||
{
|
||||
|
||||
protected function setUp()
|
||||
{
|
||||
$this->getMock('Memcache');
|
||||
|
||||
set_new_overload(array($this, 'newCallback'));
|
||||
}
|
||||
|
||||
public function testConstruct()
|
||||
{
|
||||
$config = array('key_salt' => 'some', 'host' => array('hostname' => 'localhost', 'port' => 8080));
|
||||
|
||||
|
||||
$memcache = new MemcacheCache($config);
|
||||
$this->assertAttributeInstanceOf('TestCache', 'connection', $memcache);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException Exception
|
||||
* @expectedExceptionMessage Configuration must have a
|
||||
* @TODO: MemcacheCache::__construct - empty config array passes with no host params
|
||||
*/
|
||||
public function testWrongConfig()
|
||||
{
|
||||
$config = array('some' => array());
|
||||
|
||||
$memcache = new MemcacheCache($config);
|
||||
}
|
||||
|
||||
public function testAddGet()
|
||||
{
|
||||
$memcache = new MemcacheCache(array());
|
||||
$this->assertTrue($memcache->add('key', 'value'));
|
||||
$this->assertEquals('value', $memcache->get('key'));
|
||||
$this->assertFalse($memcache->add('key', 'newvalue'));
|
||||
}
|
||||
|
||||
public function testIncrementDecrement()
|
||||
{
|
||||
$memcache = new MemcacheCache(array());
|
||||
|
||||
$memcache->add('one', 1);
|
||||
$memcache->add('two', 2);
|
||||
$memcache->add('three', 'three');
|
||||
|
||||
$this->assertTrue($memcache->increment('one'));
|
||||
$this->assertEquals(2, $memcache->get('one'));
|
||||
$this->assertTrue($memcache->decrement('two'));
|
||||
$this->assertEquals(1, $memcache->get('two'));
|
||||
|
||||
$this->assertFalse($memcache->decrement('three'));
|
||||
}
|
||||
|
||||
public function testDelete()
|
||||
{
|
||||
$memcache = new MemcacheCache(array());
|
||||
|
||||
$memcache->add('one', 1);
|
||||
$memcache->add('two', 2);
|
||||
|
||||
$memcache->del('one');
|
||||
$this->assertEquals(2, $memcache->get('two'));
|
||||
$this->assertFalse($memcache->get('one'));
|
||||
}
|
||||
|
||||
public function testFlush()
|
||||
{
|
||||
$memcache = new MemcacheCache(array());
|
||||
|
||||
$memcache->add('one', 1);
|
||||
$memcache->add('two', 2);
|
||||
$memcache->add('three', 'three');
|
||||
|
||||
$this->assertEquals('three', 'three');
|
||||
|
||||
$memcache->flush();
|
||||
|
||||
$this->assertFalse($memcache->get('three'));
|
||||
$this->assertFalse($memcache->get('one'));
|
||||
}
|
||||
|
||||
public function testSetReplace()
|
||||
{
|
||||
$memcache = new MemcacheCache(array());
|
||||
|
||||
$memcache->add('one', 1);
|
||||
$memcache->add('two', 2);
|
||||
$memcache->add('three', 'three');
|
||||
$memcache->set('one', 30);
|
||||
$memcache->replace('three', 3);
|
||||
|
||||
$this->assertEquals(30, $memcache->get('one'));
|
||||
$this->assertEquals(3, $memcache->get('three'));
|
||||
}
|
||||
|
||||
protected function newCallback($className)
|
||||
{
|
||||
switch ($className) {
|
||||
case 'Memcache':
|
||||
return 'TestCache';
|
||||
default:
|
||||
return $className;
|
||||
}
|
||||
}
|
||||
|
||||
public function tearDown()
|
||||
{
|
||||
unset_new_overload();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class TestCache
|
||||
{
|
||||
|
||||
private $vals = array();
|
||||
|
||||
public function addServer($host, $port)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public function add($key, $value, $some, $expire = 0)
|
||||
{
|
||||
if(!isset($this->vals[$key])) {
|
||||
$this->vals[$key] = $value;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public function decrement($key, $decrement = 1)
|
||||
{
|
||||
if (isset($this->vals[$key]) && is_int($this->vals[$key])) {
|
||||
$this->vals[$key]--;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public function delete($key)
|
||||
{
|
||||
if (isset($this->vals[$key])) {
|
||||
unset($this->vals[$key]);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public function flush()
|
||||
{
|
||||
$this->vals = array();
|
||||
}
|
||||
|
||||
public function get($key)
|
||||
{
|
||||
if (isset($this->vals[$key])) {
|
||||
return $this->vals[$key];
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public function increment($key, $increment = 1)
|
||||
{
|
||||
if (isset($this->vals[$key]) && is_int($this->vals[$key])) {
|
||||
$this->vals[$key]++;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public function replace($key, $value, $any = null, $expire = 0)
|
||||
{
|
||||
if (isset($this->vals[$key])) {
|
||||
$this->vals[$key] = $value;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public function set($key, $value, $any = null, $expire = 0)
|
||||
{
|
||||
$this->vals[$key] = $value;
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user