PageRenderTime 49ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/tests/TestCase/Cache/Engine/WincacheEngineTest.php

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