Test.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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/ExtendedInterface.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_ExtendedInterface
  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. || $id == '88161989b73a4cbfd0b701c446115a99'
  101. || $id == '205fc79cba24f0f0018eb92c7c8b3ba4')
  102. {
  103. return false;
  104. }
  105. if ($id=='serialized') {
  106. return serialize(array('foo'));
  107. }
  108. if ($id=='serialized2') {
  109. return serialize(array('headers' => array(), 'data' => 'foo'));
  110. }
  111. if ( $id == '71769f39054f75894288e397df04e445' || $id == '615d222619fb20b527168340cebd0578'
  112. || $id == '8a02d218a5165c467e7a5747cc6bd4b6' || $id == '648aca1366211d17cbf48e65dc570bee'
  113. || $id == '4a923ef02d7f997ca14d56dfeae25ea7') {
  114. return serialize(array('foo', 'bar'));
  115. }
  116. return 'foo';
  117. }
  118. /**
  119. * Test if a cache is available or not (for the given id)
  120. *
  121. * For this test backend only, if $id == 'false', then the method will return false
  122. * (123456 else)
  123. *
  124. * @param string $id Cache id
  125. * @return mixed|false false (a cache is not available) or "last modified" timestamp (int) of the available cache record
  126. */
  127. public function test($id)
  128. {
  129. $this->_addLog('test', array($id));
  130. if ($id=='false') {
  131. return false;
  132. }
  133. if (($id=='3c439c922209e2cb0b54d6deffccd75a')) {
  134. return false;
  135. }
  136. return 123456;
  137. }
  138. /**
  139. * Save some string datas into a cache record
  140. *
  141. * For this test backend only, if $id == 'false', then the method will return false
  142. * (true else)
  143. *
  144. * @param string $data Datas to cache
  145. * @param string $id Cache id
  146. * @param array $tags Array of strings, the cache record will be tagged by each string entry
  147. * @param int $specificLifetime If != false, set a specific lifetime for this cache record (null => infinite lifetime)
  148. * @return boolean True if no problem
  149. */
  150. public function save($data, $id, $tags = array(), $specificLifetime = false)
  151. {
  152. $this->_addLog('save', array($data, $id, $tags));
  153. if ($id=='false') {
  154. return false;
  155. }
  156. return true;
  157. }
  158. /**
  159. * Remove a cache record
  160. *
  161. * For this test backend only, if $id == 'false', then the method will return false
  162. * (true else)
  163. *
  164. * @param string $id Cache id
  165. * @return boolean True if no problem
  166. */
  167. public function remove($id)
  168. {
  169. $this->_addLog('remove', array($id));
  170. if ($id=='false') {
  171. return false;
  172. }
  173. return true;
  174. }
  175. /**
  176. * Clean some cache records
  177. *
  178. * For this test backend only, if $mode == 'false', then the method will return false
  179. * (true else)
  180. *
  181. * Available modes are :
  182. * Zend_Cache::CLEANING_MODE_ALL (default) => remove all cache entries ($tags is not used)
  183. * Zend_Cache::CLEANING_MODE_OLD => remove too old cache entries ($tags is not used)
  184. * Zend_Cache::CLEANING_MODE_MATCHING_TAG => remove cache entries matching all given tags
  185. * ($tags can be an array of strings or a single string)
  186. * Zend_Cache::CLEANING_MODE_NOT_MATCHING_TAG => remove cache entries not {matching one of the given tags}
  187. * ($tags can be an array of strings or a single string)
  188. *
  189. * @param string $mode Clean mode
  190. * @param array $tags Array of tags
  191. * @return boolean True if no problem
  192. */
  193. public function clean($mode = Zend_Cache::CLEANING_MODE_ALL, $tags = array())
  194. {
  195. $this->_addLog('clean', array($mode, $tags));
  196. if ($mode=='false') {
  197. return false;
  198. }
  199. return true;
  200. }
  201. /**
  202. * Get the last log
  203. *
  204. * @return string The last log
  205. */
  206. public function getLastLog()
  207. {
  208. return $this->_log[$this->_index - 1];
  209. }
  210. /**
  211. * Get the log index
  212. *
  213. * @return int Log index
  214. */
  215. public function getLogIndex()
  216. {
  217. return $this->_index;
  218. }
  219. /**
  220. * Get the complete log array
  221. *
  222. * @return array Complete log array
  223. */
  224. public function getAllLogs()
  225. {
  226. return $this->_log;
  227. }
  228. /**
  229. * Return true if the automatic cleaning is available for the backend
  230. *
  231. * @return boolean
  232. */
  233. public function isAutomaticCleaningAvailable()
  234. {
  235. return true;
  236. }
  237. /**
  238. * Return an array of stored cache ids
  239. *
  240. * @return array array of stored cache ids (string)
  241. */
  242. public function getIds()
  243. {
  244. return array(
  245. 'prefix_id1', 'prefix_id2'
  246. );
  247. }
  248. /**
  249. * Return an array of stored tags
  250. *
  251. * @return array array of stored tags (string)
  252. */
  253. public function getTags()
  254. {
  255. return array(
  256. 'tag1', 'tag2'
  257. );
  258. }
  259. /**
  260. * Return an array of stored cache ids which match given tags
  261. *
  262. * In case of multiple tags, a logical AND is made between tags
  263. *
  264. * @param array $tags array of tags
  265. * @return array array of matching cache ids (string)
  266. */
  267. public function getIdsMatchingTags($tags = array())
  268. {
  269. if ($tags == array('tag1', 'tag2')) {
  270. return array('prefix_id1', 'prefix_id2');
  271. }
  272. return array();
  273. }
  274. /**
  275. * Return an array of stored cache ids which don't match given tags
  276. *
  277. * In case of multiple tags, a logical OR is made between tags
  278. *
  279. * @param array $tags array of tags
  280. * @return array array of not matching cache ids (string)
  281. */
  282. public function getIdsNotMatchingTags($tags = array())
  283. {
  284. if ($tags == array('tag3', 'tag4')) {
  285. return array('prefix_id3', 'prefix_id4');
  286. }
  287. return array();
  288. }
  289. /**
  290. * Return an array of stored cache ids which match any given tags
  291. *
  292. * In case of multiple tags, a logical AND is made between tags
  293. *
  294. * @param array $tags array of tags
  295. * @return array array of any matching cache ids (string)
  296. */
  297. public function getIdsMatchingAnyTags($tags = array())
  298. {
  299. if ($tags == array('tag5', 'tag6')) {
  300. return array('prefix_id5', 'prefix_id6');
  301. }
  302. return array();
  303. }
  304. /**
  305. * Return the filling percentage of the backend storage
  306. *
  307. * @return int integer between 0 and 100
  308. */
  309. public function getFillingPercentage()
  310. {
  311. return 50;
  312. }
  313. /**
  314. * Return an array of metadatas for the given cache id
  315. *
  316. * The array must include these keys :
  317. * - expire : the expire timestamp
  318. * - tags : a string array of tags
  319. * - mtime : timestamp of last modification time
  320. *
  321. * @param string $id cache id
  322. * @return array array of metadatas (false if the cache id is not found)
  323. */
  324. public function getMetadatas($id)
  325. {
  326. return false;
  327. }
  328. /**
  329. * Give (if possible) an extra lifetime to the given cache id
  330. *
  331. * @param string $id cache id
  332. * @param int $extraLifetime
  333. * @return boolean true if ok
  334. */
  335. public function touch($id, $extraLifetime)
  336. {
  337. return true;
  338. }
  339. /**
  340. * Return an associative array of capabilities (booleans) of the backend
  341. *
  342. * The array must include these keys :
  343. * - automatic_cleaning (is automating cleaning necessary)
  344. * - tags (are tags supported)
  345. * - expired_read (is it possible to read expired cache records
  346. * (for doNotTestCacheValidity option for example))
  347. * - priority does the backend deal with priority when saving
  348. * - infinite_lifetime (is infinite lifetime can work with this backend)
  349. * - get_list (is it possible to get the list of cache ids and the complete list of tags)
  350. *
  351. * @return array associative of with capabilities
  352. */
  353. public function getCapabilities()
  354. {
  355. return array(
  356. 'automatic_cleaning' => true,
  357. 'tags' => true,
  358. 'expired_read' => false,
  359. 'priority' => true,
  360. 'infinite_lifetime' => true,
  361. 'get_list' => true
  362. );
  363. }
  364. /**
  365. * Add an event to the log array
  366. *
  367. * @param string $methodName MethodName
  368. * @param array $args Arguments
  369. * @return void
  370. */
  371. private function _addLog($methodName, $args)
  372. {
  373. $this->_log[$this->_index] = array(
  374. 'methodName' => $methodName,
  375. 'args' => $args
  376. );
  377. $this->_index = $this->_index + 1;
  378. }
  379. }