ApcEngineTest.php 7.4 KB

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