XcacheEngineTest.php 9.1 KB

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