2
0

CacheTest.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. /**
  134. * @group ZF-11885
  135. * @dataProvider dataprovider_testEncodedCacheIdsAreUsedConsistently
  136. */
  137. public function testEncodedCacheIdsAreUsedConsistently($recursive)
  138. {
  139. $helper = new Zend_Controller_Action_Helper_Cache();
  140. $cache = new Mock_Zend_Cache_Page_TestingEncodedCacheId();
  141. $helper->setCache('page', $cache);
  142. $helper->direct(array('baz'));
  143. $helper->preDispatch();
  144. $uriKey = bin2hex($this->request->getRequestUri());
  145. // Ensure that start method encodes key properly
  146. $this->assertTrue(isset($cache->items[$uriKey]));
  147. // Ensure that remove method encodes key properly
  148. $helper->removePage($this->request->getRequestUri(), $recursive);
  149. $this->assertFalse(isset($cache->items[$uriKey]));
  150. }
  151. /**
  152. * @group ZF-11885
  153. * @dataProvider dataprovider_testEncodedCacheIdsAreUsedConsistently
  154. */
  155. public function testRemovePageAcceptsPreEncodedCacheIds($recursive)
  156. {
  157. $helper = new Zend_Controller_Action_Helper_Cache();
  158. $cache = new Mock_Zend_Cache_Page_TestingEncodedCacheId();
  159. $helper->setCache('page', $cache);
  160. $helper->direct(array('baz'));
  161. $helper->preDispatch();
  162. $uriKey = bin2hex($this->request->getRequestUri());
  163. // Ensure that remove method will accept pre-encoded key
  164. $helper->removePage($uriKey, $recursive);
  165. $this->assertFalse(isset($cache->items[$uriKey]));
  166. }
  167. /**
  168. * Data provider for testEncodedCacheIdsAreUsedConsistently
  169. * @see ZF-11885
  170. */
  171. public function dataprovider_testEncodedCacheIdsAreUsedConsistently()
  172. {
  173. return array(array(true),array(false));
  174. }
  175. /**public function testPostDispatchEndsOutputBufferPageCaching()
  176. {
  177. $helper = new Zend_Controller_Action_Helper_Cache;
  178. $cache = new Mock_Zend_Cache_Page_5;
  179. $helper->setCache('page', $cache);
  180. $helper->direct(array('baz'));
  181. $helper->preDispatch();
  182. $helper->postDispatch();
  183. $this->assertEquals('verified', $cache->res);
  184. }
  185. public function testPostDispatchNotEndsOutputBufferPageCachingWithBadAction()
  186. {
  187. $helper = new Zend_Controller_Action_Helper_Cache;
  188. $cache = new Mock_Zend_Cache_Page_5;
  189. $helper->setCache('page', $cache);
  190. $helper->direct(array('action1'));
  191. $helper->preDispatch();
  192. $helper->postDispatch();
  193. $this->assertNotEquals('verified', $cache->res);
  194. }**/
  195. }
  196. class Mock_Zend_Cache_Page_1 extends Zend_Cache_Core
  197. {
  198. public function remove($id)
  199. {
  200. if ($id == bin2hex('/foo')) {return 'verified';}
  201. }
  202. }
  203. class Mock_Zend_Cache_Page_2 extends Zend_Cache_Backend
  204. {
  205. public function removeRecursively($id)
  206. {
  207. if ($id == bin2hex('/foo')) {return 'verified';}
  208. }
  209. }
  210. class Mock_Zend_Cache_Page_3 extends Zend_Cache_Core
  211. {
  212. public function clean($mode = 'all', $tags = array())
  213. {
  214. if ($mode == 'matchingAnyTag' && $tags == array('tag1'))
  215. {return 'verified';}
  216. }
  217. }
  218. class Mock_Zend_Cache_Page_4 extends Zend_Cache_Core
  219. {
  220. public $res;
  221. public $ranStart;
  222. public function start($id, array $tags = array())
  223. {
  224. $this->ranStart = 'verified';
  225. if ($id == '/foo') {
  226. $this->res = 'verified';
  227. }
  228. }
  229. }
  230. class Mock_Zend_Cache_Page_6 extends Zend_Cache_Core
  231. {
  232. public $res;
  233. public $ranStart;
  234. public function start($id, array $tags = array())
  235. {
  236. $this->ranStart = 'verified';
  237. if ($id == '/foo' && $tags == array('tag1','tag2')) {
  238. $this->res = 'verified';
  239. }
  240. }
  241. }
  242. class Mock_Zend_Cache_Page_TestingEncodedCacheId extends Zend_Cache_Core
  243. {
  244. public $items;
  245. public function start($id, array $tags = array())
  246. {
  247. $this->items[$id] = $tags;
  248. }
  249. public function remove($id)
  250. {
  251. unset($this->items[$id]);
  252. }
  253. }
  254. /**class Mock_Zend_Cache_Page_5 extends Zend_Cache_Core
  255. {
  256. public $res;
  257. public function start() {}
  258. public function end() {$this->res = 'verified';}
  259. }**/
  260. if (PHPUnit_MAIN_METHOD == "Zend_Controller_Action_Helper_CacheTest::main") {
  261. Zend_Controller_Action_Helper_CacheTest::main();
  262. }