ApcEngineTest.php 7.0 KB

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