Test.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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$
  21. */
  22. /**
  23. * @see Zend_Cache_Backend_Interface
  24. */
  25. require_once 'Zend/Cache/Backend/Interface.php';
  26. /**
  27. * @see Zend_Cache_Backend
  28. */
  29. require_once 'Zend/Cache/Backend.php';
  30. /**
  31. * @package Zend_Cache
  32. * @subpackage Zend_Cache_Backend
  33. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  34. * @license http://framework.zend.com/license/new-bsd New BSD License
  35. */
  36. class Zend_Cache_Backend_Test extends Zend_Cache_Backend implements Zend_Cache_Backend_Interface
  37. {
  38. /**
  39. * Available options
  40. *
  41. * @var array available options
  42. */
  43. protected $_options = array();
  44. /**
  45. * Frontend or Core directives
  46. *
  47. * @var array directives
  48. */
  49. protected $_directives = array();
  50. /**
  51. * Array to log actions
  52. *
  53. * @var array $_log
  54. */
  55. private $_log = array();
  56. /**
  57. * Current index for log array
  58. *
  59. * @var int $_index
  60. */
  61. private $_index = 0;
  62. /**
  63. * Constructor
  64. *
  65. * @param array $options associative array of options
  66. * @return void
  67. */
  68. public function __construct($options = array())
  69. {
  70. $this->_addLog('construct', array($options));
  71. }
  72. /**
  73. * Set the frontend directives
  74. *
  75. * @param array $directives assoc of directives
  76. * @return void
  77. */
  78. public function setDirectives($directives)
  79. {
  80. $this->_addLog('setDirectives', array($directives));
  81. }
  82. /**
  83. * Test if a cache is available for the given id and (if yes) return it (false else)
  84. *
  85. * For this test backend only, if $id == 'false', then the method will return false
  86. * if $id == 'serialized', the method will return a serialized array
  87. * ('foo' else)
  88. *
  89. * @param string $id Cache id
  90. * @param boolean $doNotTestCacheValidity If set to true, the cache validity won't be tested
  91. * @return string Cached datas (or false)
  92. */
  93. public function load($id, $doNotTestCacheValidity = false)
  94. {
  95. $this->_addLog('get', array($id, $doNotTestCacheValidity));
  96. if ( $id == 'false'
  97. || $id == 'd8523b3ee441006261eeffa5c3d3a0a7'
  98. || $id == 'e83249ea22178277d5befc2c5e2e9ace'
  99. || $id == '40f649b94977c0a6e76902e2a0b43587')
  100. {
  101. return false;
  102. }
  103. if ($id=='serialized') {
  104. return serialize(array('foo'));
  105. }
  106. if ($id=='serialized2') {
  107. return serialize(array('headers' => array(), 'data' => 'foo'));
  108. }
  109. if (($id=='71769f39054f75894288e397df04e445') or ($id=='615d222619fb20b527168340cebd0578')) {
  110. return serialize(array('foo', 'bar'));
  111. }
  112. if (($id=='8a02d218a5165c467e7a5747cc6bd4b6') or ($id=='648aca1366211d17cbf48e65dc570bee')) {
  113. return serialize(array('foo', 'bar'));
  114. }
  115. return 'foo';
  116. }
  117. /**
  118. * Test if a cache is available or not (for the given id)
  119. *
  120. * For this test backend only, if $id == 'false', then the method will return false
  121. * (123456 else)
  122. *
  123. * @param string $id Cache id
  124. * @return mixed|false false (a cache is not available) or "last modified" timestamp (int) of the available cache record
  125. */
  126. public function test($id)
  127. {
  128. $this->_addLog('test', array($id));
  129. if ($id=='false') {
  130. return false;
  131. }
  132. if (($id=='3c439c922209e2cb0b54d6deffccd75a')) {
  133. return false;
  134. }
  135. return 123456;
  136. }
  137. /**
  138. * Save some string datas into a cache record
  139. *
  140. * For this test backend only, if $id == 'false', then the method will return false
  141. * (true else)
  142. *
  143. * @param string $data Datas to cache
  144. * @param string $id Cache id
  145. * @param array $tags Array of strings, the cache record will be tagged by each string entry
  146. * @param int $specificLifetime If != false, set a specific lifetime for this cache record (null => infinite lifetime)
  147. * @return boolean True if no problem
  148. */
  149. public function save($data, $id, $tags = array(), $specificLifetime = false)
  150. {
  151. $this->_addLog('save', array($data, $id, $tags));
  152. if ($id=='false') {
  153. return false;
  154. }
  155. return true;
  156. }
  157. /**
  158. * Remove a cache record
  159. *
  160. * For this test backend only, if $id == 'false', then the method will return false
  161. * (true else)
  162. *
  163. * @param string $id Cache id
  164. * @return boolean True if no problem
  165. */
  166. public function remove($id)
  167. {
  168. $this->_addLog('remove', array($id));
  169. if ($id=='false') {
  170. return false;
  171. }
  172. return true;
  173. }
  174. /**
  175. * Clean some cache records
  176. *
  177. * For this test backend only, if $mode == 'false', then the method will return false
  178. * (true else)
  179. *
  180. * Available modes are :
  181. * Zend_Cache::CLEANING_MODE_ALL (default) => remove all cache entries ($tags is not used)
  182. * Zend_Cache::CLEANING_MODE_OLD => remove too old cache entries ($tags is not used)
  183. * Zend_Cache::CLEANING_MODE_MATCHING_TAG => remove cache entries matching all given tags
  184. * ($tags can be an array of strings or a single string)
  185. * Zend_Cache::CLEANING_MODE_NOT_MATCHING_TAG => remove cache entries not {matching one of the given tags}
  186. * ($tags can be an array of strings or a single string)
  187. *
  188. * @param string $mode Clean mode
  189. * @param array $tags Array of tags
  190. * @return boolean True if no problem
  191. */
  192. public function clean($mode = Zend_Cache::CLEANING_MODE_ALL, $tags = array())
  193. {
  194. $this->_addLog('clean', array($mode, $tags));
  195. if ($mode=='false') {
  196. return false;
  197. }
  198. return true;
  199. }
  200. /**
  201. * Get the last log
  202. *
  203. * @return string The last log
  204. */
  205. public function getLastLog()
  206. {
  207. return $this->_log[$this->_index - 1];
  208. }
  209. /**
  210. * Get the log index
  211. *
  212. * @return int Log index
  213. */
  214. public function getLogIndex()
  215. {
  216. return $this->_index;
  217. }
  218. /**
  219. * Get the complete log array
  220. *
  221. * @return array Complete log array
  222. */
  223. public function getAllLogs()
  224. {
  225. return $this->_log;
  226. }
  227. /**
  228. * Return true if the automatic cleaning is available for the backend
  229. *
  230. * @return boolean
  231. */
  232. public function isAutomaticCleaningAvailable()
  233. {
  234. return true;
  235. }
  236. /**
  237. * Add an event to the log array
  238. *
  239. * @param string $methodName MethodName
  240. * @param array $args Arguments
  241. * @return void
  242. */
  243. private function _addLog($methodName, $args)
  244. {
  245. $this->_log[$this->_index] = array(
  246. 'methodName' => $methodName,
  247. 'args' => $args
  248. );
  249. $this->_index = $this->_index + 1;
  250. }
  251. }