WincacheEngineTest.php 7.1 KB

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