From 44759299d1c58eb0ffd5b124368d4cd57014c36f Mon Sep 17 00:00:00 2001 From: =?utf8?q?C=C3=A9sar=20D=2E=20Rodas?= Date: Tue, 25 May 2010 00:05:54 -0400 Subject: [PATCH] - Improved Caching, now it is sort of functional - Added basic functional tests for caching --- lib/Cache.php | 34 ++++++++++---------- tests/ActiveMongoSuite.php | 2 +- tests/CacheTest.php | 78 ++++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 94 insertions(+), 20 deletions(-) diff --git a/lib/Cache.php b/lib/Cache.php index 6cce792..876d841 100644 --- a/lib/Cache.php +++ b/lib/Cache.php @@ -60,30 +60,27 @@ class CacheCursor Extends MongoCursor $this->pos = -1; } + function key() + { + return (string)$this->var[$this->pos]['_id']; + } + function current() { - foreach ($this->var[$this->pos] as $id => $val) { - $this->$id = $val; + if (!$this->valid()) { + return array(); } return $this->var[$this->pos]; - return $this; } function next() { - $nodel = array('var' => 1, 'pos' => 1, 'size' => 1); - foreach (array_keys(get_object_vars($this)) as $key) { - if (!isset($nodel[$key])) { - unset($this->$key); - } - } - $this->pos++; - $this->current(); + ++$this->pos; } function valid() { - return $this->pos < $this->size; + return isset($this->var[$this->pos]); } function rewind() @@ -98,6 +95,11 @@ class CacheCursor Extends MongoCursor return $this->var[$this->pos]; } + function count() + { + return count($this->var); + } + function getID() { return $this->_id; @@ -146,9 +148,9 @@ abstract class CacheDriver } } - function setMulti(Array $object, Array $ttl) + function setMulti(Array $objects, Array $ttl) { - foreach ($object as $id => $value) { + foreach ($objects as $id => $value) { if (!isset($ttl[$id])) { $ttl[$id] = 3600; } @@ -234,7 +236,7 @@ final class ActiveMongo_Cache return; } - if (!is_array($query_result)) { + if (!is_array($query_result) || count($query_result) == 0) { return; } @@ -289,7 +291,7 @@ final class ActiveMongo_Cache foreach ($cursor as $id=>$document) { $ids[$id] = $document['_id']; - $docs[$id] = $this->driver->Serialize($document); + $docs[$id] = $document; $ttl[$id] = 3600; } diff --git a/tests/ActiveMongoSuite.php b/tests/ActiveMongoSuite.php index 2a4ec94..8db64bd 100644 --- a/tests/ActiveMongoSuite.php +++ b/tests/ActiveMongoSuite.php @@ -34,6 +34,7 @@ class ActiveMongoSuite extends PHPUnit_Framework_TestSuite public static function suite() { $suite = new ActiveMongoSuite('ActiveMongo Tests'); + $suite->addTestSuite('CacheTest'); $suite->addTestSuite('QueryTest'); $suite->addTestSuite('ReferencesTest'); $suite->addTestSuite('ValidatorsTest'); @@ -41,7 +42,6 @@ class ActiveMongoSuite extends PHPUnit_Framework_TestSuite $suite->addTestSuite('HookTest'); $suite->addTestSuite('ArrayTest'); $suite->addTestSuite('SleepTest'); - $suite->addTestSuite('CacheTest'); return $suite; } diff --git a/tests/CacheTest.php b/tests/CacheTest.php index 314ec12..2711ab4 100644 --- a/tests/CacheTest.php +++ b/tests/CacheTest.php @@ -38,10 +38,18 @@ ActiveMongo_Cache::setDriver(new CacheDriverMem); class CacheTest extends PHPUnit_Framework_TestCase { - function testCache() + function __construct() + { + try { + CacheableModel::drop(); + } Catch (Exception $e) { + } + } + + function testCacheSimple() { $c = new CacheableModel; - $c->foo = 'bar'; + $c->prop = 'bar'; $c->save(); $id = $c->getID(); $c->reset(); @@ -55,7 +63,71 @@ class CacheTest extends PHPUnit_Framework_TestCase $d->doQuery(); $this->assertTrue($d->servedFromCache()); - $this->assertEquals($c->foo, $d->foo); + $this->assertEquals($c->prop, $d->prop); + + return $id; } + function testCacheMultiple() + { + $var = array('bar','foo','xxx','ccc'); + $c = new CacheableModel; + foreach ($var as $v) { + $c->reset(); + $c->var['var_name'] = $v; + $c->$v = TRUE; + $c->save(); + } + + $query = new CacheableModel; + $query->where('var.var_name IN', $var); + $query->doQuery(); + $this->assertFalse($query->servedFromCache()); + $count1 = $query->count(); + foreach ($query as $result) { + foreach ($result['var'] as $key => $value) { + $this->assertTrue(isset($result->$value)); + } + } + + $query->reset(); + $query->where('var.var_name IN', $var); + $query->doQuery(); + $this->assertTrue($query->servedFromCache()); + $count2 = $query->count(); + $this->assertEquals($count1, $count2); + $this->assertEquals($count1, count($var)); + + foreach ($query as $result) { + foreach ($result['var'] as $key => $value) { + $this->assertTrue(isset($result->$value)); + } + } + } + + /** + * @depends testCacheSimple + */ + function testUpdateCache($id) + { + $d = new CacheableModel; + $d->where('_id', $id); + $d->doQuery(); + + $this->assertEquals($d->prop, 'bar'); + $this->assertEquals(1, $d->count()); + $this->assertTrue($d->servedFromCache()); + $d->prop = 'new value'; + $d->save(); + + $c = new CacheableModel; + $c->where('_id', $id); + $c->doQuery(); + + + $this->assertTrue($d->servedFromCache()); + $this->assertEquals($c->prop, $d->prop); + + + } } -- 2.11.4.GIT