CredisTest.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751
  1. <?php
  2. require_once dirname(__FILE__).'/../Client.php';
  3. require_once dirname(__FILE__).'/CredisTestCommon.php';
  4. class CredisTest extends CredisTestCommon
  5. {
  6. /** @var Credis_Client */
  7. protected $credis;
  8. protected function setUp()
  9. {
  10. parent::setUp();
  11. $this->credis = new Credis_Client($this->redisConfig[0]['host'], $this->redisConfig[0]['port'], $this->redisConfig[0]['timeout']);
  12. if($this->useStandalone) {
  13. $this->credis->forceStandalone();
  14. }
  15. $this->credis->flushDb();
  16. }
  17. protected function tearDown()
  18. {
  19. if($this->credis) {
  20. $this->credis->close();
  21. $this->credis = NULL;
  22. }
  23. }
  24. public function testFlush()
  25. {
  26. $this->credis->set('foo','FOO');
  27. $this->assertTrue($this->credis->flushDb());
  28. $this->assertFalse($this->credis->get('foo'));
  29. }
  30. public function testReadTimeout()
  31. {
  32. $this->credis->setReadTimeout(0.0001);
  33. try {
  34. $this->credis->save();
  35. $this->fail('Expected exception (read should timeout since disk sync should take longer than 0.0001 seconds).');
  36. } catch(CredisException $e) {
  37. }
  38. $this->credis->setReadTimeout(10);
  39. $this->assertTrue(true);
  40. }
  41. public function testPHPRedisReadTimeout()
  42. {
  43. try {
  44. $this->credis->setReadTimeout(-1);
  45. } catch(CredisException $e) {
  46. $this->fail('setReadTimeout should accept -1 as timeout value');
  47. }
  48. try {
  49. $this->credis->setReadTimeout(-2);
  50. $this->fail('setReadTimeout should not accept values less than -1');
  51. } catch(CredisException $e) {
  52. }
  53. $this->assertTrue(true);
  54. }
  55. public function testScalars()
  56. {
  57. // Basic get/set
  58. $this->credis->set('foo','FOO');
  59. $this->assertEquals('FOO', $this->credis->get('foo'));
  60. $this->assertFalse($this->credis->get('nil'));
  61. // exists support
  62. $this->assertEquals($this->credis->exists('foo'), 1);
  63. $this->assertEquals($this->credis->exists('nil'), 0);
  64. // Empty string
  65. $this->credis->set('empty','');
  66. $this->assertEquals('', $this->credis->get('empty'));
  67. // UTF-8 characters
  68. $utf8str = str_repeat("quarter: ¼, micro: µ, thorn: Þ, ", 500);
  69. $this->credis->set('utf8',$utf8str);
  70. $this->assertEquals($utf8str, $this->credis->get('utf8'));
  71. // Array
  72. $this->assertTrue($this->credis->mSet(array('bar' => 'BAR', 'apple' => 'red')));
  73. $mGet = $this->credis->mGet(array('foo','bar','empty'));
  74. $this->assertTrue(in_array('FOO', $mGet));
  75. $this->assertTrue(in_array('BAR', $mGet));
  76. $this->assertTrue(in_array('', $mGet));
  77. // Non-array
  78. $mGet = $this->credis->mGet('foo','bar');
  79. $this->assertTrue(in_array('FOO', $mGet));
  80. $this->assertTrue(in_array('BAR', $mGet));
  81. // Delete strings, null response
  82. $this->assertEquals(2, $this->credis->del('foo','bar'));
  83. $this->assertFalse($this->credis->get('foo'));
  84. $this->assertFalse($this->credis->get('bar'));
  85. // Long string
  86. $longString = str_repeat(md5('asd'), 4096); // 128k (redis.h REDIS_INLINE_MAX_SIZE = 64k)
  87. $this->assertTrue($this->credis->set('long', $longString));
  88. $this->assertEquals($longString, $this->credis->get('long'));
  89. }
  90. public function testSets()
  91. {
  92. // Multiple arguments
  93. $this->assertEquals(2, $this->credis->sAdd('myset', 'Hello', 'World'));
  94. // Array Arguments
  95. $this->assertEquals(1, $this->credis->sAdd('myset', array('Hello','Cruel','World')));
  96. // Non-empty set
  97. $members = $this->credis->sMembers('myset');
  98. $this->assertEquals(3, count($members));
  99. $this->assertTrue(in_array('Hello', $members));
  100. // Empty set
  101. $this->assertEquals(array(), $this->credis->sMembers('noexist'));
  102. }
  103. public function testSortedSets()
  104. {
  105. $this->assertEquals(1, $this->credis->zAdd('myset', 1, 'Hello'));
  106. $this->assertEquals(1, $this->credis->zAdd('myset', 2.123, 'World'));
  107. $this->assertEquals(1, $this->credis->zAdd('myset', 10, 'And'));
  108. $this->assertEquals(1, $this->credis->zAdd('myset', 11, 'Goodbye'));
  109. $this->assertEquals(4, count($this->credis->zRange('myset', 0, 4)));
  110. $this->assertEquals(2, count($this->credis->zRange('myset', 0, 1)));
  111. $range = $this->credis->zRange('myset', 1, 2);
  112. $this->assertEquals(2, count($range));
  113. $this->assertEquals('World', $range[0]);
  114. $this->assertEquals('And', $range[1]);
  115. $range = $this->credis->zRange('myset', 1, 2, array('withscores' => true));
  116. $this->assertEquals(2, count($range));
  117. $this->assertTrue(array_key_exists('World', $range));
  118. $this->assertEquals(2.123, $range['World']);
  119. $this->assertTrue(array_key_exists('And', $range));
  120. $this->assertEquals(10, $range['And']);
  121. // withscores-option is off
  122. $range = $this->credis->zRange('myset', 0, 4, array('withscores'));
  123. $this->assertEquals(4, count($range));
  124. $this->assertEquals(range(0, 3), array_keys($range)); // expecting numeric array without scores
  125. $range = $this->credis->zRange('myset', 0, 4, array('withscores' => false));
  126. $this->assertEquals(4, count($range));
  127. $this->assertEquals(range(0, 3), array_keys($range));
  128. $this->assertEquals(4, count($this->credis->zRevRange('myset', 0, 4)));
  129. $this->assertEquals(2, count($this->credis->zRevRange('myset', 0, 1)));
  130. $range = $this->credis->zRevRange('myset', 0, 1, array('withscores' => true));
  131. $this->assertEquals(2, count($range));
  132. $this->assertTrue(array_key_exists('And', $range));
  133. $this->assertEquals(10, $range['And']);
  134. $this->assertTrue(array_key_exists('Goodbye', $range));
  135. $this->assertEquals(11, $range['Goodbye']);
  136. // withscores-option is off
  137. $range = $this->credis->zRevRange('myset', 0, 4, array('withscores'));
  138. $this->assertEquals(4, count($range));
  139. $this->assertEquals(range(0, 3), array_keys($range)); // expecting numeric array without scores
  140. $range = $this->credis->zRevRange('myset', 0, 4, array('withscores' => false));
  141. $this->assertEquals(4, count($range));
  142. $this->assertEquals(range(0, 3), array_keys($range));
  143. $this->assertEquals(4, count($this->credis->zRangeByScore('myset', '-inf', '+inf')));
  144. $this->assertEquals(2, count($this->credis->zRangeByScore('myset', '1', '9')));
  145. $range = $this->credis->zRangeByScore('myset', '-inf', '+inf', array('limit' => array(1, 2)));
  146. $this->assertEquals(2, count($range));
  147. $this->assertEquals('World', $range[0]);
  148. $this->assertEquals('And', $range[1]);
  149. $range = $this->credis->zRangeByScore('myset', '-inf', '+inf', array('withscores' => true, 'limit' => array(1, 2)));
  150. $this->assertEquals(2, count($range));
  151. $this->assertTrue(array_key_exists('World', $range));
  152. $this->assertEquals(2.123, $range['World']);
  153. $this->assertTrue(array_key_exists('And', $range));
  154. $this->assertEquals(10, $range['And']);
  155. $range = $this->credis->zRangeByScore('myset', 10, '+inf', array('withscores' => true));
  156. $this->assertEquals(2, count($range));
  157. $this->assertTrue(array_key_exists('And', $range));
  158. $this->assertEquals(10, $range['And']);
  159. $this->assertTrue(array_key_exists('Goodbye', $range));
  160. $this->assertEquals(11, $range['Goodbye']);
  161. // withscores-option is off
  162. $range = $this->credis->zRangeByScore('myset', '-inf', '+inf', array('withscores'));
  163. $this->assertEquals(4, count($range));
  164. $this->assertEquals(range(0, 3), array_keys($range)); // expecting numeric array without scores
  165. $range = $this->credis->zRangeByScore('myset', '-inf', '+inf', array('withscores' => false));
  166. $this->assertEquals(4, count($range));
  167. $this->assertEquals(range(0, 3), array_keys($range));
  168. $this->assertEquals(4, count($this->credis->zRevRangeByScore('myset', '+inf', '-inf')));
  169. $this->assertEquals(2, count($this->credis->zRevRangeByScore('myset', '9', '1')));
  170. $range = $this->credis->zRevRangeByScore('myset', '+inf', '-inf', array('limit' => array(1, 2)));
  171. $this->assertEquals(2, count($range));
  172. $this->assertEquals('World', $range[1]);
  173. $this->assertEquals('And', $range[0]);
  174. $range = $this->credis->zRevRangeByScore('myset', '+inf', '-inf', array('withscores' => true, 'limit' => array(1, 2)));
  175. $this->assertEquals(2, count($range));
  176. $this->assertTrue(array_key_exists('World', $range));
  177. $this->assertEquals(2.123, $range['World']);
  178. $this->assertTrue(array_key_exists('And', $range));
  179. $this->assertEquals(10, $range['And']);
  180. $range = $this->credis->zRevRangeByScore('myset', '+inf',10, array('withscores' => true));
  181. $this->assertEquals(2, count($range));
  182. $this->assertTrue(array_key_exists('And', $range));
  183. $this->assertEquals(10, $range['And']);
  184. $this->assertTrue(array_key_exists('Goodbye', $range));
  185. $this->assertEquals(11, $range['Goodbye']);
  186. // withscores-option is off
  187. $range = $this->credis->zRevRangeByScore('myset', '+inf', '-inf', array('withscores'));
  188. $this->assertEquals(4, count($range));
  189. $this->assertEquals(range(0, 3), array_keys($range)); // expecting numeric array without scores
  190. $range = $this->credis->zRevRangeByScore('myset', '+inf', '-inf', array('withscores' => false));
  191. $this->assertEquals(4, count($range));
  192. $this->assertEquals(range(0, 3), array_keys($range));
  193. // testing zunionstore (intersection of sorted sets)
  194. $this->credis->zAdd('myset1', 10, 'key1');
  195. $this->credis->zAdd('myset1', 10, 'key2');
  196. $this->credis->zAdd('myset1', 10, 'key_not_in_myset2');
  197. $this->credis->zAdd('myset2', 15, 'key1');
  198. $this->credis->zAdd('myset2', 15, 'key2');
  199. $this->credis->zAdd('myset2', 15, 'key_not_in_myset1');
  200. $this->credis->zUnionStore('myset3', array('myset1', 'myset2'));
  201. $range = $this->credis->zRangeByScore('myset3', '-inf', '+inf', array('withscores' => true));
  202. $this->assertEquals(4, count($range));
  203. $this->assertTrue(array_key_exists('key1', $range));
  204. $this->assertEquals(25, $range['key1']);
  205. $this->assertTrue(array_key_exists('key_not_in_myset1', $range));
  206. $this->assertEquals(15, $range['key_not_in_myset1']);
  207. // testing zunionstore AGGREGATE option
  208. $this->credis->zUnionStore('myset4', array('myset1', 'myset2'), array('aggregate' => 'max'));
  209. $range = $this->credis->zRangeByScore('myset4', '-inf', '+inf', array('withscores' => true));
  210. $this->assertEquals(4, count($range));
  211. $this->assertTrue(array_key_exists('key1', $range));
  212. $this->assertEquals(15, $range['key1']);
  213. $this->assertTrue(array_key_exists('key2', $range));
  214. $this->assertEquals(15, $range['key2']);
  215. // testing zunionstore WEIGHTS option
  216. $this->credis->zUnionStore('myset5', array('myset1', 'myset2'), array('weights' => array(2, 4)));
  217. $range = $this->credis->zRangeByScore('myset5', '-inf', '+inf', array('withscores' => true));
  218. $this->assertEquals(4, count($range));
  219. $this->assertTrue(array_key_exists('key1', $range));
  220. $this->assertEquals(80, $range['key1']);
  221. }
  222. public function testHashes()
  223. {
  224. $this->assertEquals(1, $this->credis->hSet('hash','field1','foo'));
  225. $this->assertEquals(0, $this->credis->hSet('hash','field1','foo'));
  226. $this->assertEquals('foo', $this->credis->hGet('hash','field1'));
  227. $this->assertEquals(NULL, $this->credis->hGet('hash','x'));
  228. $this->assertTrue($this->credis->hMSet('hash', array('field2' => 'Hello', 'field3' => 'World')));
  229. $this->assertEquals(array('field1' => 'foo', 'field2' => 'Hello', 'nilfield' => FALSE), $this->credis->hMGet('hash', array('field1','field2','nilfield')));
  230. $this->assertEquals(array(), $this->credis->hGetAll('nohash'));
  231. $this->assertEquals(array('field1' => 'foo', 'field2' => 'Hello', 'field3' => 'World'), $this->credis->hGetAll('hash'));
  232. // test integer keys
  233. $this->assertTrue($this->credis->hMSet('hashInt', array(0 => 'Hello', 1 => 'World')));
  234. $this->assertEquals(array(0 => 'Hello', 1 => 'World'), $this->credis->hGetAll('hashInt'));
  235. // Test long hash values
  236. $longString = str_repeat(md5('asd'), 4096); // 128k (redis.h REDIS_INLINE_MAX_SIZE = 64k)
  237. $this->assertEquals(1, $this->credis->hMSet('long_hash', array('count' => 1, 'data' => $longString)), 'Set long hash value');
  238. $this->assertEquals($longString, $this->credis->hGet('long_hash', 'data'), 'Get long hash value');
  239. // in piplining mode
  240. $this->assertTrue($this->credis->hMSet('hash', array('field1' => 'foo', 'field2' => 'Hello')));
  241. $this->credis->pipeline();
  242. $this->assertTrue($this->credis === $this->credis->hMGet('hash', array('field1','field2','nilfield')));
  243. $this->assertEquals(array(0 => array('field1' => 'foo', 'field2' => 'Hello', 'nilfield' => FALSE)), $this->credis->exec());
  244. $this->credis->pipeline()->multi();
  245. $this->assertTrue($this->credis === $this->credis->hMGet('hash', array('field1','field2','nilfield')));
  246. $this->assertEquals(array(0 => array('field1' => 'foo', 'field2' => 'Hello', 'nilfield' => FALSE)), $this->credis->exec());
  247. }
  248. public function testFalsey()
  249. {
  250. $this->assertEquals(Credis_Client::TYPE_NONE, $this->credis->type('foo'));
  251. }
  252. public function testPipeline()
  253. {
  254. $config = $this->credis->config('GET', '*');
  255. $this->assertEquals($config, $this->credis->pipeline()->config('GET', '*')->exec()[0]);
  256. $this->credis->pipeline();
  257. $this->pipelineTestInternal();
  258. $this->assertEquals(array(), $this->credis->pipeline()->exec());
  259. }
  260. public function testPipelineMulti()
  261. {
  262. $config = $this->credis->config('GET', '*');
  263. $this->assertEquals($config, $this->credis->pipeline()->multi()->config('GET', '*')->exec()[0]);
  264. $this->credis->pipeline()->multi();
  265. $this->pipelineTestInternal();
  266. $this->assertEquals(array(), $this->credis->pipeline()->multi()->exec());
  267. }
  268. public function testWatchMultiUnwatch()
  269. {
  270. $this->assertTrue($this->credis->watch('foo', 'bar'));
  271. $reply = $this->credis->pipeline()
  272. ->multi()
  273. ->set('foo', 1)
  274. ->set('bar', 1)
  275. ->exec();
  276. $this->assertEquals(
  277. array(
  278. true,
  279. true,
  280. ), $reply
  281. );
  282. $this->assertTrue($this->credis->unwatch());
  283. }
  284. protected function pipelineTestInternal()
  285. {
  286. $longString = str_repeat(md5('asd') . "\r\n", 500);
  287. $reply = $this->credis
  288. ->set('a', 123)
  289. ->get('a')
  290. ->sAdd('b', 123)
  291. ->sMembers('b')
  292. ->set('empty', '')
  293. ->get('empty')
  294. ->set('big', $longString)
  295. ->get('big')
  296. ->hset('hash', 'field1', 1)
  297. ->hset('hash', 'field2', 2)
  298. ->hgetall('hash')
  299. ->hmget('hash', array('field1', 'field3'))
  300. ->zadd('sortedSet', 1, 'member1')
  301. ->zadd('sortedSet', 2, 'member2')
  302. ->zadd('sortedSet', 3, 'member3')
  303. ->zcard('sortedSet')
  304. ->zrangebyscore('sortedSet', 1, 2)
  305. ->zrangebyscore('sortedSet', 1, 2, array('withscores' => true))
  306. ->zrevrangebyscore('sortedSet', 2, 1)
  307. ->zrevrangebyscore('sortedSet', 2, 1, array('withscores' => true))
  308. ->zrange('sortedSet', 0, 1)
  309. ->zrange('sortedSet', 0, 1, array('withscores' => true))
  310. ->zrevrange('sortedSet', 0, 1)
  311. ->zrevrange('sortedSet', 0, 1, array('withscores' => true))
  312. ->exec();
  313. $this->assertEquals(
  314. array(
  315. true, // set('a', 123)
  316. '123', // get('a')
  317. 1, // sAdd('b', 123)
  318. array(123), // sMembers('b')
  319. true, // set('empty', '')
  320. '', // get('empty')
  321. true, // set('big', $longString)
  322. $longString, // get('big')
  323. 1, // hset('hash', 'field1', 1)
  324. 1, // hset('hash', 'field2', 2)
  325. array( // hgetall('hash')
  326. 'field1' => 1,
  327. 'field2' => 2,
  328. ),
  329. array( // hmget('hash', array('field1', 'field3'))
  330. 'field1' => 1,
  331. 'field3' => false,
  332. ),
  333. 1, // zadd('sortedSet', 1, 'member1')
  334. 1, // zadd('sortedSet', 2, 'member2')
  335. 1, // zadd('sortedSet', 3, 'member3')
  336. 3, // zcard('sortedSet')
  337. array( // zrangebyscore('sortedSet', 1, 2)
  338. 'member1',
  339. 'member2',
  340. ),
  341. array( // zrangebyscore('sortedSet', 1, 2, array('withscores' => TRUE))
  342. 'member1' => 1.0,
  343. 'member2' => 2.0,
  344. ),
  345. array( // zrevrangebyscore('sortedSet', 1, 2)
  346. 'member2',
  347. 'member1',
  348. ),
  349. array( // zrevrangebyscore('sortedSet', 1, 2, array('withscores' => TRUE))
  350. 'member1' => 1.0,
  351. 'member2' => 2.0,
  352. ),
  353. array( // zrangebyscore('sortedSet', 1, 2)
  354. 'member1',
  355. 'member2',
  356. ),
  357. array( // zrangebyscore('sortedSet', 1, 2, array('withscores' => TRUE))
  358. 'member1' => 1.0,
  359. 'member2' => 2.0,
  360. ),
  361. array( // zrevrangebyscore('sortedSet', 1, 2)
  362. 'member3',
  363. 'member2',
  364. ),
  365. array( // zrevrangebyscore('sortedSet', 1, 2, array('withscores' => TRUE))
  366. 'member3' => 3.0,
  367. 'member2' => 2.0,
  368. ),
  369. ), $reply
  370. );
  371. }
  372. public function testTransaction()
  373. {
  374. $reply = $this->credis->multi()
  375. ->incr('foo')
  376. ->incr('bar')
  377. ->exec();
  378. $this->assertEquals(array(1,1), $reply);
  379. $reply = $this->credis->pipeline()->multi()
  380. ->incr('foo')
  381. ->incr('bar')
  382. ->exec();
  383. $this->assertEquals(array(2,2), $reply);
  384. $reply = $this->credis->multi()->pipeline()
  385. ->incr('foo')
  386. ->incr('bar')
  387. ->exec();
  388. $this->assertEquals(array(3,3), $reply);
  389. $reply = $this->credis->multi()
  390. ->set('a', 3)
  391. ->lpop('a')
  392. ->exec();
  393. $this->assertEquals(2, count($reply));
  394. $this->assertEquals(TRUE, $reply[0]);
  395. $this->assertFalse($reply[1]);
  396. }
  397. public function testServer()
  398. {
  399. $this->assertArrayHasKey('used_memory', $this->credis->info());
  400. $this->assertArrayHasKey('maxmemory', $this->credis->config('GET', 'maxmemory'));
  401. }
  402. public function testScripts()
  403. {
  404. $this->assertNull($this->credis->evalSha('1111111111111111111111111111111111111111'));
  405. $this->assertEquals(3, $this->credis->eval('return 3'));
  406. $this->assertEquals('09d3822de862f46d784e6a36848b4f0736dda47a', $this->credis->script('load', 'return 3'));
  407. $this->assertEquals(3, $this->credis->evalSha('09d3822de862f46d784e6a36848b4f0736dda47a'));
  408. $this->credis->set('foo','FOO');
  409. $this->assertEquals('FOOBAR', $this->credis->eval("return redis.call('get', KEYS[1])..ARGV[1]", 'foo', 'BAR'));
  410. $this->assertEquals(array(1,2,'three'), $this->credis->eval("return {1,2,'three'}"));
  411. try {
  412. $this->credis->eval('this-is-not-lua');
  413. $this->fail('Expected exception on invalid script.');
  414. } catch(CredisException $e) {
  415. }
  416. }
  417. public function testPubsub()
  418. {
  419. if (!$this->useStandalone && version_compare(PHP_VERSION, '7.0.0') >= 0) {
  420. $ext = new ReflectionExtension('redis');
  421. if (version_compare($ext->getVersion(), '3.1.4RC1') < 0) {
  422. $this->fail('phpredis 3.1.4 is required for subscribe/pSubscribe not to segfault with php 7.x');
  423. return;
  424. }
  425. }
  426. $timeout = 2;
  427. $time = microtime(true);
  428. $this->credis->setReadTimeout($timeout);
  429. try {
  430. $testCase = $this;
  431. $this->credis->pSubscribe(array('foobar','test*'), function ($credis, $pattern, $channel, $message) use ($testCase, &$time) {
  432. $time = time(); // Reset timeout
  433. // Test using: redis-cli publish foobar blah
  434. $testCase->assertEquals('blah', $message);
  435. });
  436. $this->fail('pSubscribe should not return.');
  437. } catch (CredisException $e) {
  438. $this->assertEquals($timeout, intval(microtime(true) - $time));
  439. if ($this->useStandalone) { // phpredis does not distinguish between timed out and disconnected
  440. $this->assertEquals($e->getCode(), CredisException::CODE_TIMED_OUT);
  441. } else {
  442. $this->assertEquals($e->getCode(), CredisException::CODE_DISCONNECTED);
  443. }
  444. }
  445. // Perform a new subscription. Client should have either unsubscribed or disconnected
  446. $timeout = 2;
  447. $time = microtime(true);
  448. $this->credis->setReadTimeout($timeout);
  449. try {
  450. $testCase = $this;
  451. $this->credis->subscribe('foobar', function ($credis, $channel, $message) use ($testCase, &$time) {
  452. $time = time(); // Reset timeout
  453. // Test using: redis-cli publish foobar blah
  454. $testCase->assertEquals('blah', $message);
  455. });
  456. $this->fail('subscribe should not return.');
  457. } catch (CredisException $e) {
  458. $this->assertEquals($timeout, intval(microtime(true) - $time));
  459. if ($this->useStandalone) { // phpredis does not distinguish between timed out and disconnected
  460. $this->assertEquals($e->getCode(), CredisException::CODE_TIMED_OUT);
  461. } else {
  462. $this->assertEquals($e->getCode(), CredisException::CODE_DISCONNECTED);
  463. }
  464. }
  465. }
  466. public function testDb()
  467. {
  468. $this->tearDown();
  469. $this->credis = new Credis_Client($this->redisConfig[0]['host'], $this->redisConfig[0]['port'], $this->redisConfig[0]['timeout'], false, 1);
  470. if ($this->useStandalone) {
  471. $this->credis->forceStandalone();
  472. }
  473. $this->assertTrue($this->credis->set('database',1));
  474. $this->credis->close();
  475. $this->credis = new Credis_Client($this->redisConfig[0]['host'], $this->redisConfig[0]['port'], $this->redisConfig[0]['timeout'], false, 0);
  476. if ($this->useStandalone) {
  477. $this->credis->forceStandalone();
  478. }
  479. $this->assertFalse($this->credis->get('database'));
  480. $this->credis = new Credis_Client($this->redisConfig[0]['host'], $this->redisConfig[0]['port'], $this->redisConfig[0]['timeout'], false, 1);
  481. if ($this->useStandalone) {
  482. $this->credis->forceStandalone();
  483. }
  484. $this->assertEquals(1,$this->credis->get('database'));
  485. }
  486. /**
  487. * @group Auth
  488. */
  489. public function testPassword()
  490. {
  491. $this->tearDown();
  492. $this->assertArrayHasKey('password',$this->redisConfig[4]);
  493. $this->credis = new Credis_Client($this->redisConfig[4]['host'], $this->redisConfig[4]['port'], $this->redisConfig[4]['timeout'], false, 0, $this->redisConfig[4]['password']);
  494. if ($this->useStandalone) {
  495. $this->credis->forceStandalone();
  496. }
  497. $this->assertInstanceOf('Credis_Client',$this->credis->connect());
  498. $this->assertTrue($this->credis->set('key','value'));
  499. $this->credis->close();
  500. $this->credis = new Credis_Client($this->redisConfig[4]['host'], $this->redisConfig[4]['port'], $this->redisConfig[4]['timeout'], false, 0, 'wrongpassword');
  501. if ($this->useStandalone) {
  502. $this->credis->forceStandalone();
  503. }
  504. try
  505. {
  506. $this->credis->connect();
  507. $this->fail('connect should fail with wrong password');
  508. }
  509. catch(CredisException $e)
  510. {
  511. $this->assertStringStartsWith('ERR invalid password', $e->getMessage());
  512. $this->credis->close();
  513. }
  514. $this->credis = new Credis_Client($this->redisConfig[4]['host'], $this->redisConfig[4]['port'], $this->redisConfig[4]['timeout'], false, 0);
  515. if ($this->useStandalone) {
  516. $this->credis->forceStandalone();
  517. }
  518. try
  519. {
  520. $this->credis->set('key', 'value');
  521. }
  522. catch(CredisException $e)
  523. {
  524. $this->assertStringStartsWith('NOAUTH Authentication required', $e->getMessage());
  525. }
  526. try
  527. {
  528. $this->credis->auth('anotherwrongpassword');
  529. }
  530. catch(CredisException $e)
  531. {
  532. $this->assertStringStartsWith('ERR invalid password', $e->getMessage());
  533. }
  534. $this->assertTrue($this->credis->auth('thepassword'));
  535. $this->assertTrue($this->credis->set('key','value'));
  536. }
  537. public function testGettersAndSetters()
  538. {
  539. $this->assertEquals($this->credis->getHost(),$this->redisConfig[0]['host']);
  540. $this->assertEquals($this->credis->getPort(),$this->redisConfig[0]['port']);
  541. $this->assertEquals($this->credis->getSelectedDb(),0);
  542. $this->assertTrue($this->credis->select(2));
  543. $this->assertEquals($this->credis->getSelectedDb(),2);
  544. $this->assertTrue($this->credis->isConnected());
  545. $this->credis->close();
  546. $this->assertFalse($this->credis->isConnected());
  547. $this->credis = new Credis_Client($this->redisConfig[0]['host'], $this->redisConfig[0]['port'], null, 'persistenceId');
  548. if ($this->useStandalone) {
  549. $this->credis->forceStandalone();
  550. }
  551. $this->assertEquals('persistenceId',$this->credis->getPersistence());
  552. $this->credis = new Credis_Client('localhost', 12345);
  553. if ($this->useStandalone) {
  554. $this->credis->forceStandalone();
  555. }
  556. $this->credis->setMaxConnectRetries(1);
  557. $this->setExpectedExceptionShim('CredisException','Connection to Redis localhost:12345 failed after 2 failures.');
  558. $this->credis->connect();
  559. }
  560. public function testConnectionStrings()
  561. {
  562. $this->credis->close();
  563. $this->credis = new Credis_Client('tcp://'.$this->redisConfig[0]['host'] . ':' . $this->redisConfig[0]['port']);
  564. if ($this->useStandalone) {
  565. $this->credis->forceStandalone();
  566. }
  567. $this->assertEquals($this->credis->getHost(),$this->redisConfig[0]['host']);
  568. $this->assertEquals($this->credis->getPort(),$this->redisConfig[0]['port']);
  569. $this->credis = new Credis_Client('tcp://'.$this->redisConfig[0]['host']);
  570. if ($this->useStandalone) {
  571. $this->credis->forceStandalone();
  572. }
  573. $this->assertEquals($this->credis->getPort(),$this->redisConfig[0]['port']);
  574. $this->credis = new Credis_Client('tcp://'.$this->redisConfig[0]['host'] . ':' . $this->redisConfig[0]['port'] . '/abc123');
  575. if ($this->useStandalone) {
  576. $this->credis->forceStandalone();
  577. }
  578. $this->assertEquals('abc123',$this->credis->getPersistence());
  579. }
  580. /**
  581. * @group UnixSocket
  582. */
  583. public function testConnectionStringsSocket()
  584. {
  585. $this->credis = new Credis_Client(realpath(__DIR__).'/redis.sock',0,null,'persistent');
  586. if ($this->useStandalone) {
  587. $this->credis->forceStandalone();
  588. }
  589. $this->credis->connect();
  590. $this->credis->set('key','value');
  591. $this->assertEquals('value',$this->credis->get('key'));
  592. }
  593. public function testInvalidTcpConnectionstring()
  594. {
  595. $this->credis->close();
  596. $this->setExpectedExceptionShim('CredisException','Invalid host format; expected tcp://host[:port][/persistence_identifier]');
  597. $this->credis = new Credis_Client('tcp://'.$this->redisConfig[0]['host'] . ':abc');
  598. if ($this->useStandalone) {
  599. $this->credis->forceStandalone();
  600. }
  601. }
  602. public function testInvalidUnixSocketConnectionstring()
  603. {
  604. $this->credis->close();
  605. $this->setExpectedExceptionShim('CredisException','Invalid unix socket format; expected unix:///path/to/redis.sock');
  606. $this->credis = new Credis_Client('unix://path/to/redis.sock');
  607. if ($this->useStandalone) {
  608. $this->credis->forceStandalone();
  609. }
  610. }
  611. public function testForceStandAloneAfterEstablishedConnection()
  612. {
  613. $this->credis->connect();
  614. if ( ! $this->useStandalone) {
  615. $this->setExpectedExceptionShim('CredisException','Cannot force Credis_Client to use standalone PHP driver after a connection has already been established.');
  616. }
  617. $this->credis->forceStandalone();
  618. $this->assertTrue(true);
  619. }
  620. public function testHscan()
  621. {
  622. $this->credis->hmset('hash',array('name' => 'Jack','age' =>33));
  623. $iterator = null;
  624. $result = $this->credis->hscan($iterator,'hash','n*',10);
  625. $this->assertEquals($iterator,0);
  626. $this->assertEquals($result,['name'=>'Jack']);
  627. }
  628. public function testSscan()
  629. {
  630. $this->credis->sadd('set','name','Jack');
  631. $this->credis->sadd('set','age','33');
  632. $iterator = null;
  633. $result = $this->credis->sscan($iterator,'set','n*',10);
  634. $this->assertEquals($iterator,0);
  635. $this->assertEquals($result,[0=>'name']);
  636. }
  637. public function testZscan()
  638. {
  639. $this->credis->zadd('sortedset',0,'name');
  640. $this->credis->zadd('sortedset',1,'age');
  641. $iterator = null;
  642. $result = $this->credis->zscan($iterator,'sortedset','n*',10);
  643. $this->assertEquals($iterator,0);
  644. $this->assertEquals($result,['name'=>'0']);
  645. }
  646. public function testscan()
  647. {
  648. $seen = array();
  649. for($i = 0; $i < 100; $i++)
  650. {
  651. $this->credis->set('name.' . $i, 'Jack');
  652. $this->credis->set('age.' . $i, '33');
  653. }
  654. $iterator = null;
  655. do
  656. {
  657. $result = $this->credis->scan($iterator, 'n*', 10);
  658. if ($result === false)
  659. {
  660. $this->assertEquals($iterator, 0);
  661. break;
  662. }
  663. else
  664. {
  665. foreach($result as $key)
  666. {
  667. $seen[$key] = true;
  668. }
  669. }
  670. }
  671. while($iterator);
  672. $this->assertEquals(count($seen), 100);
  673. }
  674. }