WincacheEngineTest.php 7.2 KB

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