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.
15 * @category Zend_Cache
17 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
18 * @license http://framework.zend.com/license/new-bsd New BSD License
22 require_once 'Zend/Cache.php';
23 require_once 'Zend/Cache/Manager.php';
24 require_once 'Zend/Config.php';
27 * @category Zend_Cache
29 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
30 * @license http://framework.zend.com/license/new-bsd New BSD License
32 class Zend_Cache_ManagerTest
extends PHPUnit_Framework_TestCase
35 public function setUp()
37 $this->_cache_dir
= $this->mkdir();
38 $this->_cache
= Zend_Cache
::factory(
40 array('automatic_serialization'=>true),
41 array('cache_dir'=>$this->_cache_dir
)
45 public function tearDown()
51 public function testSetsCacheObject()
53 $manager = new Zend_Cache_Manager
;
54 $manager->setCache('cache1', $this->_cache
);
55 $this->assertTrue($manager->getCache('cache1') instanceof Zend_Cache_Core
);
58 public function testLazyLoadsDefaultPageCache()
60 $manager = new Zend_Cache_Manager
;
61 $manager->setTemplateOptions('tagCache',array(
64 'cache_dir' => $this->_cache_dir
68 $this->assertTrue($manager->getCache('page') instanceof Zend_Cache_Frontend_Output
);
71 public function testCanOverrideCacheFrontendNameConfiguration()
73 $manager = new Zend_Cache_Manager
;
74 $manager->setTemplateOptions('tagCache',array(
77 'cache_dir' => $this->_cache_dir
81 $manager->setTemplateOptions('page', array(
86 $this->assertTrue($manager->getCache('page') instanceof Zend_Cache_Frontend_Page
);
89 public function testCanMergeTemplateCacheOptionsFromZendConfig()
91 $manager = new Zend_Cache_Manager
;
92 $config = new Zend_Config(array(
95 'cache_dir' => $this->_cache_dir
99 $manager->setTemplateOptions('tagCache', $config);
100 $options = $manager->getCacheTemplate('tagCache');
101 $this->assertEquals($this->_cache_dir
, $options['backend']['options']['cache_dir']);
104 public function testCanOverrideCacheBackendendNameConfiguration()
106 $manager = new Zend_Cache_Manager
;
107 $manager->setTemplateOptions('tagCache',array(
110 'cache_dir' => $this->_cache_dir
114 $manager->setTemplateOptions('page', array(
119 $this->assertTrue($manager->getCache('page')->getBackend() instanceof Zend_Cache_Backend_File
);
122 public function testCanOverrideCacheFrontendOptionsConfiguration()
124 $manager = new Zend_Cache_Manager
;
125 $manager->setTemplateOptions('page', array(
132 $config = $manager->getCacheTemplate('page');
133 $this->assertEquals(9999, $config['frontend']['options']['lifetime']);
136 public function testCanOverrideCacheBackendOptionsConfiguration()
138 $manager = new Zend_Cache_Manager
;
139 $manager->setTemplateOptions('page', array(
142 'public_dir' => './cacheDir'
146 $config = $manager->getCacheTemplate('page');
147 $this->assertEquals('./cacheDir', $config['backend']['options']['public_dir']);
150 public function testSetsConfigTemplate()
152 $manager = new Zend_Cache_Manager
;
157 'automatic_serialization' => true
163 'cache_dir' => '../cache',
167 $manager->setCacheTemplate('myCache', $config);
168 $this->assertSame($config, $manager->getCacheTemplate('myCache'));
171 public function testSetsConfigTemplateWithoutMultipartNameNormalisation()
173 $manager = new Zend_Cache_Manager
;
178 'automatic_serialization' => true
182 'name' => 'BlackHole'
185 $manager->setCacheTemplate('myCache', $config);
186 $this->assertSame($config, $manager->getCacheTemplate('myCache'));
189 public function testSetsOptionsTemplateUsingZendConfig()
191 $manager = new Zend_Cache_Manager
;
196 'automatic_serialization' => true
202 'cache_dir' => '../cache',
206 $manager->setCacheTemplate('myCache', new Zend_Config($config));
207 $this->assertSame($config, $manager->getCacheTemplate('myCache'));
210 public function testConfigTemplatesDetectedAsAvailableCaches()
212 $manager = new Zend_Cache_Manager
;
213 $this->assertTrue($manager->hasCache('page'));
216 public function testGettingPageCacheAlsoCreatesTagCache()
218 $manager = new Zend_Cache_Manager
;
219 $tagCacheConfig = $manager->getCacheTemplate('tagCache');
220 $tagCacheConfig['backend']['options']['cache_dir'] = $this->getTmpDir();
221 $manager->setCacheTemplate('tagCache', $tagCacheConfig);
222 $tagCache = $manager->getCache('page')->getBackend()->getOption('tag_cache');
223 $this->assertTrue($tagCache instanceof Zend_Cache_Core
);
228 public function mkdir()
230 $tmp = $this->getTmpDir();
235 public function rmdir()
237 $tmpDir = $this->getTmpDir(false);
238 foreach (glob("$tmpDir*") as $dirname) {
243 public function getTmpDir($date = true)
246 $tmp = sys_get_temp_dir();
248 $suffix = date('mdyHis');
250 if (is_writeable($tmp)) {
251 return $tmp . DIRECTORY_SEPARATOR
. 'zend_cache_tmp_dir_' . $suffix;
253 throw new Exception("no writable tmpdir found");