CacheTest.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <?php
  2. if (!defined("PHPUnit_MAIN_METHOD")) {
  3. define("PHPUnit_MAIN_METHOD", "Zend_Controller_Action_Helper_CacheTest::main");
  4. }
  5. require_once 'Zend/Controller/Action/Helper/Cache.php';
  6. require_once 'Zend/Controller/Action/HelperBroker.php';
  7. require_once 'Zend/Controller/Front.php';
  8. require_once 'Zend/Controller/Request/Http.php';
  9. require_once 'Zend/Controller/Response/Http.php';
  10. require_once 'Zend/Cache.php';
  11. require_once 'Zend/Cache/Core.php';
  12. require_once 'Zend/Cache/Backend.php';
  13. /**
  14. * Test class for Zend_Controller_Action_Helper_Cache
  15. */
  16. class Zend_Controller_Action_Helper_CacheTest extends PHPUnit_Framework_TestCase
  17. {
  18. protected $_requestUriOld;
  19. /**
  20. * Runs the test methods of this class.
  21. *
  22. * @return void
  23. */
  24. public static function main()
  25. {
  26. $suite = new PHPUnit_Framework_TestSuite("Zend_Controller_Action_Helper_CacheTest");
  27. $result = PHPUnit_TextUI_TestRunner::run($suite);
  28. }
  29. public function setUp()
  30. {
  31. $this->_requestUriOld =
  32. isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : null;
  33. $_SERVER['REQUEST_URI'] = '/foo';
  34. $this->front = Zend_Controller_Front::getInstance();
  35. $this->front->resetInstance();
  36. $this->request = new Zend_Controller_Request_Http();
  37. $this->request->setModuleName('foo')
  38. ->setControllerName('bar')
  39. ->setActionName('baz');
  40. $this->front->setRequest($this->request);
  41. }
  42. public function tearDown()
  43. {
  44. $_SERVER['REQUEST_URI'] = $this->_requestUriOld;
  45. }
  46. public function testGetterInstantiatesManager()
  47. {
  48. $helper = new Zend_Controller_Action_Helper_Cache;
  49. $this->assertTrue($helper->getManager() instanceof Zend_Cache_Manager);
  50. }
  51. public function testMethodsProxyToManager()
  52. {
  53. $helper = new Zend_Controller_Action_Helper_Cache;
  54. $this->assertTrue($helper->hasCache('page'));
  55. }
  56. public function testCacheableActionsStoredAtInit()
  57. {
  58. $helper = new Zend_Controller_Action_Helper_Cache;
  59. $helper->direct(array('action1'));
  60. $cacheable = $helper->getCacheableActions();
  61. $this->assertEquals('action1', $cacheable['bar'][0]);
  62. }
  63. public function testCacheableActionTagsStoredAtInit()
  64. {
  65. $helper = new Zend_Controller_Action_Helper_Cache;
  66. $helper->direct(array('action1'), array('tag1','tag2'));
  67. $cacheable = $helper->getCacheableTags();
  68. $this->assertSame(array('tag1','tag2'), $cacheable['bar']['action1']);
  69. }
  70. public function testCacheableActionsNeverDuplicated()
  71. {
  72. $helper = new Zend_Controller_Action_Helper_Cache;
  73. $helper->direct(array('action1','action1'));
  74. $cacheable = $helper->getCacheableActions();
  75. $this->assertEquals('action1', $cacheable['bar'][0]);
  76. }
  77. public function testCacheableActionTagsNeverDuplicated()
  78. {
  79. $helper = new Zend_Controller_Action_Helper_Cache;
  80. $helper->direct(array('action1'), array('tag1','tag1','tag2','tag2'));
  81. $cacheable = $helper->getCacheableTags();
  82. $this->assertSame(array('tag1','tag2'), $cacheable['bar']['action1']);
  83. }
  84. public function testRemovePageCallsPageCacheRemoveMethodCorrectly()
  85. {
  86. $helper = new Zend_Controller_Action_Helper_Cache;
  87. $cache = new Mock_Zend_Cache_Page_1;
  88. $helper->setCache('page', $cache);
  89. $this->assertEquals('verified', $helper->removePage('/foo'));
  90. }
  91. public function testRemovePageCallsPageCacheRemoveRecursiveMethodCorrectly()
  92. {
  93. $helper = new Zend_Controller_Action_Helper_Cache;
  94. $cache = new Mock_Zend_Cache_Page_1;
  95. $backend = new Mock_Zend_Cache_Page_2;
  96. $cache->setBackend($backend);
  97. $helper->setCache('page', $cache);
  98. $this->assertEquals('verified', $helper->removePage('/foo', true));
  99. }
  100. public function testRemovePagesTaggedCallsPageCacheCleanMethodCorrectly()
  101. {
  102. $helper = new Zend_Controller_Action_Helper_Cache;
  103. $cache = new Mock_Zend_Cache_Page_3;
  104. $helper->setCache('page', $cache);
  105. $this->assertEquals('verified', $helper->removePagesTagged(array('tag1')));
  106. }
  107. public function testPreDispatchCallsCachesStartMethod()
  108. {
  109. $helper = new Zend_Controller_Action_Helper_Cache;
  110. $cache = new Mock_Zend_Cache_Page_4;
  111. $helper->setCache('page', $cache);
  112. $helper->direct(array('baz'));
  113. $helper->preDispatch();
  114. $this->assertEquals('verified', $cache->ranStart);
  115. }
  116. public function testPreDispatchCallsCachesStartMethodWithTags()
  117. {
  118. $helper = new Zend_Controller_Action_Helper_Cache;
  119. $cache = new Mock_Zend_Cache_Page_6;
  120. $helper->setCache('page', $cache);
  121. $helper->direct(array('baz'), array('tag1','tag2'));
  122. $helper->preDispatch();
  123. $this->assertEquals('verified', $cache->ranStart);
  124. }
  125. public function testPreDispatchDoesNotCallCachesStartMethodWithBadAction()
  126. {
  127. $helper = new Zend_Controller_Action_Helper_Cache;
  128. $cache = new Mock_Zend_Cache_Page_4;
  129. $helper->setCache('page', $cache);
  130. $helper->preDispatch();
  131. $this->assertNotEquals('verified', $cache->res);
  132. }
  133. /**public function testPostDispatchEndsOutputBufferPageCaching()
  134. {
  135. $helper = new Zend_Controller_Action_Helper_Cache;
  136. $cache = new Mock_Zend_Cache_Page_5;
  137. $helper->setCache('page', $cache);
  138. $helper->direct(array('baz'));
  139. $helper->preDispatch();
  140. $helper->postDispatch();
  141. $this->assertEquals('verified', $cache->res);
  142. }
  143. public function testPostDispatchNotEndsOutputBufferPageCachingWithBadAction()
  144. {
  145. $helper = new Zend_Controller_Action_Helper_Cache;
  146. $cache = new Mock_Zend_Cache_Page_5;
  147. $helper->setCache('page', $cache);
  148. $helper->direct(array('action1'));
  149. $helper->preDispatch();
  150. $helper->postDispatch();
  151. $this->assertNotEquals('verified', $cache->res);
  152. }**/
  153. }
  154. class Mock_Zend_Cache_Page_1 extends Zend_Cache_Core
  155. {
  156. public function remove($id)
  157. {
  158. if ($id == '/foo') {return 'verified';}
  159. }
  160. }
  161. class Mock_Zend_Cache_Page_2 extends Zend_Cache_Backend
  162. {
  163. public function removeRecursively($id)
  164. {
  165. if ($id == '/foo') {return 'verified';}
  166. }
  167. }
  168. class Mock_Zend_Cache_Page_3 extends Zend_Cache_Core
  169. {
  170. public function clean($mode = 'all', $tags = array())
  171. {
  172. if ($mode == 'matchingAnyTag' && $tags == array('tag1'))
  173. {return 'verified';}
  174. }
  175. }
  176. class Mock_Zend_Cache_Page_4 extends Zend_Cache_Core
  177. {
  178. public $res;
  179. public $ranStart;
  180. public function start($id, array $tags = array())
  181. {
  182. $this->ranStart = 'verified';
  183. if ($id == '/foo') {
  184. $this->res = 'verified';
  185. }
  186. }
  187. }
  188. class Mock_Zend_Cache_Page_6 extends Zend_Cache_Core
  189. {
  190. public $res;
  191. public $ranStart;
  192. public function start($id, array $tags = array())
  193. {
  194. $this->ranStart = 'verified';
  195. if ($id == '/foo' && $tags == array('tag1','tag2')) {
  196. $this->res = 'verified';
  197. }
  198. }
  199. }
  200. /**class Mock_Zend_Cache_Page_5 extends Zend_Cache_Core
  201. {
  202. public $res;
  203. public function start() {}
  204. public function end() {$this->res = 'verified';}
  205. }**/
  206. if (PHPUnit_MAIN_METHOD == "Zend_Controller_Action_Helper_CacheTest::main") {
  207. Zend_Controller_Action_Helper_CacheTest::main();
  208. }