ApcEngineTest.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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('apc_store'), 'Apc 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. $this->skipIf(!function_exists('apc_dec'), 'No apc_dec() function, cannot test decrement().');
  138. $result = Cache::write('test_decrement', 5, 'apc');
  139. $this->assertTrue($result);
  140. $result = Cache::decrement('test_decrement', 1, 'apc');
  141. $this->assertEquals(4, $result);
  142. $result = Cache::read('test_decrement', 'apc');
  143. $this->assertEquals(4, $result);
  144. $result = Cache::decrement('test_decrement', 2, 'apc');
  145. $this->assertEquals(2, $result);
  146. $result = Cache::read('test_decrement', 'apc');
  147. $this->assertEquals(2, $result);
  148. }
  149. /**
  150. * testIncrement method
  151. *
  152. * @return void
  153. */
  154. public function testIncrement()
  155. {
  156. $this->skipIf(!function_exists('apc_inc'), 'No apc_inc() function, cannot test increment().');
  157. $result = Cache::write('test_increment', 5, 'apc');
  158. $this->assertTrue($result);
  159. $result = Cache::increment('test_increment', 1, 'apc');
  160. $this->assertEquals(6, $result);
  161. $result = Cache::read('test_increment', 'apc');
  162. $this->assertEquals(6, $result);
  163. $result = Cache::increment('test_increment', 2, 'apc');
  164. $this->assertEquals(8, $result);
  165. $result = Cache::read('test_increment', 'apc');
  166. $this->assertEquals(8, $result);
  167. }
  168. /**
  169. * test the clearing of cache keys
  170. *
  171. * @return void
  172. */
  173. public function testClear()
  174. {
  175. apc_store('not_cake', 'survive');
  176. Cache::write('some_value', 'value', 'apc');
  177. $result = Cache::clear(false, 'apc');
  178. $this->assertTrue($result);
  179. $this->assertFalse(Cache::read('some_value', 'apc'));
  180. $this->assertEquals('survive', apc_fetch('not_cake'));
  181. apc_delete('not_cake');
  182. }
  183. /**
  184. * Tests that configuring groups for stored keys return the correct values when read/written
  185. * Shows that altering the group value is equivalent to deleting all keys under the same
  186. * group
  187. *
  188. * @return void
  189. */
  190. public function testGroupsReadWrite()
  191. {
  192. Cache::config('apc_groups', [
  193. 'engine' => 'Apc',
  194. 'duration' => 0,
  195. 'groups' => ['group_a', 'group_b'],
  196. 'prefix' => 'test_'
  197. ]);
  198. $this->assertTrue(Cache::write('test_groups', 'value', 'apc_groups'));
  199. $this->assertEquals('value', Cache::read('test_groups', 'apc_groups'));
  200. apc_inc('test_group_a');
  201. $this->assertFalse(Cache::read('test_groups', 'apc_groups'));
  202. $this->assertTrue(Cache::write('test_groups', 'value2', 'apc_groups'));
  203. $this->assertEquals('value2', Cache::read('test_groups', 'apc_groups'));
  204. apc_inc('test_group_b');
  205. $this->assertFalse(Cache::read('test_groups', 'apc_groups'));
  206. $this->assertTrue(Cache::write('test_groups', 'value3', 'apc_groups'));
  207. $this->assertEquals('value3', Cache::read('test_groups', 'apc_groups'));
  208. }
  209. /**
  210. * Tests that deleteing from a groups-enabled config is possible
  211. *
  212. * @return void
  213. */
  214. public function testGroupDelete()
  215. {
  216. Cache::config('apc_groups', [
  217. 'engine' => 'Apc',
  218. 'duration' => 0,
  219. 'groups' => ['group_a', 'group_b'],
  220. 'prefix' => 'test_'
  221. ]);
  222. $this->assertTrue(Cache::write('test_groups', 'value', 'apc_groups'));
  223. $this->assertEquals('value', Cache::read('test_groups', 'apc_groups'));
  224. $this->assertTrue(Cache::delete('test_groups', 'apc_groups'));
  225. $this->assertFalse(Cache::read('test_groups', 'apc_groups'));
  226. }
  227. /**
  228. * Test clearing a cache group
  229. *
  230. * @return void
  231. */
  232. public function testGroupClear()
  233. {
  234. Cache::config('apc_groups', [
  235. 'engine' => 'Apc',
  236. 'duration' => 0,
  237. 'groups' => ['group_a', 'group_b'],
  238. 'prefix' => 'test_'
  239. ]);
  240. $this->assertTrue(Cache::write('test_groups', 'value', 'apc_groups'));
  241. $this->assertTrue(Cache::clearGroup('group_a', 'apc_groups'));
  242. $this->assertFalse(Cache::read('test_groups', 'apc_groups'));
  243. $this->assertTrue(Cache::write('test_groups', 'value2', 'apc_groups'));
  244. $this->assertTrue(Cache::clearGroup('group_b', 'apc_groups'));
  245. $this->assertFalse(Cache::read('test_groups', 'apc_groups'));
  246. }
  247. /**
  248. * Test add
  249. *
  250. * @return void
  251. */
  252. public function testAdd()
  253. {
  254. Cache::delete('test_add_key', 'apc');
  255. $result = Cache::add('test_add_key', 'test data', 'apc');
  256. $this->assertTrue($result);
  257. $expected = 'test data';
  258. $result = Cache::read('test_add_key', 'apc');
  259. $this->assertEquals($expected, $result);
  260. $result = Cache::add('test_add_key', 'test data 2', 'apc');
  261. $this->assertFalse($result);
  262. }
  263. }