/lib/Cake/Test/Case/Cache/Engine/WincacheEngineTest.php

https://github.com/kunit/cakephp · PHP · 264 lines · 143 code · 41 blank · 80 comment · 0 complexity · cc9c3433c938deac5912b8313554c15a MD5 · raw file

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