Test.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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-2009 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-2009 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. return false;
  98. }
  99. if ($id=='serialized') {
  100. return serialize(array('foo'));
  101. }
  102. if ($id=='serialized2') {
  103. return serialize(array('headers' => array(), 'data' => 'foo'));
  104. }
  105. if (($id=='71769f39054f75894288e397df04e445') or ($id=='615d222619fb20b527168340cebd0578')) {
  106. return serialize(array('foo', 'bar'));
  107. }
  108. if (($id=='8a02d218a5165c467e7a5747cc6bd4b6') or ($id=='648aca1366211d17cbf48e65dc570bee')) {
  109. return serialize(array('foo', 'bar'));
  110. }
  111. return 'foo';
  112. }
  113. /**
  114. * Test if a cache is available or not (for the given id)
  115. *
  116. * For this test backend only, if $id == 'false', then the method will return false
  117. * (123456 else)
  118. *
  119. * @param string $id Cache id
  120. * @return mixed|false false (a cache is not available) or "last modified" timestamp (int) of the available cache record
  121. */
  122. public function test($id)
  123. {
  124. $this->_addLog('test', array($id));
  125. if ($id=='false') {
  126. return false;
  127. }
  128. if (($id=='d8523b3ee441006261eeffa5c3d3a0a7') or ($id=='3c439c922209e2cb0b54d6deffccd75a')) {
  129. return false;
  130. }
  131. if (($id=='40f649b94977c0a6e76902e2a0b43587') or ($id=='e83249ea22178277d5befc2c5e2e9ace')) {
  132. return false;
  133. }
  134. return 123456;
  135. }
  136. /**
  137. * Save some string datas into a cache record
  138. *
  139. * For this test backend only, if $id == 'false', then the method will return false
  140. * (true else)
  141. *
  142. * @param string $data Datas to cache
  143. * @param string $id Cache id
  144. * @param array $tags Array of strings, the cache record will be tagged by each string entry
  145. * @param int $specificLifetime If != false, set a specific lifetime for this cache record (null => infinite lifetime)
  146. * @return boolean True if no problem
  147. */
  148. public function save($data, $id, $tags = array(), $specificLifetime = false)
  149. {
  150. $this->_addLog('save', array($data, $id, $tags));
  151. if ($id=='false') {
  152. return false;
  153. }
  154. return true;
  155. }
  156. /**
  157. * Remove a cache record
  158. *
  159. * For this test backend only, if $id == 'false', then the method will return false
  160. * (true else)
  161. *
  162. * @param string $id Cache id
  163. * @return boolean True if no problem
  164. */
  165. public function remove($id)
  166. {
  167. $this->_addLog('remove', array($id));
  168. if ($id=='false') {
  169. return false;
  170. }
  171. return true;
  172. }
  173. /**
  174. * Clean some cache records
  175. *
  176. * For this test backend only, if $mode == 'false', then the method will return false
  177. * (true else)
  178. *
  179. * Available modes are :
  180. * Zend_Cache::CLEANING_MODE_ALL (default) => remove all cache entries ($tags is not used)
  181. * Zend_Cache::CLEANING_MODE_OLD => remove too old cache entries ($tags is not used)
  182. * Zend_Cache::CLEANING_MODE_MATCHING_TAG => remove cache entries matching all given tags
  183. * ($tags can be an array of strings or a single string)
  184. * Zend_Cache::CLEANING_MODE_NOT_MATCHING_TAG => remove cache entries not {matching one of the given tags}
  185. * ($tags can be an array of strings or a single string)
  186. *
  187. * @param string $mode Clean mode
  188. * @param array $tags Array of tags
  189. * @return boolean True if no problem
  190. */
  191. public function clean($mode = Zend_Cache::CLEANING_MODE_ALL, $tags = array())
  192. {
  193. $this->_addLog('clean', array($mode, $tags));
  194. if ($mode=='false') {
  195. return false;
  196. }
  197. return true;
  198. }
  199. /**
  200. * Get the last log
  201. *
  202. * @return string The last log
  203. */
  204. public function getLastLog()
  205. {
  206. return $this->_log[$this->_index - 1];
  207. }
  208. /**
  209. * Get the log index
  210. *
  211. * @return int Log index
  212. */
  213. public function getLogIndex()
  214. {
  215. return $this->_index;
  216. }
  217. /**
  218. * Get the complete log array
  219. *
  220. * @return array Complete log array
  221. */
  222. public function getAllLogs()
  223. {
  224. return $this->_log;
  225. }
  226. /**
  227. * Return true if the automatic cleaning is available for the backend
  228. *
  229. * @return boolean
  230. */
  231. public function isAutomaticCleaningAvailable()
  232. {
  233. return true;
  234. }
  235. /**
  236. * Add an event to the log array
  237. *
  238. * @param string $methodName MethodName
  239. * @param array $args Arguments
  240. * @return void
  241. */
  242. private function _addLog($methodName, $args)
  243. {
  244. $this->_log[$this->_index] = array(
  245. 'methodName' => $methodName,
  246. 'args' => $args
  247. );
  248. $this->_index = $this->_index + 1;
  249. }
  250. }