CacheTest.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. /**
  16. * Test class for Zend_Controller_Action_Helper_Cache
  17. */
  18. class Zend_Controller_Action_Helper_CacheTest extends PHPUnit_Framework_TestCase
  19. {
  20. protected $_requestUriOld;
  21. /**
  22. * Runs the test methods of this class.
  23. *
  24. * @return void
  25. */
  26. public static function main()
  27. {
  28. require_once "PHPUnit/TextUI/TestRunner.php";
  29. $suite = new PHPUnit_Framework_TestSuite("Zend_Controller_Action_Helper_CacheTest");
  30. $result = PHPUnit_TextUI_TestRunner::run($suite);
  31. }
  32. public function setUp()
  33. {
  34. $this->_requestUriOld =
  35. isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : null;
  36. $_SERVER['REQUEST_URI'] = '/foo';
  37. $this->front = Zend_Controller_Front::getInstance();
  38. $this->front->resetInstance();
  39. $this->request = new Zend_Controller_Request_Http();
  40. $this->request->setModuleName('foo')
  41. ->setControllerName('bar')
  42. ->setActionName('baz');
  43. $this->front->setRequest($this->request);
  44. }
  45. public function tearDown()
  46. {
  47. $_SERVER['REQUEST_URI'] = $this->_requestUriOld;
  48. }
  49. public function testGetterInstantiatesManager()
  50. {
  51. $helper = new Zend_Controller_Action_Helper_Cache;
  52. $this->assertTrue($helper->getManager() instanceof Zend_Cache_Manager);
  53. }
  54. public function testMethodsProxyToManager()
  55. {
  56. $helper = new Zend_Controller_Action_Helper_Cache;
  57. $this->assertTrue($helper->hasCache('page'));
  58. }
  59. public function testCacheableActionsStoredAtInit()
  60. {
  61. $helper = new Zend_Controller_Action_Helper_Cache;
  62. $helper->direct(array('action1'));
  63. $cacheable = $helper->getCacheableActions();
  64. $this->assertEquals('action1', $cacheable['bar'][0]);
  65. }
  66. public function testCacheableActionTagsStoredAtInit()
  67. {
  68. $helper = new Zend_Controller_Action_Helper_Cache;
  69. $helper->direct(array('action1'), array('tag1','tag2'));
  70. $cacheable = $helper->getCacheableTags();
  71. $this->assertSame(array('tag1','tag2'), $cacheable['bar']['action1']);
  72. }
  73. public function testCacheableActionsNeverDuplicated()
  74. {
  75. $helper = new Zend_Controller_Action_Helper_Cache;
  76. $helper->direct(array('action1','action1'));
  77. $cacheable = $helper->getCacheableActions();
  78. $this->assertEquals('action1', $cacheable['bar'][0]);
  79. }
  80. public function testCacheableActionTagsNeverDuplicated()
  81. {
  82. $helper = new Zend_Controller_Action_Helper_Cache;
  83. $helper->direct(array('action1'), array('tag1','tag1','tag2','tag2'));
  84. $cacheable = $helper->getCacheableTags();
  85. $this->assertSame(array('tag1','tag2'), $cacheable['bar']['action1']);
  86. }
  87. public function testRemovePageCallsPageCacheRemoveMethodCorrectly()
  88. {
  89. $helper = new Zend_Controller_Action_Helper_Cache;
  90. $cache = new Mock_Zend_Cache_Page_1;
  91. $helper->setCache('page', $cache);
  92. $this->assertEquals('verified', $helper->removePage('/foo'));
  93. }
  94. public function testRemovePageCallsPageCacheRemoveRecursiveMethodCorrectly()
  95. {
  96. $helper = new Zend_Controller_Action_Helper_Cache;
  97. $cache = new Mock_Zend_Cache_Page_2;
  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->res);
  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->res);
  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_Core
  163. {
  164. public function removeRecursive($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 function start($id, array $tags = array()) {if ($id == '/foo') {
  181. $this->res = 'verified';
  182. }}
  183. }
  184. class Mock_Zend_Cache_Page_6 extends Zend_Cache_Core
  185. {
  186. public $res;
  187. public function start($id, array $tags = array()) {if ($id == '/foo' && $tags == array('tag1','tag2')) {
  188. $this->res = 'verified';
  189. }}
  190. }
  191. /**class Mock_Zend_Cache_Page_5 extends Zend_Cache_Core
  192. {
  193. public $res;
  194. public function start() {}
  195. public function end() {$this->res = 'verified';}
  196. }**/
  197. if (PHPUnit_MAIN_METHOD == "Zend_Controller_Action_Helper_CacheTest::main") {
  198. Zend_Controller_Action_Helper_CacheTest::main();
  199. }