XcacheEngineTest.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. <?php
  2. /**
  3. * XcacheEngineTest file
  4. *
  5. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  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://cakephp.org CakePHP(tm) Project
  14. * @since 1.2.0
  15. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  16. */
  17. namespace Cake\Test\TestCase\Cache\Engine;
  18. use Cake\Cache\Cache;
  19. use Cake\Core\Configure;
  20. use Cake\TestSuite\TestCase;
  21. /**
  22. * XcacheEngineTest class
  23. *
  24. */
  25. class XcacheEngineTest extends TestCase
  26. {
  27. /**
  28. * setUp method
  29. *
  30. * @return void
  31. */
  32. public function setUp()
  33. {
  34. parent::setUp();
  35. if (!extension_loaded('xcache')) {
  36. $this->markTestSkipped('Xcache is not installed or configured properly');
  37. }
  38. Cache::enable();
  39. Cache::config('xcache', ['engine' => 'Xcache', 'prefix' => 'cake_']);
  40. }
  41. /**
  42. * Helper method for testing.
  43. *
  44. * @param array $config
  45. * @return void
  46. */
  47. protected function _configCache($config = [])
  48. {
  49. $defaults = [
  50. 'className' => 'Xcache',
  51. 'prefix' => 'cake_',
  52. ];
  53. Cache::drop('xcache');
  54. Cache::config('xcache', array_merge($defaults, $config));
  55. }
  56. /**
  57. * tearDown method
  58. *
  59. * @return void
  60. */
  61. public function tearDown()
  62. {
  63. parent::tearDown();
  64. Cache::drop('xcache');
  65. Cache::drop('xcache_groups');
  66. }
  67. /**
  68. * testConfig method
  69. *
  70. * @return void
  71. */
  72. public function testConfig()
  73. {
  74. $config = Cache::engine('xcache')->config();
  75. $expecting = [
  76. 'prefix' => 'cake_',
  77. 'duration' => 3600,
  78. 'probability' => 100,
  79. ];
  80. $this->assertTrue(isset($config['PHP_AUTH_USER']));
  81. $this->assertTrue(isset($config['PHP_AUTH_PW']));
  82. unset($config['PHP_AUTH_USER'], $config['PHP_AUTH_PW']);
  83. $this->assertEquals($config, $expecting);
  84. }
  85. /**
  86. * testReadAndWriteCache method
  87. *
  88. * @return void
  89. */
  90. public function testReadAndWriteCache()
  91. {
  92. $result = Cache::read('test', 'xcache');
  93. $expecting = '';
  94. $this->assertEquals($expecting, $result);
  95. $data = 'this is a test of the emergency broadcasting system';
  96. $result = Cache::write('test', $data, 'xcache');
  97. $this->assertTrue($result);
  98. $result = Cache::read('test', 'xcache');
  99. $expecting = $data;
  100. $this->assertEquals($expecting, $result);
  101. Cache::delete('test', 'xcache');
  102. }
  103. /**
  104. * testExpiry method
  105. *
  106. * @return void
  107. */
  108. public function testExpiry()
  109. {
  110. $this->_configCache(['duration' => 1]);
  111. $result = Cache::read('test', 'xcache');
  112. $this->assertFalse($result);
  113. $data = 'this is a test of the emergency broadcasting system';
  114. $result = Cache::write('other_test', $data, 'xcache');
  115. $this->assertTrue($result);
  116. sleep(2);
  117. $result = Cache::read('other_test', 'xcache');
  118. $this->assertFalse($result);
  119. $this->_configCache(['duration' => '+1 second']);
  120. $data = 'this is a test of the emergency broadcasting system';
  121. $result = Cache::write('other_test', $data, 'xcache');
  122. $this->assertTrue($result);
  123. sleep(2);
  124. $result = Cache::read('other_test', 'xcache');
  125. $this->assertFalse($result);
  126. }
  127. /**
  128. * testDeleteCache method
  129. *
  130. * @return void
  131. */
  132. public function testDeleteCache()
  133. {
  134. $data = 'this is a test of the emergency broadcasting system';
  135. $result = Cache::write('delete_test', $data, 'xcache');
  136. $this->assertTrue($result);
  137. $result = Cache::delete('delete_test', 'xcache');
  138. $this->assertTrue($result);
  139. }
  140. /**
  141. * testClearCache method
  142. *
  143. * @return void
  144. */
  145. public function testClearCache()
  146. {
  147. $data = 'this is a test of the emergency broadcasting system';
  148. $result = Cache::write('clear_test_1', $data, 'xcache');
  149. $this->assertTrue($result);
  150. $result = Cache::write('clear_test_2', $data, 'xcache');
  151. $this->assertTrue($result);
  152. $result = Cache::clear();
  153. $this->assertTrue($result);
  154. }
  155. /**
  156. * testDecrement method
  157. *
  158. * @return void
  159. */
  160. public function testDecrement()
  161. {
  162. $result = Cache::write('test_decrement', 5, 'xcache');
  163. $this->assertTrue($result);
  164. $result = Cache::decrement('test_decrement', 'xcache');
  165. $this->assertEquals(4, $result);
  166. $result = Cache::read('test_decrement', 'xcache');
  167. $this->assertEquals(4, $result);
  168. $result = Cache::decrement('test_decrement', 2, 'xcache');
  169. $this->assertEquals(2, $result);
  170. $result = Cache::read('test_decrement', 'xcache');
  171. $this->assertEquals(2, $result);
  172. }
  173. /**
  174. * testIncrement method
  175. *
  176. * @return void
  177. */
  178. public function testIncrement()
  179. {
  180. $result = Cache::write('test_increment', 5, 'xcache');
  181. $this->assertTrue($result);
  182. $result = Cache::increment('test_increment', 'xcache');
  183. $this->assertEquals(6, $result);
  184. $result = Cache::read('test_increment', 'xcache');
  185. $this->assertEquals(6, $result);
  186. $result = Cache::increment('test_increment', 2, 'xcache');
  187. $this->assertEquals(8, $result);
  188. $result = Cache::read('test_increment', 'xcache');
  189. $this->assertEquals(8, $result);
  190. }
  191. /**
  192. * Tests that configuring groups for stored keys return the correct values when read/written
  193. * Shows that altering the group value is equivalent to deleting all keys under the same
  194. * group
  195. *
  196. * @return void
  197. */
  198. public function testGroupsReadWrite()
  199. {
  200. Cache::config('xcache_groups', [
  201. 'engine' => 'Xcache',
  202. 'duration' => 0,
  203. 'groups' => ['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. xcache_inc('test_group_a', 1);
  209. $this->assertFalse(Cache::read('test_groups', 'xcache_groups'));
  210. $this->assertTrue(Cache::write('test_groups', 'value2', 'xcache_groups'));
  211. $this->assertEquals('value2', Cache::read('test_groups', 'xcache_groups'));
  212. xcache_inc('test_group_b', 1);
  213. $this->assertFalse(Cache::read('test_groups', 'xcache_groups'));
  214. $this->assertTrue(Cache::write('test_groups', 'value3', 'xcache_groups'));
  215. $this->assertEquals('value3', Cache::read('test_groups', 'xcache_groups'));
  216. }
  217. /**
  218. * Tests that deleteing from a groups-enabled config is possible
  219. *
  220. * @return void
  221. */
  222. public function testGroupDelete()
  223. {
  224. Cache::config('xcache_groups', [
  225. 'engine' => 'Xcache',
  226. 'duration' => 0,
  227. 'groups' => ['group_a', 'group_b'],
  228. 'prefix' => 'test_'
  229. ]);
  230. $this->assertTrue(Cache::write('test_groups', 'value', 'xcache_groups'));
  231. $this->assertEquals('value', Cache::read('test_groups', 'xcache_groups'));
  232. $this->assertTrue(Cache::delete('test_groups', 'xcache_groups'));
  233. $this->assertFalse(Cache::read('test_groups', 'xcache_groups'));
  234. }
  235. /**
  236. * Test clearing a cache group
  237. *
  238. * @return void
  239. */
  240. public function testGroupClear()
  241. {
  242. Cache::config('xcache_groups', [
  243. 'engine' => 'Xcache',
  244. 'duration' => 0,
  245. 'groups' => ['group_a', 'group_b'],
  246. 'prefix' => 'test_'
  247. ]);
  248. $this->assertTrue(Cache::write('test_groups', 'value', 'xcache_groups'));
  249. $this->assertTrue(Cache::clearGroup('group_a', 'xcache_groups'));
  250. $this->assertFalse(Cache::read('test_groups', 'xcache_groups'));
  251. $this->assertTrue(Cache::write('test_groups', 'value2', 'xcache_groups'));
  252. $this->assertTrue(Cache::clearGroup('group_b', 'xcache_groups'));
  253. $this->assertFalse(Cache::read('test_groups', 'xcache_groups'));
  254. }
  255. }