RedisEngineTest.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. <?php
  2. /**
  3. * RedisEngineTest file
  4. *
  5. * CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
  6. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  7. *
  8. * Licensed under The MIT License
  9. * For full copyright and license information, please see the LICENSE.txt
  10. * Redistributions of files must retain the above copyright notice
  11. *
  12. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  13. * @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
  14. * @since 2.2.0
  15. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  16. */
  17. namespace Cake\Test\TestCase\Cache\Engine;
  18. use Cake\Cache\Cache;
  19. use Cake\Cache\Engine\RedisEngine;
  20. use Cake\Core\Configure;
  21. use Cake\TestSuite\TestCase;
  22. /**
  23. * RedisEngineTest class
  24. *
  25. */
  26. class RedisEngineTest extends TestCase {
  27. /**
  28. * setUp method
  29. *
  30. * @return void
  31. */
  32. public function setUp() {
  33. $this->assertFalse(defined('HHVM_VERSION'), 'Crashes HHVM');
  34. parent::setUp();
  35. $this->skipIf(!class_exists('Redis'), 'Redis 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('redis');
  47. Cache::drop('redis_groups');
  48. Cache::drop('redis_helper');
  49. }
  50. /**
  51. * Helper method for testing.
  52. *
  53. * @param array $config
  54. * @return void
  55. */
  56. protected function _configCache($config = []) {
  57. $defaults = [
  58. 'className' => 'Redis',
  59. 'prefix' => 'cake_',
  60. 'duration' => 3600
  61. ];
  62. Cache::drop('redis');
  63. Cache::config('redis', array_merge($defaults, $config));
  64. }
  65. /**
  66. * testConfig method
  67. *
  68. * @return void
  69. */
  70. public function testConfig() {
  71. $config = Cache::engine('redis')->config();
  72. $expecting = array(
  73. 'prefix' => 'cake_',
  74. 'duration' => 3600,
  75. 'probability' => 100,
  76. 'groups' => array(),
  77. 'server' => '127.0.0.1',
  78. 'port' => 6379,
  79. 'timeout' => 0,
  80. 'persistent' => true,
  81. 'password' => false,
  82. 'database' => 0,
  83. );
  84. $this->assertEquals($expecting, $config);
  85. }
  86. /**
  87. * testConnect method
  88. *
  89. * @return void
  90. */
  91. public function testConnect() {
  92. $Redis = new RedisEngine();
  93. $this->assertTrue($Redis->init(Cache::engine('redis')->config()));
  94. }
  95. /**
  96. * testMultiDatabaseOperations method
  97. *
  98. * @return void
  99. */
  100. public function testMultiDatabaseOperations() {
  101. Cache::config('redisdb0', array(
  102. 'engine' => 'Redis',
  103. 'prefix' => 'cake2_',
  104. 'duration' => 3600,
  105. 'persistent' => false,
  106. ));
  107. Cache::config('redisdb1', array(
  108. 'engine' => 'Redis',
  109. 'database' => 1,
  110. 'prefix' => 'cake2_',
  111. 'duration' => 3600,
  112. 'persistent' => false,
  113. ));
  114. $result = Cache::write('save_in_0', true, 'redisdb0');
  115. $exist = Cache::read('save_in_0', 'redisdb0');
  116. $this->assertTrue($result);
  117. $this->assertTrue($exist);
  118. $result = Cache::write('save_in_1', true, 'redisdb1');
  119. $this->assertTrue($result);
  120. $exist = Cache::read('save_in_0', 'redisdb1');
  121. $this->assertFalse($exist);
  122. $exist = Cache::read('save_in_1', 'redisdb1');
  123. $this->assertTrue($exist);
  124. Cache::delete('save_in_0', 'redisdb0');
  125. $exist = Cache::read('save_in_0', 'redisdb0');
  126. $this->assertFalse($exist);
  127. Cache::delete('save_in_1', 'redisdb1');
  128. $exist = Cache::read('save_in_1', 'redisdb1');
  129. $this->assertFalse($exist);
  130. Cache::drop('redisdb0');
  131. Cache::drop('redisdb1');
  132. }
  133. /**
  134. * testReadAndWriteCache method
  135. *
  136. * @return void
  137. */
  138. public function testReadAndWriteCache() {
  139. $this->_configCache(['duration' => 1]);
  140. $result = Cache::read('test', 'redis');
  141. $expecting = '';
  142. $this->assertEquals($expecting, $result);
  143. $data = 'this is a test of the emergency broadcasting system';
  144. $result = Cache::write('test', $data, 'redis');
  145. $this->assertTrue($result);
  146. $result = Cache::read('test', 'redis');
  147. $expecting = $data;
  148. $this->assertEquals($expecting, $result);
  149. $data = array(1, 2, 3);
  150. $this->assertTrue(Cache::write('array_data', $data, 'redis'));
  151. $this->assertEquals($data, Cache::read('array_data', 'redis'));
  152. Cache::delete('test', 'redis');
  153. }
  154. /**
  155. * testExpiry method
  156. *
  157. * @return void
  158. */
  159. public function testExpiry() {
  160. $this->_configCache(['duration' => 1]);
  161. $result = Cache::read('test', 'redis');
  162. $this->assertFalse($result);
  163. $data = 'this is a test of the emergency broadcasting system';
  164. $result = Cache::write('other_test', $data, 'redis');
  165. $this->assertTrue($result);
  166. sleep(2);
  167. $result = Cache::read('other_test', 'redis');
  168. $this->assertFalse($result);
  169. $this->_configCache(['duration' => '+1 second']);
  170. $data = 'this is a test of the emergency broadcasting system';
  171. $result = Cache::write('other_test', $data, 'redis');
  172. $this->assertTrue($result);
  173. sleep(2);
  174. $result = Cache::read('other_test', 'redis');
  175. $this->assertFalse($result);
  176. sleep(2);
  177. $result = Cache::read('other_test', 'redis');
  178. $this->assertFalse($result);
  179. $this->_configCache(['duration' => '+29 days']);
  180. $data = 'this is a test of the emergency broadcasting system';
  181. $result = Cache::write('long_expiry_test', $data, 'redis');
  182. $this->assertTrue($result);
  183. sleep(2);
  184. $result = Cache::read('long_expiry_test', 'redis');
  185. $expecting = $data;
  186. $this->assertEquals($expecting, $result);
  187. }
  188. /**
  189. * testDeleteCache method
  190. *
  191. * @return void
  192. */
  193. public function testDeleteCache() {
  194. $data = 'this is a test of the emergency broadcasting system';
  195. $result = Cache::write('delete_test', $data, 'redis');
  196. $this->assertTrue($result);
  197. $result = Cache::delete('delete_test', 'redis');
  198. $this->assertTrue($result);
  199. }
  200. /**
  201. * testDecrement method
  202. *
  203. * @return void
  204. */
  205. public function testDecrement() {
  206. Cache::delete('test_decrement', 'redis');
  207. $result = Cache::write('test_decrement', 5, 'redis');
  208. $this->assertTrue($result);
  209. $result = Cache::decrement('test_decrement', 1, 'redis');
  210. $this->assertEquals(4, $result);
  211. $result = Cache::read('test_decrement', 'redis');
  212. $this->assertEquals(4, $result);
  213. $result = Cache::decrement('test_decrement', 2, 'redis');
  214. $this->assertEquals(2, $result);
  215. $result = Cache::read('test_decrement', 'redis');
  216. $this->assertEquals(2, $result);
  217. }
  218. /**
  219. * testIncrement method
  220. *
  221. * @return void
  222. */
  223. public function testIncrement() {
  224. Cache::delete('test_increment', 'redis');
  225. $result = Cache::increment('test_increment', 1, 'redis');
  226. $this->assertEquals(1, $result);
  227. $result = Cache::read('test_increment', 'redis');
  228. $this->assertEquals(1, $result);
  229. $result = Cache::increment('test_increment', 2, 'redis');
  230. $this->assertEquals(3, $result);
  231. $result = Cache::read('test_increment', 'redis');
  232. $this->assertEquals(3, $result);
  233. }
  234. /**
  235. * test clearing redis.
  236. *
  237. * @return void
  238. */
  239. public function testClear() {
  240. Cache::config('redis2', array(
  241. 'engine' => 'Redis',
  242. 'prefix' => 'cake2_',
  243. 'duration' => 3600
  244. ));
  245. Cache::write('some_value', 'cache1', 'redis');
  246. $result = Cache::clear(true, 'redis');
  247. $this->assertTrue($result);
  248. $this->assertEquals('cache1', Cache::read('some_value', 'redis'));
  249. Cache::write('some_value', 'cache2', 'redis2');
  250. $result = Cache::clear(false, 'redis');
  251. $this->assertTrue($result);
  252. $this->assertFalse(Cache::read('some_value', 'redis'));
  253. $this->assertEquals('cache2', Cache::read('some_value', 'redis2'));
  254. Cache::clear(false, 'redis2');
  255. }
  256. /**
  257. * test that a 0 duration can successfully write.
  258. *
  259. * @return void
  260. */
  261. public function testZeroDuration() {
  262. $this->_configCache(['duration' => 0]);
  263. $result = Cache::write('test_key', 'written!', 'redis');
  264. $this->assertTrue($result);
  265. $result = Cache::read('test_key', 'redis');
  266. $this->assertEquals('written!', $result);
  267. }
  268. /**
  269. * Tests that configuring groups for stored keys return the correct values when read/written
  270. * Shows that altering the group value is equivalent to deleting all keys under the same
  271. * group
  272. *
  273. * @return void
  274. */
  275. public function testGroupReadWrite() {
  276. Cache::config('redis_groups', [
  277. 'engine' => 'Redis',
  278. 'duration' => 3600,
  279. 'groups' => ['group_a', 'group_b'],
  280. 'prefix' => 'test_'
  281. ]);
  282. Cache::config('redis_helper', [
  283. 'engine' => 'Redis',
  284. 'duration' => 3600,
  285. 'prefix' => 'test_'
  286. ]);
  287. $this->assertTrue(Cache::write('test_groups', 'value', 'redis_groups'));
  288. $this->assertEquals('value', Cache::read('test_groups', 'redis_groups'));
  289. Cache::increment('group_a', 1, 'redis_helper');
  290. $this->assertFalse(Cache::read('test_groups', 'redis_groups'));
  291. $this->assertTrue(Cache::write('test_groups', 'value2', 'redis_groups'));
  292. $this->assertEquals('value2', Cache::read('test_groups', 'redis_groups'));
  293. Cache::increment('group_b', 1, 'redis_helper');
  294. $this->assertFalse(Cache::read('test_groups', 'redis_groups'));
  295. $this->assertTrue(Cache::write('test_groups', 'value3', 'redis_groups'));
  296. $this->assertEquals('value3', Cache::read('test_groups', 'redis_groups'));
  297. }
  298. /**
  299. * Tests that deleteing from a groups-enabled config is possible
  300. *
  301. * @return void
  302. */
  303. public function testGroupDelete() {
  304. Cache::config('redis_groups', [
  305. 'engine' => 'Redis',
  306. 'duration' => 3600,
  307. 'groups' => ['group_a', 'group_b']
  308. ]);
  309. $this->assertTrue(Cache::write('test_groups', 'value', 'redis_groups'));
  310. $this->assertEquals('value', Cache::read('test_groups', 'redis_groups'));
  311. $this->assertTrue(Cache::delete('test_groups', 'redis_groups'));
  312. $this->assertFalse(Cache::read('test_groups', 'redis_groups'));
  313. }
  314. /**
  315. * Test clearing a cache group
  316. *
  317. * @return void
  318. */
  319. public function testGroupClear() {
  320. Cache::config('redis_groups', [
  321. 'engine' => 'Redis',
  322. 'duration' => 3600,
  323. 'groups' => ['group_a', 'group_b']
  324. ]);
  325. $this->assertTrue(Cache::write('test_groups', 'value', 'redis_groups'));
  326. $this->assertTrue(Cache::clearGroup('group_a', 'redis_groups'));
  327. $this->assertFalse(Cache::read('test_groups', 'redis_groups'));
  328. $this->assertTrue(Cache::write('test_groups', 'value2', 'redis_groups'));
  329. $this->assertTrue(Cache::clearGroup('group_b', 'redis_groups'));
  330. $this->assertFalse(Cache::read('test_groups', 'redis_groups'));
  331. }
  332. }