/lib/Cake/Test/Case/Cache/Engine/ApcEngineTest.php

https://bitbucket.org/LeThanhDat/firstdummyapp · PHP · 274 lines · 145 code · 44 blank · 85 comment · 0 complexity · 05f299238f451e91a55c7683ae93784b MD5 · raw file

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