CacheTest.php 7.6 KB

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