ApcEngineTest.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. <?php
  2. /**
  3. * ApcEngineTest file
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  8. * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * Redistributions of files must retain the above copyright notice
  12. *
  13. * @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  15. * @package Cake.Test.Case.Cache.Engine
  16. * @since CakePHP(tm) v 1.2.0.5434
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. App::uses('Cache', 'Cache');
  20. /**
  21. * ApcEngineTest class
  22. *
  23. * @package Cake.Test.Case.Cache.Engine
  24. */
  25. class ApcEngineTest extends CakeTestCase {
  26. /**
  27. * setUp method
  28. *
  29. * @return void
  30. */
  31. public function setUp() {
  32. parent::setUp();
  33. $this->skipIf(!function_exists('apc_store'), 'Apc is not installed or configured properly.');
  34. $this->_cacheDisable = Configure::read('Cache.disable');
  35. Configure::write('Cache.disable', false);
  36. Cache::config('apc', array('engine' => 'Apc', 'prefix' => 'cake_'));
  37. }
  38. /**
  39. * tearDown method
  40. *
  41. * @return void
  42. */
  43. public function tearDown() {
  44. parent::tearDown();
  45. Configure::write('Cache.disable', $this->_cacheDisable);
  46. Cache::drop('apc');
  47. Cache::drop('apc_groups');
  48. Cache::config('default');
  49. }
  50. /**
  51. * testReadAndWriteCache method
  52. *
  53. * @return void
  54. */
  55. public function testReadAndWriteCache() {
  56. Cache::set(array('duration' => 1), 'apc');
  57. $result = Cache::read('test', 'apc');
  58. $expecting = '';
  59. $this->assertEquals($expecting, $result);
  60. $data = 'this is a test of the emergency broadcasting system';
  61. $result = Cache::write('test', $data, 'apc');
  62. $this->assertTrue($result);
  63. $result = Cache::read('test', 'apc');
  64. $expecting = $data;
  65. $this->assertEquals($expecting, $result);
  66. Cache::delete('test', 'apc');
  67. }
  68. /**
  69. * Writing cache entries with duration = 0 (forever) should work.
  70. *
  71. * @return void
  72. */
  73. public function testReadWriteDurationZero() {
  74. Cache::config('apc', array('engine' => 'Apc', 'duration' => 0, 'prefix' => 'cake_'));
  75. Cache::write('zero', 'Should save', 'apc');
  76. sleep(1);
  77. $result = Cache::read('zero', 'apc');
  78. $this->assertEquals('Should save', $result);
  79. }
  80. /**
  81. * testExpiry method
  82. *
  83. * @return void
  84. */
  85. public function testExpiry() {
  86. Cache::set(array('duration' => 1), 'apc');
  87. $result = Cache::read('test', 'apc');
  88. $this->assertFalse($result);
  89. $data = 'this is a test of the emergency broadcasting system';
  90. $result = Cache::write('other_test', $data, 'apc');
  91. $this->assertTrue($result);
  92. sleep(2);
  93. $result = Cache::read('other_test', 'apc');
  94. $this->assertFalse($result);
  95. Cache::set(array('duration' => 1), 'apc');
  96. $data = 'this is a test of the emergency broadcasting system';
  97. $result = Cache::write('other_test', $data, 'apc');
  98. $this->assertTrue($result);
  99. sleep(2);
  100. $result = Cache::read('other_test', 'apc');
  101. $this->assertFalse($result);
  102. sleep(2);
  103. $result = Cache::read('other_test', 'apc');
  104. $this->assertFalse($result);
  105. }
  106. /**
  107. * testDeleteCache method
  108. *
  109. * @return void
  110. */
  111. public function testDeleteCache() {
  112. $data = 'this is a test of the emergency broadcasting system';
  113. $result = Cache::write('delete_test', $data, 'apc');
  114. $this->assertTrue($result);
  115. $result = Cache::delete('delete_test', 'apc');
  116. $this->assertTrue($result);
  117. }
  118. /**
  119. * testDecrement method
  120. *
  121. * @return void
  122. */
  123. public function testDecrement() {
  124. $this->skipIf(!function_exists('apc_dec'), 'No apc_dec() function, cannot test decrement().');
  125. $result = Cache::write('test_decrement', 5, 'apc');
  126. $this->assertTrue($result);
  127. $result = Cache::decrement('test_decrement', 1, 'apc');
  128. $this->assertEquals(4, $result);
  129. $result = Cache::read('test_decrement', 'apc');
  130. $this->assertEquals(4, $result);
  131. $result = Cache::decrement('test_decrement', 2, 'apc');
  132. $this->assertEquals(2, $result);
  133. $result = Cache::read('test_decrement', 'apc');
  134. $this->assertEquals(2, $result);
  135. }
  136. /**
  137. * testIncrement method
  138. *
  139. * @return void
  140. */
  141. public function testIncrement() {
  142. $this->skipIf(!function_exists('apc_inc'), 'No apc_inc() function, cannot test increment().');
  143. $result = Cache::write('test_increment', 5, 'apc');
  144. $this->assertTrue($result);
  145. $result = Cache::increment('test_increment', 1, 'apc');
  146. $this->assertEquals(6, $result);
  147. $result = Cache::read('test_increment', 'apc');
  148. $this->assertEquals(6, $result);
  149. $result = Cache::increment('test_increment', 2, 'apc');
  150. $this->assertEquals(8, $result);
  151. $result = Cache::read('test_increment', 'apc');
  152. $this->assertEquals(8, $result);
  153. }
  154. /**
  155. * test the clearing of cache keys
  156. *
  157. * @return void
  158. */
  159. public function testClear() {
  160. apc_store('not_cake', 'survive');
  161. Cache::write('some_value', 'value', 'apc');
  162. $result = Cache::clear(false, 'apc');
  163. $this->assertTrue($result);
  164. $this->assertFalse(Cache::read('some_value', 'apc'));
  165. $this->assertEquals('survive', apc_fetch('not_cake'));
  166. apc_delete('not_cake');
  167. }
  168. /**
  169. * Tests that configuring groups for stored keys return the correct values when read/written
  170. * Shows that altering the group value is equivalent to deleting all keys under the same
  171. * group
  172. *
  173. * @return void
  174. */
  175. public function testGroupsReadWrite() {
  176. Cache::config('apc_groups', array(
  177. 'engine' => 'Apc',
  178. 'duration' => 0,
  179. 'groups' => array('group_a', 'group_b'),
  180. 'prefix' => 'test_'
  181. ));
  182. $this->assertTrue(Cache::write('test_groups', 'value', 'apc_groups'));
  183. $this->assertEquals('value', Cache::read('test_groups', 'apc_groups'));
  184. apc_inc('test_group_a');
  185. $this->assertFalse(Cache::read('test_groups', 'apc_groups'));
  186. $this->assertTrue(Cache::write('test_groups', 'value2', 'apc_groups'));
  187. $this->assertEquals('value2', Cache::read('test_groups', 'apc_groups'));
  188. apc_inc('test_group_b');
  189. $this->assertFalse(Cache::read('test_groups', 'apc_groups'));
  190. $this->assertTrue(Cache::write('test_groups', 'value3', 'apc_groups'));
  191. $this->assertEquals('value3', Cache::read('test_groups', 'apc_groups'));
  192. }
  193. /**
  194. * Tests that deleteing from a groups-enabled config is possible
  195. *
  196. * @return void
  197. */
  198. public function testGroupDelete() {
  199. Cache::config('apc_groups', array(
  200. 'engine' => 'Apc',
  201. 'duration' => 0,
  202. 'groups' => array('group_a', 'group_b'),
  203. 'prefix' => 'test_'
  204. ));
  205. $this->assertTrue(Cache::write('test_groups', 'value', 'apc_groups'));
  206. $this->assertEquals('value', Cache::read('test_groups', 'apc_groups'));
  207. $this->assertTrue(Cache::delete('test_groups', 'apc_groups'));
  208. $this->assertFalse(Cache::read('test_groups', 'apc_groups'));
  209. }
  210. /**
  211. * Test clearing a cache group
  212. *
  213. * @return void
  214. */
  215. public function testGroupClear() {
  216. Cache::config('apc_groups', array(
  217. 'engine' => 'Apc',
  218. 'duration' => 0,
  219. 'groups' => array('group_a', 'group_b'),
  220. 'prefix' => 'test_'
  221. ));
  222. $this->assertTrue(Cache::write('test_groups', 'value', 'apc_groups'));
  223. $this->assertTrue(Cache::clearGroup('group_a', 'apc_groups'));
  224. $this->assertFalse(Cache::read('test_groups', 'apc_groups'));
  225. $this->assertTrue(Cache::write('test_groups', 'value2', 'apc_groups'));
  226. $this->assertTrue(Cache::clearGroup('group_b', 'apc_groups'));
  227. $this->assertFalse(Cache::read('test_groups', 'apc_groups'));
  228. }
  229. }