XcacheEngineTest.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. <?php
  2. /**
  3. * XcacheEngineTest file
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  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/2.0/en/development/testing.html 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. parent::setUp();
  33. if (!function_exists('xcache_set')) {
  34. $this->markTestSkipped('Xcache is not installed or configured properly');
  35. }
  36. $this->_cacheDisable = Configure::read('Cache.disable');
  37. Configure::write('Cache.disable', false);
  38. Cache::config('xcache', array('engine' => 'Xcache', 'prefix' => 'cake_'));
  39. }
  40. /**
  41. * tearDown method
  42. *
  43. * @return void
  44. */
  45. public function tearDown() {
  46. parent::tearDown();
  47. Configure::write('Cache.disable', $this->_cacheDisable);
  48. Cache::drop('xcache');
  49. Cache::drop('xcache_groups');
  50. Cache::config('default');
  51. }
  52. /**
  53. * testSettings method
  54. *
  55. * @return void
  56. */
  57. public function testSettings() {
  58. $settings = Cache::settings();
  59. $expecting = array(
  60. 'prefix' => 'cake_',
  61. 'duration' => 3600,
  62. 'probability' => 100,
  63. 'engine' => 'Xcache',
  64. );
  65. $this->assertTrue(isset($settings['PHP_AUTH_USER']));
  66. $this->assertTrue(isset($settings['PHP_AUTH_PW']));
  67. unset($settings['PHP_AUTH_USER'], $settings['PHP_AUTH_PW']);
  68. $this->assertEquals($settings, $expecting);
  69. }
  70. /**
  71. * testReadAndWriteCache method
  72. *
  73. * @return void
  74. */
  75. public function testReadAndWriteCache() {
  76. Cache::set(array('duration' => 1));
  77. $result = Cache::read('test');
  78. $expecting = '';
  79. $this->assertEquals($expecting, $result);
  80. $data = 'this is a test of the emergency broadcasting system';
  81. $result = Cache::write('test', $data);
  82. $this->assertTrue($result);
  83. $result = Cache::read('test');
  84. $expecting = $data;
  85. $this->assertEquals($expecting, $result);
  86. Cache::delete('test');
  87. }
  88. /**
  89. * testExpiry method
  90. *
  91. * @return void
  92. */
  93. public function testExpiry() {
  94. Cache::set(array('duration' => 1));
  95. $result = Cache::read('test');
  96. $this->assertFalse($result);
  97. $data = 'this is a test of the emergency broadcasting system';
  98. $result = Cache::write('other_test', $data);
  99. $this->assertTrue($result);
  100. sleep(2);
  101. $result = Cache::read('other_test');
  102. $this->assertFalse($result);
  103. Cache::set(array('duration' => "+1 second"));
  104. $data = 'this is a test of the emergency broadcasting system';
  105. $result = Cache::write('other_test', $data);
  106. $this->assertTrue($result);
  107. sleep(2);
  108. $result = Cache::read('other_test');
  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);
  119. $this->assertTrue($result);
  120. $result = Cache::delete('delete_test');
  121. $this->assertTrue($result);
  122. }
  123. /**
  124. * testClearCache method
  125. *
  126. * @return void
  127. */
  128. public function testClearCache() {
  129. $data = 'this is a test of the emergency broadcasting system';
  130. $result = Cache::write('clear_test_1', $data);
  131. $this->assertTrue($result);
  132. $result = Cache::write('clear_test_2', $data);
  133. $this->assertTrue($result);
  134. $result = Cache::clear();
  135. $this->assertTrue($result);
  136. }
  137. /**
  138. * testDecrement method
  139. *
  140. * @return void
  141. */
  142. public function testDecrement() {
  143. $result = Cache::write('test_decrement', 5);
  144. $this->assertTrue($result);
  145. $result = Cache::decrement('test_decrement');
  146. $this->assertEquals(4, $result);
  147. $result = Cache::read('test_decrement');
  148. $this->assertEquals(4, $result);
  149. $result = Cache::decrement('test_decrement', 2);
  150. $this->assertEquals(2, $result);
  151. $result = Cache::read('test_decrement');
  152. $this->assertEquals(2, $result);
  153. }
  154. /**
  155. * testIncrement method
  156. *
  157. * @return void
  158. */
  159. public function testIncrement() {
  160. $result = Cache::write('test_increment', 5);
  161. $this->assertTrue($result);
  162. $result = Cache::increment('test_increment');
  163. $this->assertEquals(6, $result);
  164. $result = Cache::read('test_increment');
  165. $this->assertEquals(6, $result);
  166. $result = Cache::increment('test_increment', 2);
  167. $this->assertEquals(8, $result);
  168. $result = Cache::read('test_increment');
  169. $this->assertEquals(8, $result);
  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('xcache_groups', array(
  180. 'engine' => 'Xcache',
  181. 'duration' => 0,
  182. 'groups' => array('group_a', 'group_b'),
  183. 'prefix' => 'test_'
  184. ));
  185. $this->assertTrue(Cache::write('test_groups', 'value', 'xcache_groups'));
  186. $this->assertEquals('value', Cache::read('test_groups', 'xcache_groups'));
  187. xcache_inc('test_group_a', 1);
  188. $this->assertFalse(Cache::read('test_groups', 'xcache_groups'));
  189. $this->assertTrue(Cache::write('test_groups', 'value2', 'xcache_groups'));
  190. $this->assertEquals('value2', Cache::read('test_groups', 'xcache_groups'));
  191. xcache_inc('test_group_b', 1);
  192. $this->assertFalse(Cache::read('test_groups', 'xcache_groups'));
  193. $this->assertTrue(Cache::write('test_groups', 'value3', 'xcache_groups'));
  194. $this->assertEquals('value3', Cache::read('test_groups', 'xcache_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('xcache_groups', array(
  203. 'engine' => 'Xcache',
  204. 'duration' => 0,
  205. 'groups' => array('group_a', 'group_b'),
  206. 'prefix' => 'test_'
  207. ));
  208. $this->assertTrue(Cache::write('test_groups', 'value', 'xcache_groups'));
  209. $this->assertEquals('value', Cache::read('test_groups', 'xcache_groups'));
  210. $this->assertTrue(Cache::delete('test_groups', 'xcache_groups'));
  211. $this->assertFalse(Cache::read('test_groups', 'xcache_groups'));
  212. }
  213. /**
  214. * Test clearing a cache group
  215. *
  216. * @return void
  217. */
  218. public function testGroupClear() {
  219. Cache::config('xcache_groups', array(
  220. 'engine' => 'Xcache',
  221. 'duration' => 0,
  222. 'groups' => array('group_a', 'group_b'),
  223. 'prefix' => 'test_'
  224. ));
  225. $this->assertTrue(Cache::write('test_groups', 'value', 'xcache_groups'));
  226. $this->assertTrue(Cache::clearGroup('group_a', 'xcache_groups'));
  227. $this->assertFalse(Cache::read('test_groups', 'xcache_groups'));
  228. $this->assertTrue(Cache::write('test_groups', 'value2', 'xcache_groups'));
  229. $this->assertTrue(Cache::clearGroup('group_b', 'xcache_groups'));
  230. $this->assertFalse(Cache::read('test_groups', 'xcache_groups'));
  231. }
  232. }