WincacheEngineTest.php 9.5 KB

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