XcacheEngineTest.php 9.1 KB

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