ManagerTest.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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_Cache
  16. * @package UnitTests
  17. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id$
  20. */
  21. require_once 'Zend/Cache.php';
  22. require_once 'Zend/Cache/Manager.php';
  23. require_once 'Zend/Config.php';
  24. /**
  25. * @category Zend_Cache
  26. * @package UnitTests
  27. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  28. * @license http://framework.zend.com/license/new-bsd New BSD License
  29. */
  30. class Zend_Cache_ManagerTest extends PHPUnit_Framework_TestCase
  31. {
  32. public function setUp()
  33. {
  34. $this->_cache_dir = $this->mkdir();
  35. $this->_cache = Zend_Cache::factory(
  36. 'Core', 'File',
  37. array('automatic_serialization'=>true),
  38. array('cache_dir'=>$this->_cache_dir)
  39. );
  40. }
  41. public function tearDown()
  42. {
  43. $this->rmdir();
  44. $this->_cache = null;
  45. }
  46. public function testSetsCacheObject()
  47. {
  48. $manager = new Zend_Cache_Manager;
  49. $manager->setCache('cache1', $this->_cache);
  50. $this->assertTrue($manager->getCache('cache1') instanceof Zend_Cache_Core);
  51. }
  52. public function testLazyLoadsDefaultPageCache()
  53. {
  54. $manager = new Zend_Cache_Manager;
  55. $manager->setTemplateOptions('tagCache',array(
  56. 'backend' => array(
  57. 'options' => array(
  58. 'cache_dir' => $this->_cache_dir
  59. )
  60. )
  61. ));
  62. $this->assertTrue($manager->getCache('page') instanceof Zend_Cache_Frontend_Output);
  63. }
  64. public function testCanOverrideCacheFrontendNameConfiguration()
  65. {
  66. $manager = new Zend_Cache_Manager;
  67. $manager->setTemplateOptions('tagCache',array(
  68. 'backend' => array(
  69. 'options' => array(
  70. 'cache_dir' => $this->_cache_dir
  71. )
  72. )
  73. ));
  74. $manager->setTemplateOptions('page', array(
  75. 'frontend' => array(
  76. 'name'=> 'Page'
  77. )
  78. ));
  79. $this->assertTrue($manager->getCache('page') instanceof Zend_Cache_Frontend_Page);
  80. }
  81. public function testCanMergeTemplateCacheOptionsFromZendConfig()
  82. {
  83. $manager = new Zend_Cache_Manager;
  84. $config = new Zend_Config(array(
  85. 'backend' => array(
  86. 'options' => array(
  87. 'cache_dir' => $this->_cache_dir
  88. )
  89. )
  90. ));
  91. $manager->setTemplateOptions('tagCache', $config);
  92. $options = $manager->getCacheTemplate('tagCache');
  93. $this->assertEquals($this->_cache_dir, $options['backend']['options']['cache_dir']);
  94. }
  95. public function testCanOverrideCacheBackendendNameConfiguration()
  96. {
  97. $manager = new Zend_Cache_Manager;
  98. $manager->setTemplateOptions('tagCache',array(
  99. 'backend' => array(
  100. 'options' => array(
  101. 'cache_dir' => $this->_cache_dir
  102. )
  103. )
  104. ));
  105. $manager->setTemplateOptions('page', array(
  106. 'backend' => array(
  107. 'name'=> 'File'
  108. )
  109. ));
  110. $this->assertTrue($manager->getCache('page')->getBackend() instanceof Zend_Cache_Backend_File);
  111. }
  112. public function testCanOverrideCacheFrontendOptionsConfiguration()
  113. {
  114. $manager = new Zend_Cache_Manager;
  115. $manager->setTemplateOptions('page', array(
  116. 'frontend' => array(
  117. 'options'=> array(
  118. 'lifetime' => 9999
  119. )
  120. )
  121. ));
  122. $config = $manager->getCacheTemplate('page');
  123. $this->assertEquals(9999, $config['frontend']['options']['lifetime']);
  124. }
  125. public function testCanOverrideCacheBackendOptionsConfiguration()
  126. {
  127. $manager = new Zend_Cache_Manager;
  128. $manager->setTemplateOptions('page', array(
  129. 'backend' => array(
  130. 'options'=> array(
  131. 'public_dir' => './cacheDir'
  132. )
  133. )
  134. ));
  135. $config = $manager->getCacheTemplate('page');
  136. $this->assertEquals('./cacheDir', $config['backend']['options']['public_dir']);
  137. }
  138. public function testSetsConfigTemplate()
  139. {
  140. $manager = new Zend_Cache_Manager;
  141. $config = array(
  142. 'frontend' => array(
  143. 'name' => 'Core',
  144. 'options' => array(
  145. 'automatic_serialization' => true
  146. )
  147. ),
  148. 'backend' => array(
  149. 'name' => 'File',
  150. 'options' => array(
  151. 'cache_dir' => '../cache',
  152. )
  153. )
  154. );
  155. $manager->setCacheTemplate('myCache', $config);
  156. $this->assertSame($config, $manager->getCacheTemplate('myCache'));
  157. }
  158. public function testSetsOptionsTemplateUsingZendConfig()
  159. {
  160. $manager = new Zend_Cache_Manager;
  161. $config = array(
  162. 'frontend' => array(
  163. 'name' => 'Core',
  164. 'options' => array(
  165. 'automatic_serialization' => true
  166. )
  167. ),
  168. 'backend' => array(
  169. 'name' => 'File',
  170. 'options' => array(
  171. 'cache_dir' => '../cache',
  172. )
  173. )
  174. );
  175. $manager->setCacheTemplate('myCache', new Zend_Config($config));
  176. $this->assertSame($config, $manager->getCacheTemplate('myCache'));
  177. }
  178. public function testConfigTemplatesDetectedAsAvailableCaches()
  179. {
  180. $manager = new Zend_Cache_Manager;
  181. $this->assertTrue($manager->hasCache('page'));
  182. }
  183. public function testGettingPageCacheAlsoCreatesTagCache()
  184. {
  185. $manager = new Zend_Cache_Manager;
  186. $tagCacheConfig = $manager->getCacheTemplate('tagCache');
  187. $tagCacheConfig['backend']['options']['cache_dir'] = $this->getTmpDir();
  188. $manager->setCacheTemplate('tagCache', $tagCacheConfig);
  189. $tagCache = $manager->getCache('page')->getBackend()->getOption('tag_cache');
  190. $this->assertTrue($tagCache instanceof Zend_Cache_Core);
  191. }
  192. // Helper Methods
  193. public function mkdir()
  194. {
  195. $tmp = $this->getTmpDir();
  196. @mkdir($tmp);
  197. return $tmp;
  198. }
  199. public function rmdir()
  200. {
  201. $tmpDir = $this->getTmpDir(false);
  202. foreach (glob("$tmpDir*") as $dirname) {
  203. @rmdir($dirname);
  204. }
  205. }
  206. public function getTmpDir($date = true)
  207. {
  208. $suffix = '';
  209. $tmp = sys_get_temp_dir();
  210. if ($date) {
  211. $suffix = date('mdyHis');
  212. }
  213. if (is_writeable($tmp)) {
  214. return $tmp . DIRECTORY_SEPARATOR . 'zend_cache_tmp_dir_' . $suffix;
  215. } else {
  216. throw new Exception("no writable tmpdir found");
  217. }
  218. }
  219. }