RedisEngineTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  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. * test write numbers method
  177. *
  178. * @return void
  179. */
  180. public function testWriteNumbers()
  181. {
  182. $result = Cache::write('test-counter', 1, 'redis');
  183. $this->assertSame(1, Cache::read('test-counter', 'redis'));
  184. $result = Cache::write('test-counter', 0, 'redis');
  185. $this->assertSame(0, Cache::read('test-counter', 'redis'));
  186. $result = Cache::write('test-counter', -1, 'redis');
  187. $this->assertSame(-1, Cache::read('test-counter', 'redis'));
  188. }
  189. /**
  190. * testReadAndWriteCache method
  191. *
  192. * @return void
  193. */
  194. public function testReadAndWriteCache()
  195. {
  196. $this->_configCache(['duration' => 1]);
  197. $result = Cache::read('test', 'redis');
  198. $expecting = '';
  199. $this->assertEquals($expecting, $result);
  200. $data = 'this is a test of the emergency broadcasting system';
  201. $result = Cache::write('test', $data, 'redis');
  202. $this->assertTrue($result);
  203. $result = Cache::read('test', 'redis');
  204. $expecting = $data;
  205. $this->assertEquals($expecting, $result);
  206. $data = [1, 2, 3];
  207. $this->assertTrue(Cache::write('array_data', $data, 'redis'));
  208. $this->assertEquals($data, Cache::read('array_data', 'redis'));
  209. Cache::delete('test', 'redis');
  210. }
  211. /**
  212. * testExpiry method
  213. *
  214. * @return void
  215. */
  216. public function testExpiry()
  217. {
  218. $this->_configCache(['duration' => 1]);
  219. $result = Cache::read('test', 'redis');
  220. $this->assertFalse($result);
  221. $data = 'this is a test of the emergency broadcasting system';
  222. $result = Cache::write('other_test', $data, 'redis');
  223. $this->assertTrue($result);
  224. sleep(2);
  225. $result = Cache::read('other_test', 'redis');
  226. $this->assertFalse($result);
  227. $this->_configCache(['duration' => '+1 second']);
  228. $data = 'this is a test of the emergency broadcasting system';
  229. $result = Cache::write('other_test', $data, 'redis');
  230. $this->assertTrue($result);
  231. sleep(2);
  232. $result = Cache::read('other_test', 'redis');
  233. $this->assertFalse($result);
  234. sleep(2);
  235. $result = Cache::read('other_test', 'redis');
  236. $this->assertFalse($result);
  237. $this->_configCache(['duration' => '+29 days']);
  238. $data = 'this is a test of the emergency broadcasting system';
  239. $result = Cache::write('long_expiry_test', $data, 'redis');
  240. $this->assertTrue($result);
  241. sleep(2);
  242. $result = Cache::read('long_expiry_test', 'redis');
  243. $expecting = $data;
  244. $this->assertEquals($expecting, $result);
  245. }
  246. /**
  247. * testDeleteCache method
  248. *
  249. * @return void
  250. */
  251. public function testDeleteCache()
  252. {
  253. $data = 'this is a test of the emergency broadcasting system';
  254. $result = Cache::write('delete_test', $data, 'redis');
  255. $this->assertTrue($result);
  256. $result = Cache::delete('delete_test', 'redis');
  257. $this->assertTrue($result);
  258. }
  259. /**
  260. * testDecrement method
  261. *
  262. * @return void
  263. */
  264. public function testDecrement()
  265. {
  266. Cache::delete('test_decrement', 'redis');
  267. $result = Cache::write('test_decrement', 5, 'redis');
  268. $this->assertTrue($result);
  269. $result = Cache::decrement('test_decrement', 1, 'redis');
  270. $this->assertEquals(4, $result);
  271. $result = Cache::read('test_decrement', 'redis');
  272. $this->assertEquals(4, $result);
  273. $result = Cache::decrement('test_decrement', 2, 'redis');
  274. $this->assertEquals(2, $result);
  275. $result = Cache::read('test_decrement', 'redis');
  276. $this->assertEquals(2, $result);
  277. }
  278. /**
  279. * testIncrement method
  280. *
  281. * @return void
  282. */
  283. public function testIncrement()
  284. {
  285. Cache::delete('test_increment', 'redis');
  286. $result = Cache::increment('test_increment', 1, 'redis');
  287. $this->assertEquals(1, $result);
  288. $result = Cache::read('test_increment', 'redis');
  289. $this->assertEquals(1, $result);
  290. $result = Cache::increment('test_increment', 2, 'redis');
  291. $this->assertEquals(3, $result);
  292. $result = Cache::read('test_increment', 'redis');
  293. $this->assertEquals(3, $result);
  294. }
  295. /**
  296. * test clearing redis.
  297. *
  298. * @return void
  299. */
  300. public function testClear()
  301. {
  302. Cache::config('redis2', [
  303. 'engine' => 'Redis',
  304. 'prefix' => 'cake2_',
  305. 'duration' => 3600
  306. ]);
  307. Cache::write('some_value', 'cache1', 'redis');
  308. $result = Cache::clear(true, 'redis');
  309. $this->assertTrue($result);
  310. $this->assertEquals('cache1', Cache::read('some_value', 'redis'));
  311. Cache::write('some_value', 'cache2', 'redis2');
  312. $result = Cache::clear(false, 'redis');
  313. $this->assertTrue($result);
  314. $this->assertFalse(Cache::read('some_value', 'redis'));
  315. $this->assertEquals('cache2', Cache::read('some_value', 'redis2'));
  316. Cache::clear(false, 'redis2');
  317. }
  318. /**
  319. * test that a 0 duration can successfully write.
  320. *
  321. * @return void
  322. */
  323. public function testZeroDuration()
  324. {
  325. $this->_configCache(['duration' => 0]);
  326. $result = Cache::write('test_key', 'written!', 'redis');
  327. $this->assertTrue($result);
  328. $result = Cache::read('test_key', 'redis');
  329. $this->assertEquals('written!', $result);
  330. }
  331. /**
  332. * Tests that configuring groups for stored keys return the correct values when read/written
  333. * Shows that altering the group value is equivalent to deleting all keys under the same
  334. * group
  335. *
  336. * @return void
  337. */
  338. public function testGroupReadWrite()
  339. {
  340. Cache::config('redis_groups', [
  341. 'engine' => 'Redis',
  342. 'duration' => 3600,
  343. 'groups' => ['group_a', 'group_b'],
  344. 'prefix' => 'test_'
  345. ]);
  346. Cache::config('redis_helper', [
  347. 'engine' => 'Redis',
  348. 'duration' => 3600,
  349. 'prefix' => 'test_'
  350. ]);
  351. $this->assertTrue(Cache::write('test_groups', 'value', 'redis_groups'));
  352. $this->assertEquals('value', Cache::read('test_groups', 'redis_groups'));
  353. Cache::increment('group_a', 1, 'redis_helper');
  354. $this->assertFalse(Cache::read('test_groups', 'redis_groups'));
  355. $this->assertTrue(Cache::write('test_groups', 'value2', 'redis_groups'));
  356. $this->assertEquals('value2', Cache::read('test_groups', 'redis_groups'));
  357. Cache::increment('group_b', 1, 'redis_helper');
  358. $this->assertFalse(Cache::read('test_groups', 'redis_groups'));
  359. $this->assertTrue(Cache::write('test_groups', 'value3', 'redis_groups'));
  360. $this->assertEquals('value3', Cache::read('test_groups', 'redis_groups'));
  361. }
  362. /**
  363. * Tests that deleteing from a groups-enabled config is possible
  364. *
  365. * @return void
  366. */
  367. public function testGroupDelete()
  368. {
  369. Cache::config('redis_groups', [
  370. 'engine' => 'Redis',
  371. 'duration' => 3600,
  372. 'groups' => ['group_a', 'group_b']
  373. ]);
  374. $this->assertTrue(Cache::write('test_groups', 'value', 'redis_groups'));
  375. $this->assertEquals('value', Cache::read('test_groups', 'redis_groups'));
  376. $this->assertTrue(Cache::delete('test_groups', 'redis_groups'));
  377. $this->assertFalse(Cache::read('test_groups', 'redis_groups'));
  378. }
  379. /**
  380. * Test clearing a cache group
  381. *
  382. * @return void
  383. */
  384. public function testGroupClear()
  385. {
  386. Cache::config('redis_groups', [
  387. 'engine' => 'Redis',
  388. 'duration' => 3600,
  389. 'groups' => ['group_a', 'group_b']
  390. ]);
  391. $this->assertTrue(Cache::write('test_groups', 'value', 'redis_groups'));
  392. $this->assertTrue(Cache::clearGroup('group_a', 'redis_groups'));
  393. $this->assertFalse(Cache::read('test_groups', 'redis_groups'));
  394. $this->assertTrue(Cache::write('test_groups', 'value2', 'redis_groups'));
  395. $this->assertTrue(Cache::clearGroup('group_b', 'redis_groups'));
  396. $this->assertFalse(Cache::read('test_groups', 'redis_groups'));
  397. }
  398. /**
  399. * Test add
  400. *
  401. * @return void
  402. */
  403. public function testAdd()
  404. {
  405. Cache::delete('test_add_key', 'redis');
  406. $result = Cache::add('test_add_key', 'test data', 'redis');
  407. $this->assertTrue($result);
  408. $expected = 'test data';
  409. $result = Cache::read('test_add_key', 'redis');
  410. $this->assertEquals($expected, $result);
  411. $result = Cache::add('test_add_key', 'test data 2', 'redis');
  412. $this->assertFalse($result);
  413. }
  414. }