XcacheEngineTest.php 7.0 KB

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