|
1 <?php |
|
2 /** |
|
3 * Zend Framework |
|
4 * |
|
5 * LICENSE |
|
6 * |
|
7 * This source file is subject to the new BSD license that is bundled |
|
8 * with this package in the file LICENSE.txt. |
|
9 * It is also available through the world-wide-web at this URL: |
|
10 * http://framework.zend.com/license/new-bsd |
|
11 * If you did not receive a copy of the license and are unable to |
|
12 * obtain it through the world-wide-web, please send an email |
|
13 * to license@zend.com so we can send you a copy immediately. |
|
14 * |
|
15 * @category Zend |
|
16 * @package Zend_Cache |
|
17 * @subpackage Zend_Cache_Backend |
|
18 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
|
19 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
20 * @version $Id: Test.php 23051 2010-10-07 17:01:21Z mabe $ |
|
21 */ |
|
22 |
|
23 |
|
24 /** |
|
25 * @see Zend_Cache_Backend_Interface |
|
26 */ |
|
27 require_once 'Zend/Cache/Backend/ExtendedInterface.php'; |
|
28 |
|
29 /** |
|
30 * @see Zend_Cache_Backend |
|
31 */ |
|
32 require_once 'Zend/Cache/Backend.php'; |
|
33 |
|
34 /** |
|
35 * @package Zend_Cache |
|
36 * @subpackage Zend_Cache_Backend |
|
37 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
|
38 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
39 */ |
|
40 class Zend_Cache_Backend_Test extends Zend_Cache_Backend implements Zend_Cache_Backend_ExtendedInterface |
|
41 { |
|
42 /** |
|
43 * Available options |
|
44 * |
|
45 * @var array available options |
|
46 */ |
|
47 protected $_options = array(); |
|
48 |
|
49 /** |
|
50 * Frontend or Core directives |
|
51 * |
|
52 * @var array directives |
|
53 */ |
|
54 protected $_directives = array(); |
|
55 |
|
56 /** |
|
57 * Array to log actions |
|
58 * |
|
59 * @var array $_log |
|
60 */ |
|
61 private $_log = array(); |
|
62 |
|
63 /** |
|
64 * Current index for log array |
|
65 * |
|
66 * @var int $_index |
|
67 */ |
|
68 private $_index = 0; |
|
69 |
|
70 /** |
|
71 * Constructor |
|
72 * |
|
73 * @param array $options associative array of options |
|
74 * @return void |
|
75 */ |
|
76 public function __construct($options = array()) |
|
77 { |
|
78 $this->_addLog('construct', array($options)); |
|
79 } |
|
80 |
|
81 /** |
|
82 * Set the frontend directives |
|
83 * |
|
84 * @param array $directives assoc of directives |
|
85 * @return void |
|
86 */ |
|
87 public function setDirectives($directives) |
|
88 { |
|
89 $this->_addLog('setDirectives', array($directives)); |
|
90 } |
|
91 |
|
92 /** |
|
93 * Test if a cache is available for the given id and (if yes) return it (false else) |
|
94 * |
|
95 * For this test backend only, if $id == 'false', then the method will return false |
|
96 * if $id == 'serialized', the method will return a serialized array |
|
97 * ('foo' else) |
|
98 * |
|
99 * @param string $id Cache id |
|
100 * @param boolean $doNotTestCacheValidity If set to true, the cache validity won't be tested |
|
101 * @return string Cached datas (or false) |
|
102 */ |
|
103 public function load($id, $doNotTestCacheValidity = false) |
|
104 { |
|
105 $this->_addLog('get', array($id, $doNotTestCacheValidity)); |
|
106 |
|
107 if ( $id == 'false' |
|
108 || $id == 'd8523b3ee441006261eeffa5c3d3a0a7' |
|
109 || $id == 'e83249ea22178277d5befc2c5e2e9ace' |
|
110 || $id == '40f649b94977c0a6e76902e2a0b43587' |
|
111 || $id == '88161989b73a4cbfd0b701c446115a99' |
|
112 || $id == '205fc79cba24f0f0018eb92c7c8b3ba4' |
|
113 || $id == '170720e35f38150b811f68a937fb042d') |
|
114 { |
|
115 return false; |
|
116 } |
|
117 if ($id=='serialized') { |
|
118 return serialize(array('foo')); |
|
119 } |
|
120 if ($id=='serialized2') { |
|
121 return serialize(array('headers' => array(), 'data' => 'foo')); |
|
122 } |
|
123 if ( $id == '71769f39054f75894288e397df04e445' || $id == '615d222619fb20b527168340cebd0578' |
|
124 || $id == '8a02d218a5165c467e7a5747cc6bd4b6' || $id == '648aca1366211d17cbf48e65dc570bee' |
|
125 || $id == '4a923ef02d7f997ca14d56dfeae25ea7') { |
|
126 return serialize(array('foo', 'bar')); |
|
127 } |
|
128 return 'foo'; |
|
129 } |
|
130 |
|
131 /** |
|
132 * Test if a cache is available or not (for the given id) |
|
133 * |
|
134 * For this test backend only, if $id == 'false', then the method will return false |
|
135 * (123456 else) |
|
136 * |
|
137 * @param string $id Cache id |
|
138 * @return mixed|false false (a cache is not available) or "last modified" timestamp (int) of the available cache record |
|
139 */ |
|
140 public function test($id) |
|
141 { |
|
142 $this->_addLog('test', array($id)); |
|
143 if ($id=='false') { |
|
144 return false; |
|
145 } |
|
146 if (($id=='3c439c922209e2cb0b54d6deffccd75a')) { |
|
147 return false; |
|
148 } |
|
149 return 123456; |
|
150 } |
|
151 |
|
152 /** |
|
153 * Save some string datas into a cache record |
|
154 * |
|
155 * For this test backend only, if $id == 'false', then the method will return false |
|
156 * (true else) |
|
157 * |
|
158 * @param string $data Datas to cache |
|
159 * @param string $id Cache id |
|
160 * @param array $tags Array of strings, the cache record will be tagged by each string entry |
|
161 * @param int $specificLifetime If != false, set a specific lifetime for this cache record (null => infinite lifetime) |
|
162 * @return boolean True if no problem |
|
163 */ |
|
164 public function save($data, $id, $tags = array(), $specificLifetime = false) |
|
165 { |
|
166 $this->_addLog('save', array($data, $id, $tags)); |
|
167 if ($id=='false') { |
|
168 return false; |
|
169 } |
|
170 return true; |
|
171 } |
|
172 |
|
173 /** |
|
174 * Remove a cache record |
|
175 * |
|
176 * For this test backend only, if $id == 'false', then the method will return false |
|
177 * (true else) |
|
178 * |
|
179 * @param string $id Cache id |
|
180 * @return boolean True if no problem |
|
181 */ |
|
182 public function remove($id) |
|
183 { |
|
184 $this->_addLog('remove', array($id)); |
|
185 if ($id=='false') { |
|
186 return false; |
|
187 } |
|
188 return true; |
|
189 } |
|
190 |
|
191 /** |
|
192 * Clean some cache records |
|
193 * |
|
194 * For this test backend only, if $mode == 'false', then the method will return false |
|
195 * (true else) |
|
196 * |
|
197 * Available modes are : |
|
198 * Zend_Cache::CLEANING_MODE_ALL (default) => remove all cache entries ($tags is not used) |
|
199 * Zend_Cache::CLEANING_MODE_OLD => remove too old cache entries ($tags is not used) |
|
200 * Zend_Cache::CLEANING_MODE_MATCHING_TAG => remove cache entries matching all given tags |
|
201 * ($tags can be an array of strings or a single string) |
|
202 * Zend_Cache::CLEANING_MODE_NOT_MATCHING_TAG => remove cache entries not {matching one of the given tags} |
|
203 * ($tags can be an array of strings or a single string) |
|
204 * |
|
205 * @param string $mode Clean mode |
|
206 * @param array $tags Array of tags |
|
207 * @return boolean True if no problem |
|
208 */ |
|
209 public function clean($mode = Zend_Cache::CLEANING_MODE_ALL, $tags = array()) |
|
210 { |
|
211 $this->_addLog('clean', array($mode, $tags)); |
|
212 if ($mode=='false') { |
|
213 return false; |
|
214 } |
|
215 return true; |
|
216 } |
|
217 |
|
218 /** |
|
219 * Get the last log |
|
220 * |
|
221 * @return string The last log |
|
222 */ |
|
223 public function getLastLog() |
|
224 { |
|
225 return $this->_log[$this->_index - 1]; |
|
226 } |
|
227 |
|
228 /** |
|
229 * Get the log index |
|
230 * |
|
231 * @return int Log index |
|
232 */ |
|
233 public function getLogIndex() |
|
234 { |
|
235 return $this->_index; |
|
236 } |
|
237 |
|
238 /** |
|
239 * Get the complete log array |
|
240 * |
|
241 * @return array Complete log array |
|
242 */ |
|
243 public function getAllLogs() |
|
244 { |
|
245 return $this->_log; |
|
246 } |
|
247 |
|
248 /** |
|
249 * Return true if the automatic cleaning is available for the backend |
|
250 * |
|
251 * @return boolean |
|
252 */ |
|
253 public function isAutomaticCleaningAvailable() |
|
254 { |
|
255 return true; |
|
256 } |
|
257 |
|
258 /** |
|
259 * Return an array of stored cache ids |
|
260 * |
|
261 * @return array array of stored cache ids (string) |
|
262 */ |
|
263 public function getIds() |
|
264 { |
|
265 return array( |
|
266 'prefix_id1', 'prefix_id2' |
|
267 ); |
|
268 } |
|
269 |
|
270 /** |
|
271 * Return an array of stored tags |
|
272 * |
|
273 * @return array array of stored tags (string) |
|
274 */ |
|
275 public function getTags() |
|
276 { |
|
277 return array( |
|
278 'tag1', 'tag2' |
|
279 ); |
|
280 } |
|
281 |
|
282 /** |
|
283 * Return an array of stored cache ids which match given tags |
|
284 * |
|
285 * In case of multiple tags, a logical AND is made between tags |
|
286 * |
|
287 * @param array $tags array of tags |
|
288 * @return array array of matching cache ids (string) |
|
289 */ |
|
290 public function getIdsMatchingTags($tags = array()) |
|
291 { |
|
292 if ($tags == array('tag1', 'tag2')) { |
|
293 return array('prefix_id1', 'prefix_id2'); |
|
294 } |
|
295 |
|
296 return array(); |
|
297 } |
|
298 |
|
299 /** |
|
300 * Return an array of stored cache ids which don't match given tags |
|
301 * |
|
302 * In case of multiple tags, a logical OR is made between tags |
|
303 * |
|
304 * @param array $tags array of tags |
|
305 * @return array array of not matching cache ids (string) |
|
306 */ |
|
307 public function getIdsNotMatchingTags($tags = array()) |
|
308 { |
|
309 if ($tags == array('tag3', 'tag4')) { |
|
310 return array('prefix_id3', 'prefix_id4'); |
|
311 } |
|
312 |
|
313 return array(); |
|
314 } |
|
315 |
|
316 /** |
|
317 * Return an array of stored cache ids which match any given tags |
|
318 * |
|
319 * In case of multiple tags, a logical AND is made between tags |
|
320 * |
|
321 * @param array $tags array of tags |
|
322 * @return array array of any matching cache ids (string) |
|
323 */ |
|
324 public function getIdsMatchingAnyTags($tags = array()) |
|
325 { |
|
326 if ($tags == array('tag5', 'tag6')) { |
|
327 return array('prefix_id5', 'prefix_id6'); |
|
328 } |
|
329 |
|
330 return array(); |
|
331 } |
|
332 |
|
333 /** |
|
334 * Return the filling percentage of the backend storage |
|
335 * |
|
336 * @return int integer between 0 and 100 |
|
337 */ |
|
338 public function getFillingPercentage() |
|
339 { |
|
340 return 50; |
|
341 } |
|
342 |
|
343 /** |
|
344 * Return an array of metadatas for the given cache id |
|
345 * |
|
346 * The array must include these keys : |
|
347 * - expire : the expire timestamp |
|
348 * - tags : a string array of tags |
|
349 * - mtime : timestamp of last modification time |
|
350 * |
|
351 * @param string $id cache id |
|
352 * @return array array of metadatas (false if the cache id is not found) |
|
353 */ |
|
354 public function getMetadatas($id) |
|
355 { |
|
356 return false; |
|
357 } |
|
358 |
|
359 /** |
|
360 * Give (if possible) an extra lifetime to the given cache id |
|
361 * |
|
362 * @param string $id cache id |
|
363 * @param int $extraLifetime |
|
364 * @return boolean true if ok |
|
365 */ |
|
366 public function touch($id, $extraLifetime) |
|
367 { |
|
368 return true; |
|
369 } |
|
370 |
|
371 /** |
|
372 * Return an associative array of capabilities (booleans) of the backend |
|
373 * |
|
374 * The array must include these keys : |
|
375 * - automatic_cleaning (is automating cleaning necessary) |
|
376 * - tags (are tags supported) |
|
377 * - expired_read (is it possible to read expired cache records |
|
378 * (for doNotTestCacheValidity option for example)) |
|
379 * - priority does the backend deal with priority when saving |
|
380 * - infinite_lifetime (is infinite lifetime can work with this backend) |
|
381 * - get_list (is it possible to get the list of cache ids and the complete list of tags) |
|
382 * |
|
383 * @return array associative of with capabilities |
|
384 */ |
|
385 public function getCapabilities() |
|
386 { |
|
387 return array( |
|
388 'automatic_cleaning' => true, |
|
389 'tags' => true, |
|
390 'expired_read' => false, |
|
391 'priority' => true, |
|
392 'infinite_lifetime' => true, |
|
393 'get_list' => true |
|
394 ); |
|
395 } |
|
396 |
|
397 /** |
|
398 * Add an event to the log array |
|
399 * |
|
400 * @param string $methodName MethodName |
|
401 * @param array $args Arguments |
|
402 * @return void |
|
403 */ |
|
404 private function _addLog($methodName, $args) |
|
405 { |
|
406 $this->_log[$this->_index] = array( |
|
407 'methodName' => $methodName, |
|
408 'args' => $args |
|
409 ); |
|
410 $this->_index = $this->_index + 1; |
|
411 } |
|
412 |
|
413 } |