XcacheEngineTest.php 9.1 KB

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