Test.php 12 KB

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