CacheTest.php 7.4 KB

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