- Improved cloned objects support
[activemongo.git] / tests / CacheTest.php
blob1e4a0aaa5c4d3bf720e013874447143a49249ab4
1 <?php
3 require "../lib/plugin/Cache/Cache.php";
6 class CacheableModel extends ActiveMongo
8 public static $cacheable = TRUE;
11 class CacheDriverMem extends CacheDriver
13 private $mem;
15 function flush()
17 $this->mem = array();
20 function config($name, $value)
22 switch ($name) {
23 case 'return':
24 return $value;
25 case 'true':
26 return TRUE;
27 case 'false':
28 return FALSE;
32 function get($key, &$document)
34 if (isset($this->mem[$key])) {
35 $document = $this->deserialize($this->mem[$key]);
36 return TRUE;
38 return FALSE;
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
57 function testInit()
59 try {
60 CacheableModel::drop();
61 } Catch (ActiveMongo_Exception $e) {
63 ActiveMongo_Cache::setDriver(new CacheDriverMem);
66 /**
67 * @depends testInit
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');
76 /**
77 * @depends testInit
79 function testCacheSimple()
81 $c = new CacheableModel;
82 $c->prop = 'bar';
83 $c->save();
84 $id = $c->getID();
85 $c->reset();
87 $c->where('_id', $id);
88 $c->doQuery();
89 $this->assertFalse($c->servedFromCache());
91 $d = new CacheableModel;
92 $d->where('_id', $id);
93 $d->doQuery();
95 $this->assertTrue($d->servedFromCache());
96 $this->assertEquals($c->prop, $d->prop);
98 return $id;
101 function testCacheMultiple()
103 $var = array('bar','foo','xxx','ccc');
104 $c = new CacheableModel;
105 foreach ($var as $v) {
106 $c->reset();
107 $c->var['var_name'] = $v;
108 $c->$v = TRUE;
109 $c->save();
112 $query = new CacheableModel;
113 $query->where('var.var_name IN', $var);
114 $query->doQuery();
115 $this->assertFalse($query->servedFromCache());
116 $count1 = $query->count();
117 foreach ($query as $result) {
118 foreach ($result['var'] as $key => $value) {
119 $this->assertTrue(isset($result->$value));
123 $query->reset();
124 $query->where('var.var_name IN', $var);
125 $query->doQuery();
126 $this->assertTrue($query->servedFromCache());
127 $count2 = $query->count();
128 $this->assertEquals($count1, $count2);
129 $this->assertEquals($count1, count($var));
131 foreach ($query as $result) {
132 foreach ($result['var'] as $key => $value) {
133 $this->assertTrue(isset($result->$value));
137 return $var;
141 * @depends testCacheSimple
143 function testUpdateCache($id)
145 $d = new CacheableModel;
146 $d->where('_id', $id);
147 $d->doQuery();
149 $this->assertEquals($d->prop, 'bar');
150 $this->assertEquals(1, $d->count());
151 $this->assertTrue($d->servedFromCache());
152 $d->prop = 'new value';
153 $d->save();
155 $c = new CacheableModel;
156 $c->where('_id', $id);
157 $c->doQuery();
160 $this->assertTrue($d->servedFromCache());
161 $this->assertEquals($c->prop, $d->prop);
163 return $id;
167 * @depends testCacheMultiple
169 function testUpdateQueryCache($vars)
171 /* assert the query is cached */
172 $query = new CacheableModel;
173 $query->where('var.var_name IN', $vars);
174 $query->doQuery();
175 $this->assertTrue($query->servedFromCache());
176 $this->assertEquals(count($query), count($vars));
178 /* add a new element */
179 $new = new CacheableModel;
180 $new->var['var_name'] = 'xxx';
181 $new->xxx = TRUE;
182 $new->save();
184 /* query to mongodb, without cache */
185 $query = new CacheableModel;
186 $query->where('var.var_name IN', $vars);
187 $query->doQuery(FALSE);
188 $this->assertFalse($query->servedFromCache());
189 $this->assertEquals(count($query), count($vars)+1);
191 /* check if the cache was overrided */
192 $query = new CacheableModel;
193 $query->where('var.var_name IN', $vars);
194 $query->doQuery();
195 $this->assertTrue($query->servedFromCache());
196 $this->assertEquals(count($query), count($vars)+1);
200 * @depends testUpdateCache
202 function testFetchFromCache($id)
204 /* Test if one object is missing in the cache
205 * is loaded properly by activemongo cache
207 ActiveMongo_Cache::deleteObject($id);
208 $this->assertFalse(ActiveMongo_Cache::getObject($id));
209 $d = new CacheableModel;
210 $d->where('_id', $id);
211 $d->doQuery();
212 $this->assertTrue($d->servedFromCache());
213 $this->assertEquals('new value', $d->prop);
214 $this->assertTrue(is_array(ActiveMongo_Cache::getObject($id)));
217 function testDrivers()
219 $drivers = glob("../lib/plugin/Cache/*.php");
220 foreach ($drivers as $drive) {
221 if (substr($drive,-9) == 'Cache.php') {
222 continue;
224 require $drive;
225 if (!ActiveMongo_Cache::isDriverActived()) {
226 continue;
228 ActiveMongo_Cache::flushCache();
229 CacheableModel::drop();
230 $id = $this->testCacheSimple();
231 $vars = $this->testCacheMultiple();
232 $this->testUpdateCache($id);
233 $this->testFetchFromCache($id);
234 $this->testUpdateQueryCache($vars);