File indexing completed on 2024-05-26 06:02:46

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category   Zend
0016  * @package    Zend_Cache
0017  * @subpackage Zend_Cache_Backend
0018  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0019  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0020  * @version    $Id$
0021  */
0022 
0023 
0024 /**
0025  * @see Zend_Cache_Backend_Interface
0026  */
0027 // require_once 'Zend/Cache/Backend/ExtendedInterface.php';
0028 
0029 /**
0030  * @see Zend_Cache_Backend
0031  */
0032 // require_once 'Zend/Cache/Backend.php';
0033 
0034 /**
0035  * @package    Zend_Cache
0036  * @subpackage Zend_Cache_Backend
0037  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0038  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0039  */
0040 class Zend_Cache_Backend_Test extends Zend_Cache_Backend implements Zend_Cache_Backend_ExtendedInterface
0041 {
0042     /**
0043      * Available options
0044      *
0045      * @var array available options
0046      */
0047     protected $_options = array();
0048 
0049     /**
0050      * Frontend or Core directives
0051      *
0052      * @var array directives
0053      */
0054     protected $_directives = array();
0055 
0056     /**
0057      * Array to log actions
0058      *
0059      * @var array $_log
0060      */
0061     private $_log = array();
0062 
0063     /**
0064      * Current index for log array
0065      *
0066      * @var int $_index
0067      */
0068     private $_index = 0;
0069 
0070     /**
0071      * Constructor
0072      *
0073      * @param  array $options associative array of options
0074      * @return void
0075      */
0076     public function __construct($options = array())
0077     {
0078         $this->_addLog('construct', array($options));
0079     }
0080 
0081     /**
0082      * Set the frontend directives
0083      *
0084      * @param  array $directives assoc of directives
0085      * @return void
0086      */
0087     public function setDirectives($directives)
0088     {
0089         $this->_addLog('setDirectives', array($directives));
0090     }
0091 
0092     /**
0093      * Test if a cache is available for the given id and (if yes) return it (false else)
0094      *
0095      * For this test backend only, if $id == 'false', then the method will return false
0096      * if $id == 'serialized', the method will return a serialized array
0097      * ('foo' else)
0098      *
0099      * @param  string  $id                     Cache id
0100      * @param  boolean $doNotTestCacheValidity If set to true, the cache validity won't be tested
0101      * @return string Cached datas (or false)
0102      */
0103     public function load($id, $doNotTestCacheValidity = false)
0104     {
0105         $this->_addLog('get', array($id, $doNotTestCacheValidity));
0106 
0107         if ( $id == 'false'
0108           || $id == 'd8523b3ee441006261eeffa5c3d3a0a7'
0109           || $id == 'e83249ea22178277d5befc2c5e2e9ace'
0110           || $id == '40f649b94977c0a6e76902e2a0b43587'
0111           || $id == '88161989b73a4cbfd0b701c446115a99'
0112           || $id == '205fc79cba24f0f0018eb92c7c8b3ba4'
0113           || $id == '170720e35f38150b811f68a937fb042d')
0114         {
0115             return false;
0116         }
0117         if ($id=='serialized') {
0118             return serialize(array('foo'));
0119         }
0120         if ($id=='serialized2') {
0121             return serialize(array('headers' => array(), 'data' => 'foo'));
0122         }
0123         if ( $id == '71769f39054f75894288e397df04e445' || $id == '615d222619fb20b527168340cebd0578'
0124           || $id == '8a02d218a5165c467e7a5747cc6bd4b6' || $id == '648aca1366211d17cbf48e65dc570bee'
0125           || $id == '4a923ef02d7f997ca14d56dfeae25ea7') {
0126             return serialize(array('foo', 'bar'));
0127         }
0128         if ( $id == 'f53c7d912cc523d9a65834c8286eceb9') {
0129             return serialize(array('foobar'));
0130         }
0131         return 'foo';
0132     }
0133 
0134     /**
0135      * Test if a cache is available or not (for the given id)
0136      *
0137      * For this test backend only, if $id == 'false', then the method will return false
0138      * (123456 else)
0139      *
0140      * @param  string $id Cache id
0141      * @return mixed|false false (a cache is not available) or "last modified" timestamp (int) of the available cache record
0142      */
0143     public function test($id)
0144     {
0145         $this->_addLog('test', array($id));
0146         if ($id=='false') {
0147             return false;
0148         }
0149         if (($id=='3c439c922209e2cb0b54d6deffccd75a')) {
0150             return false;
0151         }
0152         return 123456;
0153     }
0154 
0155     /**
0156      * Save some string datas into a cache record
0157      *
0158      * For this test backend only, if $id == 'false', then the method will return false
0159      * (true else)
0160      *
0161      * @param  string $data             Datas to cache
0162      * @param  string $id               Cache id
0163      * @param  array  $tags             Array of strings, the cache record will be tagged by each string entry
0164      * @param  int    $specificLifetime If != false, set a specific lifetime for this cache record (null => infinite lifetime)
0165      * @return boolean True if no problem
0166      */
0167     public function save($data, $id, $tags = array(), $specificLifetime = false)
0168     {
0169         $this->_addLog('save', array($data, $id, $tags));
0170         if (substr($id,-5)=='false') {
0171             return false;
0172         }
0173         return true;
0174     }
0175 
0176     /**
0177      * Remove a cache record
0178      *
0179      * For this test backend only, if $id == 'false', then the method will return false
0180      * (true else)
0181      *
0182      * @param  string $id Cache id
0183      * @return boolean True if no problem
0184      */
0185     public function remove($id)
0186     {
0187         $this->_addLog('remove', array($id));
0188         if (substr($id,-5)=='false') {
0189             return false;
0190         }
0191         return true;
0192     }
0193 
0194     /**
0195      * Clean some cache records
0196      *
0197      * For this test backend only, if $mode == 'false', then the method will return false
0198      * (true else)
0199      *
0200      * Available modes are :
0201      * Zend_Cache::CLEANING_MODE_ALL (default)    => remove all cache entries ($tags is not used)
0202      * Zend_Cache::CLEANING_MODE_OLD              => remove too old cache entries ($tags is not used)
0203      * Zend_Cache::CLEANING_MODE_MATCHING_TAG     => remove cache entries matching all given tags
0204      *                                               ($tags can be an array of strings or a single string)
0205      * Zend_Cache::CLEANING_MODE_NOT_MATCHING_TAG => remove cache entries not {matching one of the given tags}
0206      *                                               ($tags can be an array of strings or a single string)
0207      *
0208      * @param  string $mode Clean mode
0209      * @param  array  $tags Array of tags
0210      * @return boolean True if no problem
0211      */
0212     public function clean($mode = Zend_Cache::CLEANING_MODE_ALL, $tags = array())
0213     {
0214         $this->_addLog('clean', array($mode, $tags));
0215         if ($mode=='false') {
0216             return false;
0217         }
0218         return true;
0219     }
0220 
0221     /**
0222      * Get the last log
0223      *
0224      * @return string The last log
0225      */
0226     public function getLastLog()
0227     {
0228         return $this->_log[$this->_index - 1];
0229     }
0230 
0231     /**
0232      * Get the log index
0233      *
0234      * @return int Log index
0235      */
0236     public function getLogIndex()
0237     {
0238         return $this->_index;
0239     }
0240 
0241     /**
0242      * Get the complete log array
0243      *
0244      * @return array Complete log array
0245      */
0246     public function getAllLogs()
0247     {
0248         return $this->_log;
0249     }
0250 
0251     /**
0252      * Return true if the automatic cleaning is available for the backend
0253      *
0254      * @return boolean
0255      */
0256     public function isAutomaticCleaningAvailable()
0257     {
0258         return true;
0259     }
0260 
0261     /**
0262      * Return an array of stored cache ids
0263      *
0264      * @return array array of stored cache ids (string)
0265      */
0266     public function getIds()
0267     {
0268         return array(
0269             'prefix_id1', 'prefix_id2'
0270         );
0271     }
0272 
0273     /**
0274      * Return an array of stored tags
0275      *
0276      * @return array array of stored tags (string)
0277      */
0278     public function getTags()
0279     {
0280         return array(
0281             'tag1', 'tag2'
0282         );
0283     }
0284 
0285     /**
0286      * Return an array of stored cache ids which match given tags
0287      *
0288      * In case of multiple tags, a logical AND is made between tags
0289      *
0290      * @param array $tags array of tags
0291      * @return array array of matching cache ids (string)
0292      */
0293     public function getIdsMatchingTags($tags = array())
0294     {
0295         if ($tags == array('tag1', 'tag2')) {
0296             return array('prefix_id1', 'prefix_id2');
0297         }
0298 
0299         return array();
0300     }
0301 
0302     /**
0303      * Return an array of stored cache ids which don't match given tags
0304      *
0305      * In case of multiple tags, a logical OR is made between tags
0306      *
0307      * @param array $tags array of tags
0308      * @return array array of not matching cache ids (string)
0309      */
0310     public function getIdsNotMatchingTags($tags = array())
0311     {
0312         if ($tags == array('tag3', 'tag4')) {
0313             return array('prefix_id3', 'prefix_id4');
0314         }
0315 
0316         return array();
0317     }
0318 
0319     /**
0320      * Return an array of stored cache ids which match any given tags
0321      *
0322      * In case of multiple tags, a logical AND is made between tags
0323      *
0324      * @param array $tags array of tags
0325      * @return array array of any matching cache ids (string)
0326      */
0327     public function getIdsMatchingAnyTags($tags = array())
0328     {
0329         if ($tags == array('tag5', 'tag6')) {
0330             return array('prefix_id5', 'prefix_id6');
0331         }
0332 
0333         return array();
0334     }
0335 
0336     /**
0337      * Return the filling percentage of the backend storage
0338      *
0339      * @return int integer between 0 and 100
0340      */
0341     public function getFillingPercentage()
0342     {
0343         return 50;
0344     }
0345 
0346     /**
0347      * Return an array of metadatas for the given cache id
0348      *
0349      * The array must include these keys :
0350      * - expire : the expire timestamp
0351      * - tags : a string array of tags
0352      * - mtime : timestamp of last modification time
0353      *
0354      * @param string $id cache id
0355      * @return array array of metadatas (false if the cache id is not found)
0356      */
0357     public function getMetadatas($id)
0358     {
0359         return false;
0360     }
0361 
0362     /**
0363      * Give (if possible) an extra lifetime to the given cache id
0364      *
0365      * @param string $id cache id
0366      * @param int $extraLifetime
0367      * @return boolean true if ok
0368      */
0369     public function touch($id, $extraLifetime)
0370     {
0371         return true;
0372     }
0373 
0374     /**
0375      * Return an associative array of capabilities (booleans) of the backend
0376      *
0377      * The array must include these keys :
0378      * - automatic_cleaning (is automating cleaning necessary)
0379      * - tags (are tags supported)
0380      * - expired_read (is it possible to read expired cache records
0381      *                 (for doNotTestCacheValidity option for example))
0382      * - priority does the backend deal with priority when saving
0383      * - infinite_lifetime (is infinite lifetime can work with this backend)
0384      * - get_list (is it possible to get the list of cache ids and the complete list of tags)
0385      *
0386      * @return array associative of with capabilities
0387      */
0388     public function getCapabilities()
0389     {
0390         return array(
0391             'automatic_cleaning' => true,
0392             'tags'               => true,
0393             'expired_read'       => false,
0394             'priority'           => true,
0395             'infinite_lifetime'  => true,
0396             'get_list'           => true
0397         );
0398     }
0399 
0400     /**
0401      * Add an event to the log array
0402      *
0403      * @param  string $methodName MethodName
0404      * @param  array  $args       Arguments
0405      * @return void
0406      */
0407     private function _addLog($methodName, $args)
0408     {
0409         $this->_log[$this->_index] = array(
0410             'methodName' => $methodName,
0411             'args' => $args
0412         );
0413         $this->_index = $this->_index + 1;
0414     }
0415 
0416 }