/tests/TestCase/Cache/Engine/WincacheEngineTest.php

https://github.com/LubosRemplik/cakephp · PHP · 279 lines · 160 code · 40 blank · 79 comment · 0 complexity · 216f25b8ad52c7948c0b2eb441b79cf4 MD5 · raw file

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