WincacheEngineTest.php 7.2 KB

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