ApcEngineTest.php 7.0 KB

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