ApcEngineTest.php 8.3 KB

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