WincacheEngineTest.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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. * testDeleteCache method
  122. *
  123. * @return void
  124. */
  125. public function testDeleteCache()
  126. {
  127. $data = 'this is a test of the emergency broadcasting system';
  128. $result = Cache::write('delete_test', $data, 'wincache');
  129. $this->assertTrue($result);
  130. $result = Cache::delete('delete_test', 'wincache');
  131. $this->assertTrue($result);
  132. }
  133. /**
  134. * testDecrement method
  135. *
  136. * @return void
  137. */
  138. public function testDecrement()
  139. {
  140. $this->skipIf(
  141. !function_exists('wincache_ucache_dec'),
  142. 'No wincache_ucache_dec() function, cannot test decrement().'
  143. );
  144. $result = Cache::write('test_decrement', 5, 'wincache');
  145. $this->assertTrue($result);
  146. $result = Cache::decrement('test_decrement', 1, 'wincache');
  147. $this->assertEquals(4, $result);
  148. $result = Cache::read('test_decrement', 'wincache');
  149. $this->assertEquals(4, $result);
  150. $result = Cache::decrement('test_decrement', 2, 'wincache');
  151. $this->assertEquals(2, $result);
  152. $result = Cache::read('test_decrement', 'wincache');
  153. $this->assertEquals(2, $result);
  154. }
  155. /**
  156. * testIncrement method
  157. *
  158. * @return void
  159. */
  160. public function testIncrement()
  161. {
  162. $this->skipIf(
  163. !function_exists('wincache_ucache_inc'),
  164. 'No wincache_inc() function, cannot test increment().'
  165. );
  166. $result = Cache::write('test_increment', 5, 'wincache');
  167. $this->assertTrue($result);
  168. $result = Cache::increment('test_increment', 1, 'wincache');
  169. $this->assertEquals(6, $result);
  170. $result = Cache::read('test_increment', 'wincache');
  171. $this->assertEquals(6, $result);
  172. $result = Cache::increment('test_increment', 2, 'wincache');
  173. $this->assertEquals(8, $result);
  174. $result = Cache::read('test_increment', 'wincache');
  175. $this->assertEquals(8, $result);
  176. }
  177. /**
  178. * test the clearing of cache keys
  179. *
  180. * @return void
  181. */
  182. public function testClear()
  183. {
  184. wincache_ucache_set('not_cake', 'safe');
  185. Cache::write('some_value', 'value', 'wincache');
  186. $result = Cache::clear('wincache');
  187. $this->assertTrue($result);
  188. $this->assertNull(Cache::read('some_value', 'wincache'));
  189. $this->assertEquals('safe', wincache_ucache_get('not_cake'));
  190. }
  191. /**
  192. * Tests that configuring groups for stored keys return the correct values when read/written
  193. * Shows that altering the group value is equivalent to deleting all keys under the same
  194. * group
  195. *
  196. * @return void
  197. */
  198. public function testGroupsReadWrite()
  199. {
  200. Cache::setConfig('wincache_groups', [
  201. 'engine' => 'Wincache',
  202. 'duration' => 0,
  203. 'groups' => ['group_a', 'group_b'],
  204. 'prefix' => 'test_',
  205. ]);
  206. $this->assertTrue(Cache::write('test_groups', 'value', 'wincache_groups'));
  207. $this->assertEquals('value', Cache::read('test_groups', 'wincache_groups'));
  208. wincache_ucache_inc('test_group_a');
  209. $this->assertNull(Cache::read('test_groups', 'wincache_groups'));
  210. $this->assertTrue(Cache::write('test_groups', 'value2', 'wincache_groups'));
  211. $this->assertEquals('value2', Cache::read('test_groups', 'wincache_groups'));
  212. wincache_ucache_inc('test_group_b');
  213. $this->assertNull(Cache::read('test_groups', 'wincache_groups'));
  214. $this->assertTrue(Cache::write('test_groups', 'value3', 'wincache_groups'));
  215. $this->assertEquals('value3', Cache::read('test_groups', 'wincache_groups'));
  216. }
  217. /**
  218. * Tests that deleting from a groups-enabled config is possible
  219. *
  220. * @return void
  221. */
  222. public function testGroupDelete()
  223. {
  224. Cache::setConfig('wincache_groups', [
  225. 'engine' => 'Wincache',
  226. 'duration' => 0,
  227. 'groups' => ['group_a', 'group_b'],
  228. 'prefix' => 'test_',
  229. ]);
  230. $this->assertTrue(Cache::write('test_groups', 'value', 'wincache_groups'));
  231. $this->assertEquals('value', Cache::read('test_groups', 'wincache_groups'));
  232. $this->assertTrue(Cache::delete('test_groups', 'wincache_groups'));
  233. $this->assertNull(Cache::read('test_groups', 'wincache_groups'));
  234. }
  235. /**
  236. * Test clearing a cache group
  237. *
  238. * @return void
  239. */
  240. public function testGroupClear()
  241. {
  242. Cache::setConfig('wincache_groups', [
  243. 'engine' => 'Wincache',
  244. 'duration' => 0,
  245. 'groups' => ['group_a', 'group_b'],
  246. 'prefix' => 'test_',
  247. ]);
  248. $this->assertTrue(Cache::write('test_groups', 'value', 'wincache_groups'));
  249. $this->assertTrue(Cache::clearGroup('group_a', 'wincache_groups'));
  250. $this->assertNull(Cache::read('test_groups', 'wincache_groups'));
  251. $this->assertTrue(Cache::write('test_groups', 'value2', 'wincache_groups'));
  252. $this->assertTrue(Cache::clearGroup('group_b', 'wincache_groups'));
  253. $this->assertNull(Cache::read('test_groups', 'wincache_groups'));
  254. }
  255. }