WincacheEngineTest.php 9.2 KB

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