ManagerTest.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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-2014 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-2014 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. /**
  53. * @group ZF-10220
  54. */
  55. public function testGetAvialableCaches()
  56. {
  57. $manager = new Zend_Cache_Manager();
  58. $caches = $manager->getCaches();
  59. $this->assertTrue(is_array($caches));
  60. $this->assertArrayHasKey('default', $caches);
  61. }
  62. public function testLazyLoadsDefaultPageCache()
  63. {
  64. $manager = new Zend_Cache_Manager;
  65. $manager->setTemplateOptions('pagetag',array(
  66. 'backend' => array(
  67. 'options' => array(
  68. 'cache_dir' => $this->_cache_dir
  69. )
  70. )
  71. ));
  72. $this->assertTrue($manager->getCache('page') instanceof Zend_Cache_Frontend_Capture);
  73. }
  74. public function testCanOverrideCacheFrontendNameConfiguration()
  75. {
  76. $manager = new Zend_Cache_Manager;
  77. $manager->setTemplateOptions('pagetag',array(
  78. 'backend' => array(
  79. 'options' => array(
  80. 'cache_dir' => $this->_cache_dir
  81. )
  82. )
  83. ));
  84. $manager->setTemplateOptions('page', array(
  85. 'frontend' => array(
  86. 'name'=> 'Page'
  87. )
  88. ));
  89. $this->assertTrue($manager->getCache('page') instanceof Zend_Cache_Frontend_Page);
  90. }
  91. public function testCanMergeTemplateCacheOptionsFromZendConfig()
  92. {
  93. $manager = new Zend_Cache_Manager;
  94. $config = new Zend_Config(array(
  95. 'backend' => array(
  96. 'options' => array(
  97. 'cache_dir' => $this->_cache_dir
  98. )
  99. )
  100. ));
  101. $manager->setTemplateOptions('pagetag', $config);
  102. $options = $manager->getCacheTemplate('pagetag');
  103. $this->assertEquals($this->_cache_dir, $options['backend']['options']['cache_dir']);
  104. }
  105. public function testCanOverrideCacheBackendendNameConfiguration()
  106. {
  107. $manager = new Zend_Cache_Manager;
  108. $manager->setTemplateOptions('pagetag',array(
  109. 'backend' => array(
  110. 'options' => array(
  111. 'cache_dir' => $this->_cache_dir
  112. )
  113. )
  114. ));
  115. $manager->setTemplateOptions('page', array(
  116. 'backend' => array(
  117. 'name'=> 'File'
  118. )
  119. ));
  120. $this->assertTrue($manager->getCache('page')->getBackend() instanceof Zend_Cache_Backend_File);
  121. }
  122. public function testCanOverrideCacheFrontendOptionsConfiguration()
  123. {
  124. $manager = new Zend_Cache_Manager;
  125. $manager->setTemplateOptions('page', array(
  126. 'frontend' => array(
  127. 'options'=> array(
  128. 'lifetime' => 9999
  129. )
  130. )
  131. ));
  132. $config = $manager->getCacheTemplate('page');
  133. $this->assertEquals(9999, $config['frontend']['options']['lifetime']);
  134. }
  135. public function testCanOverrideCacheBackendOptionsConfiguration()
  136. {
  137. $manager = new Zend_Cache_Manager;
  138. $manager->setTemplateOptions('page', array(
  139. 'backend' => array(
  140. 'options'=> array(
  141. 'public_dir' => './cacheDir'
  142. )
  143. )
  144. ));
  145. $config = $manager->getCacheTemplate('page');
  146. $this->assertEquals('./cacheDir', $config['backend']['options']['public_dir']);
  147. }
  148. public function testSetsConfigTemplate()
  149. {
  150. $manager = new Zend_Cache_Manager;
  151. $config = array(
  152. 'frontend' => array(
  153. 'name' => 'Core',
  154. 'options' => array(
  155. 'automatic_serialization' => true
  156. )
  157. ),
  158. 'backend' => array(
  159. 'name' => 'File',
  160. 'options' => array(
  161. 'cache_dir' => '../cache',
  162. )
  163. )
  164. );
  165. $manager->setCacheTemplate('myCache', $config);
  166. $this->assertSame($config, $manager->getCacheTemplate('myCache'));
  167. }
  168. public function testSetsConfigTemplateWithoutMultipartNameNormalisation()
  169. {
  170. $manager = new Zend_Cache_Manager;
  171. $config = array(
  172. 'frontend' => array(
  173. 'name' => 'Core',
  174. 'options' => array(
  175. 'automatic_serialization' => true
  176. )
  177. ),
  178. 'backend' => array(
  179. 'name' => 'BlackHole'
  180. )
  181. );
  182. $manager->setCacheTemplate('myCache', $config);
  183. $this->assertSame($config, $manager->getCacheTemplate('myCache'));
  184. }
  185. public function testSetsOptionsTemplateUsingZendConfig()
  186. {
  187. $manager = new Zend_Cache_Manager;
  188. $config = array(
  189. 'frontend' => array(
  190. 'name' => 'Core',
  191. 'options' => array(
  192. 'automatic_serialization' => true
  193. )
  194. ),
  195. 'backend' => array(
  196. 'name' => 'File',
  197. 'options' => array(
  198. 'cache_dir' => '../cache',
  199. )
  200. )
  201. );
  202. $manager->setCacheTemplate('myCache', new Zend_Config($config));
  203. $this->assertSame($config, $manager->getCacheTemplate('myCache'));
  204. }
  205. public function testConfigTemplatesDetectedAsAvailableCaches()
  206. {
  207. $manager = new Zend_Cache_Manager;
  208. $this->assertTrue($manager->hasCache('page'));
  209. }
  210. public function testGettingPageCacheAlsoCreatesTagCache()
  211. {
  212. $manager = new Zend_Cache_Manager;
  213. $tagCacheConfig = $manager->getCacheTemplate('tagCache');
  214. $tagCacheConfig['backend']['options']['cache_dir'] = $this->getTmpDir();
  215. $manager->setTemplateOptions('pagetag', $tagCacheConfig);
  216. $tagCache = $manager->getCache('page')->getBackend()->getOption('tag_cache');
  217. $this->assertTrue($tagCache instanceof Zend_Cache_Core);
  218. }
  219. /**
  220. * @group GH-189
  221. */
  222. public function testSetsOptionsWithCustomFrontendAndBackendNamingAndAutoload()
  223. {
  224. $manager = new Zend_Cache_Manager;
  225. $manager->setTemplateOptions(
  226. 'page',
  227. array(
  228. 'frontend' => array(
  229. 'customFrontendNaming' => true,
  230. ),
  231. 'backend' => array(
  232. 'customBackendNaming' => true,
  233. ),
  234. 'frontendBackendAutoload' => true,
  235. )
  236. );
  237. $config = $manager->getCacheTemplate('page');
  238. $this->assertTrue($config['frontend']['customFrontendNaming']);
  239. $this->assertTrue($config['backend']['customBackendNaming']);
  240. $this->assertTrue($config['frontendBackendAutoload']);
  241. }
  242. // Helper Methods
  243. public function mkdir()
  244. {
  245. $tmp = $this->getTmpDir();
  246. @mkdir($tmp);
  247. return $tmp;
  248. }
  249. public function rmdir()
  250. {
  251. $tmpDir = $this->getTmpDir(false);
  252. foreach (glob("$tmpDir*") as $dirname) {
  253. @rmdir($dirname);
  254. }
  255. }
  256. public function getTmpDir($date = true)
  257. {
  258. $suffix = '';
  259. $tmp = sys_get_temp_dir();
  260. if ($date) {
  261. $suffix = date('mdyHis');
  262. }
  263. if (is_writeable($tmp)) {
  264. return $tmp . DIRECTORY_SEPARATOR . 'zend_cache_tmp_dir_' . $suffix;
  265. } else {
  266. throw new Exception("no writable tmpdir found");
  267. }
  268. }
  269. }