ApcEngineTest.php 8.3 KB

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