RedisEngineTest.php 12 KB

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