XcacheEngineTest.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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. 'groups' => [],
  80. ];
  81. $this->assertTrue(isset($config['PHP_AUTH_USER']));
  82. $this->assertTrue(isset($config['PHP_AUTH_PW']));
  83. unset($config['PHP_AUTH_USER'], $config['PHP_AUTH_PW']);
  84. $this->assertEquals($config, $expecting);
  85. }
  86. /**
  87. * testReadAndWriteCache method
  88. *
  89. * @return void
  90. */
  91. public function testReadAndWriteCache()
  92. {
  93. $result = Cache::read('test', 'xcache');
  94. $expecting = '';
  95. $this->assertEquals($expecting, $result);
  96. $data = 'this is a test of the emergency broadcasting system';
  97. $result = Cache::write('test', $data, 'xcache');
  98. $this->assertTrue($result);
  99. $result = Cache::read('test', 'xcache');
  100. $expecting = $data;
  101. $this->assertEquals($expecting, $result);
  102. Cache::delete('test', 'xcache');
  103. }
  104. /**
  105. * testExpiry method
  106. *
  107. * @return void
  108. */
  109. public function testExpiry()
  110. {
  111. $this->_configCache(['duration' => 1]);
  112. $result = Cache::read('test', 'xcache');
  113. $this->assertFalse($result);
  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. $this->_configCache(['duration' => '+1 second']);
  121. $data = 'this is a test of the emergency broadcasting system';
  122. $result = Cache::write('other_test', $data, 'xcache');
  123. $this->assertTrue($result);
  124. sleep(2);
  125. $result = Cache::read('other_test', 'xcache');
  126. $this->assertFalse($result);
  127. }
  128. /**
  129. * testDeleteCache method
  130. *
  131. * @return void
  132. */
  133. public function testDeleteCache()
  134. {
  135. $data = 'this is a test of the emergency broadcasting system';
  136. $result = Cache::write('delete_test', $data, 'xcache');
  137. $this->assertTrue($result);
  138. $result = Cache::delete('delete_test', 'xcache');
  139. $this->assertTrue($result);
  140. }
  141. /**
  142. * testClearCache method
  143. *
  144. * @return void
  145. */
  146. public function testClearCache()
  147. {
  148. if ((PHP_SAPI === 'cli' || PHP_SAPI === 'phpdbg')) {
  149. $this->markTestSkipped('Xcache administration functions are not available for the CLI.');
  150. }
  151. $data = 'this is a test of the emergency broadcasting system';
  152. $result = Cache::write('clear_test_1', $data, 'xcache');
  153. $this->assertTrue($result);
  154. $result = Cache::write('clear_test_2', $data, 'xcache');
  155. $this->assertTrue($result);
  156. $result = Cache::clear(false, 'xcache');
  157. $this->assertTrue($result);
  158. }
  159. /**
  160. * testDecrement method
  161. *
  162. * @return void
  163. */
  164. public function testDecrement()
  165. {
  166. $result = Cache::write('test_decrement', 5, 'xcache');
  167. $this->assertTrue($result);
  168. $result = Cache::decrement('test_decrement', 1, 'xcache');
  169. $this->assertEquals(4, $result);
  170. $result = Cache::read('test_decrement', 'xcache');
  171. $this->assertEquals(4, $result);
  172. $result = Cache::decrement('test_decrement', 2, 'xcache');
  173. $this->assertEquals(2, $result);
  174. $result = Cache::read('test_decrement', 'xcache');
  175. $this->assertEquals(2, $result);
  176. }
  177. /**
  178. * testIncrement method
  179. *
  180. * @return void
  181. */
  182. public function testIncrement()
  183. {
  184. $result = Cache::write('test_increment', 5, 'xcache');
  185. $this->assertTrue($result);
  186. $result = Cache::increment('test_increment', 1, 'xcache');
  187. $this->assertEquals(6, $result);
  188. $result = Cache::read('test_increment', 'xcache');
  189. $this->assertEquals(6, $result);
  190. $result = Cache::increment('test_increment', 2, 'xcache');
  191. $this->assertEquals(8, $result);
  192. $result = Cache::read('test_increment', 'xcache');
  193. $this->assertEquals(8, $result);
  194. }
  195. /**
  196. * Tests that configuring groups for stored keys return the correct values when read/written
  197. * Shows that altering the group value is equivalent to deleting all keys under the same
  198. * group
  199. *
  200. * @return void
  201. */
  202. public function testGroupsReadWrite()
  203. {
  204. Cache::config('xcache_groups', [
  205. 'engine' => 'Xcache',
  206. 'duration' => 0,
  207. 'groups' => ['group_a', 'group_b'],
  208. 'prefix' => 'test_'
  209. ]);
  210. $this->assertTrue(Cache::write('test_groups', 'value', 'xcache_groups'));
  211. $this->assertEquals('value', Cache::read('test_groups', 'xcache_groups'));
  212. xcache_inc('test_group_a', 1);
  213. $this->assertFalse(Cache::read('test_groups', 'xcache_groups'));
  214. $this->assertTrue(Cache::write('test_groups', 'value2', 'xcache_groups'));
  215. $this->assertEquals('value2', Cache::read('test_groups', 'xcache_groups'));
  216. xcache_inc('test_group_b', 1);
  217. $this->assertFalse(Cache::read('test_groups', 'xcache_groups'));
  218. $this->assertTrue(Cache::write('test_groups', 'value3', 'xcache_groups'));
  219. $this->assertEquals('value3', Cache::read('test_groups', 'xcache_groups'));
  220. }
  221. /**
  222. * Tests that deleteing from a groups-enabled config is possible
  223. *
  224. * @return void
  225. */
  226. public function testGroupDelete()
  227. {
  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->assertEquals('value', Cache::read('test_groups', 'xcache_groups'));
  236. $this->assertTrue(Cache::delete('test_groups', 'xcache_groups'));
  237. $this->assertFalse(Cache::read('test_groups', 'xcache_groups'));
  238. }
  239. /**
  240. * Test clearing a cache group
  241. *
  242. * @return void
  243. */
  244. public function testGroupClear()
  245. {
  246. Cache::config('xcache_groups', [
  247. 'engine' => 'Xcache',
  248. 'duration' => 0,
  249. 'groups' => ['group_a', 'group_b'],
  250. 'prefix' => 'test_'
  251. ]);
  252. $this->assertTrue(Cache::write('test_groups', 'value', 'xcache_groups'));
  253. $this->assertTrue(Cache::clearGroup('group_a', 'xcache_groups'));
  254. $this->assertFalse(Cache::read('test_groups', 'xcache_groups'));
  255. $this->assertTrue(Cache::write('test_groups', 'value2', 'xcache_groups'));
  256. $this->assertTrue(Cache::clearGroup('group_b', 'xcache_groups'));
  257. $this->assertFalse(Cache::read('test_groups', 'xcache_groups'));
  258. }
  259. }