XcacheEngineTest.php 7.3 KB

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