From 556c2b84f4131712e0f572075a26c9056b4fae24 Mon Sep 17 00:00:00 2001 From: Anton Grebnev Date: Tue, 25 Oct 2011 18:13:39 +0400 Subject: [PATCH] Cache classes tested --- tests/cache/CacheKeyTest.php | 85 ++++++++++++++++ tests/cache/CacherTest.php | 70 +++++++++++++ tests/cache/MemcacheCacheTest.php | 204 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 359 insertions(+) create mode 100644 tests/cache/CacheKeyTest.php create mode 100644 tests/cache/CacherTest.php create mode 100644 tests/cache/MemcacheCacheTest.php diff --git a/tests/cache/CacheKeyTest.php b/tests/cache/CacheKeyTest.php new file mode 100644 index 0000000..2b050ef --- /dev/null +++ b/tests/cache/CacheKeyTest.php @@ -0,0 +1,85 @@ + + * @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()); + } + +} \ No newline at end of file diff --git a/tests/cache/CacherTest.php b/tests/cache/CacherTest.php new file mode 100644 index 0000000..dfcc565 --- /dev/null +++ b/tests/cache/CacherTest.php @@ -0,0 +1,70 @@ + + * @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(); + } + +} + diff --git a/tests/cache/MemcacheCacheTest.php b/tests/cache/MemcacheCacheTest.php new file mode 100644 index 0000000..ec8bd02 --- /dev/null +++ b/tests/cache/MemcacheCacheTest.php @@ -0,0 +1,204 @@ + + * @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; + } + +} \ No newline at end of file