WincacheEngineTest.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://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. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 2.0.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Cache\Engine;
  16. use Cake\Cache\Cache;
  17. use Cake\Core\Configure;
  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::config('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. * testExpiry method
  84. *
  85. * @return void
  86. */
  87. public function testExpiry()
  88. {
  89. $this->_configCache(['duration' => 1]);
  90. $result = Cache::read('test', 'wincache');
  91. $this->assertFalse($result);
  92. $data = 'this is a test of the emergency broadcasting system';
  93. $result = Cache::write('other_test', $data, 'wincache');
  94. $this->assertTrue($result);
  95. sleep(2);
  96. $result = Cache::read('other_test', 'wincache');
  97. $this->assertFalse($result);
  98. $data = 'this is a test of the emergency broadcasting system';
  99. $result = Cache::write('other_test', $data, 'wincache');
  100. $this->assertTrue($result);
  101. sleep(2);
  102. $result = Cache::read('other_test', 'wincache');
  103. $this->assertFalse($result);
  104. }
  105. /**
  106. * testDeleteCache method
  107. *
  108. * @return void
  109. */
  110. public function testDeleteCache()
  111. {
  112. $data = 'this is a test of the emergency broadcasting system';
  113. $result = Cache::write('delete_test', $data, 'wincache');
  114. $this->assertTrue($result);
  115. $result = Cache::delete('delete_test', 'wincache');
  116. $this->assertTrue($result);
  117. }
  118. /**
  119. * testDecrement method
  120. *
  121. * @return void
  122. */
  123. public function testDecrement()
  124. {
  125. $this->skipIf(
  126. !function_exists('wincache_ucache_dec'),
  127. 'No wincache_ucache_dec() function, cannot test decrement().'
  128. );
  129. $result = Cache::write('test_decrement', 5, 'wincache');
  130. $this->assertTrue($result);
  131. $result = Cache::decrement('test_decrement', 1, 'wincache');
  132. $this->assertEquals(4, $result);
  133. $result = Cache::read('test_decrement', 'wincache');
  134. $this->assertEquals(4, $result);
  135. $result = Cache::decrement('test_decrement', 2, 'wincache');
  136. $this->assertEquals(2, $result);
  137. $result = Cache::read('test_decrement', 'wincache');
  138. $this->assertEquals(2, $result);
  139. }
  140. /**
  141. * testIncrement method
  142. *
  143. * @return void
  144. */
  145. public function testIncrement()
  146. {
  147. $this->skipIf(
  148. !function_exists('wincache_ucache_inc'),
  149. 'No wincache_inc() function, cannot test increment().'
  150. );
  151. $result = Cache::write('test_increment', 5, 'wincache');
  152. $this->assertTrue($result);
  153. $result = Cache::increment('test_increment', 1, 'wincache');
  154. $this->assertEquals(6, $result);
  155. $result = Cache::read('test_increment', 'wincache');
  156. $this->assertEquals(6, $result);
  157. $result = Cache::increment('test_increment', 2, 'wincache');
  158. $this->assertEquals(8, $result);
  159. $result = Cache::read('test_increment', 'wincache');
  160. $this->assertEquals(8, $result);
  161. }
  162. /**
  163. * test the clearing of cache keys
  164. *
  165. * @return void
  166. */
  167. public function testClear()
  168. {
  169. wincache_ucache_set('not_cake', 'safe');
  170. Cache::write('some_value', 'value', 'wincache');
  171. $result = Cache::clear(false, 'wincache');
  172. $this->assertTrue($result);
  173. $this->assertFalse(Cache::read('some_value', 'wincache'));
  174. $this->assertEquals('safe', wincache_ucache_get('not_cake'));
  175. }
  176. /**
  177. * Tests that configuring groups for stored keys return the correct values when read/written
  178. * Shows that altering the group value is equivalent to deleting all keys under the same
  179. * group
  180. *
  181. * @return void
  182. */
  183. public function testGroupsReadWrite()
  184. {
  185. Cache::config('wincache_groups', [
  186. 'engine' => 'Wincache',
  187. 'duration' => 0,
  188. 'groups' => ['group_a', 'group_b'],
  189. 'prefix' => 'test_'
  190. ]);
  191. $this->assertTrue(Cache::write('test_groups', 'value', 'wincache_groups'));
  192. $this->assertEquals('value', Cache::read('test_groups', 'wincache_groups'));
  193. wincache_ucache_inc('test_group_a');
  194. $this->assertFalse(Cache::read('test_groups', 'wincache_groups'));
  195. $this->assertTrue(Cache::write('test_groups', 'value2', 'wincache_groups'));
  196. $this->assertEquals('value2', Cache::read('test_groups', 'wincache_groups'));
  197. wincache_ucache_inc('test_group_b');
  198. $this->assertFalse(Cache::read('test_groups', 'wincache_groups'));
  199. $this->assertTrue(Cache::write('test_groups', 'value3', 'wincache_groups'));
  200. $this->assertEquals('value3', Cache::read('test_groups', 'wincache_groups'));
  201. }
  202. /**
  203. * Tests that deleting from a groups-enabled config is possible
  204. *
  205. * @return void
  206. */
  207. public function testGroupDelete()
  208. {
  209. Cache::config('wincache_groups', [
  210. 'engine' => 'Wincache',
  211. 'duration' => 0,
  212. 'groups' => ['group_a', 'group_b'],
  213. 'prefix' => 'test_'
  214. ]);
  215. $this->assertTrue(Cache::write('test_groups', 'value', 'wincache_groups'));
  216. $this->assertEquals('value', Cache::read('test_groups', 'wincache_groups'));
  217. $this->assertTrue(Cache::delete('test_groups', 'wincache_groups'));
  218. $this->assertFalse(Cache::read('test_groups', 'wincache_groups'));
  219. }
  220. /**
  221. * Test clearing a cache group
  222. *
  223. * @return void
  224. */
  225. public function testGroupClear()
  226. {
  227. Cache::config('wincache_groups', [
  228. 'engine' => 'Wincache',
  229. 'duration' => 0,
  230. 'groups' => ['group_a', 'group_b'],
  231. 'prefix' => 'test_'
  232. ]);
  233. $this->assertTrue(Cache::write('test_groups', 'value', 'wincache_groups'));
  234. $this->assertTrue(Cache::clearGroup('group_a', 'wincache_groups'));
  235. $this->assertFalse(Cache::read('test_groups', 'wincache_groups'));
  236. $this->assertTrue(Cache::write('test_groups', 'value2', 'wincache_groups'));
  237. $this->assertTrue(Cache::clearGroup('group_b', 'wincache_groups'));
  238. $this->assertFalse(Cache::read('test_groups', 'wincache_groups'));
  239. }
  240. }