XcacheEngineTest.php 8.5 KB

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