XcacheEngineTest.php 7.3 KB

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