2
0

CacheManagerTest.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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
  16. * @package Zend_Application
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. if (!defined('PHPUnit_MAIN_METHOD')) {
  23. define('PHPUnit_MAIN_METHOD', 'Zend_Application_Resource_CacheManagerTest::main');
  24. }
  25. /**
  26. * Test helper
  27. */
  28. require_once dirname(__FILE__) . '/../../../TestHelper.php';
  29. /**
  30. * Zend_Loader_Autoloader
  31. */
  32. require_once 'Zend/Loader/Autoloader.php';
  33. /**
  34. * Zend_Controller_Front
  35. */
  36. require_once 'Zend/Controller/Front.php';
  37. /**
  38. * Zend_Application_Resource_Cachemanager
  39. */
  40. require_once 'Zend/Application/Resource/Cachemanager.php';
  41. /**
  42. * Zend_Cache_Backend
  43. */
  44. require_once 'Zend/Cache/Backend.php';
  45. /**
  46. * Zend_Cache_Core
  47. */
  48. require_once 'Zend/Cache/Core.php';
  49. /**
  50. * @category Zend
  51. * @package Zend_Application
  52. * @subpackage UnitTests
  53. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  54. * @license http://framework.zend.com/license/new-bsd New BSD License
  55. * @group Zend_Application
  56. */
  57. class Zend_Application_Resource_CacheManagerTest extends PHPUnit_Framework_TestCase
  58. {
  59. public static function main()
  60. {
  61. $suite = new PHPUnit_Framework_TestSuite(__CLASS__);
  62. $result = PHPUnit_TextUI_TestRunner::run($suite);
  63. }
  64. public function setUp()
  65. {
  66. // Store original autoloaders
  67. $this->loaders = spl_autoload_functions();
  68. if (!is_array($this->loaders)) {
  69. // spl_autoload_functions does not return empty array when no
  70. // autoloaders registered...
  71. $this->loaders = array();
  72. }
  73. Zend_Loader_Autoloader::resetInstance();
  74. $this->autoloader = Zend_Loader_Autoloader::getInstance();
  75. $this->application = new Zend_Application('testing');
  76. require_once dirname(__FILE__) . '/../_files/ZfAppBootstrap.php';
  77. $this->bootstrap = new ZfAppBootstrap($this->application);
  78. }
  79. public function tearDown()
  80. {
  81. // Restore original autoloaders
  82. $loaders = spl_autoload_functions();
  83. foreach ($loaders as $loader) {
  84. spl_autoload_unregister($loader);
  85. }
  86. foreach ($this->loaders as $loader) {
  87. spl_autoload_register($loader);
  88. }
  89. Zend_Controller_Front::getInstance()->resetInstance();
  90. // Reset autoloader instance so it doesn't affect other tests
  91. Zend_Loader_Autoloader::resetInstance();
  92. }
  93. public function testInitializationCreatesCacheManagerInstance()
  94. {
  95. $resource = new Zend_Application_Resource_Cachemanager(array());
  96. $resource->init();
  97. $this->assertTrue($resource->getCachemanager() instanceof Zend_Cache_Manager);
  98. }
  99. public function testShouldReturnCacheManagerWhenComplete()
  100. {
  101. $resource = new Zend_Application_Resource_Cachemanager(array());
  102. $manager = $resource->init();
  103. $this->assertTrue($manager instanceof Zend_Cache_Manager);
  104. }
  105. public function testShouldMergeConfigsIfOptionsPassedForDefaultCacheTemplate()
  106. {
  107. $options = array(
  108. 'page' => array(
  109. 'backend' => array(
  110. 'options' => array(
  111. 'cache_dir' => '/foo'
  112. )
  113. )
  114. )
  115. );
  116. $resource = new Zend_Application_Resource_Cachemanager($options);
  117. $manager = $resource->init();
  118. $cacheTemplate = $manager->getCacheTemplate('page');
  119. $this->assertEquals('/foo', $cacheTemplate['backend']['options']['cache_dir']);
  120. }
  121. public function testShouldCreateNewCacheTemplateIfConfigNotMatchesADefaultTemplate()
  122. {
  123. $options = array(
  124. 'foo' => array(
  125. 'backend' => array(
  126. 'options' => array(
  127. 'cache_dir' => '/foo'
  128. )
  129. )
  130. )
  131. );
  132. $resource = new Zend_Application_Resource_Cachemanager($options);
  133. $manager = $resource->init();
  134. $cacheTemplate = $manager->getCacheTemplate('foo');
  135. $this->assertSame($options['foo'], $cacheTemplate);
  136. }
  137. public function testShouldNotMeddleWithFrontendOrBackendCapitalisation()
  138. {
  139. $options = array(
  140. 'foo' => array(
  141. 'backend' => array(
  142. 'name' => 'BlackHole'
  143. )
  144. )
  145. );
  146. $resource = new Zend_Application_Resource_Cachemanager($options);
  147. $manager = $resource->init();
  148. $cacheTemplate = $manager->getCacheTemplate('foo');
  149. $this->assertEquals('BlackHole', $cacheTemplate['backend']['name']);
  150. }
  151. public function testEmptyBackendOptionsShouldNotResultInError()
  152. {
  153. $options = array(
  154. 'foo' => array(
  155. 'frontend' => array(
  156. 'name' => 'Core',
  157. 'options' => array(
  158. 'lifetime' => 7200,
  159. ),
  160. ),
  161. 'backend' => array(
  162. 'name' => 'black.hole',
  163. ),
  164. ),
  165. );
  166. $resource = new Zend_Application_Resource_Cachemanager($options);
  167. $manager = $resource->init();
  168. $cache = $manager->getCache('foo');
  169. $this->assertTrue($cache instanceof Zend_Cache_Core);
  170. }
  171. /**
  172. * @group ZF-9738
  173. */
  174. public function testZendServer()
  175. {
  176. if (!function_exists('zend_disk_cache_store')) {
  177. $this->markTestSkipped('ZendServer is required for this test');
  178. }
  179. $options = array(
  180. 'foo' => array(
  181. 'frontend' => array(
  182. 'name' => 'Core',
  183. 'options' => array(
  184. 'lifetime' => 7200,
  185. ),
  186. ),
  187. 'backend' => array(
  188. 'name' => 'ZendServer_Disk',
  189. ),
  190. ),
  191. );
  192. $resource = new Zend_Application_Resource_Cachemanager($options);
  193. $manager = $resource->init();
  194. $cache = $manager->getCache('foo')->getBackend();
  195. $this->assertTrue($cache instanceof Zend_Cache_Backend_ZendServer_Disk);
  196. }
  197. /**
  198. * @group ZF-9737
  199. */
  200. public function testCustomFrontendBackendNaming()
  201. {
  202. $options = array(
  203. 'zf9737' => array(
  204. 'frontend' => array(
  205. 'name' => 'custom-naming',
  206. 'customFrontendNaming' => false),
  207. 'backend' => array('name' => 'Zend_Cache_Backend_Custom_Naming',
  208. 'customBackendNaming' => true),
  209. 'frontendBackendAutoload' => true)
  210. );
  211. $resource = new Zend_Application_Resource_Cachemanager($options);
  212. $manager = $resource->init();
  213. $cache = $manager->getCache('zf9737');
  214. $this->assertTrue($cache->getBackend() instanceof Zend_Cache_Backend_Custom_Naming);
  215. $this->assertTrue($cache instanceof Zend_Cache_Frontend_CustomNaming);
  216. }
  217. }
  218. class Zend_Cache_Backend_Custom_Naming extends Zend_Cache_Backend
  219. {
  220. }
  221. class Zend_Cache_Frontend_CustomNaming extends Zend_Cache_Core
  222. {
  223. }
  224. if (PHPUnit_MAIN_METHOD == 'Zend_Application_Resource_CacheManagerTest::main') {
  225. Zend_Application_Resource_CacheManagerTest::main();
  226. }