RedisEngineTest.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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. 'unix_socket' => false
  84. );
  85. $this->assertEquals($expecting, $config);
  86. }
  87. /**
  88. * testConnect method
  89. *
  90. * @return void
  91. */
  92. public function testConnect() {
  93. $Redis = new RedisEngine();
  94. $this->assertTrue($Redis->init(Cache::engine('redis')->config()));
  95. }
  96. /**
  97. * testMultiDatabaseOperations method
  98. *
  99. * @return void
  100. */
  101. public function testMultiDatabaseOperations() {
  102. Cache::config('redisdb0', array(
  103. 'engine' => 'Redis',
  104. 'prefix' => 'cake2_',
  105. 'duration' => 3600,
  106. 'persistent' => false,
  107. ));
  108. Cache::config('redisdb1', array(
  109. 'engine' => 'Redis',
  110. 'database' => 1,
  111. 'prefix' => 'cake2_',
  112. 'duration' => 3600,
  113. 'persistent' => false,
  114. ));
  115. $result = Cache::write('save_in_0', true, 'redisdb0');
  116. $exist = Cache::read('save_in_0', 'redisdb0');
  117. $this->assertTrue($result);
  118. $this->assertTrue($exist);
  119. $result = Cache::write('save_in_1', true, 'redisdb1');
  120. $this->assertTrue($result);
  121. $exist = Cache::read('save_in_0', 'redisdb1');
  122. $this->assertFalse($exist);
  123. $exist = Cache::read('save_in_1', 'redisdb1');
  124. $this->assertTrue($exist);
  125. Cache::delete('save_in_0', 'redisdb0');
  126. $exist = Cache::read('save_in_0', 'redisdb0');
  127. $this->assertFalse($exist);
  128. Cache::delete('save_in_1', 'redisdb1');
  129. $exist = Cache::read('save_in_1', 'redisdb1');
  130. $this->assertFalse($exist);
  131. Cache::drop('redisdb0');
  132. Cache::drop('redisdb1');
  133. }
  134. /**
  135. * testReadAndWriteCache method
  136. *
  137. * @return void
  138. */
  139. public function testReadAndWriteCache() {
  140. $this->_configCache(['duration' => 1]);
  141. $result = Cache::read('test', 'redis');
  142. $expecting = '';
  143. $this->assertEquals($expecting, $result);
  144. $data = 'this is a test of the emergency broadcasting system';
  145. $result = Cache::write('test', $data, 'redis');
  146. $this->assertTrue($result);
  147. $result = Cache::read('test', 'redis');
  148. $expecting = $data;
  149. $this->assertEquals($expecting, $result);
  150. $data = array(1, 2, 3);
  151. $this->assertTrue(Cache::write('array_data', $data, 'redis'));
  152. $this->assertEquals($data, Cache::read('array_data', 'redis'));
  153. Cache::delete('test', 'redis');
  154. }
  155. /**
  156. * testExpiry method
  157. *
  158. * @return void
  159. */
  160. public function testExpiry() {
  161. $this->_configCache(['duration' => 1]);
  162. $result = Cache::read('test', 'redis');
  163. $this->assertFalse($result);
  164. $data = 'this is a test of the emergency broadcasting system';
  165. $result = Cache::write('other_test', $data, 'redis');
  166. $this->assertTrue($result);
  167. sleep(2);
  168. $result = Cache::read('other_test', 'redis');
  169. $this->assertFalse($result);
  170. $this->_configCache(['duration' => '+1 second']);
  171. $data = 'this is a test of the emergency broadcasting system';
  172. $result = Cache::write('other_test', $data, 'redis');
  173. $this->assertTrue($result);
  174. sleep(2);
  175. $result = Cache::read('other_test', 'redis');
  176. $this->assertFalse($result);
  177. sleep(2);
  178. $result = Cache::read('other_test', 'redis');
  179. $this->assertFalse($result);
  180. $this->_configCache(['duration' => '+29 days']);
  181. $data = 'this is a test of the emergency broadcasting system';
  182. $result = Cache::write('long_expiry_test', $data, 'redis');
  183. $this->assertTrue($result);
  184. sleep(2);
  185. $result = Cache::read('long_expiry_test', 'redis');
  186. $expecting = $data;
  187. $this->assertEquals($expecting, $result);
  188. }
  189. /**
  190. * testDeleteCache method
  191. *
  192. * @return void
  193. */
  194. public function testDeleteCache() {
  195. $data = 'this is a test of the emergency broadcasting system';
  196. $result = Cache::write('delete_test', $data, 'redis');
  197. $this->assertTrue($result);
  198. $result = Cache::delete('delete_test', 'redis');
  199. $this->assertTrue($result);
  200. }
  201. /**
  202. * testDecrement method
  203. *
  204. * @return void
  205. */
  206. public function testDecrement() {
  207. Cache::delete('test_decrement', 'redis');
  208. $result = Cache::write('test_decrement', 5, 'redis');
  209. $this->assertTrue($result);
  210. $result = Cache::decrement('test_decrement', 1, 'redis');
  211. $this->assertEquals(4, $result);
  212. $result = Cache::read('test_decrement', 'redis');
  213. $this->assertEquals(4, $result);
  214. $result = Cache::decrement('test_decrement', 2, 'redis');
  215. $this->assertEquals(2, $result);
  216. $result = Cache::read('test_decrement', 'redis');
  217. $this->assertEquals(2, $result);
  218. }
  219. /**
  220. * testIncrement method
  221. *
  222. * @return void
  223. */
  224. public function testIncrement() {
  225. Cache::delete('test_increment', 'redis');
  226. $result = Cache::increment('test_increment', 1, 'redis');
  227. $this->assertEquals(1, $result);
  228. $result = Cache::read('test_increment', 'redis');
  229. $this->assertEquals(1, $result);
  230. $result = Cache::increment('test_increment', 2, 'redis');
  231. $this->assertEquals(3, $result);
  232. $result = Cache::read('test_increment', 'redis');
  233. $this->assertEquals(3, $result);
  234. }
  235. /**
  236. * test clearing redis.
  237. *
  238. * @return void
  239. */
  240. public function testClear() {
  241. Cache::config('redis2', array(
  242. 'engine' => 'Redis',
  243. 'prefix' => 'cake2_',
  244. 'duration' => 3600
  245. ));
  246. Cache::write('some_value', 'cache1', 'redis');
  247. $result = Cache::clear(true, 'redis');
  248. $this->assertTrue($result);
  249. $this->assertEquals('cache1', Cache::read('some_value', 'redis'));
  250. Cache::write('some_value', 'cache2', 'redis2');
  251. $result = Cache::clear(false, 'redis');
  252. $this->assertTrue($result);
  253. $this->assertFalse(Cache::read('some_value', 'redis'));
  254. $this->assertEquals('cache2', Cache::read('some_value', 'redis2'));
  255. Cache::clear(false, 'redis2');
  256. }
  257. /**
  258. * test that a 0 duration can successfully write.
  259. *
  260. * @return void
  261. */
  262. public function testZeroDuration() {
  263. $this->_configCache(['duration' => 0]);
  264. $result = Cache::write('test_key', 'written!', 'redis');
  265. $this->assertTrue($result);
  266. $result = Cache::read('test_key', 'redis');
  267. $this->assertEquals('written!', $result);
  268. }
  269. /**
  270. * Tests that configuring groups for stored keys return the correct values when read/written
  271. * Shows that altering the group value is equivalent to deleting all keys under the same
  272. * group
  273. *
  274. * @return void
  275. */
  276. public function testGroupReadWrite() {
  277. Cache::config('redis_groups', [
  278. 'engine' => 'Redis',
  279. 'duration' => 3600,
  280. 'groups' => ['group_a', 'group_b'],
  281. 'prefix' => 'test_'
  282. ]);
  283. Cache::config('redis_helper', [
  284. 'engine' => 'Redis',
  285. 'duration' => 3600,
  286. 'prefix' => 'test_'
  287. ]);
  288. $this->assertTrue(Cache::write('test_groups', 'value', 'redis_groups'));
  289. $this->assertEquals('value', Cache::read('test_groups', 'redis_groups'));
  290. Cache::increment('group_a', 1, 'redis_helper');
  291. $this->assertFalse(Cache::read('test_groups', 'redis_groups'));
  292. $this->assertTrue(Cache::write('test_groups', 'value2', 'redis_groups'));
  293. $this->assertEquals('value2', Cache::read('test_groups', 'redis_groups'));
  294. Cache::increment('group_b', 1, 'redis_helper');
  295. $this->assertFalse(Cache::read('test_groups', 'redis_groups'));
  296. $this->assertTrue(Cache::write('test_groups', 'value3', 'redis_groups'));
  297. $this->assertEquals('value3', Cache::read('test_groups', 'redis_groups'));
  298. }
  299. /**
  300. * Tests that deleteing from a groups-enabled config is possible
  301. *
  302. * @return void
  303. */
  304. public function testGroupDelete() {
  305. Cache::config('redis_groups', [
  306. 'engine' => 'Redis',
  307. 'duration' => 3600,
  308. 'groups' => ['group_a', 'group_b']
  309. ]);
  310. $this->assertTrue(Cache::write('test_groups', 'value', 'redis_groups'));
  311. $this->assertEquals('value', Cache::read('test_groups', 'redis_groups'));
  312. $this->assertTrue(Cache::delete('test_groups', 'redis_groups'));
  313. $this->assertFalse(Cache::read('test_groups', 'redis_groups'));
  314. }
  315. /**
  316. * Test clearing a cache group
  317. *
  318. * @return void
  319. */
  320. public function testGroupClear() {
  321. Cache::config('redis_groups', [
  322. 'engine' => 'Redis',
  323. 'duration' => 3600,
  324. 'groups' => ['group_a', 'group_b']
  325. ]);
  326. $this->assertTrue(Cache::write('test_groups', 'value', 'redis_groups'));
  327. $this->assertTrue(Cache::clearGroup('group_a', 'redis_groups'));
  328. $this->assertFalse(Cache::read('test_groups', 'redis_groups'));
  329. $this->assertTrue(Cache::write('test_groups', 'value2', 'redis_groups'));
  330. $this->assertTrue(Cache::clearGroup('group_b', 'redis_groups'));
  331. $this->assertFalse(Cache::read('test_groups', 'redis_groups'));
  332. }
  333. }