WincacheEngineTest.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. <?php
  2. /**
  3. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  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://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  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. */
  23. class WincacheEngineTest extends TestCase {
  24. /**
  25. * setUp method
  26. *
  27. * @return void
  28. */
  29. public function setUp() {
  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. parent::tearDown();
  43. Cache::drop('wincache');
  44. Cache::drop('wincache_groups');
  45. }
  46. /**
  47. * Helper method for testing.
  48. *
  49. * @param array $config
  50. * @return void
  51. */
  52. protected function _configCache($config = []) {
  53. $defaults = [
  54. 'className' => 'Wincache',
  55. 'prefix' => 'cake_'
  56. ];
  57. Cache::drop('wincache');
  58. Cache::config('wincache', array_merge($defaults, $config));
  59. }
  60. /**
  61. * testReadAndWriteCache method
  62. *
  63. * @return void
  64. */
  65. public function testReadAndWriteCache() {
  66. $this->_configCache(['duration' => 1]);
  67. $result = Cache::read('test', 'wincache');
  68. $expecting = '';
  69. $this->assertEquals($expecting, $result);
  70. $data = 'this is a test of the emergency broadcasting system';
  71. $result = Cache::write('test', $data, 'wincache');
  72. $this->assertTrue($result);
  73. $result = Cache::read('test', 'wincache');
  74. $expecting = $data;
  75. $this->assertEquals($expecting, $result);
  76. Cache::delete('test', 'wincache');
  77. }
  78. /**
  79. * testExpiry method
  80. *
  81. * @return void
  82. */
  83. public function testExpiry() {
  84. $this->_configCache(['duration' => 1]);
  85. $result = Cache::read('test', 'wincache');
  86. $this->assertFalse($result);
  87. $data = 'this is a test of the emergency broadcasting system';
  88. $result = Cache::write('other_test', $data, 'wincache');
  89. $this->assertTrue($result);
  90. sleep(2);
  91. $result = Cache::read('other_test', 'wincache');
  92. $this->assertFalse($result);
  93. $data = 'this is a test of the emergency broadcasting system';
  94. $result = Cache::write('other_test', $data, 'wincache');
  95. $this->assertTrue($result);
  96. sleep(2);
  97. $result = Cache::read('other_test', 'wincache');
  98. $this->assertFalse($result);
  99. }
  100. /**
  101. * testDeleteCache method
  102. *
  103. * @return void
  104. */
  105. public function testDeleteCache() {
  106. $data = 'this is a test of the emergency broadcasting system';
  107. $result = Cache::write('delete_test', $data, 'wincache');
  108. $this->assertTrue($result);
  109. $result = Cache::delete('delete_test', 'wincache');
  110. $this->assertTrue($result);
  111. }
  112. /**
  113. * testDecrement method
  114. *
  115. * @return void
  116. */
  117. public function testDecrement() {
  118. $this->skipIf(
  119. !function_exists('wincache_ucache_dec'),
  120. 'No wincache_ucache_dec() function, cannot test decrement().'
  121. );
  122. $result = Cache::write('test_decrement', 5, 'wincache');
  123. $this->assertTrue($result);
  124. $result = Cache::decrement('test_decrement', 1, 'wincache');
  125. $this->assertEquals(4, $result);
  126. $result = Cache::read('test_decrement', 'wincache');
  127. $this->assertEquals(4, $result);
  128. $result = Cache::decrement('test_decrement', 2, 'wincache');
  129. $this->assertEquals(2, $result);
  130. $result = Cache::read('test_decrement', 'wincache');
  131. $this->assertEquals(2, $result);
  132. }
  133. /**
  134. * testIncrement method
  135. *
  136. * @return void
  137. */
  138. public function testIncrement() {
  139. $this->skipIf(
  140. !function_exists('wincache_ucache_inc'),
  141. 'No wincache_inc() function, cannot test increment().'
  142. );
  143. $result = Cache::write('test_increment', 5, 'wincache');
  144. $this->assertTrue($result);
  145. $result = Cache::increment('test_increment', 1, 'wincache');
  146. $this->assertEquals(6, $result);
  147. $result = Cache::read('test_increment', 'wincache');
  148. $this->assertEquals(6, $result);
  149. $result = Cache::increment('test_increment', 2, 'wincache');
  150. $this->assertEquals(8, $result);
  151. $result = Cache::read('test_increment', 'wincache');
  152. $this->assertEquals(8, $result);
  153. }
  154. /**
  155. * test the clearing of cache keys
  156. *
  157. * @return void
  158. */
  159. public function testClear() {
  160. wincache_ucache_set('not_cake', 'safe');
  161. Cache::write('some_value', 'value', 'wincache');
  162. $result = Cache::clear(false, 'wincache');
  163. $this->assertTrue($result);
  164. $this->assertFalse(Cache::read('some_value', 'wincache'));
  165. $this->assertEquals('safe', wincache_ucache_get('not_cake'));
  166. }
  167. /**
  168. * Tests that configuring groups for stored keys return the correct values when read/written
  169. * Shows that altering the group value is equivalent to deleting all keys under the same
  170. * group
  171. *
  172. * @return void
  173. */
  174. public function testGroupsReadWrite() {
  175. Cache::config('wincache_groups', [
  176. 'engine' => 'Wincache',
  177. 'duration' => 0,
  178. 'groups' => ['group_a', 'group_b'],
  179. 'prefix' => 'test_'
  180. ]);
  181. $this->assertTrue(Cache::write('test_groups', 'value', 'wincache_groups'));
  182. $this->assertEquals('value', Cache::read('test_groups', 'wincache_groups'));
  183. wincache_ucache_inc('test_group_a');
  184. $this->assertFalse(Cache::read('test_groups', 'wincache_groups'));
  185. $this->assertTrue(Cache::write('test_groups', 'value2', 'wincache_groups'));
  186. $this->assertEquals('value2', Cache::read('test_groups', 'wincache_groups'));
  187. wincache_ucache_inc('test_group_b');
  188. $this->assertFalse(Cache::read('test_groups', 'wincache_groups'));
  189. $this->assertTrue(Cache::write('test_groups', 'value3', 'wincache_groups'));
  190. $this->assertEquals('value3', Cache::read('test_groups', 'wincache_groups'));
  191. }
  192. /**
  193. * Tests that deleteing from a groups-enabled config is possible
  194. *
  195. * @return void
  196. */
  197. public function testGroupDelete() {
  198. Cache::config('wincache_groups', [
  199. 'engine' => 'Wincache',
  200. 'duration' => 0,
  201. 'groups' => ['group_a', 'group_b'],
  202. 'prefix' => 'test_'
  203. ]);
  204. $this->assertTrue(Cache::write('test_groups', 'value', 'wincache_groups'));
  205. $this->assertEquals('value', Cache::read('test_groups', 'wincache_groups'));
  206. $this->assertTrue(Cache::delete('test_groups', 'wincache_groups'));
  207. $this->assertFalse(Cache::read('test_groups', 'wincache_groups'));
  208. }
  209. /**
  210. * Test clearing a cache group
  211. *
  212. * @return void
  213. */
  214. public function testGroupClear() {
  215. Cache::config('wincache_groups', [
  216. 'engine' => 'Wincache',
  217. 'duration' => 0,
  218. 'groups' => ['group_a', 'group_b'],
  219. 'prefix' => 'test_'
  220. ]);
  221. $this->assertTrue(Cache::write('test_groups', 'value', 'wincache_groups'));
  222. $this->assertTrue(Cache::clearGroup('group_a', 'wincache_groups'));
  223. $this->assertFalse(Cache::read('test_groups', 'wincache_groups'));
  224. $this->assertTrue(Cache::write('test_groups', 'value2', 'wincache_groups'));
  225. $this->assertTrue(Cache::clearGroup('group_b', 'wincache_groups'));
  226. $this->assertFalse(Cache::read('test_groups', 'wincache_groups'));
  227. }
  228. }