3 require "../lib/plugin/Cache/Cache.php";
6 class CacheableModel
extends ActiveMongo
8 public static $cacheable = TRUE;
11 class CacheDriverMem
extends CacheDriver
20 function config($name, $value)
32 function get($key, &$document)
34 if (isset($this->mem
[$key])) {
35 $document = $this->deserialize($this->mem
[$key]);
41 function set($key, $document, $ttl)
43 $this->mem
[$key] = $this->serialize($document);
46 function delete(Array $keys)
48 foreach ($keys as $key) {
49 unset($this->mem
[$key]);
55 class CacheTest
extends PHPUnit_Framework_TestCase
60 CacheableModel
::drop();
61 } Catch (ActiveMongo_Exception
$e) {
63 ActiveMongo_Cache
::setDriver(new CacheDriverMem
);
69 function testCacheDriverConfig()
71 $this->assertTrue(ActiveMongo_Cache
::config('true', NULL));
72 $this->assertFalse(ActiveMongo_Cache
::config('false', NULL));
73 $this->assertEquals(ActiveMongo_Cache
::config('return', 'foo'), 'foo');
79 function testCacheSimple()
81 $c = new CacheableModel
;
87 $c->where('_id', $id);
89 $this->assertFalse($c->servedFromCache());
91 /* Disconnect to test the cache works properly */
92 ActiveMongo
::disconnect();
93 $this->assertFalse(ActiveMongo
::isConnected());
95 $d = new CacheableModel
;
96 $d->where('_id', $id);
99 $this->assertFalse(ActiveMongo
::isConnected());
100 $this->assertTrue($d->servedFromCache());
101 $this->assertEquals($c->prop
, $d->prop
);
103 /* non-cached query, to see if it is reconnected to mongodb */
104 $d = new CacheableModel
;
105 $d->where('_id', 'foobar');
108 $this->assertFalse($d->servedFromCache());
109 $this->assertTrue(ActiveMongo
::isConnected());
114 function testCacheMultiple()
116 $var = array('bar','foo','xxx','ccc');
117 $c = new CacheableModel
;
118 foreach ($var as $v) {
120 $c->var['var_name'] = $v;
125 $query = new CacheableModel
;
126 $query->where('var.var_name IN', $var);
128 $this->assertFalse($query->servedFromCache());
129 $count1 = $query->count();
130 foreach ($query as $result) {
131 foreach ($result['var'] as $key => $value) {
132 $this->assertTrue(isset($result->$value));
137 $query->where('var.var_name IN', $var);
139 $this->assertTrue($query->servedFromCache());
140 $count2 = $query->count();
141 $this->assertEquals($count1, $count2);
142 $this->assertEquals($count1, count($var));
144 foreach ($query as $result) {
145 foreach ($result['var'] as $key => $value) {
146 $this->assertTrue(isset($result->$value));
154 * @depends testCacheSimple
156 function testUpdateCache($id)
158 $d = new CacheableModel
;
159 $d->where('_id', $id);
162 $this->assertEquals($d->prop
, 'bar');
163 $this->assertEquals(1, $d->count());
164 $this->assertTrue($d->servedFromCache());
165 $d->prop
= 'new value';
168 $c = new CacheableModel
;
169 $c->where('_id', $id);
173 $this->assertTrue($d->servedFromCache());
174 $this->assertEquals($c->prop
, $d->prop
);
180 * @depends testCacheMultiple
182 function testUpdateQueryCache($vars)
184 /* assert the query is cached */
185 $query = new CacheableModel
;
186 $query->where('var.var_name IN', $vars);
188 $this->assertTrue($query->servedFromCache());
189 $this->assertEquals(count($query), count($vars));
191 /* add a new element */
192 $new = new CacheableModel
;
193 $new->var['var_name'] = 'xxx';
197 /* query to mongodb, without cache */
198 $query = new CacheableModel
;
199 $query->where('var.var_name IN', $vars);
200 $query->doQuery(FALSE);
201 $this->assertFalse($query->servedFromCache());
202 $this->assertEquals(count($query), count($vars)+
1);
204 /* check if the cache was overrided */
205 $query = new CacheableModel
;
206 $query->where('var.var_name IN', $vars);
208 $this->assertTrue($query->servedFromCache());
209 $this->assertEquals(count($query), count($vars)+
1);
213 * @depends testUpdateCache
215 function testFetchFromCache($id)
217 /* Test if one object is missing in the cache
218 * is loaded properly by activemongo cache
220 ActiveMongo_Cache
::deleteObject($id);
221 $this->assertFalse(ActiveMongo_Cache
::getObject($id));
222 $d = new CacheableModel
;
223 $d->where('_id', $id);
225 $this->assertTrue($d->servedFromCache());
226 $this->assertEquals('new value', $d->prop
);
227 $this->assertTrue(is_array(ActiveMongo_Cache
::getObject($id)));
230 function testDrivers()
232 $drivers = glob("../lib/plugin/Cache/*.php");
233 foreach ($drivers as $drive) {
234 if (substr($drive,-9) == 'Cache.php') {
238 if (!ActiveMongo_Cache
::isDriverActived()) {
241 ActiveMongo_Cache
::flushCache();
242 CacheableModel
::drop();
243 $id = $this->testCacheSimple();
244 $vars = $this->testCacheMultiple();
245 $this->testUpdateCache($id);
246 $this->testFetchFromCache($id);
247 $this->testUpdateQueryCache($vars);