ApcEngineTest.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. <?php
  2. /**
  3. * ApcEngineTest file
  4. *
  5. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  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://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  13. * @since CakePHP(tm) v 1.2.0.5434
  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. * 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. Cache::enable();
  37. $this->_configCache();
  38. }
  39. /**
  40. * tearDown method
  41. *
  42. * @return void
  43. */
  44. public function tearDown() {
  45. parent::tearDown();
  46. Cache::drop('apc');
  47. Cache::drop('apc_groups');
  48. }
  49. /**
  50. * Helper method for testing.
  51. *
  52. * @return void
  53. */
  54. protected function _configCache($config = []) {
  55. $defaults = [
  56. 'className' => 'Apc',
  57. 'prefix' => 'cake_',
  58. ];
  59. Cache::drop('apc');
  60. Cache::config('apc', array_merge($defaults, $config));
  61. }
  62. /**
  63. * testReadAndWriteCache method
  64. *
  65. * @return void
  66. */
  67. public function testReadAndWriteCache() {
  68. $this->_configCache(['duration' => 1]);
  69. $result = Cache::read('test', 'apc');
  70. $expecting = '';
  71. $this->assertEquals($expecting, $result);
  72. $data = 'this is a test of the emergency broadcasting system';
  73. $result = Cache::write('test', $data, 'apc');
  74. $this->assertTrue($result);
  75. $result = Cache::read('test', 'apc');
  76. $expecting = $data;
  77. $this->assertEquals($expecting, $result);
  78. Cache::delete('test', 'apc');
  79. }
  80. /**
  81. * Writing cache entries with duration = 0 (forever) should work.
  82. *
  83. * @return void
  84. */
  85. public function testReadWriteDurationZero() {
  86. Cache::drop('apc');
  87. Cache::config('apc', ['engine' => 'Apc', 'duration' => 0, 'prefix' => 'cake_']);
  88. Cache::write('zero', 'Should save', 'apc');
  89. sleep(1);
  90. $result = Cache::read('zero', 'apc');
  91. $this->assertEquals('Should save', $result);
  92. }
  93. /**
  94. * testExpiry method
  95. *
  96. * @return void
  97. */
  98. public function testExpiry() {
  99. $this->_configCache(['duration' => 1]);
  100. $result = Cache::read('test', 'apc');
  101. $this->assertFalse($result);
  102. $data = 'this is a test of the emergency broadcasting system';
  103. $result = Cache::write('other_test', $data, 'apc');
  104. $this->assertTrue($result);
  105. sleep(2);
  106. $result = Cache::read('other_test', 'apc');
  107. $this->assertFalse($result);
  108. }
  109. /**
  110. * testDeleteCache method
  111. *
  112. * @return void
  113. */
  114. public function testDeleteCache() {
  115. $data = 'this is a test of the emergency broadcasting system';
  116. $result = Cache::write('delete_test', $data, 'apc');
  117. $this->assertTrue($result);
  118. $result = Cache::delete('delete_test', 'apc');
  119. $this->assertTrue($result);
  120. }
  121. /**
  122. * testDecrement method
  123. *
  124. * @return void
  125. */
  126. public function testDecrement() {
  127. $this->skipIf(!function_exists('apc_dec'), 'No apc_dec() function, cannot test decrement().');
  128. $result = Cache::write('test_decrement', 5, 'apc');
  129. $this->assertTrue($result);
  130. $result = Cache::decrement('test_decrement', 1, 'apc');
  131. $this->assertEquals(4, $result);
  132. $result = Cache::read('test_decrement', 'apc');
  133. $this->assertEquals(4, $result);
  134. $result = Cache::decrement('test_decrement', 2, 'apc');
  135. $this->assertEquals(2, $result);
  136. $result = Cache::read('test_decrement', 'apc');
  137. $this->assertEquals(2, $result);
  138. }
  139. /**
  140. * testIncrement method
  141. *
  142. * @return void
  143. */
  144. public function testIncrement() {
  145. $this->skipIf(!function_exists('apc_inc'), 'No apc_inc() function, cannot test increment().');
  146. $result = Cache::write('test_increment', 5, 'apc');
  147. $this->assertTrue($result);
  148. $result = Cache::increment('test_increment', 1, 'apc');
  149. $this->assertEquals(6, $result);
  150. $result = Cache::read('test_increment', 'apc');
  151. $this->assertEquals(6, $result);
  152. $result = Cache::increment('test_increment', 2, 'apc');
  153. $this->assertEquals(8, $result);
  154. $result = Cache::read('test_increment', 'apc');
  155. $this->assertEquals(8, $result);
  156. }
  157. /**
  158. * test the clearing of cache keys
  159. *
  160. * @return void
  161. */
  162. public function testClear() {
  163. apc_store('not_cake', 'survive');
  164. Cache::write('some_value', 'value', 'apc');
  165. $result = Cache::clear(false, 'apc');
  166. $this->assertTrue($result);
  167. $this->assertFalse(Cache::read('some_value', 'apc'));
  168. $this->assertEquals('survive', apc_fetch('not_cake'));
  169. apc_delete('not_cake');
  170. }
  171. /**
  172. * Tests that configuring groups for stored keys return the correct values when read/written
  173. * Shows that altering the group value is equivalent to deleting all keys under the same
  174. * group
  175. *
  176. * @return void
  177. */
  178. public function testGroupsReadWrite() {
  179. Cache::config('apc_groups', [
  180. 'engine' => 'Apc',
  181. 'duration' => 0,
  182. 'groups' => array('group_a', 'group_b'),
  183. 'prefix' => 'test_'
  184. ]);
  185. $this->assertTrue(Cache::write('test_groups', 'value', 'apc_groups'));
  186. $this->assertEquals('value', Cache::read('test_groups', 'apc_groups'));
  187. apc_inc('test_group_a');
  188. $this->assertFalse(Cache::read('test_groups', 'apc_groups'));
  189. $this->assertTrue(Cache::write('test_groups', 'value2', 'apc_groups'));
  190. $this->assertEquals('value2', Cache::read('test_groups', 'apc_groups'));
  191. apc_inc('test_group_b');
  192. $this->assertFalse(Cache::read('test_groups', 'apc_groups'));
  193. $this->assertTrue(Cache::write('test_groups', 'value3', 'apc_groups'));
  194. $this->assertEquals('value3', Cache::read('test_groups', 'apc_groups'));
  195. }
  196. /**
  197. * Tests that deleteing from a groups-enabled config is possible
  198. *
  199. * @return void
  200. */
  201. public function testGroupDelete() {
  202. Cache::config('apc_groups', array(
  203. 'engine' => 'Apc',
  204. 'duration' => 0,
  205. 'groups' => array('group_a', 'group_b'),
  206. 'prefix' => 'test_'
  207. ));
  208. $this->assertTrue(Cache::write('test_groups', 'value', 'apc_groups'));
  209. $this->assertEquals('value', Cache::read('test_groups', 'apc_groups'));
  210. $this->assertTrue(Cache::delete('test_groups', 'apc_groups'));
  211. $this->assertFalse(Cache::read('test_groups', 'apc_groups'));
  212. }
  213. /**
  214. * Test clearing a cache group
  215. *
  216. * @return void
  217. */
  218. public function testGroupClear() {
  219. Cache::config('apc_groups', array(
  220. 'engine' => 'Apc',
  221. 'duration' => 0,
  222. 'groups' => array('group_a', 'group_b'),
  223. 'prefix' => 'test_'
  224. ));
  225. $this->assertTrue(Cache::write('test_groups', 'value', 'apc_groups'));
  226. $this->assertTrue(Cache::clearGroup('group_a', 'apc_groups'));
  227. $this->assertFalse(Cache::read('test_groups', 'apc_groups'));
  228. $this->assertTrue(Cache::write('test_groups', 'value2', 'apc_groups'));
  229. $this->assertTrue(Cache::clearGroup('group_b', 'apc_groups'));
  230. $this->assertFalse(Cache::read('test_groups', 'apc_groups'));
  231. }
  232. }