XcacheEngineTest.php 7.1 KB

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