RedisEngineTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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. /**
  29. * setUp method
  30. *
  31. * @return void
  32. */
  33. public function setUp()
  34. {
  35. parent::setUp();
  36. $this->skipIf(!class_exists('Redis'), 'Redis extension is not installed or configured properly.');
  37. // @codingStandardsIgnoreStart
  38. $socket = @fsockopen('127.0.0.1', 6379, $errno, $errstr, 1);
  39. // @codingStandardsIgnoreEnd
  40. $this->skipIf(!$socket, 'Redis is not running.');
  41. fclose($socket);
  42. Cache::enable();
  43. $this->_configCache();
  44. }
  45. /**
  46. * tearDown method
  47. *
  48. * @return void
  49. */
  50. public function tearDown()
  51. {
  52. parent::tearDown();
  53. Cache::drop('redis');
  54. Cache::drop('redis_groups');
  55. Cache::drop('redis_helper');
  56. }
  57. /**
  58. * Helper method for testing.
  59. *
  60. * @param array $config
  61. * @return void
  62. */
  63. protected function _configCache($config = [])
  64. {
  65. $defaults = [
  66. 'className' => 'Redis',
  67. 'prefix' => 'cake_',
  68. 'duration' => 3600
  69. ];
  70. Cache::drop('redis');
  71. Cache::config('redis', array_merge($defaults, $config));
  72. }
  73. /**
  74. * testConfig method
  75. *
  76. * @return void
  77. */
  78. public function testConfig()
  79. {
  80. $config = Cache::engine('redis')->config();
  81. $expecting = [
  82. 'prefix' => 'cake_',
  83. 'duration' => 3600,
  84. 'probability' => 100,
  85. 'groups' => [],
  86. 'server' => '127.0.0.1',
  87. 'port' => 6379,
  88. 'timeout' => 0,
  89. 'persistent' => true,
  90. 'password' => false,
  91. 'database' => 0,
  92. 'unix_socket' => false,
  93. 'host' => null,
  94. ];
  95. $this->assertEquals($expecting, $config);
  96. }
  97. /**
  98. * testConfigDsn method
  99. *
  100. * @return void
  101. */
  102. public function testConfigDsn()
  103. {
  104. Cache::config('redis_dsn', [
  105. 'url' => 'redis://localhost:6379?database=1&prefix=redis_'
  106. ]);
  107. $config = Cache::engine('redis_dsn')->config();
  108. $expecting = [
  109. 'prefix' => 'redis_',
  110. 'duration' => 3600,
  111. 'probability' => 100,
  112. 'groups' => [],
  113. 'server' => 'localhost',
  114. 'port' => 6379,
  115. 'timeout' => 0,
  116. 'persistent' => true,
  117. 'password' => false,
  118. 'database' => '1',
  119. 'unix_socket' => false,
  120. 'host' => 'localhost',
  121. 'scheme' => 'redis',
  122. ];
  123. $this->assertEquals($expecting, $config);
  124. Cache::drop('redis_dsn');
  125. }
  126. /**
  127. * testConnect method
  128. *
  129. * @return void
  130. */
  131. public function testConnect()
  132. {
  133. $Redis = new RedisEngine();
  134. $this->assertTrue($Redis->init(Cache::engine('redis')->config()));
  135. }
  136. /**
  137. * testMultiDatabaseOperations method
  138. *
  139. * @return void
  140. */
  141. public function testMultiDatabaseOperations()
  142. {
  143. Cache::config('redisdb0', [
  144. 'engine' => 'Redis',
  145. 'prefix' => 'cake2_',
  146. 'duration' => 3600,
  147. 'persistent' => false,
  148. ]);
  149. Cache::config('redisdb1', [
  150. 'engine' => 'Redis',
  151. 'database' => 1,
  152. 'prefix' => 'cake2_',
  153. 'duration' => 3600,
  154. 'persistent' => false,
  155. ]);
  156. $result = Cache::write('save_in_0', true, 'redisdb0');
  157. $exist = Cache::read('save_in_0', 'redisdb0');
  158. $this->assertTrue($result);
  159. $this->assertTrue($exist);
  160. $result = Cache::write('save_in_1', true, 'redisdb1');
  161. $this->assertTrue($result);
  162. $exist = Cache::read('save_in_0', 'redisdb1');
  163. $this->assertFalse($exist);
  164. $exist = Cache::read('save_in_1', 'redisdb1');
  165. $this->assertTrue($exist);
  166. Cache::delete('save_in_0', 'redisdb0');
  167. $exist = Cache::read('save_in_0', 'redisdb0');
  168. $this->assertFalse($exist);
  169. Cache::delete('save_in_1', 'redisdb1');
  170. $exist = Cache::read('save_in_1', 'redisdb1');
  171. $this->assertFalse($exist);
  172. Cache::drop('redisdb0');
  173. Cache::drop('redisdb1');
  174. }
  175. /**
  176. * testReadAndWriteCache method
  177. *
  178. * @return void
  179. */
  180. public function testReadAndWriteCache()
  181. {
  182. $this->_configCache(['duration' => 1]);
  183. $result = Cache::read('test', 'redis');
  184. $expecting = '';
  185. $this->assertEquals($expecting, $result);
  186. $data = 'this is a test of the emergency broadcasting system';
  187. $result = Cache::write('test', $data, 'redis');
  188. $this->assertTrue($result);
  189. $result = Cache::read('test', 'redis');
  190. $expecting = $data;
  191. $this->assertEquals($expecting, $result);
  192. $data = [1, 2, 3];
  193. $this->assertTrue(Cache::write('array_data', $data, 'redis'));
  194. $this->assertEquals($data, Cache::read('array_data', 'redis'));
  195. Cache::delete('test', 'redis');
  196. }
  197. /**
  198. * testExpiry method
  199. *
  200. * @return void
  201. */
  202. public function testExpiry()
  203. {
  204. $this->_configCache(['duration' => 1]);
  205. $result = Cache::read('test', 'redis');
  206. $this->assertFalse($result);
  207. $data = 'this is a test of the emergency broadcasting system';
  208. $result = Cache::write('other_test', $data, 'redis');
  209. $this->assertTrue($result);
  210. sleep(2);
  211. $result = Cache::read('other_test', 'redis');
  212. $this->assertFalse($result);
  213. $this->_configCache(['duration' => '+1 second']);
  214. $data = 'this is a test of the emergency broadcasting system';
  215. $result = Cache::write('other_test', $data, 'redis');
  216. $this->assertTrue($result);
  217. sleep(2);
  218. $result = Cache::read('other_test', 'redis');
  219. $this->assertFalse($result);
  220. sleep(2);
  221. $result = Cache::read('other_test', 'redis');
  222. $this->assertFalse($result);
  223. $this->_configCache(['duration' => '+29 days']);
  224. $data = 'this is a test of the emergency broadcasting system';
  225. $result = Cache::write('long_expiry_test', $data, 'redis');
  226. $this->assertTrue($result);
  227. sleep(2);
  228. $result = Cache::read('long_expiry_test', 'redis');
  229. $expecting = $data;
  230. $this->assertEquals($expecting, $result);
  231. }
  232. /**
  233. * testDeleteCache method
  234. *
  235. * @return void
  236. */
  237. public function testDeleteCache()
  238. {
  239. $data = 'this is a test of the emergency broadcasting system';
  240. $result = Cache::write('delete_test', $data, 'redis');
  241. $this->assertTrue($result);
  242. $result = Cache::delete('delete_test', 'redis');
  243. $this->assertTrue($result);
  244. }
  245. /**
  246. * testDecrement method
  247. *
  248. * @return void
  249. */
  250. public function testDecrement()
  251. {
  252. Cache::delete('test_decrement', 'redis');
  253. $result = Cache::write('test_decrement', 5, 'redis');
  254. $this->assertTrue($result);
  255. $result = Cache::decrement('test_decrement', 1, 'redis');
  256. $this->assertEquals(4, $result);
  257. $result = Cache::read('test_decrement', 'redis');
  258. $this->assertEquals(4, $result);
  259. $result = Cache::decrement('test_decrement', 2, 'redis');
  260. $this->assertEquals(2, $result);
  261. $result = Cache::read('test_decrement', 'redis');
  262. $this->assertEquals(2, $result);
  263. }
  264. /**
  265. * testIncrement method
  266. *
  267. * @return void
  268. */
  269. public function testIncrement()
  270. {
  271. Cache::delete('test_increment', 'redis');
  272. $result = Cache::increment('test_increment', 1, 'redis');
  273. $this->assertEquals(1, $result);
  274. $result = Cache::read('test_increment', 'redis');
  275. $this->assertEquals(1, $result);
  276. $result = Cache::increment('test_increment', 2, 'redis');
  277. $this->assertEquals(3, $result);
  278. $result = Cache::read('test_increment', 'redis');
  279. $this->assertEquals(3, $result);
  280. }
  281. /**
  282. * test clearing redis.
  283. *
  284. * @return void
  285. */
  286. public function testClear()
  287. {
  288. Cache::config('redis2', [
  289. 'engine' => 'Redis',
  290. 'prefix' => 'cake2_',
  291. 'duration' => 3600
  292. ]);
  293. Cache::write('some_value', 'cache1', 'redis');
  294. $result = Cache::clear(true, 'redis');
  295. $this->assertTrue($result);
  296. $this->assertEquals('cache1', Cache::read('some_value', 'redis'));
  297. Cache::write('some_value', 'cache2', 'redis2');
  298. $result = Cache::clear(false, 'redis');
  299. $this->assertTrue($result);
  300. $this->assertFalse(Cache::read('some_value', 'redis'));
  301. $this->assertEquals('cache2', Cache::read('some_value', 'redis2'));
  302. Cache::clear(false, 'redis2');
  303. }
  304. /**
  305. * test that a 0 duration can successfully write.
  306. *
  307. * @return void
  308. */
  309. public function testZeroDuration()
  310. {
  311. $this->_configCache(['duration' => 0]);
  312. $result = Cache::write('test_key', 'written!', 'redis');
  313. $this->assertTrue($result);
  314. $result = Cache::read('test_key', 'redis');
  315. $this->assertEquals('written!', $result);
  316. }
  317. /**
  318. * Tests that configuring groups for stored keys return the correct values when read/written
  319. * Shows that altering the group value is equivalent to deleting all keys under the same
  320. * group
  321. *
  322. * @return void
  323. */
  324. public function testGroupReadWrite()
  325. {
  326. Cache::config('redis_groups', [
  327. 'engine' => 'Redis',
  328. 'duration' => 3600,
  329. 'groups' => ['group_a', 'group_b'],
  330. 'prefix' => 'test_'
  331. ]);
  332. Cache::config('redis_helper', [
  333. 'engine' => 'Redis',
  334. 'duration' => 3600,
  335. 'prefix' => 'test_'
  336. ]);
  337. $this->assertTrue(Cache::write('test_groups', 'value', 'redis_groups'));
  338. $this->assertEquals('value', Cache::read('test_groups', 'redis_groups'));
  339. Cache::increment('group_a', 1, 'redis_helper');
  340. $this->assertFalse(Cache::read('test_groups', 'redis_groups'));
  341. $this->assertTrue(Cache::write('test_groups', 'value2', 'redis_groups'));
  342. $this->assertEquals('value2', Cache::read('test_groups', 'redis_groups'));
  343. Cache::increment('group_b', 1, 'redis_helper');
  344. $this->assertFalse(Cache::read('test_groups', 'redis_groups'));
  345. $this->assertTrue(Cache::write('test_groups', 'value3', 'redis_groups'));
  346. $this->assertEquals('value3', Cache::read('test_groups', 'redis_groups'));
  347. }
  348. /**
  349. * Tests that deleteing from a groups-enabled config is possible
  350. *
  351. * @return void
  352. */
  353. public function testGroupDelete()
  354. {
  355. Cache::config('redis_groups', [
  356. 'engine' => 'Redis',
  357. 'duration' => 3600,
  358. 'groups' => ['group_a', 'group_b']
  359. ]);
  360. $this->assertTrue(Cache::write('test_groups', 'value', 'redis_groups'));
  361. $this->assertEquals('value', Cache::read('test_groups', 'redis_groups'));
  362. $this->assertTrue(Cache::delete('test_groups', 'redis_groups'));
  363. $this->assertFalse(Cache::read('test_groups', 'redis_groups'));
  364. }
  365. /**
  366. * Test clearing a cache group
  367. *
  368. * @return void
  369. */
  370. public function testGroupClear()
  371. {
  372. Cache::config('redis_groups', [
  373. 'engine' => 'Redis',
  374. 'duration' => 3600,
  375. 'groups' => ['group_a', 'group_b']
  376. ]);
  377. $this->assertTrue(Cache::write('test_groups', 'value', 'redis_groups'));
  378. $this->assertTrue(Cache::clearGroup('group_a', 'redis_groups'));
  379. $this->assertFalse(Cache::read('test_groups', 'redis_groups'));
  380. $this->assertTrue(Cache::write('test_groups', 'value2', 'redis_groups'));
  381. $this->assertTrue(Cache::clearGroup('group_b', 'redis_groups'));
  382. $this->assertFalse(Cache::read('test_groups', 'redis_groups'));
  383. }
  384. /**
  385. * Test add
  386. *
  387. * @return void
  388. */
  389. public function testAdd()
  390. {
  391. Cache::delete('test_add_key', 'redis');
  392. $result = Cache::add('test_add_key', 'test data', 'redis');
  393. $this->assertTrue($result);
  394. $expected = 'test data';
  395. $result = Cache::read('test_add_key', 'redis');
  396. $this->assertEquals($expected, $result);
  397. $result = Cache::add('test_add_key', 'test data 2', 'redis');
  398. $this->assertFalse($result);
  399. }
  400. }