Test.php 7.6 KB

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