ZF-8222: format class name for class checks
[zend.git] / tests / Zend / Cache / ManagerTest.php
blob9a9170322a92c14991d29519862e81050536caba
1 <?php
2 /**
3 * Zend Framework
5 * LICENSE
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
16 * @package UnitTests
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
19 * @version $Id$
22 require_once 'Zend/Cache.php';
23 require_once 'Zend/Cache/Manager.php';
24 require_once 'Zend/Config.php';
26 /**
27 * @category Zend_Cache
28 * @package UnitTests
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(
39 'Core', 'File',
40 array('automatic_serialization'=>true),
41 array('cache_dir'=>$this->_cache_dir)
45 public function tearDown()
47 $this->rmdir();
48 $this->_cache = null;
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(
62 'backend' => array(
63 'options' => array(
64 'cache_dir' => $this->_cache_dir
67 ));
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(
75 'backend' => array(
76 'options' => array(
77 'cache_dir' => $this->_cache_dir
80 ));
81 $manager->setTemplateOptions('page', array(
82 'frontend' => array(
83 'name'=> 'Page'
85 ));
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(
93 'backend' => array(
94 'options' => array(
95 'cache_dir' => $this->_cache_dir
98 ));
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(
108 'backend' => array(
109 'options' => array(
110 'cache_dir' => $this->_cache_dir
114 $manager->setTemplateOptions('page', array(
115 'backend' => array(
116 'name'=> 'File'
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(
126 'frontend' => array(
127 'options'=> array(
128 'lifetime' => 9999
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(
140 'backend' => array(
141 'options'=> 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;
153 $config = array(
154 'frontend' => array(
155 'name' => 'Core',
156 'options' => array(
157 'automatic_serialization' => true
160 'backend' => array(
161 'name' => 'File',
162 'options' => array(
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;
174 $config = array(
175 'frontend' => array(
176 'name' => 'Core',
177 'options' => array(
178 'automatic_serialization' => true
181 'backend' => array(
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;
192 $config = array(
193 'frontend' => array(
194 'name' => 'Core',
195 'options' => array(
196 'automatic_serialization' => true
199 'backend' => array(
200 'name' => 'File',
201 'options' => array(
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);
226 // Helper Methods
228 public function mkdir()
230 $tmp = $this->getTmpDir();
231 @mkdir($tmp);
232 return $tmp;
235 public function rmdir()
237 $tmpDir = $this->getTmpDir(false);
238 foreach (glob("$tmpDir*") as $dirname) {
239 @rmdir($dirname);
243 public function getTmpDir($date = true)
245 $suffix = '';
246 $tmp = sys_get_temp_dir();
247 if ($date) {
248 $suffix = date('mdyHis');
250 if (is_writeable($tmp)) {
251 return $tmp . DIRECTORY_SEPARATOR . 'zend_cache_tmp_dir_' . $suffix;
252 } else {
253 throw new Exception("no writable tmpdir found");