XcacheEngineTest.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. * @since CakePHP(tm) v 1.2.0.5434
  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. * 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. Cache::enable();
  37. Cache::config('xcache', ['engine' => 'Xcache', 'prefix' => 'cake_']);
  38. }
  39. /**
  40. * Helper method for testing.
  41. *
  42. * @return void
  43. */
  44. protected function _configCache($config = []) {
  45. $defaults = [
  46. 'className' => 'Xcache',
  47. 'prefix' => 'cake_',
  48. ];
  49. Cache::drop('xcache');
  50. Cache::config('xcache', array_merge($defaults, $config));
  51. }
  52. /**
  53. * tearDown method
  54. *
  55. * @return void
  56. */
  57. public function tearDown() {
  58. parent::tearDown();
  59. Cache::drop('xcache');
  60. Cache::drop('xcache_groups');
  61. }
  62. /**
  63. * testConfig method
  64. *
  65. * @return void
  66. */
  67. public function testConfig() {
  68. $config = Cache::engine('xcache')->config();
  69. $expecting = [
  70. 'prefix' => 'cake_',
  71. 'duration' => 3600,
  72. 'probability' => 100,
  73. ];
  74. $this->assertTrue(isset($config['PHP_AUTH_USER']));
  75. $this->assertTrue(isset($config['PHP_AUTH_PW']));
  76. unset($config['PHP_AUTH_USER'], $config['PHP_AUTH_PW']);
  77. $this->assertEquals($config, $expecting);
  78. }
  79. /**
  80. * testReadAndWriteCache method
  81. *
  82. * @return void
  83. */
  84. public function testReadAndWriteCache() {
  85. $result = Cache::read('test', 'xcache');
  86. $expecting = '';
  87. $this->assertEquals($expecting, $result);
  88. $data = 'this is a test of the emergency broadcasting system';
  89. $result = Cache::write('test', $data, 'xcache');
  90. $this->assertTrue($result);
  91. $result = Cache::read('test', 'xcache');
  92. $expecting = $data;
  93. $this->assertEquals($expecting, $result);
  94. Cache::delete('test', 'xcache');
  95. }
  96. /**
  97. * testExpiry method
  98. *
  99. * @return void
  100. */
  101. public function testExpiry() {
  102. $this->_configCache(['duration' => 1]);
  103. $result = Cache::read('test', 'xcache');
  104. $this->assertFalse($result);
  105. $data = 'this is a test of the emergency broadcasting system';
  106. $result = Cache::write('other_test', $data, 'xcache');
  107. $this->assertTrue($result);
  108. sleep(2);
  109. $result = Cache::read('other_test', 'xcache');
  110. $this->assertFalse($result);
  111. $this->_configCache(['duration' => '+1 second']);
  112. $data = 'this is a test of the emergency broadcasting system';
  113. $result = Cache::write('other_test', $data, 'xcache');
  114. $this->assertTrue($result);
  115. sleep(2);
  116. $result = Cache::read('other_test', 'xcache');
  117. $this->assertFalse($result);
  118. }
  119. /**
  120. * testDeleteCache method
  121. *
  122. * @return void
  123. */
  124. public function testDeleteCache() {
  125. $data = 'this is a test of the emergency broadcasting system';
  126. $result = Cache::write('delete_test', $data, 'xcache');
  127. $this->assertTrue($result);
  128. $result = Cache::delete('delete_test', 'xcache');
  129. $this->assertTrue($result);
  130. }
  131. /**
  132. * testClearCache method
  133. *
  134. * @return void
  135. */
  136. public function testClearCache() {
  137. $data = 'this is a test of the emergency broadcasting system';
  138. $result = Cache::write('clear_test_1', $data, 'xcache');
  139. $this->assertTrue($result);
  140. $result = Cache::write('clear_test_2', $data, 'xcache');
  141. $this->assertTrue($result);
  142. $result = Cache::clear();
  143. $this->assertTrue($result);
  144. }
  145. /**
  146. * testDecrement method
  147. *
  148. * @return void
  149. */
  150. public function testDecrement() {
  151. $result = Cache::write('test_decrement', 5, 'xcache');
  152. $this->assertTrue($result);
  153. $result = Cache::decrement('test_decrement', 'xcache');
  154. $this->assertEquals(4, $result);
  155. $result = Cache::read('test_decrement', 'xcache');
  156. $this->assertEquals(4, $result);
  157. $result = Cache::decrement('test_decrement', 2, 'xcache');
  158. $this->assertEquals(2, $result);
  159. $result = Cache::read('test_decrement', 'xcache');
  160. $this->assertEquals(2, $result);
  161. }
  162. /**
  163. * testIncrement method
  164. *
  165. * @return void
  166. */
  167. public function testIncrement() {
  168. $result = Cache::write('test_increment', 5, 'xcache');
  169. $this->assertTrue($result);
  170. $result = Cache::increment('test_increment', 'xcache');
  171. $this->assertEquals(6, $result);
  172. $result = Cache::read('test_increment', 'xcache');
  173. $this->assertEquals(6, $result);
  174. $result = Cache::increment('test_increment', 2, 'xcache');
  175. $this->assertEquals(8, $result);
  176. $result = Cache::read('test_increment', 'xcache');
  177. $this->assertEquals(8, $result);
  178. }
  179. /**
  180. * Tests that configuring groups for stored keys return the correct values when read/written
  181. * Shows that altering the group value is equivalent to deleting all keys under the same
  182. * group
  183. *
  184. * @return void
  185. */
  186. public function testGroupsReadWrite() {
  187. Cache::config('xcache_groups', [
  188. 'engine' => 'Xcache',
  189. 'duration' => 0,
  190. 'groups' => ['group_a', 'group_b'],
  191. 'prefix' => 'test_'
  192. ]);
  193. $this->assertTrue(Cache::write('test_groups', 'value', 'xcache_groups'));
  194. $this->assertEquals('value', Cache::read('test_groups', 'xcache_groups'));
  195. xcache_inc('test_group_a', 1);
  196. $this->assertFalse(Cache::read('test_groups', 'xcache_groups'));
  197. $this->assertTrue(Cache::write('test_groups', 'value2', 'xcache_groups'));
  198. $this->assertEquals('value2', Cache::read('test_groups', 'xcache_groups'));
  199. xcache_inc('test_group_b', 1);
  200. $this->assertFalse(Cache::read('test_groups', 'xcache_groups'));
  201. $this->assertTrue(Cache::write('test_groups', 'value3', 'xcache_groups'));
  202. $this->assertEquals('value3', Cache::read('test_groups', 'xcache_groups'));
  203. }
  204. /**
  205. * Tests that deleteing from a groups-enabled config is possible
  206. *
  207. * @return void
  208. */
  209. public function testGroupDelete() {
  210. Cache::config('xcache_groups', [
  211. 'engine' => 'Xcache',
  212. 'duration' => 0,
  213. 'groups' => ['group_a', 'group_b'],
  214. 'prefix' => 'test_'
  215. ]);
  216. $this->assertTrue(Cache::write('test_groups', 'value', 'xcache_groups'));
  217. $this->assertEquals('value', Cache::read('test_groups', 'xcache_groups'));
  218. $this->assertTrue(Cache::delete('test_groups', 'xcache_groups'));
  219. $this->assertFalse(Cache::read('test_groups', 'xcache_groups'));
  220. }
  221. /**
  222. * Test clearing a cache group
  223. *
  224. * @return void
  225. */
  226. public function testGroupClear() {
  227. Cache::config('xcache_groups', [
  228. 'engine' => 'Xcache',
  229. 'duration' => 0,
  230. 'groups' => ['group_a', 'group_b'],
  231. 'prefix' => 'test_'
  232. ]);
  233. $this->assertTrue(Cache::write('test_groups', 'value', 'xcache_groups'));
  234. $this->assertTrue(Cache::clearGroup('group_a', 'xcache_groups'));
  235. $this->assertFalse(Cache::read('test_groups', 'xcache_groups'));
  236. $this->assertTrue(Cache::write('test_groups', 'value2', 'xcache_groups'));
  237. $this->assertTrue(Cache::clearGroup('group_b', 'xcache_groups'));
  238. $this->assertFalse(Cache::read('test_groups', 'xcache_groups'));
  239. }
  240. }