Tests for new find types
[activemongo.git] / tests / CacheTest.php
blob0e96dbffae3ef6253f6fd36f7cf80486db8e1e7f
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 /* Disconnect to test the cache works properly */
92 ActiveMongo::disconnect();
93 $this->assertFalse(ActiveMongo::isConnected());
95 $d = new CacheableModel;
96 $d->where('_id', $id);
97 $d->doQuery();
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');
106 $d->doQuery();
108 $this->assertFalse($d->servedFromCache());
109 $this->assertTrue(ActiveMongo::isConnected());
111 return $id;
114 function testCacheMultiple()
116 $var = array('bar','foo','xxx','ccc');
117 $c = new CacheableModel;
118 foreach ($var as $v) {
119 $c->reset();
120 $c->var['var_name'] = $v;
121 $c->$v = TRUE;
122 $c->save();
125 $query = new CacheableModel;
126 $query->where('var.var_name IN', $var);
127 $query->doQuery();
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));
136 $query->reset();
137 $query->where('var.var_name IN', $var);
138 $query->doQuery();
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));
150 return $var;
154 * @depends testCacheSimple
156 function testUpdateCache($id)
158 $d = new CacheableModel;
159 $d->where('_id', $id);
160 $d->doQuery();
162 $this->assertEquals($d->prop, 'bar');
163 $this->assertEquals(1, $d->count());
164 $this->assertTrue($d->servedFromCache());
165 $d->prop = 'new value';
166 $d->save();
168 $c = new CacheableModel;
169 $c->where('_id', $id);
170 $c->doQuery();
173 $this->assertTrue($d->servedFromCache());
174 $this->assertEquals($c->prop, $d->prop);
176 return $id;
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);
187 $query->doQuery();
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';
194 $new->xxx = TRUE;
195 $new->save();
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);
207 $query->doQuery();
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);
224 $d->doQuery();
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') {
235 continue;
237 require $drive;
238 if (!ActiveMongo_Cache::isDriverActived()) {
239 continue;
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);