XcacheEngineTest.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. <?php
  2. /**
  3. * XcacheEngineTest file
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
  8. * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * Redistributions of files must retain the above copyright notice
  12. *
  13. * @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
  15. * @package Cake.Test.Case.Cache.Engine
  16. * @since CakePHP(tm) v 1.2.0.5434
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. App::uses('Cache', 'Cache');
  20. /**
  21. * XcacheEngineTest class
  22. *
  23. * @package Cake.Test.Case.Cache.Engine
  24. */
  25. class XcacheEngineTest extends CakeTestCase {
  26. /**
  27. * setUp method
  28. *
  29. * @return void
  30. */
  31. public function setUp() {
  32. if (!function_exists('xcache_set')) {
  33. $this->markTestSkipped('Xcache is not installed or configured properly');
  34. }
  35. $this->_cacheDisable = Configure::read('Cache.disable');
  36. Configure::write('Cache.disable', false);
  37. Cache::config('xcache', array('engine' => 'Xcache', 'prefix' => 'cake_'));
  38. }
  39. /**
  40. * tearDown method
  41. *
  42. * @return void
  43. */
  44. public function tearDown() {
  45. Configure::write('Cache.disable', $this->_cacheDisable);
  46. Cache::drop('xcache');
  47. Cache::drop('xcache_groups');
  48. Cache::config('default');
  49. }
  50. /**
  51. * testSettings method
  52. *
  53. * @return void
  54. */
  55. public function testSettings() {
  56. $settings = Cache::settings();
  57. $expecting = array(
  58. 'prefix' => 'cake_',
  59. 'duration' => 3600,
  60. 'probability' => 100,
  61. 'engine' => 'Xcache',
  62. );
  63. $this->assertTrue(isset($settings['PHP_AUTH_USER']));
  64. $this->assertTrue(isset($settings['PHP_AUTH_PW']));
  65. unset($settings['PHP_AUTH_USER'], $settings['PHP_AUTH_PW']);
  66. $this->assertEquals($settings, $expecting);
  67. }
  68. /**
  69. * testReadAndWriteCache method
  70. *
  71. * @return void
  72. */
  73. public function testReadAndWriteCache() {
  74. Cache::set(array('duration' => 1));
  75. $result = Cache::read('test');
  76. $expecting = '';
  77. $this->assertEquals($expecting, $result);
  78. $data = 'this is a test of the emergency broadcasting system';
  79. $result = Cache::write('test', $data);
  80. $this->assertTrue($result);
  81. $result = Cache::read('test');
  82. $expecting = $data;
  83. $this->assertEquals($expecting, $result);
  84. Cache::delete('test');
  85. }
  86. /**
  87. * testExpiry method
  88. *
  89. * @return void
  90. */
  91. public function testExpiry() {
  92. Cache::set(array('duration' => 1));
  93. $result = Cache::read('test');
  94. $this->assertFalse($result);
  95. $data = 'this is a test of the emergency broadcasting system';
  96. $result = Cache::write('other_test', $data);
  97. $this->assertTrue($result);
  98. sleep(2);
  99. $result = Cache::read('other_test');
  100. $this->assertFalse($result);
  101. Cache::set(array('duration' => "+1 second"));
  102. $data = 'this is a test of the emergency broadcasting system';
  103. $result = Cache::write('other_test', $data);
  104. $this->assertTrue($result);
  105. sleep(2);
  106. $result = Cache::read('other_test');
  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);
  117. $this->assertTrue($result);
  118. $result = Cache::delete('delete_test');
  119. $this->assertTrue($result);
  120. }
  121. /**
  122. * testClearCache method
  123. *
  124. * @return void
  125. */
  126. public function testClearCache() {
  127. $data = 'this is a test of the emergency broadcasting system';
  128. $result = Cache::write('clear_test_1', $data);
  129. $this->assertTrue($result);
  130. $result = Cache::write('clear_test_2', $data);
  131. $this->assertTrue($result);
  132. $result = Cache::clear();
  133. $this->assertTrue($result);
  134. }
  135. /**
  136. * testDecrement method
  137. *
  138. * @return void
  139. */
  140. public function testDecrement() {
  141. $result = Cache::write('test_decrement', 5);
  142. $this->assertTrue($result);
  143. $result = Cache::decrement('test_decrement');
  144. $this->assertEquals(4, $result);
  145. $result = Cache::read('test_decrement');
  146. $this->assertEquals(4, $result);
  147. $result = Cache::decrement('test_decrement', 2);
  148. $this->assertEquals(2, $result);
  149. $result = Cache::read('test_decrement');
  150. $this->assertEquals(2, $result);
  151. }
  152. /**
  153. * testIncrement method
  154. *
  155. * @return void
  156. */
  157. public function testIncrement() {
  158. $result = Cache::write('test_increment', 5);
  159. $this->assertTrue($result);
  160. $result = Cache::increment('test_increment');
  161. $this->assertEquals(6, $result);
  162. $result = Cache::read('test_increment');
  163. $this->assertEquals(6, $result);
  164. $result = Cache::increment('test_increment', 2);
  165. $this->assertEquals(8, $result);
  166. $result = Cache::read('test_increment');
  167. $this->assertEquals(8, $result);
  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('xcache_groups', array(
  178. 'engine' => 'Xcache',
  179. 'duration' => 0,
  180. 'groups' => array('group_a', 'group_b'),
  181. 'prefix' => 'test_'
  182. ));
  183. $this->assertTrue(Cache::write('test_groups', 'value', 'xcache_groups'));
  184. $this->assertEquals('value', Cache::read('test_groups', 'xcache_groups'));
  185. xcache_inc('test_group_a', 1);
  186. $this->assertFalse(Cache::read('test_groups', 'xcache_groups'));
  187. $this->assertTrue(Cache::write('test_groups', 'value2', 'xcache_groups'));
  188. $this->assertEquals('value2', Cache::read('test_groups', 'xcache_groups'));
  189. xcache_inc('test_group_b', 1);
  190. $this->assertFalse(Cache::read('test_groups', 'xcache_groups'));
  191. $this->assertTrue(Cache::write('test_groups', 'value3', 'xcache_groups'));
  192. $this->assertEquals('value3', Cache::read('test_groups', 'xcache_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('xcache_groups', array(
  201. 'engine' => 'Xcache',
  202. 'duration' => 0,
  203. 'groups' => array('group_a', 'group_b'),
  204. 'prefix' => 'test_'
  205. ));
  206. $this->assertTrue(Cache::write('test_groups', 'value', 'xcache_groups'));
  207. $this->assertEquals('value', Cache::read('test_groups', 'xcache_groups'));
  208. $this->assertTrue(Cache::delete('test_groups', 'xcache_groups'));
  209. $this->assertFalse(Cache::read('test_groups', 'xcache_groups'));
  210. }
  211. /**
  212. * Test clearing a cache group
  213. *
  214. * @return void
  215. **/
  216. public function testGroupClear() {
  217. Cache::config('xcache_groups', array(
  218. 'engine' => 'Xcache',
  219. 'duration' => 0,
  220. 'groups' => array('group_a', 'group_b'),
  221. 'prefix' => 'test_'
  222. ));
  223. $this->assertTrue(Cache::write('test_groups', 'value', 'xcache_groups'));
  224. $this->assertTrue(Cache::clearGroup('group_a', 'xcache_groups'));
  225. $this->assertFalse(Cache::read('test_groups', 'xcache_groups'));
  226. $this->assertTrue(Cache::write('test_groups', 'value2', 'xcache_groups'));
  227. $this->assertTrue(Cache::clearGroup('group_b', 'xcache_groups'));
  228. $this->assertFalse(Cache::read('test_groups', 'xcache_groups'));
  229. }
  230. }