ApcEngineTest.php 8.3 KB

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