CacheManagerTest.php 8.7 KB

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