WincacheEngineTest.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  5. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  6. *
  7. * Licensed under The MIT License
  8. * For full copyright and license information, please see the LICENSE.txt
  9. * Redistributions of files must retain the above copyright notice
  10. *
  11. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  12. * @link https://cakephp.org CakePHP(tm) Project
  13. * @since 2.0.0
  14. * @license https://opensource.org/licenses/mit-license.php MIT License
  15. */
  16. namespace Cake\Test\TestCase\Cache\Engine;
  17. use Cake\Cache\Cache;
  18. use Cake\TestSuite\TestCase;
  19. /**
  20. * WincacheEngineTest class
  21. */
  22. class WincacheEngineTest extends TestCase
  23. {
  24. /**
  25. * setUp method
  26. *
  27. * @return void
  28. */
  29. public function setUp()
  30. {
  31. parent::setUp();
  32. $this->skipIf(!function_exists('wincache_ucache_set'), 'Wincache is not installed or configured properly.');
  33. $this->skipIf(!ini_get('wincache.enablecli'), 'Wincache is not enabled on the CLI.');
  34. Cache::enable();
  35. $this->_configCache();
  36. }
  37. /**
  38. * tearDown method
  39. *
  40. * @return void
  41. */
  42. public function tearDown()
  43. {
  44. parent::tearDown();
  45. Cache::drop('wincache');
  46. Cache::drop('wincache_groups');
  47. }
  48. /**
  49. * Helper method for testing.
  50. *
  51. * @param array $config
  52. * @return void
  53. */
  54. protected function _configCache($config = [])
  55. {
  56. $defaults = [
  57. 'className' => 'Wincache',
  58. 'prefix' => 'cake_',
  59. ];
  60. Cache::drop('wincache');
  61. Cache::setConfig('wincache', array_merge($defaults, $config));
  62. }
  63. /**
  64. * testReadAndWriteCache method
  65. *
  66. * @return void
  67. */
  68. public function testReadAndWriteCache()
  69. {
  70. $this->_configCache(['duration' => 1]);
  71. $result = Cache::read('test', 'wincache');
  72. $expecting = '';
  73. $this->assertEquals($expecting, $result);
  74. $data = 'this is a test of the emergency broadcasting system';
  75. $result = Cache::write('test', $data, 'wincache');
  76. $this->assertTrue($result);
  77. $result = Cache::read('test', 'wincache');
  78. $expecting = $data;
  79. $this->assertEquals($expecting, $result);
  80. Cache::delete('test', 'wincache');
  81. }
  82. /**
  83. * Test get with default value
  84. *
  85. * @return void
  86. */
  87. public function testGetDefaultValue()
  88. {
  89. $wincache = Cache::pool('wincache');
  90. $this->assertFalse($wincache->get('nope', false));
  91. $this->assertNull($wincache->get('nope', null));
  92. $this->assertTrue($wincache->get('nope', true));
  93. $this->assertSame(0, $wincache->get('nope', 0));
  94. $wincache->set('yep', 0);
  95. $this->assertSame(0, $wincache->get('yep', false));
  96. }
  97. /**
  98. * testExpiry method
  99. *
  100. * @return void
  101. */
  102. public function testExpiry()
  103. {
  104. $this->_configCache(['duration' => 1]);
  105. $result = Cache::read('test', 'wincache');
  106. $this->assertNull($result);
  107. $data = 'this is a test of the emergency broadcasting system';
  108. $result = Cache::write('other_test', $data, 'wincache');
  109. $this->assertTrue($result);
  110. sleep(2);
  111. $result = Cache::read('other_test', 'wincache');
  112. $this->assertNull($result);
  113. $data = 'this is a test of the emergency broadcasting system';
  114. $result = Cache::write('other_test', $data, 'wincache');
  115. $this->assertTrue($result);
  116. sleep(2);
  117. $result = Cache::read('other_test', 'wincache');
  118. $this->assertNull($result);
  119. }
  120. /**
  121. * test set ttl parameter
  122. *
  123. * @return void
  124. */
  125. public function testSetWithTtl()
  126. {
  127. $this->_configCache(['duration' => 99]);
  128. $engine = Cache::pool('wincache');
  129. $this->assertNull($engine->get('test'));
  130. $data = 'this is a test of the emergency broadcasting system';
  131. $this->assertTrue($engine->set('default_ttl', $data));
  132. $this->assertTrue($engine->set('int_ttl', $data, 1));
  133. $this->assertTrue($engine->set('interval_ttl', $data, new DateInterval('PT1S')));
  134. sleep(2);
  135. $this->assertNull($engine->get('int_ttl'));
  136. $this->assertNull($engine->get('interval_ttl'));
  137. $this->assertSame($data, $engine->get('default_ttl'));
  138. }
  139. /**
  140. * testDeleteCache method
  141. *
  142. * @return void
  143. */
  144. public function testDeleteCache()
  145. {
  146. $data = 'this is a test of the emergency broadcasting system';
  147. $result = Cache::write('delete_test', $data, 'wincache');
  148. $this->assertTrue($result);
  149. $result = Cache::delete('delete_test', 'wincache');
  150. $this->assertTrue($result);
  151. }
  152. /**
  153. * testDecrement method
  154. *
  155. * @return void
  156. */
  157. public function testDecrement()
  158. {
  159. $this->skipIf(
  160. !function_exists('wincache_ucache_dec'),
  161. 'No wincache_ucache_dec() function, cannot test decrement().'
  162. );
  163. $result = Cache::write('test_decrement', 5, 'wincache');
  164. $this->assertTrue($result);
  165. $result = Cache::decrement('test_decrement', 1, 'wincache');
  166. $this->assertEquals(4, $result);
  167. $result = Cache::read('test_decrement', 'wincache');
  168. $this->assertEquals(4, $result);
  169. $result = Cache::decrement('test_decrement', 2, 'wincache');
  170. $this->assertEquals(2, $result);
  171. $result = Cache::read('test_decrement', 'wincache');
  172. $this->assertEquals(2, $result);
  173. }
  174. /**
  175. * testIncrement method
  176. *
  177. * @return void
  178. */
  179. public function testIncrement()
  180. {
  181. $this->skipIf(
  182. !function_exists('wincache_ucache_inc'),
  183. 'No wincache_inc() function, cannot test increment().'
  184. );
  185. $result = Cache::write('test_increment', 5, 'wincache');
  186. $this->assertTrue($result);
  187. $result = Cache::increment('test_increment', 1, 'wincache');
  188. $this->assertEquals(6, $result);
  189. $result = Cache::read('test_increment', 'wincache');
  190. $this->assertEquals(6, $result);
  191. $result = Cache::increment('test_increment', 2, 'wincache');
  192. $this->assertEquals(8, $result);
  193. $result = Cache::read('test_increment', 'wincache');
  194. $this->assertEquals(8, $result);
  195. }
  196. /**
  197. * test the clearing of cache keys
  198. *
  199. * @return void
  200. */
  201. public function testClear()
  202. {
  203. wincache_ucache_set('not_cake', 'safe');
  204. Cache::write('some_value', 'value', 'wincache');
  205. $result = Cache::clear('wincache');
  206. $this->assertTrue($result);
  207. $this->assertNull(Cache::read('some_value', 'wincache'));
  208. $this->assertEquals('safe', wincache_ucache_get('not_cake'));
  209. }
  210. /**
  211. * Tests that configuring groups for stored keys return the correct values when read/written
  212. * Shows that altering the group value is equivalent to deleting all keys under the same
  213. * group
  214. *
  215. * @return void
  216. */
  217. public function testGroupsReadWrite()
  218. {
  219. Cache::setConfig('wincache_groups', [
  220. 'engine' => 'Wincache',
  221. 'duration' => 0,
  222. 'groups' => ['group_a', 'group_b'],
  223. 'prefix' => 'test_',
  224. ]);
  225. $this->assertTrue(Cache::write('test_groups', 'value', 'wincache_groups'));
  226. $this->assertEquals('value', Cache::read('test_groups', 'wincache_groups'));
  227. wincache_ucache_inc('test_group_a');
  228. $this->assertNull(Cache::read('test_groups', 'wincache_groups'));
  229. $this->assertTrue(Cache::write('test_groups', 'value2', 'wincache_groups'));
  230. $this->assertEquals('value2', Cache::read('test_groups', 'wincache_groups'));
  231. wincache_ucache_inc('test_group_b');
  232. $this->assertNull(Cache::read('test_groups', 'wincache_groups'));
  233. $this->assertTrue(Cache::write('test_groups', 'value3', 'wincache_groups'));
  234. $this->assertEquals('value3', Cache::read('test_groups', 'wincache_groups'));
  235. }
  236. /**
  237. * Tests that deleting from a groups-enabled config is possible
  238. *
  239. * @return void
  240. */
  241. public function testGroupDelete()
  242. {
  243. Cache::setConfig('wincache_groups', [
  244. 'engine' => 'Wincache',
  245. 'duration' => 0,
  246. 'groups' => ['group_a', 'group_b'],
  247. 'prefix' => 'test_',
  248. ]);
  249. $this->assertTrue(Cache::write('test_groups', 'value', 'wincache_groups'));
  250. $this->assertEquals('value', Cache::read('test_groups', 'wincache_groups'));
  251. $this->assertTrue(Cache::delete('test_groups', 'wincache_groups'));
  252. $this->assertNull(Cache::read('test_groups', 'wincache_groups'));
  253. }
  254. /**
  255. * Test clearing a cache group
  256. *
  257. * @return void
  258. */
  259. public function testGroupClear()
  260. {
  261. Cache::setConfig('wincache_groups', [
  262. 'engine' => 'Wincache',
  263. 'duration' => 0,
  264. 'groups' => ['group_a', 'group_b'],
  265. 'prefix' => 'test_',
  266. ]);
  267. $this->assertTrue(Cache::write('test_groups', 'value', 'wincache_groups'));
  268. $this->assertTrue(Cache::clearGroup('group_a', 'wincache_groups'));
  269. $this->assertNull(Cache::read('test_groups', 'wincache_groups'));
  270. $this->assertTrue(Cache::write('test_groups', 'value2', 'wincache_groups'));
  271. $this->assertTrue(Cache::clearGroup('group_b', 'wincache_groups'));
  272. $this->assertNull(Cache::read('test_groups', 'wincache_groups'));
  273. }
  274. }