PageRenderTime 40ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/TestRedis.php

http://github.com/owlient/phpredis
PHP | 2698 lines | 1983 code | 511 blank | 204 comment | 99 complexity | 599d7740d00578f69a0e5cc2b1b3c148 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. require_once 'PHPUnit/Framework/TestCase.php';
  3. echo "Note: these tests might take up to a minute. Don't worry :-)\n";
  4. class Redis_Test extends PHPUnit_Framework_TestCase
  5. {
  6. const HOST = '127.0.0.1';
  7. const PORT = 6379;
  8. const AUTH = NULL; //replace with a string to use Redis authentication
  9. /**
  10. * @var Redis
  11. */
  12. public $redis;
  13. public function setUp()
  14. {
  15. $this->redis = $this->newInstance();
  16. }
  17. private function newInstance() {
  18. $r = new Redis();
  19. $r->connect(self::HOST, self::PORT);
  20. if(self::AUTH) {
  21. $this->assertTrue($r->auth(self::AUTH));
  22. }
  23. return $r;
  24. }
  25. public function tearDown()
  26. {
  27. if($this->redis) {
  28. $this->redis->close();
  29. }
  30. unset($this->redis);
  31. }
  32. public function reset()
  33. {
  34. $this->setUp();
  35. $this->tearDown();
  36. }
  37. public function testPing()
  38. {
  39. $this->assertEquals('+PONG', $this->redis->ping());
  40. $count = 1000;
  41. while($count --) {
  42. $this->assertEquals('+PONG', $this->redis->ping());
  43. }
  44. }
  45. public function testBitsets() {
  46. $this->redis->delete('key');
  47. $this->assertTrue(0 === $this->redis->getBit('key', 0));
  48. $this->assertTrue(FALSE === $this->redis->getBit('key', -1));
  49. $this->assertTrue(0 === $this->redis->getBit('key', 100000));
  50. $this->redis->set('key', "\xff");
  51. for($i = 0; $i < 8; $i++) {
  52. $this->assertTrue(1 === $this->redis->getBit('key', $i));
  53. }
  54. $this->assertTrue(0 === $this->redis->getBit('key', 8));
  55. // negative offset doesn't work
  56. $this->assertTrue(FALSE === $this->redis->setBit('key', -1, 0));
  57. $this->assertTrue(1 === $this->redis->getBit('key', 0));
  58. // change bit 0
  59. $this->assertTrue(1 === $this->redis->setBit('key', 0, 0));
  60. $this->assertTrue(0 === $this->redis->setBit('key', 0, 0));
  61. $this->assertTrue(0 === $this->redis->getBit('key', 0));
  62. $this->assertTrue("\x7f" === $this->redis->get('key'));
  63. // change bit 1
  64. $this->assertTrue(1 === $this->redis->setBit('key', 1, 0));
  65. $this->assertTrue(0 === $this->redis->setBit('key', 1, 0));
  66. $this->assertTrue(0 === $this->redis->getBit('key', 1));
  67. $this->assertTrue("\x3f" === $this->redis->get('key'));
  68. // change bit > 1
  69. $this->assertTrue(1 === $this->redis->setBit('key', 2, 0));
  70. $this->assertTrue(0 === $this->redis->setBit('key', 2, 0));
  71. $this->assertTrue(0 === $this->redis->getBit('key', 2));
  72. $this->assertTrue("\x1f" === $this->redis->get('key'));
  73. // values above 1 are changed to 1 but don't overflow on bits to the right.
  74. $this->assertTrue(0 === $this->redis->setBit('key', 0, 0xff));
  75. $this->assertTrue("\x9f" === $this->redis->get('key'));
  76. }
  77. public function test1000() {
  78. $s = str_repeat('A', 1000);
  79. $this->redis->set('x', $s);
  80. $this->assertEquals($s, $this->redis->get('x'));
  81. $s = str_repeat('A', 1000000);
  82. $this->redis->set('x', $s);
  83. $this->assertEquals($s, $this->redis->get('x'));
  84. }
  85. public function testErr() {
  86. $this->redis->set('x', '-ERR');
  87. $this->assertEquals($this->redis->get('x'), '-ERR');
  88. }
  89. public function testSet()
  90. {
  91. $this->assertEquals(TRUE, $this->redis->set('key', 'nil'));
  92. $this->assertEquals('nil', $this->redis->get('key'));
  93. $this->assertEquals(TRUE, $this->redis->set('key', 'val'));
  94. $this->assertEquals('val', $this->redis->get('key'));
  95. $this->assertEquals('val', $this->redis->get('key'));
  96. $this->redis->delete('keyNotExist');
  97. $this->assertEquals(FALSE, $this->redis->get('keyNotExist'));
  98. $this->redis->set('key2', 'val');
  99. $this->assertEquals('val', $this->redis->get('key2'));
  100. $value = 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA';
  101. $this->redis->set('key2', $value);
  102. $this->assertEquals($value, $this->redis->get('key2'));
  103. $this->assertEquals($value, $this->redis->get('key2'));
  104. $this->redis->delete('key');
  105. $this->redis->delete('key2');
  106. $i = 66000;
  107. $value2 = 'X';
  108. while($i--) {
  109. $value2 .= 'A';
  110. }
  111. $value2 .= 'X';
  112. $this->redis->set('key', $value2);
  113. $this->assertEquals($value2, $this->redis->get('key'));
  114. $this->redis->delete('key');
  115. $this->assertEquals(False, $this->redis->get('key'));
  116. $data = gzcompress('42');
  117. $this->assertEquals(True, $this->redis->set('key', $data));
  118. $this->assertEquals('42', gzuncompress($this->redis->get('key')));
  119. $this->redis->delete('key');
  120. $data = gzcompress('value1');
  121. $this->assertEquals(True, $this->redis->set('key', $data));
  122. $this->assertEquals('value1', gzuncompress($this->redis->get('key')));
  123. $this->redis->delete('key');
  124. $this->assertEquals(TRUE, $this->redis->set('key', 0));
  125. $this->assertEquals('0', $this->redis->get('key'));
  126. $this->assertEquals(TRUE, $this->redis->set('key', 1));
  127. $this->assertEquals('1', $this->redis->get('key'));
  128. $this->assertEquals(TRUE, $this->redis->set('key', 0.1));
  129. $this->assertEquals('0.1', $this->redis->get('key'));
  130. $this->assertEquals(TRUE, $this->redis->set('key', '0.1'));
  131. $this->assertEquals('0.1', $this->redis->get('key'));
  132. $this->assertEquals(TRUE, $this->redis->set('key', TRUE));
  133. $this->assertEquals('1', $this->redis->get('key'));
  134. $this->assertEquals(True, $this->redis->set('key', ''));
  135. $this->assertEquals('', $this->redis->get('key'));
  136. $this->assertEquals(True, $this->redis->set('key', NULL));
  137. $this->assertEquals('', $this->redis->get('key'));
  138. $this->assertEquals(True, $this->redis->set('key', gzcompress('42')));
  139. $this->assertEquals('42', gzuncompress($this->redis->get('key')));
  140. }
  141. public function testGetSet() {
  142. $this->redis->delete('key');
  143. $this->assertTrue($this->redis->getSet('key', '42') === FALSE);
  144. $this->assertTrue($this->redis->getSet('key', '123') === '42');
  145. $this->assertTrue($this->redis->getSet('key', '123') === '123');
  146. }
  147. public function testRandomKey() {
  148. for($i = 0; $i < 1000; $i++) {
  149. $k = $this->redis->randomKey();
  150. $this->assertTrue($this->redis->exists($k));
  151. }
  152. }
  153. public function testRename() {
  154. // strings
  155. $this->redis->delete('key0');
  156. $this->redis->set('key0', 'val0');
  157. $this->redis->renameKey('key0', 'key1');
  158. $this->assertTrue($this->redis->get('key0') === FALSE);
  159. $this->assertTrue($this->redis->get('key1') === 'val0');
  160. // lists
  161. $this->redis->delete('key0');
  162. $this->redis->lPush('key0', 'val0');
  163. $this->redis->lPush('key0', 'val1');
  164. $this->redis->renameKey('key0', 'key1');
  165. $this->assertTrue($this->redis->lGetRange('key0', 0, -1) === array());
  166. $this->assertTrue($this->redis->lGetRange('key1', 0, -1) === array('val1', 'val0'));
  167. }
  168. public function testRenameNx() {
  169. // strings
  170. $this->redis->delete('key0');
  171. $this->redis->set('key0', 'val0');
  172. $this->redis->set('key1', 'val1');
  173. $this->assertTrue($this->redis->renameNx('key0', 'key1') === FALSE);
  174. $this->assertTrue($this->redis->get('key0') === 'val0');
  175. $this->assertTrue($this->redis->get('key1') === 'val1');
  176. // lists
  177. $this->redis->delete('key0');
  178. $this->redis->delete('key1');
  179. $this->redis->lPush('key0', 'val0');
  180. $this->redis->lPush('key0', 'val1');
  181. $this->redis->lPush('key1', 'val1-0');
  182. $this->redis->lPush('key1', 'val1-1');
  183. $this->redis->renameNx('key0', 'key1');
  184. $this->assertTrue($this->redis->lGetRange('key0', 0, -1) === array('val1', 'val0'));
  185. $this->assertTrue($this->redis->lGetRange('key1', 0, -1) === array('val1-1', 'val1-0'));
  186. $this->redis->delete('key2');
  187. $this->redis->renameNx('key0', 'key2');
  188. $this->assertTrue($this->redis->lGetRange('key0', 0, -1) === array());
  189. $this->assertTrue($this->redis->lGetRange('key2', 0, -1) === array('val1', 'val0'));
  190. }
  191. public function testMultiple() {
  192. $this->redis->delete('k1');
  193. $this->redis->delete('k2');
  194. $this->redis->delete('k3');
  195. $this->redis->set('k1', 'v1');
  196. $this->redis->set('k2', 'v2');
  197. $this->redis->set('k3', 'v3');
  198. $this->redis->set(1, 'test');
  199. $this->assertEquals(array('v1'), $this->redis->getMultiple(array('k1')));
  200. $this->assertEquals(array('v1', 'v3', false), $this->redis->getMultiple(array('k1', 'k3', 'NoKey')));
  201. $this->assertEquals(array('v1', 'v2', 'v3'), $this->redis->getMultiple(array('k1', 'k2', 'k3')));
  202. $this->assertEquals(array('v1', 'v2', 'v3'), $this->redis->getMultiple(array('k1', 'k2', 'k3')));
  203. $this->redis->set('k5', '$1111111111');
  204. $this->assertEquals(array(0 => '$1111111111'), $this->redis->getMultiple(array('k5')));
  205. $this->assertEquals(array(0 => 'test'), $this->redis->getMultiple(array(1))); // non-string
  206. }
  207. public function testMultipleBin() {
  208. $this->redis->delete('k1');
  209. $this->redis->delete('k2');
  210. $this->redis->delete('k3');
  211. $this->redis->set('k1', gzcompress('v1'));
  212. $this->redis->set('k2', gzcompress('v2'));
  213. $this->redis->set('k3', gzcompress('v3'));
  214. $this->assertEquals(array(gzcompress('v1'), gzcompress('v2'), gzcompress('v3')), $this->redis->getMultiple(array('k1', 'k2', 'k3')));
  215. $this->assertEquals(array(gzcompress('v1'), gzcompress('v2'), gzcompress('v3')), $this->redis->getMultiple(array('k1', 'k2', 'k3')));
  216. }
  217. public function testSetTimeout() {
  218. $this->redis->delete('key');
  219. $this->redis->set('key', 'value');
  220. $this->assertEquals('value', $this->redis->get('key'));
  221. $this->redis->setTimeout('key', 1);
  222. $this->assertEquals('value', $this->redis->get('key'));
  223. sleep(2);
  224. $this->assertEquals(False, $this->redis->get('key'));
  225. }
  226. public function testExpireAt() {
  227. $this->redis->delete('key');
  228. $this->redis->set('key', 'value');
  229. $now = time(NULL);
  230. $this->redis->expireAt('key', $now + 1);
  231. $this->assertEquals('value', $this->redis->get('key'));
  232. sleep(2);
  233. $this->assertEquals(FALSE, $this->redis->get('key'));
  234. }
  235. public function testSetEx() {
  236. $this->redis->delete('key');
  237. $this->assertTrue($this->redis->setex('key', 7, 'val') === TRUE);
  238. $this->assertTrue($this->redis->ttl('key') ===7);
  239. $this->assertTrue($this->redis->get('key') === 'val');
  240. }
  241. public function testSetNX() {
  242. $this->redis->set('key', 42);
  243. $this->assertTrue($this->redis->setnx('key', 'err') === FALSE);
  244. $this->assertTrue($this->redis->get('key') === '42');
  245. $this->redis->delete('key');
  246. $this->assertTrue($this->redis->setnx('key', '42') === TRUE);
  247. $this->assertTrue($this->redis->get('key') === '42');
  248. }
  249. public function testIncr()
  250. {
  251. $this->redis->set('key', 0);
  252. $this->redis->incr('key');
  253. $this->assertEquals(1, $this->redis->get('key'));
  254. $this->redis->incr('key');
  255. $this->assertEquals(2, $this->redis->get('key'));
  256. $this->redis->incr('key', 3);
  257. $this->assertEquals(5, $this->redis->get('key'));
  258. $this->redis->incrBy('key', 3);
  259. $this->assertEquals(8, $this->redis->get('key'));
  260. $this->redis->incrBy('key', 1);
  261. $this->assertEquals(9, $this->redis->get('key'));
  262. $this->redis->incrBy('key', -1);
  263. $this->assertEquals(8, $this->redis->get('key'));
  264. $this->redis->delete('key');
  265. $this->redis->set('key', 'abc');
  266. $this->redis->incr('key');
  267. $this->assertTrue("abc" === $this->redis->get('key'));
  268. $this->redis->incr('key');
  269. $this->assertTrue("abc" === $this->redis->get('key'));
  270. }
  271. public function testDecr()
  272. {
  273. $this->redis->set('key', 5);
  274. $this->redis->decr('key');
  275. $this->assertEquals(4, $this->redis->get('key'));
  276. $this->redis->decr('key');
  277. $this->assertEquals(3, $this->redis->get('key'));
  278. $this->redis->decr('key', 2);
  279. $this->assertEquals(1, $this->redis->get('key'));
  280. $this->redis->decr('key', 2);
  281. $this->assertEquals(-1, $this->redis->get('key'));
  282. $this->redis->decrBy('key', 2);
  283. $this->assertEquals(-3, $this->redis->get('key'));
  284. $this->redis->decrBy('key', 1);
  285. $this->assertEquals(-4, $this->redis->get('key'));
  286. $this->redis->decr('key', -10);
  287. $this->assertEquals(6, $this->redis->get('key'));
  288. }
  289. public function testExists()
  290. {
  291. $this->redis->delete('key');
  292. $this->assertFalse($this->redis->exists('key'));
  293. $this->redis->set('key', 'val');
  294. $this->assertEquals(True, $this->redis->exists('key'));
  295. }
  296. public function testGetKeys()
  297. {
  298. $pattern = 'getKeys-test-';
  299. for($i = 1; $i < 10; $i++) {
  300. $this->redis->set($pattern.$i, $i);
  301. }
  302. $this->redis->delete($pattern.'3');
  303. $keys = $this->redis->getKeys($pattern.'*');
  304. $this->redis->set($pattern.'3', 'something');
  305. $keys2 = $this->redis->getKeys($pattern.'*');
  306. $this->assertEquals((count($keys) + 1), count($keys2));
  307. // empty array when no key matches
  308. $this->assertEquals(array(), $this->redis->getKeys(rand().rand().rand().'*'));
  309. }
  310. public function testDelete()
  311. {
  312. $key = 'key' . rand();
  313. $this->redis->set($key, 'val');
  314. $this->assertEquals('val', $this->redis->get($key));
  315. $this->assertEquals(1, $this->redis->delete($key));
  316. $this->assertEquals(null, $this->redis->get($key));
  317. // multiple, all existing
  318. $this->redis->set('x', 0);
  319. $this->redis->set('y', 1);
  320. $this->redis->set('z', 2);
  321. $this->assertEquals(3, $this->redis->delete('x', 'y', 'z'));
  322. $this->assertEquals(NULL, $this->redis->get('x'));
  323. $this->assertEquals(NULL, $this->redis->get('y'));
  324. $this->assertEquals(NULL, $this->redis->get('z'));
  325. // multiple, none existing
  326. $this->assertEquals(0, $this->redis->delete('x', 'y', 'z'));
  327. $this->assertEquals(NULL, $this->redis->get('x'));
  328. $this->assertEquals(NULL, $this->redis->get('y'));
  329. $this->assertEquals(NULL, $this->redis->get('z'));
  330. // multiple, some existing
  331. $this->redis->set('y', 1);
  332. $this->assertEquals(1, $this->redis->delete('x', 'y', 'z'));
  333. $this->assertEquals(NULL, $this->redis->get('y'));
  334. $this->redis->set('x', 0);
  335. $this->redis->set('y', 1);
  336. $this->assertEquals(2, $this->redis->delete(array('x', 'y')));
  337. }
  338. public function testType()
  339. {
  340. // 0 => none, (key didn't exist)
  341. // 1=> string,
  342. // 2 => set,
  343. // 3 => list,
  344. // 4 => zset,
  345. // 5 => hash
  346. // string
  347. $this->redis->set('key', 'val');
  348. $this->assertEquals(Redis::REDIS_STRING, $this->redis->type('key'));
  349. // list
  350. $this->redis->lPush('keyList', 'val0');
  351. $this->redis->lPush('keyList', 'val1');
  352. $this->assertEquals(Redis::REDIS_LIST, $this->redis->type('keyList'));
  353. // set
  354. $this->redis->delete('keySet');
  355. $this->redis->sAdd('keySet', 'val0');
  356. $this->redis->sAdd('keySet', 'val1');
  357. $this->assertEquals(Redis::REDIS_SET, $this->redis->type('keySet'));
  358. // zset
  359. $this->redis->delete('keyZSet');
  360. $this->redis->zAdd('keyZSet', 0, 'val0');
  361. $this->redis->zAdd('keyZSet', 1, 'val1');
  362. $this->assertEquals(Redis::REDIS_ZSET, $this->redis->type('keyZSet'));
  363. // hash
  364. $this->redis->delete('keyHash');
  365. $this->redis->hSet('keyHash', 'key0', 'val0');
  366. $this->redis->hSet('keyHash', 'key1', 'val1');
  367. $this->assertEquals(Redis::REDIS_HASH, $this->redis->type('keyHash'));
  368. //None
  369. $this->assertEquals(Redis::REDIS_NOT_FOUND, $this->redis->type('keyNotExists'));
  370. }
  371. public function testStr() {
  372. $this->redis->set('key', 'val1');
  373. $this->assertTrue($this->redis->append('key', 'val2') === 8);
  374. $this->assertTrue($this->redis->get('key') === 'val1val2');
  375. $this->assertTrue($this->redis->append('keyNotExist', 'value') === 5);
  376. $this->assertTrue($this->redis->get('keyNotExist') === 'value');
  377. $this->redis->set('key', 'This is a string') ;
  378. $this->assertTrue($this->redis->getRange('key', 0, 3) === 'This');
  379. $this->assertTrue($this->redis->getRange('key', -6, -1) === 'string');
  380. $this->assertTrue($this->redis->getRange('key', -6, 100000) === 'string');
  381. $this->assertTrue($this->redis->get('key') === 'This is a string');
  382. $this->redis->set('key', 'This is a string') ;
  383. $this->assertTrue($this->redis->strlen('key') === 16);
  384. $this->redis->set('key', 10) ;
  385. $this->assertTrue($this->redis->strlen('key') === 2);
  386. $this->redis->set('key', '') ;
  387. $this->assertTrue($this->redis->strlen('key') === 0);
  388. $this->redis->set('key', '000') ;
  389. $this->assertTrue($this->redis->strlen('key') === 3);
  390. }
  391. // PUSH, POP : LPUSH, LPOP
  392. public function testlPop()
  393. {
  394. // rpush => tail
  395. // lpush => head
  396. $this->redis->delete('list');
  397. $this->redis->lPush('list', 'val');
  398. $this->redis->lPush('list', 'val2');
  399. $this->redis->rPush('list', 'val3');
  400. // 'list' = [ 'val2', 'val', 'val3']
  401. $this->assertEquals('val2', $this->redis->lPop('list'));
  402. $this->assertEquals('val', $this->redis->lPop('list'));
  403. $this->assertEquals('val3', $this->redis->lPop('list'));
  404. $this->assertEquals(FALSE, $this->redis->lPop('list'));
  405. // testing binary data
  406. $this->redis->delete('list');
  407. $this->assertEquals(1, $this->redis->lPush('list', gzcompress('val1')));
  408. $this->assertEquals(2, $this->redis->lPush('list', gzcompress('val2')));
  409. $this->assertEquals(3, $this->redis->lPush('list', gzcompress('val3')));
  410. $this->assertEquals('val3', gzuncompress($this->redis->lPop('list')));
  411. $this->assertEquals('val2', gzuncompress($this->redis->lPop('list')));
  412. $this->assertEquals('val1', gzuncompress($this->redis->lPop('list')));
  413. }
  414. // PUSH, POP : RPUSH, RPOP
  415. public function testrPop()
  416. {
  417. // rpush => tail
  418. // lpush => head
  419. $this->redis->delete('list');
  420. $this->redis->rPush('list', 'val');
  421. $this->redis->rPush('list', 'val2');
  422. $this->redis->lPush('list', 'val3');
  423. // 'list' = [ 'val3', 'val', 'val2']
  424. $this->assertEquals('val2', $this->redis->rPop('list'));
  425. $this->assertEquals('val', $this->redis->rPop('list'));
  426. $this->assertEquals('val3', $this->redis->rPop('list'));
  427. $this->assertEquals(FALSE, $this->redis->rPop('list'));
  428. // testing binary data
  429. $this->redis->delete('list');
  430. $this->assertEquals(1, $this->redis->rPush('list', gzcompress('val1')));
  431. $this->assertEquals(2, $this->redis->rPush('list', gzcompress('val2')));
  432. $this->assertEquals(3, $this->redis->rPush('list', gzcompress('val3')));
  433. $this->assertEquals('val3', gzuncompress($this->redis->rPop('list')));
  434. $this->assertEquals('val2', gzuncompress($this->redis->rPop('list')));
  435. $this->assertEquals('val1', gzuncompress($this->redis->rPop('list')));
  436. }
  437. public function testblockingPop() {
  438. // non blocking blPop, brPop
  439. $this->redis->delete('list');
  440. $this->redis->lPush('list', 'val1');
  441. $this->redis->lPush('list', 'val2');
  442. $this->assertTrue($this->redis->blPop(array('list'), 2) === array('list', 'val2'));
  443. $this->assertTrue($this->redis->blPop(array('list'), 2) === array('list', 'val1'));
  444. $this->redis->delete('list');
  445. $this->redis->lPush('list', 'val1');
  446. $this->redis->lPush('list', 'val2');
  447. $this->assertTrue($this->redis->brPop(array('list'), 1) === array('list', 'val1'));
  448. $this->assertTrue($this->redis->brPop(array('list'), 1) === array('list', 'val2'));
  449. // blocking blpop, brpop
  450. $this->redis->delete('list');
  451. $this->assertTrue($this->redis->blPop(array('list'), 1) === array());
  452. $this->assertTrue($this->redis->brPop(array('list'), 1) === array());
  453. // TODO: fix this broken test.
  454. // $this->redis->delete('list');
  455. // $params = array(
  456. // 0 => array("pipe", "r"),
  457. // 1 => array("pipe", "w"),
  458. // 2 => array("file", "/dev/null", "w")
  459. // );
  460. // if(function_exists('proc_open')) {
  461. // $env = array('PHPREDIS_key' =>'list', 'PHPREDIS_value' => 'value');
  462. // $process = proc_open('php', $params, $pipes, '/tmp', $env);
  463. //
  464. // if (is_resource($process)) {
  465. // fwrite($pipes[0], '<?php
  466. // sleep(2);
  467. // $r = new Redis;
  468. // $r->connect("'.self::HOST.'", '.self::PORT.');
  469. // if("'.addslashes(self::AUTH).'") {
  470. // $r->auth("'.addslashes(self::AUTH).'");
  471. // }
  472. // $r->lPush($_ENV["PHPREDIS_key"], $_ENV["PHPREDIS_value"]);
  473. // ?' . '>');
  474. //
  475. // fclose($pipes[0]);
  476. // fclose($pipes[1]);
  477. // $re = proc_close($process);
  478. //
  479. // $this->assertTrue($this->redis->blPop(array('list'), 5) === array("list", "value"));
  480. // }
  481. // }
  482. }
  483. public function testlSize()
  484. {
  485. $this->redis->delete('list');
  486. $this->redis->lPush('list', 'val');
  487. $this->assertEquals(1, $this->redis->lSize('list'));
  488. $this->redis->lPush('list', 'val2');
  489. $this->assertEquals(2, $this->redis->lSize('list'));
  490. $this->assertEquals('val2', $this->redis->lPop('list'));
  491. $this->assertEquals(1, $this->redis->lSize('list'));
  492. $this->assertEquals('val', $this->redis->lPop('list'));
  493. $this->assertEquals(0, $this->redis->lSize('list'));
  494. $this->assertEquals(FALSE, $this->redis->lPop('list'));
  495. $this->assertEquals(0, $this->redis->lSize('list')); // empty returns 0
  496. $this->redis->delete('list');
  497. $this->assertEquals(0, $this->redis->lSize('list')); // non-existent returns 0
  498. $this->redis->set('list', 'actually not a list');
  499. $this->assertEquals(FALSE, $this->redis->lSize('list'));// not a list returns FALSE
  500. }
  501. //lInsert, lPopx, rPopx
  502. public function testlPopx() {
  503. //test lPushx/rPushx
  504. $this->redis->delete('keyNotExists');
  505. $this->assertTrue($this->redis->lPushx('keyNotExists', 'value') === 0);
  506. $this->assertTrue($this->redis->rPushx('keyNotExists', 'value') === 0);
  507. $this->redis->delete('key');
  508. $this->redis->lPush('key', 'val0');
  509. $this->assertTrue($this->redis->lPushx('key', 'val1') === 2);
  510. $this->assertTrue($this->redis->rPushx('key', 'val2') === 3);
  511. $this->assertTrue($this->redis->lGetRange('key', 0, -1) === array('val1', 'val0', 'val2'));
  512. //test linsert
  513. $this->redis->delete('key');
  514. $this->redis->lPush('key', 'val0');
  515. $this->assertTrue($this->redis->lInsert('keyNotExists', Redis::AFTER, 'val1', 'val2') === 0);
  516. $this->assertTrue($this->redis->lInsert('key', Redis::BEFORE, 'valX', 'val2') === -1);
  517. $this->assertTrue($this->redis->lInsert('key', Redis::AFTER, 'val0', 'val1') === 2);
  518. $this->assertTrue($this->redis->lInsert('key', Redis::BEFORE, 'val0', 'val2') === 3);
  519. $this->assertTrue($this->redis->lGetRange('key', 0, -1) === array('val2', 'val0', 'val1'));
  520. }
  521. // ltrim, lsize, lpop
  522. public function testlistTrim()
  523. {
  524. $this->redis->delete('list');
  525. $this->redis->lPush('list', 'val');
  526. $this->redis->lPush('list', 'val2');
  527. $this->redis->lPush('list', 'val3');
  528. $this->redis->lPush('list', 'val4');
  529. $this->assertEquals(TRUE, $this->redis->listTrim('list', 0, 2));
  530. $this->assertEquals(3, $this->redis->lSize('list'));
  531. $this->redis->listTrim('list', 0, 0);
  532. $this->assertEquals(1, $this->redis->lSize('list'));
  533. $this->assertEquals('val4', $this->redis->lPop('list'));
  534. $this->assertEquals(TRUE, $this->redis->listTrim('list', 10, 10000));
  535. $this->assertEquals(TRUE, $this->redis->listTrim('list', 10000, 10));
  536. // test invalid type
  537. $this->redis->set('list', 'not a list...');
  538. $this->assertEquals(FALSE, $this->redis->listTrim('list', 0, 2));
  539. }
  540. public function setupSort() {
  541. // people with name, age, salary
  542. $this->redis->set('person:name_1', 'Alice');
  543. $this->redis->set('person:age_1', 27);
  544. $this->redis->set('person:salary_1', 2500);
  545. $this->redis->set('person:name_2', 'Bob');
  546. $this->redis->set('person:age_2', 34);
  547. $this->redis->set('person:salary_2', 2000);
  548. $this->redis->set('person:name_3', 'Carol');
  549. $this->redis->set('person:age_3', 25);
  550. $this->redis->set('person:salary_3', 2800);
  551. $this->redis->set('person:name_4', 'Dave');
  552. $this->redis->set('person:age_4', 41);
  553. $this->redis->set('person:salary_4', 3100);
  554. // set-up
  555. $this->redis->delete('person:id');
  556. foreach(array(1,2,3,4) as $id) {
  557. $this->redis->lPush('person:id', $id);
  558. }
  559. }
  560. public function testSortAsc() {
  561. $this->setupSort();
  562. $this->assertTrue(FALSE === $this->redis->sortAsc(NULL));
  563. // sort by age and get IDs
  564. $byAgeAsc = array('3','1','2','4');
  565. $this->assertEquals($byAgeAsc, $this->redis->sortAsc('person:id', 'person:age_*'));
  566. $this->assertEquals($byAgeAsc, $this->redis->sort('person:id', array('by' => 'person:age_*', 'sort' => 'asc')));
  567. $this->assertEquals(array('1', '2', '3', '4'), $this->redis->sortAsc('person:id', NULL)); // check that NULL works.
  568. $this->assertEquals(array('1', '2', '3', '4'), $this->redis->sortAsc('person:id', NULL, NULL)); // for all fields.
  569. $this->assertEquals(array('1', '2', '3', '4'), $this->redis->sort('person:id', array('sort' => 'asc')));
  570. // sort by age and get names
  571. $byAgeAsc = array('Carol','Alice','Bob','Dave');
  572. $this->assertEquals($byAgeAsc, $this->redis->sortAsc('person:id', 'person:age_*', 'person:name_*'));
  573. $this->assertEquals($byAgeAsc, $this->redis->sort('person:id', array('by' => 'person:age_*', 'get' => 'person:name_*', 'sort' => 'asc')));
  574. $this->assertEquals(array_slice($byAgeAsc, 0, 2), $this->redis->sortAsc('person:id', 'person:age_*', 'person:name_*', 0, 2));
  575. $this->assertEquals(array_slice($byAgeAsc, 0, 2), $this->redis->sort('person:id', array('by' => 'person:age_*', 'get' => 'person:name_*', 'limit' => array(0, 2), 'sort' => 'asc')));
  576. $this->assertEquals(array_slice($byAgeAsc, 1, 2), $this->redis->sortAsc('person:id', 'person:age_*', 'person:name_*', 1, 2));
  577. $this->assertEquals(array_slice($byAgeAsc, 1, 2), $this->redis->sort('person:id', array('by' => 'person:age_*', 'get' => 'person:name_*', 'limit' => array(1, 2), 'sort' => 'asc')));
  578. $this->assertEquals(array_slice($byAgeAsc, 0, 3), $this->redis->sortAsc('person:id', 'person:age_*', 'person:name_*', NULL, 3)); // NULL is transformed to 0 if there is something after it.
  579. $this->assertEquals($byAgeAsc, $this->redis->sortAsc('person:id', 'person:age_*', 'person:name_*', 0, 4));
  580. $this->assertEquals($byAgeAsc, $this->redis->sort('person:id', array('by' => 'person:age_*', 'get' => 'person:name_*', 'limit' => array(0, 4))));
  581. $this->assertEquals(array(), $this->redis->sortAsc('person:id', 'person:age_*', 'person:name_*', NULL, NULL)); // NULL, NULL is the same as (0,0). That returns no element.
  582. // sort by salary and get ages
  583. $agesBySalaryAsc = array('34', '27', '25', '41');
  584. $this->assertEquals($agesBySalaryAsc, $this->redis->sortAsc('person:id', 'person:salary_*', 'person:age_*'));
  585. $this->assertEquals($agesBySalaryAsc, $this->redis->sort('person:id', array('by' => 'person:salary_*', 'get' => 'person:age_*', 'sort' => 'asc')));
  586. $agesAndSalaries = $this->redis->sort('person:id', array('by' => 'person:salary_*', 'get' => array('person:age_*', 'person:salary_*'), 'sort' => 'asc'));
  587. $this->assertEquals(array('34', '2000', '27', '2500', '25', '2800', '41', '3100'), $agesAndSalaries);
  588. // sort non-alpha doesn't change all-string lists
  589. // list → [ghi, def, abc]
  590. $list = array('abc', 'def', 'ghi');
  591. $this->redis->delete('list');
  592. foreach($list as $i) {
  593. $this->redis->lPush('list', $i);
  594. }
  595. // SORT list → [ghi, def, abc]
  596. $this->assertEquals(array_reverse($list), $this->redis->sortAsc('list'));
  597. $this->assertEquals(array_reverse($list), $this->redis->sort('list', array('sort' => 'asc')));
  598. // SORT list ALPHA → [abc, def, ghi]
  599. $this->assertEquals($list, $this->redis->sortAscAlpha('list'));
  600. $this->assertEquals($list, $this->redis->sort('list', array('sort' => 'asc', 'alpha' => TRUE)));
  601. }
  602. public function testSortDesc() {
  603. $this->setupSort();
  604. // sort by age and get IDs
  605. $byAgeDesc = array('4','2','1','3');
  606. $this->assertEquals($byAgeDesc, $this->redis->sortDesc('person:id', 'person:age_*'));
  607. // sort by age and get names
  608. $byAgeDesc = array('Dave', 'Bob', 'Alice', 'Carol');
  609. $this->assertEquals($byAgeDesc, $this->redis->sortDesc('person:id', 'person:age_*', 'person:name_*'));
  610. $this->assertEquals(array_slice($byAgeDesc, 0, 2), $this->redis->sortDesc('person:id', 'person:age_*', 'person:name_*', 0, 2));
  611. $this->assertEquals(array_slice($byAgeDesc, 1, 2), $this->redis->sortDesc('person:id', 'person:age_*', 'person:name_*', 1, 2));
  612. // sort by salary and get ages
  613. $agesBySalaryDesc = array('41', '25', '27', '34');
  614. $this->assertEquals($agesBySalaryDesc, $this->redis->sortDesc('person:id', 'person:salary_*', 'person:age_*'));
  615. // sort non-alpha doesn't change all-string lists
  616. $list = array('def', 'abc', 'ghi');
  617. $this->redis->delete('list');
  618. foreach($list as $i) {
  619. $this->redis->lPush('list', $i);
  620. }
  621. // SORT list → [ghi, abc, def]
  622. $this->assertEquals(array_reverse($list), $this->redis->sortDesc('list'));
  623. // SORT list ALPHA → [abc, def, ghi]
  624. $this->assertEquals(array('ghi', 'def', 'abc'), $this->redis->sortDescAlpha('list'));
  625. }
  626. // LINDEX
  627. public function testlGet() {
  628. $this->redis->delete('list');
  629. $this->redis->lPush('list', 'val');
  630. $this->redis->lPush('list', 'val2');
  631. $this->redis->lPush('list', 'val3');
  632. $this->assertEquals('val3', $this->redis->lGet('list', 0));
  633. $this->assertEquals('val2', $this->redis->lGet('list', 1));
  634. $this->assertEquals('val', $this->redis->lGet('list', 2));
  635. $this->assertEquals('val', $this->redis->lGet('list', -1));
  636. $this->assertEquals('val2', $this->redis->lGet('list', -2));
  637. $this->assertEquals('val3', $this->redis->lGet('list', -3));
  638. $this->assertEquals(FALSE, $this->redis->lGet('list', -4));
  639. $this->redis->rPush('list', 'val4');
  640. $this->assertEquals('val4', $this->redis->lGet('list', 3));
  641. $this->assertEquals('val4', $this->redis->lGet('list', -1));
  642. }
  643. // lRem testing
  644. public function testlRemove() {
  645. $this->redis->delete('list');
  646. $this->redis->lPush('list', 'a');
  647. $this->redis->lPush('list', 'b');
  648. $this->redis->lPush('list', 'c');
  649. $this->redis->lPush('list', 'c');
  650. $this->redis->lPush('list', 'b');
  651. $this->redis->lPush('list', 'c');
  652. // ['c', 'b', 'c', 'c', 'b', 'a']
  653. $return = $this->redis->lRemove('list', 'b', 2);
  654. // ['c', 'c', 'c', 'a']
  655. $this->assertEquals(2, $return);
  656. $this->assertEquals('c', $this->redis->lGET('list', 0));
  657. $this->assertEquals('c', $this->redis->lGET('list', 1));
  658. $this->assertEquals('c', $this->redis->lGET('list', 2));
  659. $this->assertEquals('a', $this->redis->lGET('list', 3));
  660. $this->redis->delete('list');
  661. $this->redis->lPush('list', 'a');
  662. $this->redis->lPush('list', 'b');
  663. $this->redis->lPush('list', 'c');
  664. $this->redis->lPush('list', 'c');
  665. $this->redis->lPush('list', 'b');
  666. $this->redis->lPush('list', 'c');
  667. // ['c', 'b', 'c', 'c', 'b', 'a']
  668. $this->redis->lRemove('list', 'c', -2);
  669. // ['c', 'b', 'b', 'a']
  670. $this->assertEquals(2, $return);
  671. $this->assertEquals('c', $this->redis->lGET('list', 0));
  672. $this->assertEquals('b', $this->redis->lGET('list', 1));
  673. $this->assertEquals('b', $this->redis->lGET('list', 2));
  674. $this->assertEquals('a', $this->redis->lGET('list', 3));
  675. // remove each element
  676. $this->assertEquals(1, $this->redis->lRemove('list', 'a', 0));
  677. $this->assertEquals(0, $this->redis->lRemove('list', 'x', 0));
  678. $this->assertEquals(2, $this->redis->lRemove('list', 'b', 0));
  679. $this->assertEquals(1, $this->redis->lRemove('list', 'c', 0));
  680. $this->assertEquals(FALSE, $this->redis->get('list'));
  681. $this->redis->set('list', 'actually not a list');
  682. $this->assertEquals(FALSE, $this->redis->lRemove('list', 'x'));
  683. }
  684. public function testsAdd()
  685. {
  686. $this->redis->delete('set');
  687. $this->assertEquals(TRUE, $this->redis->sAdd('set', 'val'));
  688. $this->assertEquals(FALSE, $this->redis->sAdd('set', 'val'));
  689. $this->assertTrue($this->redis->sContains('set', 'val'));
  690. $this->assertFalse($this->redis->sContains('set', 'val2'));
  691. $this->assertEquals(TRUE, $this->redis->sAdd('set', 'val2'));
  692. $this->assertTrue($this->redis->sContains('set', 'val2'));
  693. }
  694. public function testsSize()
  695. {
  696. $this->redis->delete('set');
  697. $this->assertEquals(TRUE, $this->redis->sAdd('set', 'val'));
  698. $this->assertEquals(1, $this->redis->sSize('set'));
  699. $this->assertEquals(TRUE, $this->redis->sAdd('set', 'val2'));
  700. $this->assertEquals(2, $this->redis->sSize('set'));
  701. }
  702. public function testsRemove()
  703. {
  704. $this->redis->delete('set');
  705. $this->redis->sAdd('set', 'val');
  706. $this->redis->sAdd('set', 'val2');
  707. $this->redis->sRemove('set', 'val');
  708. $this->assertEquals(1, $this->redis->sSize('set'));
  709. $this->redis->sRemove('set', 'val2');
  710. $this->assertEquals(0, $this->redis->sSize('set'));
  711. }
  712. public function testsMove()
  713. {
  714. $this->redis->delete('set0');
  715. $this->redis->delete('set1');
  716. $this->redis->sAdd('set0', 'val');
  717. $this->redis->sAdd('set0', 'val2');
  718. $this->assertTrue($this->redis->sMove('set0', 'set1', 'val'));
  719. $this->assertFalse($this->redis->sMove('set0', 'set1', 'val'));
  720. $this->assertFalse($this->redis->sMove('set0', 'set1', 'val-what'));
  721. $this->assertEquals(1, $this->redis->sSize('set0'));
  722. $this->assertEquals(1, $this->redis->sSize('set1'));
  723. $this->assertEquals(array('val2'), $this->redis->sGetMembers('set0'));
  724. $this->assertEquals(array('val'), $this->redis->sGetMembers('set1'));
  725. }
  726. public function testsPop()
  727. {
  728. $this->redis->delete('set0');
  729. $this->assertTrue($this->redis->sPop('set0') === FALSE);
  730. $this->redis->sAdd('set0', 'val');
  731. $this->redis->sAdd('set0', 'val2');
  732. $v0 = $this->redis->sPop('set0');
  733. $this->assertTrue(1 === $this->redis->sSize('set0'));
  734. $this->assertTrue($v0 === 'val' || $v0 === 'val2');
  735. $v1 = $this->redis->sPop('set0');
  736. $this->assertTrue(0 === $this->redis->sSize('set0'));
  737. $this->assertTrue(($v0 === 'val' && $v1 === 'val2') || ($v1 === 'val' && $v0 === 'val2'));
  738. $this->assertTrue($this->redis->sPop('set0') === FALSE);
  739. }
  740. public function testsRandMember() {
  741. $this->redis->delete('set0');
  742. $this->assertTrue($this->redis->sRandMember('set0') === FALSE);
  743. $this->redis->sAdd('set0', 'val');
  744. $this->redis->sAdd('set0', 'val2');
  745. $got = array();
  746. while(true) {
  747. $v = $this->redis->sRandMember('set0');
  748. $this->assertTrue(2 === $this->redis->sSize('set0')); // no change.
  749. $this->assertTrue($v === 'val' || $v === 'val2');
  750. $got[$v] = $v;
  751. if(count($got) == 2) {
  752. break;
  753. }
  754. }
  755. }
  756. public function testsContains()
  757. {
  758. $this->redis->delete('set');
  759. $this->redis->sAdd('set', 'val');
  760. $this->assertTrue($this->redis->sContains('set', 'val'));
  761. $this->assertFalse($this->redis->sContains('set', 'val2'));
  762. }
  763. public function testsGetMembers()
  764. {
  765. $this->redis->delete('set');
  766. $this->redis->sAdd('set', 'val');
  767. $this->redis->sAdd('set', 'val2');
  768. $this->redis->sAdd('set', 'val3');
  769. $array = array('val', 'val2', 'val3');
  770. $this->assertEquals($array, $this->redis->sGetMembers('set'));
  771. $this->assertEquals($array, $this->redis->sMembers('set')); // test alias
  772. }
  773. public function testlSet() {
  774. $this->redis->delete('list');
  775. $this->redis->lPush('list', 'val');
  776. $this->redis->lPush('list', 'val2');
  777. $this->redis->lPush('list', 'val3');
  778. $this->assertEquals($this->redis->lGet('list', 0), 'val3');
  779. $this->assertEquals($this->redis->lGet('list', 1), 'val2');
  780. $this->assertEquals($this->redis->lGet('list', 2), 'val');
  781. $this->assertEquals(TRUE, $this->redis->lSet('list', 1, 'valx'));
  782. $this->assertEquals($this->redis->lGet('list', 0), 'val3');
  783. $this->assertEquals($this->redis->lGet('list', 1), 'valx');
  784. $this->assertEquals($this->redis->lGet('list', 2), 'val');
  785. }
  786. public function testsInter() {
  787. $this->redis->delete('x'); // set of odd numbers
  788. $this->redis->delete('y'); // set of prime numbers
  789. $this->redis->delete('z'); // set of squares
  790. $this->redis->delete('t'); // set of numbers of the form n^2 - 1
  791. $x = array(1,3,5,7,9,11,13,15,17,19,21,23,25);
  792. foreach($x as $i) {
  793. $this->redis->sAdd('x', $i);
  794. }
  795. $y = array(1,2,3,5,7,11,13,17,19,23);
  796. foreach($y as $i) {
  797. $this->redis->sAdd('y', $i);
  798. }
  799. $z = array(1,4,9,16,25);
  800. foreach($z as $i) {
  801. $this->redis->sAdd('z', $i);
  802. }
  803. $t = array(2,5,10,17,26);
  804. foreach($t as $i) {
  805. $this->redis->sAdd('t', $i);
  806. }
  807. $xy = $this->redis->sInter('x', 'y'); // odd prime numbers
  808. foreach($xy as $i) {
  809. $i = (int)$i;
  810. $this->assertTrue(in_array($i, array_intersect($x, $y)));
  811. }
  812. $xy = $this->redis->sInter(array('x', 'y')); // odd prime numbers, as array.
  813. foreach($xy as $i) {
  814. $i = (int)$i;
  815. $this->assertTrue(in_array($i, array_intersect($x, $y)));
  816. }
  817. $yz = $this->redis->sInter('y', 'z'); // set of odd squares
  818. foreach($yz as $i) {
  819. $i = (int)$i;
  820. $this->assertTrue(in_array($i, array_intersect($y, $z)));
  821. }
  822. $yz = $this->redis->sInter(array('y', 'z')); // set of odd squares, as array
  823. foreach($yz as $i) {
  824. $i = (int)$i;
  825. $this->assertTrue(in_array($i, array_intersect($y, $z)));
  826. }
  827. $zt = $this->redis->sInter('z', 't'); // prime squares
  828. $this->assertTrue($zt === array());
  829. $zt = $this->redis->sInter(array('z', 't')); // prime squares, as array
  830. $this->assertTrue($zt === array());
  831. $xyz = $this->redis->sInter('x', 'y', 'z');// odd prime squares
  832. $this->assertTrue($xyz === array('1'));
  833. $xyz = $this->redis->sInter(array('x', 'y', 'z'));// odd prime squares, with an array as a parameter
  834. $this->assertTrue($xyz === array('1'));
  835. $nil = $this->redis->sInter();
  836. $this->assertTrue($nil === FALSE);
  837. $nil = $this->redis->sInter(array());
  838. $this->assertTrue($nil === FALSE);
  839. }
  840. public function testsInterStore() {
  841. $this->redis->delete('x'); // set of odd numbers
  842. $this->redis->delete('y'); // set of prime numbers
  843. $this->redis->delete('z'); // set of squares
  844. $this->redis->delete('t'); // set of numbers of the form n^2 - 1
  845. $x = array(1,3,5,7,9,11,13,15,17,19,21,23,25);
  846. foreach($x as $i) {
  847. $this->redis->sAdd('x', $i);
  848. }
  849. $y = array(1,2,3,5,7,11,13,17,19,23);
  850. foreach($y as $i) {
  851. $this->redis->sAdd('y', $i);
  852. }
  853. $z = array(1,4,9,16,25);
  854. foreach($z as $i) {
  855. $this->redis->sAdd('z', $i);
  856. }
  857. $t = array(2,5,10,17,26);
  858. foreach($t as $i) {
  859. $this->redis->sAdd('t', $i);
  860. }
  861. $count = $this->redis->sInterStore('k', 'x', 'y'); // odd prime numbers
  862. $this->assertEquals($count, $this->redis->sSize('k'));
  863. foreach(array_intersect($x, $y) as $i) {
  864. $this->assertTrue($this->redis->sContains('k', $i));
  865. }
  866. $count = $this->redis->sInterStore('k', 'y', 'z'); // set of odd squares
  867. $this->assertEquals($count, $this->redis->sSize('k'));
  868. foreach(array_intersect($y, $z) as $i) {
  869. $this->assertTrue($this->redis->sContains('k', $i));
  870. }
  871. $count = $this->redis->sInterStore('k', 'z', 't'); // squares of the form n^2 + 1
  872. $this->assertEquals($count, 0);
  873. $this->assertEquals($count, $this->redis->sSize('k'));
  874. $this->redis->delete('z');
  875. $xyz = $this->redis->sInterStore('k', 'x', 'y', 'z'); // only z missing, expect 0.
  876. $this->assertTrue($xyz === 0);
  877. $this->redis->delete('y');
  878. $xyz = $this->redis->sInterStore('k', 'x', 'y', 'z'); // y and z missing, expect 0.
  879. $this->assertTrue($xyz === 0);
  880. $this->redis->delete('x');
  881. $xyz = $this->redis->sInterStore('k', 'x', 'y', 'z'); // x y and z ALL missing, expect 0.
  882. $this->assertTrue($xyz === 0);
  883. $o = $this->redis->sInterStore('k');
  884. $this->assertTrue($o === FALSE); // error, wrong parameter count
  885. }
  886. public function testsUnion() {
  887. $this->redis->delete('x'); // set of odd numbers
  888. $this->redis->delete('y'); // set of prime numbers
  889. $this->redis->delete('z'); // set of squares
  890. $this->redis->delete('t'); // set of numbers of the form n^2 - 1
  891. $x = array(1,3,5,7,9,11,13,15,17,19,21,23,25);
  892. foreach($x as $i) {
  893. $this->redis->sAdd('x', $i);
  894. }
  895. $y = array(1,2,3,5,7,11,13,17,19,23);
  896. foreach($y as $i) {
  897. $this->redis->sAdd('y', $i);
  898. }
  899. $z = array(1,4,9,16,25);
  900. foreach($z as $i) {
  901. $this->redis->sAdd('z', $i);
  902. }
  903. $t = array(2,5,10,17,26);
  904. foreach($t as $i) {
  905. $this->redis->sAdd('t', $i);
  906. }
  907. $xy = $this->redis->sUnion('x', 'y'); // x U y
  908. foreach($xy as $i) {
  909. $i = (int)$i;
  910. $this->assertTrue(in_array($i, array_merge($x, $y)));
  911. }
  912. $yz = $this->redis->sUnion('y', 'z'); // y U Z
  913. foreach($yz as $i) {
  914. $i = (int)$i;
  915. $this->assertTrue(in_array($i, array_merge($y, $z)));
  916. }
  917. $zt = $this->redis->sUnion('z', 't'); // z U t
  918. foreach($zt as $i) {
  919. $i = (int)$i;
  920. $this->assertTrue(in_array($i, array_merge($z, $t)));
  921. }
  922. $xyz = $this->redis->sUnion('x', 'y', 'z'); // x U y U z
  923. foreach($xyz as $i) {
  924. $i = (int)$i;
  925. $this->assertTrue(in_array($i, array_merge($x, $y, $z)));
  926. }
  927. $nil = $this->redis->sUnion();
  928. $this->assertTrue($nil === FALSE);
  929. }
  930. public function testsUnionStore() {
  931. $this->redis->delete('x'); // set of odd numbers
  932. $this->redis->delete('y'); // set of prime numbers
  933. $this->redis->delete('z'); // set of squares
  934. $this->redis->delete('t'); // set of numbers of the form n^2 - 1
  935. $x = array(1,3,5,7,9,11,13,15,17,19,21,23,25);
  936. foreach($x as $i) {
  937. $this->redis->sAdd('x', $i);
  938. }
  939. $y = array(1,2,3,5,7,11,13,17,19,23);
  940. foreach($y as $i) {
  941. $this->redis->sAdd('y', $i);
  942. }
  943. $z = array(1,4,9,16,25);
  944. foreach($z as $i) {
  945. $this->redis->sAdd('z', $i);
  946. }
  947. $t = array(2,5,10,17,26);
  948. foreach($t as $i) {
  949. $this->redis->sAdd('t', $i);
  950. }
  951. $count = $this->redis->sUnionStore('k', 'x', 'y'); // x U y
  952. $xy = array_unique(array_merge($x, $y));
  953. $this->assertEquals($count, count($xy));
  954. foreach($xy as $i) {
  955. $i = (int)$i;
  956. $this->assertTrue($this->redis->sContains('k', $i));
  957. }
  958. $count = $this->redis->sUnionStore('k', 'y', 'z'); // y U z
  959. $yz = array_unique(array_merge($y, $z));
  960. $this->assertEquals($count, count($yz));
  961. foreach($yz as $i) {
  962. $i = (int)$i;
  963. $this->assertTrue($this->redis->sContains('k', $i));
  964. }
  965. $count = $this->redis->sUnionStore('k', 'z', 't'); // z U t
  966. $zt = array_unique(array_merge($z, $t));
  967. $this->assertEquals($count, count($zt));
  968. foreach($zt as $i) {
  969. $i = (int)$i;
  970. $this->assertTrue($this->redis->sContains('k', $i));
  971. }
  972. $count = $this->redis->sUnionStore('k', 'x', 'y', 'z'); // x U y U z
  973. $xyz = array_unique(array_merge($x, $y, $z));
  974. $this->assertEquals($count, count($xyz));
  975. foreach($xyz as $i) {
  976. $i = (int)$i;
  977. $this->assertTrue($this->redis->sContains('k', $i));
  978. }
  979. $this->redis->delete('x'); // x missing now
  980. $count = $this->redis->sUnionStore('k', 'x', 'y', 'z'); // x U y U z
  981. $this->assertTrue($count === count(array_unique(array_merge($y, $z))));
  982. $this->redis->delete('y'); // x and y missing
  983. $count = $this->redis->sUnionStore('k', 'x', 'y', 'z'); // x U y U z
  984. $this->assertTrue($count === count(array_unique($z)));
  985. $this->redis->delete('z'); // x, y, and z ALL missing
  986. $count = $this->redis->sUnionStore('k', 'x', 'y', 'z'); // x U y U z
  987. $this->assertTrue($count === 0);
  988. $count = $this->redis->sUnionStore('k'); // Union on nothing...
  989. $this->assertTrue($count === FALSE);
  990. }
  991. public function testsDiff() {
  992. $this->redis->delete('x'); // set of odd numbers
  993. $this->redis->delete('y'); // set of prime numbers
  994. $this->redis->delete('z'); // set of squares
  995. $this->redis->delete('t'); // set of numbers of the form n^2 - 1
  996. $x = array(1,3,5,7,9,11,13,15,17,19,21,23,25);
  997. foreach($x as $i) {
  998. $this->redis->sAdd('x', $i);
  999. }
  1000. $y = array(1,2,3,5,7,11,13,17,19,23);
  1001. foreach($y as $i) {
  1002. $this->redis->sAdd('y', $i);
  1003. }
  1004. $z = array(1,4,9,16,25);
  1005. foreach($z as $i) {
  1006. $this->redis->sAdd('z', $i);
  1007. }
  1008. $t = array(2,5,10,17,26);
  1009. foreach($t as $i) {
  1010. $this->redis->sAdd('t', $i);
  1011. }
  1012. $xy = $this->redis->sDiff('x', 'y'); // x U y
  1013. foreach($xy as $i) {
  1014. $i = (int)$i;
  1015. $this->assertTrue(in_array($i, array_diff($x, $y)));
  1016. }
  1017. $yz = $this->redis->sDiff('y', 'z'); // y U Z
  1018. foreach($yz as $i) {
  1019. $i = (int)$i;
  1020. $this->assertTrue(in_array($i, array_diff($y, $z)));
  1021. }
  1022. $zt = $this->redis->sDiff('z', 't'); // z U t
  1023. foreach($zt as $i) {
  1024. $i = (int)$i;
  1025. $this->assertTrue(in_array($i, array_diff($z, $t)));
  1026. }
  1027. $xyz = $this->redis->sDiff('x', 'y', 'z'); // x U y U z
  1028. foreach($xyz as $i) {
  1029. $i = (int)$i;
  1030. $this->assertTrue(in_array($i, array_diff($x, $y, $z)));
  1031. }
  1032. $nil = $this->redis->sDiff();
  1033. $this->assertTrue($nil === FALSE);
  1034. }
  1035. public function testsDiffStore() {
  1036. $this->redis->delete('x'); // set of odd numbers
  1037. $this->redis->delete('y'); // set of prime numbers
  1038. $this->redis->delete('z'); // set of squares
  1039. $this->redis->delete('t'); // set of numbers of the form n^2 - 1
  1040. $x = array(1,3,5,7,9,11,13,15,17,19,21,23,25);
  1041. foreach($x as $i) {
  1042. $this->redis->sAdd('x', $i);
  1043. }
  1044. $y = array(1,2,3,5,7,11,13,17,19,23);
  1045. foreach($y as $i) {
  1046. $this->redis->sAdd('y', $i);
  1047. }
  1048. $z = array(1,4,9,16,25);
  1049. foreach($z as $i) {
  1050. $this->redis->sAdd('z', $i);
  1051. }
  1052. $t = array(2,5,10,17,26);
  1053. foreach($t as $i) {
  1054. $this->redis->sAdd('t', $i);
  1055. }
  1056. $count = $this->redis->sDiffStore('k', 'x', 'y'); // x - y
  1057. $xy = array_unique(array_diff($x, $y));
  1058. $this->assertEquals($count, count($xy));
  1059. foreach($xy as $i) {
  1060. $i = (int)$i;
  1061. $this->assertTrue($this->redis->sContains('k', $i));
  1062. }
  1063. $count = $this->redis->sDiffStore('k', 'y', 'z'); // y - z
  1064. $yz = array_unique(array_diff($y, $z));
  1065. $this->assertEquals($count, count($yz));
  1066. foreach($yz as $i) {
  1067. $i = (int)$i;
  1068. $this->assertTrue($this->redis->sContains('k', $i));
  1069. }
  1070. $count = $this->redis->sDiffStore('k', 'z', 't'); // z - t
  1071. $zt = array_unique(array_diff($z, $t));
  1072. $this->assertEquals($count, count($zt));
  1073. foreach($zt as $i) {
  1074. $i = (int)$i;
  1075. $this->assertTrue($this->redis->sContains('k', $i));
  1076. }
  1077. $count = $this->redis->sDiffStore('k', 'x', 'y', 'z'); // x - y - z
  1078. $xyz = array_unique(array_diff($x, $y, $z));
  1079. $this->assertEquals($count, count($xyz));
  1080. foreach($xyz as $i) {
  1081. $i = (int)$i;
  1082. $this->assertTrue($this->redis->sContains('k', $i));
  1083. }
  1084. $this->redis->delete('x'); // x missing now
  1085. $count = $this->redis->sDiffStore('k', 'x', 'y', 'z'); // x - y - z
  1086. $this->assertTrue($count === 0);
  1087. $this->redis->delete('y'); // x and y missing
  1088. $count = $this->redis->sDiffStore('k', 'x', 'y', 'z'); // x - y - z
  1089. $this->assertTrue($count === 0);
  1090. $this->redis->delete('z'); // x, y, and z ALL missing
  1091. $count = $this->redis->sDiffStore('k', 'x', 'y', 'z'); // x - y - z
  1092. $this->assertTrue($count === 0);
  1093. $count = $this->redis->sDiffStore('k'); // diff on nothing...
  1094. $this->assertTrue($count === FALSE);
  1095. }
  1096. public function testlGetRange() {
  1097. $this->redis->delete('list');
  1098. $this->redis->lPush('list', 'val');
  1099. $this->redis->lPush('list', 'val2');
  1100. $this->redis->lPush('list', 'val3');
  1101. // pos : 0 1 2
  1102. // pos : -3 -2 -1
  1103. // list: [val3, val2, val]
  1104. $this->assertEquals($this->redis->lGetRange('list', 0, 0), array('val3'));
  1105. $this->assertEquals($this->redis->lGetRange('list', 0, 1), array('val3', 'val2'));
  1106. $this->assertEquals($this->redis->lGetRange('list', 0, 2), array('val3', 'val2', 'val'));
  1107. $this->assertEquals($this->redis->lGetRange('list', 0, 3), array('val3', 'val2', 'val'));
  1108. $this->assertEquals($this->redis->lGetRange('list', 0, -1), array('val3', 'val2', 'val'));
  1109. $this->assertEquals($this->redis->lGetRange('list', 0, -2), array('val3', 'val2'));
  1110. $this->assertEquals($this->redis->lGetRange('list', -2, -1), array('val2', 'val'));
  1111. $this->redis->delete('list');
  1112. $this->assertEquals($this->redis->lGetRange('list', 0, -1), array());
  1113. }
  1114. // public function testsave() {
  1115. // $this->assertTrue($this->redis->save() === TRUE); // don't really know how else to test this...
  1116. // }
  1117. // public function testbgSave() {
  1118. // // let's try to fill the DB and then bgSave twice. We expect the second one to fail.
  1119. // for($i = 0; $i < 10e+4; $i++) {
  1120. // $s = md5($i);
  1121. // $this->redis->set($s, $s);
  1122. // }
  1123. // $this->assertTrue($this->redis->bgSave() === TRUE); // the first one should work.
  1124. // $this->assertTrue($this->redis->bgSave() === FALSE); // the second one should fail (still working on the first one)
  1125. // }
  1126. //
  1127. // public function testlastSave() {
  1128. // while(!$this->redis->save()) {
  1129. // sleep(1);
  1130. // }
  1131. // $t_php = microtime(TRUE);
  1132. // $t_redis = $this->redis->lastSave();
  1133. //
  1134. // $this->assertTrue($t_php - $t_redis < 10000); // check that it's approximately what we've measured in PHP.
  1135. // }
  1136. //
  1137. // public function testflushDb() {
  1138. // $this->redis->set('x', 'y');
  1139. // $this->assertTrue($this->redis->flushDb());
  1140. // $this->assertTrue($this->redis->getKeys('*') === array());
  1141. // }
  1142. //
  1143. // public function testflushAll() {
  1144. // $this->redis->set('x', 'y');
  1145. // $this->assertTrue($this->redis->flushAll());
  1146. // $this->assertTrue($this->redis->getKeys('*') === array());
  1147. // }
  1148. public function testdbSize() {
  1149. $this->assertTrue($this->redis->flushDB());
  1150. $this->redis->set('x', 'y');
  1151. $this->assertTrue($this->redis->dbSize() === 1);
  1152. }
  1153. public function testttl() {
  1154. $this->redis->set('x', 'y');
  1155. $this->redis->setTimeout('x', 5);
  1156. for($i = 5; $i > 0; $i--) {
  1157. $this->assertEquals($i, $this->redis->ttl('x'));
  1158. sleep(1);
  1159. }
  1160. }
  1161. public function testPersist() {
  1162. $this->redis->set('x', 'y');
  1163. $this->redis->setTimeout('x', 100);
  1164. $this->assertTrue(TRUE === $this->redis->persist('x')); // true if there is a timeout
  1165. $this->assertTrue(-1 === $this->redis->ttl('x')); // -1: timeout has been removed.
  1166. $this->assertTrue(FALSE === $this->redis->persist('x')); // false if there is no timeout
  1167. $this->redis->delete('x');
  1168. $this->assertTrue(FALSE === $this->redis->persist('x')); // false if the key doesn’t exist.
  1169. }
  1170. public function testinfo() {
  1171. $info = $this->redis->info();
  1172. $keys = array(
  1173. "redis_version",
  1174. "arch_bits",
  1175. "uptime_in_seconds",
  1176. "uptime_in_days",
  1177. "connected_clients",
  1178. "connected_slaves",
  1179. "used_memory",
  1180. "changes_since_last_save",
  1181. "bgsave_in_progress",
  1182. "last_save_time",
  1183. "total_connections_received",
  1184. "total_commands_processed",
  1185. "role");
  1186. foreach($keys as $k) {
  1187. $this->assertTrue(in_array($k, array_keys($info)));
  1188. }
  1189. }
  1190. public function testSelect() {
  1191. $this->assertFalse($this->redis->select(-1));
  1192. $this->assertTrue($this->redis->select(0));
  1193. }
  1194. public function testMset() {
  1195. $this->redis->delete('x', 'y', 'z'); // remove x y z
  1196. $this->assertTrue($this->redis->mset(array('x' => 'a', 'y' => 'b', 'z' => 'c'))); // set x y z
  1197. $this->assertEquals($this->redis->mget(array('x', 'y', 'z')), array('a', 'b', 'c')); // check x y z
  1198. $this->redis->delete('x'); // delete just x
  1199. $this->assertTrue($this->redis->mset(array('x' => 'a', 'y' => 'b', 'z' => 'c'))); // set x y z
  1200. $this->assertEquals($this->redis->mget(array('x', 'y', 'z')), array('a', 'b', 'c')); // check x y z
  1201. $this->assertFalse($this->redis->mset(array())); // set ø → FALSE
  1202. }
  1203. public function testMsetNX() {
  1204. $this->redis->delete('x', 'y', 'z'); // remove x y z
  1205. $this->assertTrue(TRUE === $this->redis->msetnx(array('x' => 'a', 'y' => 'b', 'z' => 'c'))); // set x y z
  1206. $this->assertEquals($this->redis->mget(array('x', 'y', 'z')), array('a', 'b', 'c')); // check x y z
  1207. $this->redis->delete('x'); // delete just x
  1208. $this->assertTrue(FALSE === $this->redis->msetnx(array('x' => 'A', 'y' => 'B', 'z' => 'C'))); // set x y z
  1209. $this->assertEquals($this->redis->mget(array('x', 'y', 'z')), array(FALSE, 'b', 'c')); // check x y z
  1210. $this->assertFalse($this->redis->msetnx(array())); // set ø → FALSE
  1211. }
  1212. public function testRpopLpush() {
  1213. // standard case.
  1214. $this->redis->delete('x', 'y');
  1215. $this->redis->lpush('x', 'abc');
  1216. $this->redis->lpush('x', 'def'); // x = [def, abc]
  1217. $this->redis->lpush('y', '123');
  1218. $this->redis->lpush('y', '456'); // y = [456, 123]
  1219. $this->assertEquals($this->redis->rpoplpush('x', 'y'), 'abc'); // we RPOP x, yielding abc.
  1220. $this->assertEquals($this->redis->lgetRange('x', 0, -1), array('def')); // only def remains in x.
  1221. $this->assertEquals($this->redis->lgetRange('y', 0, -1), array('abc', '456', '123')); // abc has been lpushed to y.
  1222. // with an empty source, expecting no change.
  1223. $this->redis->delete('x', 'y');
  1224. $this->assertTrue(FALSE === $this->redis->rpoplpush('x', 'y'));
  1225. $this->assertTrue(array() === $this->redis->lgetRange('x', 0, -1));
  1226. $this->assertTrue(array() === $this->redis->lgetRange('y', 0, -1));
  1227. }
  1228. public function testZX() {
  1229. $this->redis->delete('key');
  1230. $this->assertTrue(array() === $this->redis->zRange('key', 0, -1));
  1231. $this->assertTrue(array() === $this->redis->zRange('key', 0, -1, true));
  1232. $this->assertTrue(1 === $this->redis->zAdd('key', 0, 'val0'));
  1233. $this->assertTrue(1 === $this->redis->zAdd('key', 2, 'val2'));
  1234. $this->assertTrue(1 === $this->redis->zAdd('key', 1, 'val1'));
  1235. $this->assertTrue(1 === $this->redis->zAdd('key', 3, 'val3'));
  1236. $this->assertTrue(array('val0', 'val1', 'val2', 'val3') === $this->redis->zRange('key', 0, -1));
  1237. // withscores
  1238. $ret = $this->redis->zRange('key', 0, -1, true);
  1239. $this->assertTrue(count($ret) == 4);
  1240. $this->assertTrue($ret['val0'] == 0);
  1241. $this->assertTrue($ret['val1'] == 1);
  1242. $this->assertTrue($ret['val2'] == 2);
  1243. $this->assertTrue($ret['val3'] == 3);
  1244. $this->assertTrue(0 === $this->redis->zDelete('key', 'valX'));
  1245. $this->assertTrue(1 === $this->redis->zDelete('key', 'val3'));
  1246. $this->assertTrue(array('val0', 'val1', 'val2') === $this->redis->zRange('key', 0, -1));
  1247. // zGetReverseRange
  1248. $this->assertTrue(1 === $this->redis->zAdd('key', 3, 'val3'));
  1249. $this->assertTrue(1 === $this->redis->zAdd('key', 3, 'aal3'));
  1250. $zero_to_three = $this->redis->zRangeByScore('key', 0, 3);
  1251. $this->assertTrue(array('val0', 'val1', 'val2', 'aal3', 'val3') === $zero_to_three || array('val0', 'val1', 'val2', 'val3', 'aal3') === $zero_to_three);
  1252. $three_to_zero = $this->redis->zRevRangeByScore('key', 3, 0);
  1253. $this->assertTrue(array_reverse(array('val0', 'val1', 'val2', 'aal3', 'val3')) === $three_to_zero || array_reverse(array('val0', 'val1', 'val2', 'val3', 'aal3')) === $three_to_zero);
  1254. $this->assertTrue(5 === $this->redis->zCount('key', 0, 3));
  1255. // withscores
  1256. $this->redis->zRemove('key', 'aal3');
  1257. $zero_to_three = $this->redis->zRangeByScore('key', 0, 3, array('withscores' => TRUE));
  1258. $this->assertTrue(array('val0' => 0, 'val1' => 1, 'val2' => 2, 'val3' => 3) == $zero_to_three);
  1259. $this->assertTrue(4 === $this->redis->zCount('key', 0, 3));
  1260. // limit
  1261. $this->assertTrue(array('val0') === $this->redis->zRangeByScore('key', 0, 3, array('limit' => array(0, 1))));
  1262. $this->assertTrue(array('val0', 'val1') === $this->redis->zRangeByScore('key', 0, 3, array('limit' => array(0, 2))));
  1263. $this->assertTrue(array('val1', 'val2') === $this->redis->zRangeByScore('key', 0, 3, array('limit' => array(1, 2))));
  1264. $this->assertTrue(array('val0', 'val1') === $this->redis->zRangeByScore('key', 0, 1, array('limit' => array(0, 100))));
  1265. $this->assertTrue(array('val3') === $this->redis->zRevRangeByScore('key', 3, 0, array('limit' => array(0, 1))));
  1266. $this->assertTrue(array('val3', 'val2') === $this->redis->zRevRangeByScore('key', 3, 0, array('limit' => array(0, 2))));
  1267. $this->assertTrue(array('val2', 'val1') === $this->redis->zRevRangeByScore('key', 3, 0, array('limit' => array(1, 2))));
  1268. $this->assertTrue(array('val1', 'val0') === $this->redis->zRevRangeByScore('key', 1, 0, array('limit' => array(0, 100))));
  1269. $this->assertTrue(4 === $this->redis->zSize('key'));
  1270. $this->assertTrue(1.0 === $this->redis->zScore('key', 'val1'));
  1271. $this->assertFalse($this->redis->zScore('key', 'val'));
  1272. $this->assertFalse($this->redis->zScore(3, 2));
  1273. // with () and +inf, -inf
  1274. $this->redis->delete('zset');
  1275. $this->redis->zAdd('zset', 1, 'foo');
  1276. $this->redis->zAdd('zset', 2, 'bar');
  1277. $this->redis->zAdd('zset', 3, 'biz');
  1278. $this->redis->zAdd('zset', 4, 'foz');
  1279. $this->assertTrue(array('foo' => 1, 'bar' => 2, 'biz' => 3, 'foz' => 4) == $this->redis->zRangeByScore('zset', '-inf', '+inf', array('withscores' => TRUE)));
  1280. $this->assertTrue(array('foo' => 1, 'bar' => 2) == $this->redis->zRangeByScore('zset', 1, 2, array('withscores' => TRUE)));
  1281. $this->assertTrue(array('bar' => 2) == $this->redis->zRangeByScore('zset', '(1', 2, array('withscores' => TRUE)));
  1282. $this->assertTrue(array() == $this->redis->zRangeByScore('zset', '(1', '(2', array('withscores' => TRUE)));
  1283. $this->assertTrue(4 == $this->redis->zCount('zset', '-inf', '+inf'));
  1284. $this->assertTrue(2 == $this->redis->zCount('zset', 1, 2));
  1285. $this->assertTrue(1 == $this->redis->zCount('zset', '(1', 2));
  1286. $this->assertTrue(0 == $this->redis->zCount('zset', '(1', '(2'));
  1287. // zincrby
  1288. $this->redis->delete('key');
  1289. $this->assertTrue(1.0 === $this->redis->zIncrBy('key', 1, 'val1'));
  1290. $this->assertTrue(1.0 === $this->redis->zScore('key', 'val1'));
  1291. $this->assertTrue(2.5 === $this->redis->zIncrBy('key', 1.5, 'val1'));
  1292. $this->assertTrue(2.5 === $this->redis->zScore('key', 'val1'));
  1293. //zUnion
  1294. $this->redis->delete('key1');
  1295. $this->redis->delete('key2');
  1296. $this->redis->delete('key3');
  1297. $this->redis->delete('keyU');
  1298. $this->redis->zAdd('key1', 0, 'val0');
  1299. $this->redis->zAdd('key1', 1, 'val1');
  1300. $this->redis->zAdd('key2', 2, 'val2');
  1301. $this->redis->zAdd('key2', 3, 'val3');
  1302. $this->redis->zAdd('key3', 4, 'val4');
  1303. $this->redis->zAdd('key3', 5, 'val5');
  1304. $this->assertTrue(4 === $this->redis->zUnion('keyU', array('key1', 'key3')));
  1305. $this->assertTrue(array('val0', 'val1', 'val4', 'val5') === $this->redis->zRange('keyU', 0, -1));
  1306. // Union on non existing keys
  1307. $this->redis->delete('keyU');
  1308. $this->assertTrue(0 === $this->redis->zUnion('keyU', array('X', 'Y')));
  1309. $this->assertTrue(array() === $this->redis->zRange('keyU', 0, -1));
  1310. // !Exist U Exist
  1311. $this->redis->delete('keyU');
  1312. $this->assertTrue(2 === $this->redis->zUnion('keyU', array('key1', 'X')));
  1313. $this->assertTrue($this->redis->zRange('key1', 0, -1) === $this->redis->zRange('keyU', 0, -1));
  1314. // test weighted zUnion
  1315. $this->redis->delete('keyZ');
  1316. $this->assertTrue(4 === $this->redis->zUnion('keyZ', array('key1', 'key2'), array(1, 1)));
  1317. $this->assertTrue(array('val0', 'val1', 'val2', 'val3') === $this->redis->zRange('keyZ', 0, -1));
  1318. $this->redis->zDeleteRangeByScore('keyZ', 0, 10);
  1319. $this->assertTrue(4 === $this->redis->zUnion('keyZ', array('key1', 'key2'), array(5, 1)));
  1320. $this->assertTrue(array('val0', 'val2', 'val3', 'val1') === $this->redis->zRange('keyZ', 0, -1));
  1321. $this->redis->delete('key1');
  1322. $this->redis->delete('key2');
  1323. $this->redis->delete('key3');
  1324. // ZREMRANGEBYRANK
  1325. $this->redis->zAdd('key1', 1, 'one');
  1326. $this->redis->zAdd('key1', 2, 'two');
  1327. $this->redis->zAdd('key1', 3, 'three');
  1328. $this->assertTrue(2 === $this->redis->zremrangebyrank('key1', 0, 1));
  1329. $this->assertTrue(array('three' => 3) == $this->redis->zRange('key1', 0, -1, TRUE));
  1330. $this->redis->delete('key1');
  1331. // zInter
  1332. $this->redis->zAdd('key1', 0, 'val0');
  1333. $this->redis->zAdd('key1', 1, 'val1');
  1334. $this->redis->zAdd('key1', 3, 'val3');
  1335. $this->redis->zAdd('key2', 2, 'val1');
  1336. $this->redis->zAdd('key2', 3, 'val3');
  1337. $this->redis->zAdd('key3', 4, 'val3');
  1338. $this->redis->zAdd('key3', 5, 'val5');
  1339. $this->redis->delete('keyI');
  1340. $this->assertTrue(2 === $this->redis->zInter('keyI', array('key1', 'key2')));
  1341. $this->assertTrue(array('val1', 'val3') === $this->redis->zRange('keyI', 0, -1));
  1342. // Union on non existing keys
  1343. $this->assertTrue(0 === $this->redis->zInter('keyX', array('X', 'Y')));
  1344. $this->assertTrue(array() === $this->redis->zRange('keyX', 0, -1));
  1345. // !Exist U Exist
  1346. $this->assertTrue(0 === $this->redis->zInter('keyY', array('key1', 'X')));
  1347. $this->assertTrue(array() === $this->redis->zRange('keyY', 0, -1));
  1348. // test weighted zInter
  1349. $this->redis->delete('key1');
  1350. $this->redis->delete('key2');
  1351. $this->redis->delete('key3');
  1352. $this->redis->zAdd('key1', 0, 'val0');
  1353. $this->redis->zAdd('key1', 1, 'val1');
  1354. $this->redis->zAdd('key1', 3, 'val3');
  1355. $this->redis->zAdd('key2', 2, 'val1');
  1356. $this->redis->zAdd('key2', 1, 'val3');
  1357. $this->redis->zAdd('key3', 7, 'val1');
  1358. $this->redis->zAdd('key3', 3, 'val3');
  1359. $this->redis->delete('keyI');
  1360. $this->assertTrue(2 === $this->redis->zInter('keyI', array('key1', 'key2'), array(1, 1)));
  1361. $this->assertTrue(array('val1', 'val3') === $this->redis->zRange('keyI', 0, -1));
  1362. $this->redis->delete('keyI');
  1363. $this->assertTrue( 2 === $this->redis->zInter('keyI', array('key1', 'key2', 'key3'), array(1, 5, 1), 'min'));
  1364. $this->assertTrue(array('val1', 'val3') === $this->redis->zRange('keyI', 0, -1));
  1365. $this->redis->delete('keyI');
  1366. $this->assertTrue( 2 === $this->redis->zInter('keyI', array('key1', 'key2', 'key3'), array(1, 5, 1), 'max'));
  1367. $this->assertTrue(array('val3', 'val1') === $this->redis->zRange('keyI', 0, -1));
  1368. // zrank, zrevrank
  1369. $this->redis->delete('z');
  1370. $this->redis->zadd('z', 1, 'one');
  1371. $this->redis->zadd('z', 2, 'two');
  1372. $this->redis->zadd('z', 5, 'five');
  1373. $this->assertTrue(0 === $this->redis->zRank('z', 'one'));
  1374. $this->assertTrue(1 === $this->redis->zRank('z', 'two'));
  1375. $this->assertTrue(2 === $this->redis->zRank('z', 'five'));
  1376. $this->assertTrue(2 === $this->redis->zRevRank('z', 'one'));
  1377. $this->assertTrue(1 === $this->redis->zRevRank('z', 'two'));
  1378. $this->assertTrue(0 === $this->redis->zRevRank('z', 'five'));
  1379. }
  1380. public function testHashes() {
  1381. $this->redis->delete('h', 'key');
  1382. $this->assertTrue(0 === $this->redis->hLen('h'));
  1383. $this->assertTrue(1 === $this->redis->hSet('h', 'a', 'a-value'));
  1384. $this->assertTrue(1 === $this->redis->hLen('h'));
  1385. $this->assertTrue(1 === $this->redis->hSet('h', 'b', 'b-value'));
  1386. $this->assertTrue(2 === $this->redis->hLen('h'));
  1387. $this->assertTrue('a-value' === $this->redis->hGet('h', 'a')); // simple get
  1388. $this->assertTrue('b-value' === $this->redis->hGet('h', 'b')); // simple get
  1389. $this->assertTrue(0 === $this->redis->hSet('h', 'a', 'another-value')); // replacement
  1390. $this->assertTrue('another-value' === $this->redis->hGet('h', 'a')); // get the new value
  1391. $this->assertTrue('b-value' === $this->redis->hGet('h', 'b')); // simple get
  1392. $this->assertTrue(FALSE === $this->redis->hGet('h', 'c')); // unknown hash member
  1393. $this->assertTrue(FALSE === $this->redis->hGet('key', 'c')); // unknownkey
  1394. // hDel
  1395. $this->assertTrue(TRUE === $this->redis->hDel('h', 'a')); // TRUE on success
  1396. $this->assertTrue(FALSE === $this->redis->hDel('h', 'a')); // FALSE on failure
  1397. $this->redis->delete('h');
  1398. $this->redis->hSet('h', 'x', 'a');
  1399. $this->redis->hSet('h', 'y', 'b');
  1400. // hsetnx
  1401. $this->redis->delete('h');
  1402. $this->assertTrue(TRUE === $this->redis->hSetNx('h', 'x', 'a'));
  1403. $this->assertTrue(TRUE === $this->redis->hSetNx('h', 'y', 'b'));
  1404. $this->assertTrue(FALSE === $this->redis->hSetNx('h', 'x', '?'));
  1405. $this->assertTrue(FALSE === $this->redis->hSetNx('h', 'y', '?'));
  1406. $this->assertTrue('a' === $this->redis->hGet('h', 'x'));
  1407. $this->assertTrue('b' === $this->redis->hGet('h', 'y'));
  1408. // keys
  1409. $keys = $this->redis->hKeys('h');
  1410. $this->assertTrue($keys === array('x', 'y') || $keys === array('y', 'x'));
  1411. // values
  1412. $values = $this->redis->hVals('h');
  1413. $this->assertTrue($values === array('a', 'b') || $values === array('b', 'a'));
  1414. // keys + values
  1415. $all = $this->redis->hGetAll('h');
  1416. $this->assertTrue($all === array('x' => 'a', 'y' => 'b') || $all === array('y' => 'b', 'x' => 'a'));
  1417. // hExists
  1418. $this->assertTrue(TRUE === $this->redis->hExists('h', 'x'));
  1419. $this->assertTrue(TRUE === $this->redis->hExists('h', 'y'));
  1420. $this->assertTrue(FALSE === $this->redis->hExists('h', 'w'));
  1421. $this->redis->delete('h');
  1422. $this->assertTrue(FALSE === $this->redis->hExists('h', 'x'));
  1423. // hIncrBy
  1424. $this->redis->delete('h');
  1425. $this->assertTrue(2 === $this->redis->hIncrBy('h', 'x', 2));
  1426. $this->assertTrue(3 === $this->redis->hIncrBy('h', 'x', 1));
  1427. $this->assertTrue(2 === $this->redis->hIncrBy('h', 'x', -1));
  1428. $this->assertTrue(FALSE === $this->redis->hIncrBy('h', 'x', "not-a-number"));
  1429. $this->assertTrue("2" === $this->redis->hGet('h', 'x'));
  1430. $this->redis->hSet('h', 'y', 'not-a-number');
  1431. $this->assertTrue(FALSE === $this->redis->hIncrBy('h', 'y', 1));
  1432. // hmset
  1433. $this->redis->delete('h');
  1434. $this->assertTrue(TRUE === $this->redis->hMset('h', array('x' => 123, 'y' => 456, 'z' => 'abc')));
  1435. $this->assertTrue('123' === $this->redis->hGet('h', 'x'));
  1436. $this->assertTrue('456' === $this->redis->hGet('h', 'y'));
  1437. $this->assertTrue('abc' === $this->redis->hGet('h', 'z'));
  1438. $this->assertTrue(FALSE === $this->redis->hGet('h', 't'));
  1439. // hmget
  1440. $this->assertTrue(array('x' => '123', 'y' => '456') === $this->redis->hMget('h', array('x', 'y')));
  1441. $this->assertTrue(array('z' => 'abc') === $this->redis->hMget('h', array('z')));
  1442. $this->assertTrue(array('x' => '123', 't' => FALSE, 'y' => '456') === $this->redis->hMget('h', array('x', 't', 'y')));
  1443. $this->assertFalse(array(123 => 'x') === $this->redis->hMget('h', array(123)));
  1444. $this->assertTrue(array(123 => FALSE) === $this->redis->hMget('h', array(123)));
  1445. // hmget/hmset with numeric fields
  1446. $this->redis->del('h');
  1447. $this->assertTrue(TRUE === $this->redis->hMset('h', array(123 => 'x', 'y' => 456)));
  1448. $this->assertTrue('x' === $this->redis->hGet('h', 123));
  1449. $this->assertTrue('x' === $this->redis->hGet('h', '123'));
  1450. $this->assertTrue('456' === $this->redis->hGet('h', 'y'));
  1451. $this->assertTrue(array(123 => 'x', 'y' => '456') === $this->redis->hMget('h', array('123', 'y')));
  1452. // check non-string types.
  1453. $this->redis->delete('h1');
  1454. $this->assertTrue(TRUE === $this->redis->hMSet('h1', array('x' => 0, 'y' => array(), 'z' => new stdclass(), 't' => NULL)));
  1455. $h1 = $this->redis->hGetAll('h1');
  1456. $this->assertTrue('0' === $h1['x']);
  1457. $this->assertTrue('Array' === $h1['y']);
  1458. $this->assertTrue('Object' === $h1['z']);
  1459. $this->assertTrue('' === $h1['t']);
  1460. }
  1461. public function testSetRange() {
  1462. $this->redis->delete('key');
  1463. $this->redis->set('key', 'hello world');
  1464. $this->redis->setRange('key', 6, 'redis');
  1465. $this->assertTrue('hello redis' === $this->redis->get('key'));
  1466. $this->redis->setRange('key', 6, 'you'); // don't cut off the end
  1467. $this->assertTrue('hello youis' === $this->redis->get('key'));
  1468. $this->redis->set('key', 'hello world');
  1469. // $this->assertTrue(11 === $this->redis->setRange('key', -6, 'redis')); // works with negative offsets too! (disabled because not all versions support this)
  1470. // $this->assertTrue('hello redis' === $this->redis->get('key'));
  1471. // fill with zeros if needed
  1472. $this->redis->delete('key');
  1473. $this->redis->setRange('key', 6, 'foo');
  1474. $this->assertTrue("\x00\x00\x00\x00\x00\x00foo" === $this->redis->get('key'));
  1475. }
  1476. public function testMultiExec() {
  1477. $this->sequence(Redis::MULTI);
  1478. // with prefix as well
  1479. $this->redis->setOption(Redis::OPT_PREFIX, "test:");
  1480. $this->sequence(Redis::MULTI);
  1481. $this->redis->setOption(Redis::OPT_PREFIX, "");
  1482. $this->redis->set('x', '42');
  1483. $this->assertTrue(TRUE === $this->redis->watch('x'));
  1484. $ret = $this->redis->multi()
  1485. ->get('x')
  1486. ->exec();
  1487. // successful transaction
  1488. $this->assertTrue($ret === array('42'));
  1489. // failed transaction
  1490. $this->redis->watch('x');
  1491. $r = $this->newInstance(); // new instance, modifying `x'.
  1492. $r->incr('x');
  1493. $ret = $this->redis->multi()
  1494. ->get('x')
  1495. ->exec();
  1496. $this->assertTrue($ret === FALSE); // failed because another client changed our watched key between WATCH and EXEC.
  1497. // watch and unwatch
  1498. $this->redis->watch('x');
  1499. $r->incr('x'); // other instance
  1500. $this->redis->unwatch(); // cancel transaction watch
  1501. $ret = $this->redis->multi()
  1502. ->get('x')
  1503. ->exec();
  1504. $this->assertTrue($ret === array('44')); // succeeded since we've cancel the WATCH command.
  1505. }
  1506. public function testPipeline() {
  1507. $this->sequence(Redis::PIPELINE);
  1508. // with prefix as well
  1509. $this->redis->setOption(Redis::OPT_PREFIX, "test:");
  1510. $this->sequence(Redis::PIPELINE);
  1511. $this->redis->setOption(Redis::OPT_PREFIX, "");
  1512. }
  1513. protected function sequence($mode) {
  1514. $ret = $this->redis->multi($mode)
  1515. ->set('x', 42)
  1516. ->info()
  1517. ->type('x')
  1518. ->get('x')
  1519. ->exec();
  1520. $this->assertTrue(is_array($ret));
  1521. $i = 0;
  1522. $this->assertTrue($ret[$i++] == TRUE);
  1523. $this->assertTrue(is_array($ret[$i++]));
  1524. $this->assertTrue($ret[$i++] === Redis::REDIS_STRING);
  1525. $this->assertTrue($ret[$i] === '42' || $ret[$i] === 42);
  1526. $serializer = $this->redis->getOption(Redis::OPT_SERIALIZER);
  1527. $this->redis->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_NONE); // testing incr, which doesn't work with the serializer
  1528. $ret = $this->redis->multi($mode)
  1529. ->delete('key1')
  1530. ->set('key1', 'value1')
  1531. ->get('key1')
  1532. ->getSet('key1', 'value2')
  1533. ->get('key1')
  1534. ->set('key2', 4)
  1535. ->incr('key2')
  1536. ->get('key2')
  1537. ->decr('key2')
  1538. ->get('key2')
  1539. ->renameKey('key2', 'key3')
  1540. ->get('key3')
  1541. ->renameNx('key3', 'key1')
  1542. ->renameKey('key3', 'key2')
  1543. ->incr('key2', 5)
  1544. ->get('key2')
  1545. ->decr('key2', 5)
  1546. ->get('key2')
  1547. ->exec();
  1548. $this->assertTrue(is_array($ret));
  1549. $i = 0;
  1550. $this->assertTrue(is_long($ret[$i++]));
  1551. $this->assertTrue($ret[$i++] == TRUE);
  1552. $this->assertTrue($ret[$i++] == 'value1');
  1553. $this->assertTrue($ret[$i++] == 'value1');
  1554. $this->assertTrue($ret[$i++] == 'value2');
  1555. $this->assertTrue($ret[$i++] == TRUE);
  1556. $this->assertTrue($ret[$i++] == 5);
  1557. $this->assertTrue($ret[$i++] == 5);
  1558. $this->assertTrue($ret[$i++] == 4);
  1559. $this->assertTrue($ret[$i++] == 4);
  1560. $this->assertTrue($ret[$i++] == TRUE);
  1561. $this->assertTrue($ret[$i++] == 4);
  1562. $this->assertTrue($ret[$i++] == FALSE);
  1563. $this->assertTrue($ret[$i++] == TRUE);
  1564. $this->assertTrue($ret[$i++] == TRUE);
  1565. $this->assertTrue($ret[$i++] == 9);
  1566. $this->assertTrue($ret[$i++] == TRUE);
  1567. $this->assertTrue($ret[$i++] == 4);
  1568. $this->assertTrue(count($ret) == $i);
  1569. $this->redis->setOption(Redis::OPT_SERIALIZER, $serializer);
  1570. $ret = $this->redis->multi($mode)
  1571. ->delete('key1')
  1572. ->delete('key2')
  1573. ->set('key1', 'val1')
  1574. ->setnx('key1', 'valX')
  1575. ->setnx('key2', 'valX')
  1576. ->exists('key1')
  1577. ->exists('key3')
  1578. ->ping()
  1579. ->exec();
  1580. $this->assertTrue(is_array($ret));
  1581. $this->assertTrue($ret[0] == TRUE);
  1582. $this->assertTrue($ret[1] == TRUE);
  1583. $this->assertTrue($ret[2] == TRUE);
  1584. $this->assertTrue($ret[3] == FALSE);
  1585. $this->assertTrue($ret[4] == TRUE);
  1586. $this->assertTrue($ret[5] == TRUE);
  1587. $this->assertTrue($ret[6] == FALSE);
  1588. $this->assertTrue($ret[7] == '+PONG');
  1589. $ret = $this->redis->multi($mode)
  1590. ->randomKey()
  1591. ->exec();
  1592. $ret = $this->redis->multi($mode)
  1593. ->exec();
  1594. $this->assertTrue($ret == array());
  1595. // ttl, mget, mset, msetnx, expire, expireAt
  1596. $this->redis->delete('key');
  1597. $ret = $this->redis->multi($mode)
  1598. ->ttl('key')
  1599. ->mget(array('key1', 'key2', 'key3'))
  1600. ->mset(array('key3' => 'value3', 'key4' => 'value4'))
  1601. ->set('key', 'value')
  1602. ->expire('key', 5)
  1603. ->ttl('key')
  1604. ->expireAt('key', '0000')
  1605. ->exec();
  1606. $this->assertTrue(is_array($ret));
  1607. $i = 0;
  1608. $this->assertTrue($ret[$i++] == -1);
  1609. $this->assertTrue($ret[$i++] === array('val1', 'valX', FALSE)); // mget
  1610. $this->assertTrue($ret[$i++] === TRUE); // mset
  1611. $this->assertTrue($ret[$i++] === TRUE); // set
  1612. $this->assertTrue($ret[$i++] === TRUE); // expire
  1613. $this->assertTrue($ret[$i++] === 5); // ttl
  1614. $this->assertTrue($ret[$i++] === TRUE); // expireAt
  1615. $this->assertTrue(count($ret) == $i);
  1616. $ret = $this->redis->multi($mode)
  1617. ->set('lkey', 'x')
  1618. ->set('lDest', 'y')
  1619. ->delete('lkey', 'lDest')
  1620. ->rpush('lkey', 'lvalue')
  1621. ->lpush('lkey', 'lvalue')
  1622. ->lpush('lkey', 'lvalue')
  1623. ->lpush('lkey', 'lvalue')
  1624. ->lpush('lkey', 'lvalue')
  1625. ->lpush('lkey', 'lvalue')
  1626. ->rpoplpush('lkey', 'lDest')
  1627. ->lGetRange('lDest', 0, -1)
  1628. ->lpop('lkey')
  1629. ->llen('lkey')
  1630. ->lRemove('lkey', 'lvalue', 3)
  1631. ->llen('lkey')
  1632. ->lget('lkey', 0)
  1633. ->lGetRange('lkey', 0, -1)
  1634. ->lSet('lkey', 1, "newValue") // check errors on key not exists
  1635. ->lGetRange('lkey', 0, -1)
  1636. ->llen('lkey')
  1637. ->exec();
  1638. $this->assertTrue(is_array($ret));
  1639. $i = 0;
  1640. $this->assertTrue($ret[$i++] === TRUE); // SET
  1641. $this->assertTrue($ret[$i++] === TRUE); // SET
  1642. $this->assertTrue($ret[$i++] === 2); // deleting 2 keys
  1643. $this->assertTrue($ret[$i++] === 1); // rpush, now 1 element
  1644. $this->assertTrue($ret[$i++] === 2); // lpush, now 2 elements
  1645. $this->assertTrue($ret[$i++] === 3); // lpush, now 3 elements
  1646. $this->assertTrue($ret[$i++] === 4); // lpush, now 4 elements
  1647. $this->assertTrue($ret[$i++] === 5); // lpush, now 5 elements
  1648. $this->assertTrue($ret[$i++] === 6); // lpush, now 6 elements
  1649. $this->assertTrue($ret[$i++] === 'lvalue'); // rpoplpush returns the element: "lvalue"
  1650. $this->assertTrue($ret[$i++] === array('lvalue')); // lDest contains only that one element.
  1651. $this->assertTrue($ret[$i++] === 'lvalue'); // removing a second element from lkey, now 4 elements left ↓
  1652. $this->assertTrue($ret[$i++] === 4); // 4 elements left, after 2 pops.
  1653. $this->assertTrue($ret[$i++] === 3); // removing 3 elements, now 1 left.
  1654. $this->assertTrue($ret[$i++] === 1); // 1 element left
  1655. $this->assertTrue($ret[$i++] === "lvalue"); // this is the current head.
  1656. $this->assertTrue($ret[$i++] === array("lvalue")); // this is the current list.
  1657. $this->assertTrue($ret[$i++] === FALSE); // updating a non-existent element fails.
  1658. $this->assertTrue($ret[$i++] === array("lvalue")); // this is the current list.
  1659. $this->assertTrue($ret[$i++] === 1); // 1 element left
  1660. $this->assertTrue(count($ret) == $i);
  1661. $ret = $this->redis->multi(Redis::PIPELINE)
  1662. ->delete('lkey', 'lDest')
  1663. ->rpush('lkey', 'lvalue')
  1664. ->lpush('lkey', 'lvalue')
  1665. ->lpush('lkey', 'lvalue')
  1666. ->rpoplpush('lkey', 'lDest')
  1667. ->lGetRange('lDest', 0, -1)
  1668. ->lpop('lkey')
  1669. ->exec();
  1670. $this->assertTrue(is_array($ret));
  1671. $i = 0;
  1672. $this->assertTrue($ret[$i++] <= 2); // deleted 0, 1, or 2 items
  1673. $this->assertTrue($ret[$i++] === 1); // 1 element in the list
  1674. $this->assertTrue($ret[$i++] === 2); // 2 elements in the list
  1675. $this->assertTrue($ret[$i++] === 3); // 3 elements in the list
  1676. $this->assertTrue($ret[$i++] === 'lvalue'); // rpoplpush returns the element: "lvalue"
  1677. $this->assertTrue($ret[$i++] === array('lvalue')); // rpoplpush returns the element: "lvalue"
  1678. $this->assertTrue($ret[$i++] === 'lvalue'); // pop returns the front element: "lvalue"
  1679. $this->assertTrue(count($ret) == $i);
  1680. // general command
  1681. $ret = $this->redis->multi($mode)
  1682. ->select(3)
  1683. ->set('keyAAA', 'value')
  1684. ->set('keyAAB', 'value')
  1685. ->dbSize()
  1686. ->lastsave()
  1687. ->exec();
  1688. $this->redis->select(0); // back to normal
  1689. $i = 0;
  1690. $this->assertTrue(is_array($ret));
  1691. $this->assertTrue($ret[$i++] === TRUE); // select
  1692. $this->assertTrue($ret[$i++] === TRUE); // set
  1693. $this->assertTrue($ret[$i++] === TRUE); // set
  1694. $this->assertTrue(is_long($ret[$i++])); // dbsize
  1695. $this->assertTrue(is_long($ret[$i++])); // lastsave
  1696. $this->assertTrue(count($ret) === $i);
  1697. $serializer = $this->redis->getOption(Redis::OPT_SERIALIZER);
  1698. $this->redis->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_NONE); // testing incr, which doesn't work with the serializer
  1699. $ret = $this->redis->multi($mode)
  1700. ->delete('key1')
  1701. ->set('key1', 'value1')
  1702. ->get('key1')
  1703. ->getSet('key1', 'value2')
  1704. ->get('key1')
  1705. ->set('key2', 4)
  1706. ->incr('key2')
  1707. ->get('key2')
  1708. ->decr('key2')
  1709. ->get('key2')
  1710. ->renameKey('key2', 'key3')
  1711. ->get('key3')
  1712. ->renameNx('key3', 'key1')
  1713. ->renameKey('key3', 'key2')
  1714. ->incr('key2', 5)
  1715. ->get('key2')
  1716. ->decr('key2', 5)
  1717. ->get('key2')
  1718. ->exec();
  1719. $i = 0;
  1720. $this->assertTrue(is_array($ret));
  1721. $this->assertTrue(is_long($ret[$i]) && $ret[$i] <= 1); $i++;
  1722. $this->assertTrue($ret[$i++] == TRUE);
  1723. $this->assertTrue($ret[$i++] == 'value1');
  1724. $this->assertTrue($ret[$i++] == 'value1');
  1725. $this->assertTrue($ret[$i++] == 'value2');
  1726. $this->assertTrue($ret[$i++] == TRUE);
  1727. $this->assertTrue($ret[$i++] == 5);
  1728. $this->assertTrue($ret[$i++] == 5);
  1729. $this->assertTrue($ret[$i++] == 4);
  1730. $this->assertTrue($ret[$i++] == 4);
  1731. $this->assertTrue($ret[$i++] == TRUE);
  1732. $this->assertTrue($ret[$i++] == 4);
  1733. $this->assertTrue($ret[$i++] == FALSE);
  1734. $this->assertTrue($ret[$i++] == TRUE);
  1735. $this->assertTrue($ret[$i++] == TRUE);
  1736. $this->assertTrue($ret[$i++] == 9);
  1737. $this->assertTrue($ret[$i++] == TRUE);
  1738. $this->assertTrue($ret[$i++] == 4);
  1739. $this->redis->setOption(Redis::OPT_SERIALIZER, $serializer);
  1740. $ret = $this->redis->multi($mode)
  1741. ->delete('key1')
  1742. ->delete('key2')
  1743. ->set('key1', 'val1')
  1744. ->setnx('key1', 'valX')
  1745. ->setnx('key2', 'valX')
  1746. ->exists('key1')
  1747. ->exists('key3')
  1748. ->ping()
  1749. ->exec();
  1750. $this->assertTrue(is_array($ret));
  1751. $this->assertTrue($ret[0] == TRUE);
  1752. $this->assertTrue($ret[1] == TRUE);
  1753. $this->assertTrue($ret[2] == TRUE);
  1754. $this->assertTrue($ret[3] == FALSE);
  1755. $this->assertTrue($ret[4] == TRUE);
  1756. $this->assertTrue($ret[5] == TRUE);
  1757. $this->assertTrue($ret[6] == FALSE);
  1758. $this->assertTrue($ret[7] == '+PONG');
  1759. $ret = $this->redis->multi($mode)
  1760. ->randomKey()
  1761. ->exec();
  1762. $this->assertTrue(is_array($ret) && count($ret) === 1);
  1763. $this->assertTrue(is_string($ret[0]));
  1764. // ttl, mget, mset, msetnx, expire, expireAt
  1765. $ret = $this->redis->multi($mode)
  1766. ->ttl('key')
  1767. ->mget(array('key1', 'key2', 'key3'))
  1768. ->mset(array('key3' => 'value3', 'key4' => 'value4'))
  1769. ->set('key', 'value')
  1770. ->expire('key', 5)
  1771. ->ttl('key')
  1772. ->expireAt('key', '0000')
  1773. ->exec();
  1774. $i = 0;
  1775. $this->assertTrue(is_array($ret));
  1776. $this->assertTrue(is_long($ret[$i++]));
  1777. $this->assertTrue(is_array($ret[$i]) && count($ret[$i]) === 3); // mget
  1778. $i++;
  1779. $this->assertTrue($ret[$i++] === TRUE); // mset always returns TRUE
  1780. $this->assertTrue($ret[$i++] === TRUE); // set always returns TRUE
  1781. $this->assertTrue($ret[$i++] === TRUE); // expire always returns TRUE
  1782. $this->assertTrue($ret[$i++] === 5); // TTL was just set.
  1783. $this->assertTrue($ret[$i++] === TRUE); // expireAt returns TRUE for an existing key
  1784. $this->assertTrue(count($ret) === $i);
  1785. // lists
  1786. $ret = $this->redis->multi($mode)
  1787. ->delete('lkey', 'lDest')
  1788. ->rpush('lkey', 'lvalue')
  1789. ->lpush('lkey', 'lvalue')
  1790. ->lpush('lkey', 'lvalue')
  1791. ->lpush('lkey', 'lvalue')
  1792. ->lpush('lkey', 'lvalue')
  1793. ->lpush('lkey', 'lvalue')
  1794. ->rpoplpush('lkey', 'lDest')
  1795. ->lGetRange('lDest', 0, -1)
  1796. ->lpop('lkey')
  1797. ->llen('lkey')
  1798. ->lRemove('lkey', 'lvalue', 3)
  1799. ->llen('lkey')
  1800. ->lget('lkey', 0)
  1801. ->lGetRange('lkey', 0, -1)
  1802. ->lSet('lkey', 1, "newValue") // check errors on missing key
  1803. ->lGetRange('lkey', 0, -1)
  1804. ->llen('lkey')
  1805. ->exec();
  1806. $this->assertTrue(is_array($ret));
  1807. $i = 0;
  1808. $this->assertTrue($ret[$i] >= 0 && $ret[$i] <= 2); // delete
  1809. $i++;
  1810. $this->assertTrue($ret[$i++] === 1); // 1 value
  1811. $this->assertTrue($ret[$i++] === 2); // 2 values
  1812. $this->assertTrue($ret[$i++] === 3); // 3 values
  1813. $this->assertTrue($ret[$i++] === 4); // 4 values
  1814. $this->assertTrue($ret[$i++] === 5); // 5 values
  1815. $this->assertTrue($ret[$i++] === 6); // 6 values
  1816. $this->assertTrue($ret[$i++] === 'lvalue');
  1817. $this->assertTrue($ret[$i++] === array('lvalue')); // 1 value only in lDest
  1818. $this->assertTrue($ret[$i++] === 'lvalue'); // now 4 values left
  1819. $this->assertTrue($ret[$i++] === 4);
  1820. $this->assertTrue($ret[$i++] === 3); // removing 3 elements.
  1821. $this->assertTrue($ret[$i++] === 1); // length is now 1
  1822. $this->assertTrue($ret[$i++] === 'lvalue'); // this is the head
  1823. $this->assertTrue($ret[$i++] === array('lvalue')); // 1 value only in lkey
  1824. $this->assertTrue($ret[$i++] === FALSE); // can't set list[1] if we only have a single value in it.
  1825. $this->assertTrue($ret[$i++] === array('lvalue')); // the previous error didn't touch anything.
  1826. $this->assertTrue($ret[$i++] === 1); // the previous error didn't change the length
  1827. $this->assertTrue(count($ret) === $i);
  1828. // sets
  1829. $ret = $this->redis->multi($mode)
  1830. ->delete('skey1', 'skey2', 'skeydest', 'skeyUnion', 'sDiffDest')
  1831. ->sadd('skey1', 'sValue1')
  1832. ->sadd('skey1', 'sValue2')
  1833. ->sadd('skey1', 'sValue3')
  1834. ->sadd('skey1', 'sValue4')
  1835. ->sadd('skey2', 'sValue1')
  1836. ->sadd('skey2', 'sValue2')
  1837. ->sSize('skey1')
  1838. ->sRemove('skey1', 'sValue2')
  1839. ->sSize('skey1')
  1840. ->sMove('skey1', 'skey2', 'sValue4')
  1841. ->sSize('skey2')
  1842. ->sContains('skey2', 'sValue4')
  1843. ->sMembers('skey1')
  1844. ->sMembers('skey2')
  1845. ->sInter('skey1', 'skey2')
  1846. ->sInterStore('skeydest', 'skey1', 'skey2')
  1847. ->sMembers('skeydest')
  1848. ->sUnion('skey2', 'skeydest')
  1849. ->sUnionStore('skeyUnion', 'skey2', 'skeydest')
  1850. ->sMembers('skeyUnion')
  1851. ->sDiff('skey1', 'skey2')
  1852. ->sDiffStore('sDiffDest', 'skey1', 'skey2')
  1853. ->sMembers('sDiffDest')
  1854. ->sPop('skey2')
  1855. ->sSize('skey2')
  1856. ->exec();
  1857. $i = 0;
  1858. $this->assertTrue(is_array($ret));
  1859. $this->assertTrue(is_long($ret[$i]) && $ret[$i] >= 0 && $ret[$i] <= 5); $i++; // deleted at most 5 values.
  1860. $this->assertTrue($ret[$i++] === TRUE); // skey1 now has 1 element.
  1861. $this->assertTrue($ret[$i++] === TRUE); // skey1 now has 2 elements.
  1862. $this->assertTrue($ret[$i++] === TRUE); // skey1 now has 3 elements.
  1863. $this->assertTrue($ret[$i++] === TRUE); // skey1 now has 4 elements.
  1864. $this->assertTrue($ret[$i++] === TRUE); // skey2 now has 1 element.
  1865. $this->assertTrue($ret[$i++] === TRUE); // skey2 now has 2 elements.
  1866. $this->assertTrue($ret[$i++] === 4);
  1867. $this->assertTrue($ret[$i++] === TRUE); // we did remove that value.
  1868. $this->assertTrue($ret[$i++] === 3); // now 3 values only.
  1869. $this->assertTrue($ret[$i++] === TRUE); // the move did succeed.
  1870. $this->assertTrue($ret[$i++] === 3); // sKey2 now has 3 values.
  1871. $this->assertTrue($ret[$i++] === TRUE); // sKey2 does contain sValue4.
  1872. foreach(array('sValue1', 'sValue3') as $k) { // sKey1 contains sValue1 and sValue3.
  1873. $this->assertTrue(in_array($k, $ret[$i]));
  1874. }
  1875. $this->assertTrue(count($ret[$i++]) === 2);
  1876. foreach(array('sValue1', 'sValue2', 'sValue4') as $k) { // sKey2 contains sValue1, sValue2, and sValue4.
  1877. $this->assertTrue(in_array($k, $ret[$i]));
  1878. }
  1879. $this->assertTrue(count($ret[$i++]) === 3);
  1880. $this->assertTrue($ret[$i++] === array('sValue1')); // intersection
  1881. $this->assertTrue($ret[$i++] === 1); // intersection + store → 1 value in the destination set.
  1882. $this->assertTrue($ret[$i++] === array('sValue1')); // sinterstore destination contents
  1883. foreach(array('sValue1', 'sValue2', 'sValue4') as $k) { // (skeydest U sKey2) contains sValue1, sValue2, and sValue4.
  1884. $this->assertTrue(in_array($k, $ret[$i]));
  1885. }
  1886. $this->assertTrue(count($ret[$i++]) === 3); // union size
  1887. $this->assertTrue($ret[$i++] === 3); // unionstore size
  1888. foreach(array('sValue1', 'sValue2', 'sValue4') as $k) { // (skeyUnion) contains sValue1, sValue2, and sValue4.
  1889. $this->assertTrue(in_array($k, $ret[$i]));
  1890. }
  1891. $this->assertTrue(count($ret[$i++]) === 3); // skeyUnion size
  1892. $this->assertTrue($ret[$i++] === array('sValue3')); // diff skey1, skey2 : only sValue3 is not shared.
  1893. $this->assertTrue($ret[$i++] === 1); // sdiffstore size == 1
  1894. $this->assertTrue($ret[$i++] === array('sValue3')); // contents of sDiffDest
  1895. $this->assertTrue(in_array($ret[$i++], array('sValue1', 'sValue2', 'sValue4'))); // we removed an element from sKey2
  1896. $this->assertTrue($ret[$i++] === 2); // sKey2 now has 2 elements only.
  1897. $this->assertTrue(count($ret) === $i);
  1898. // sorted sets
  1899. $ret = $this->redis->multi($mode)
  1900. ->delete('zkey1', 'zkey2', 'zkey5')
  1901. ->zadd('zkey1', 1, 'zValue1')
  1902. ->zadd('zkey1', 5, 'zValue5')
  1903. ->zadd('zkey1', 2, 'zValue2')
  1904. ->zRange('zkey1', 0, -1)
  1905. ->zDelete('zkey1', 'zValue2')
  1906. ->zRange('zkey1', 0, -1)
  1907. ->zadd('zkey1', 11, 'zValue11')
  1908. ->zadd('zkey1', 12, 'zValue12')
  1909. ->zadd('zkey1', 13, 'zValue13')
  1910. ->zadd('zkey1', 14, 'zValue14')
  1911. ->zadd('zkey1', 15, 'zValue15')
  1912. ->zDeleteRangeByScore('zkey1', 11, 13)
  1913. ->zrange('zkey1', 0, -1)
  1914. ->zReverseRange('zkey1', 0, -1)
  1915. ->zRangeByScore('zkey1', 1, 6)
  1916. ->zCard('zkey1')
  1917. ->zScore('zkey1', 'zValue15')
  1918. ->zadd('zkey2', 5, 'zValue5')
  1919. ->zadd('zkey2', 2, 'zValue2')
  1920. ->zInter('zInter', array('zkey1', 'zkey2'))
  1921. ->zRange('zkey1', 0, -1)
  1922. ->zRange('zkey2', 0, -1)
  1923. ->zRange('zInter', 0, -1)
  1924. ->zUnion('zUnion', array('zkey1', 'zkey2'))
  1925. ->zRange('zUnion', 0, -1)
  1926. ->zadd('zkey5', 5, 'zValue5')
  1927. ->zIncrBy('zkey5', 3, 'zValue5') // fix this
  1928. ->zScore('zkey5', 'zValue5')
  1929. ->exec();
  1930. $i = 0;
  1931. $this->assertTrue(is_array($ret));
  1932. $this->assertTrue(is_long($ret[$i]) && $ret[$i] >= 0 && $ret[$i] <= 3); $i++; // deleting at most 3 keys
  1933. $this->assertTrue($ret[$i++] === 1);
  1934. $this->assertTrue($ret[$i++] === 1);
  1935. $this->assertTrue($ret[$i++] === 1);
  1936. $this->assertTrue($ret[$i++] === array('zValue1', 'zValue2', 'zValue5'));
  1937. $this->assertTrue($ret[$i++] === 1);
  1938. $this->assertTrue($ret[$i++] === array('zValue1', 'zValue5'));
  1939. $this->assertTrue($ret[$i++] === 1); // adding zValue11
  1940. $this->assertTrue($ret[$i++] === 1); // adding zValue12
  1941. $this->assertTrue($ret[$i++] === 1); // adding zValue13
  1942. $this->assertTrue($ret[$i++] === 1); // adding zValue14
  1943. $this->assertTrue($ret[$i++] === 1); // adding zValue15
  1944. $this->assertTrue($ret[$i++] === 3); // deleted zValue11, zValue12, zValue13
  1945. $this->assertTrue($ret[$i++] === array('zValue1', 'zValue5', 'zValue14', 'zValue15'));
  1946. $this->assertTrue($ret[$i++] === array('zValue15', 'zValue14', 'zValue5', 'zValue1'));
  1947. $this->assertTrue($ret[$i++] === array('zValue1', 'zValue5'));
  1948. $this->assertTrue($ret[$i++] === 4); // 4 elements
  1949. $this->assertTrue($ret[$i++] === 15.0);
  1950. $this->assertTrue($ret[$i++] === 1); // added value
  1951. $this->assertTrue($ret[$i++] === 1); // added value
  1952. $this->assertTrue($ret[$i++] === 1); // zinter only has 1 value
  1953. $this->assertTrue($ret[$i++] === array('zValue1', 'zValue5', 'zValue14', 'zValue15')); // zkey1 contents
  1954. $this->assertTrue($ret[$i++] === array('zValue2', 'zValue5')); // zkey2 contents
  1955. $this->assertTrue($ret[$i++] === array('zValue5')); // zinter contents
  1956. $this->assertTrue($ret[$i++] === 5); // zUnion has 5 values (1,2,5,14,15)
  1957. $this->assertTrue($ret[$i++] === array('zValue1', 'zValue2', 'zValue5', 'zValue14', 'zValue15')); // zunion contents
  1958. $this->assertTrue($ret[$i++] === 1); // added value to zkey5, with score 5
  1959. $this->assertTrue($ret[$i++] === 8.0); // incremented score by 3 → it is now 8.
  1960. $this->assertTrue($ret[$i++] === 8.0); // current score is 8.
  1961. $this->assertTrue(count($ret) === $i);
  1962. $serializer = $this->redis->getOption(Redis::OPT_SERIALIZER);
  1963. $this->redis->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_NONE); // testing incr, which doesn't work with the serializer
  1964. // hash
  1965. $ret = $this->redis->multi($mode)
  1966. ->delete('hkey1')
  1967. ->hset('hkey1', 'key1', 'value1')
  1968. ->hset('hkey1', 'key2', 'value2')
  1969. ->hset('hkey1', 'key3', 'value3')
  1970. ->hmget('hkey1', array('key1', 'key2', 'key3'))
  1971. ->hget('hkey1', 'key1')
  1972. ->hlen('hkey1')
  1973. ->hdel('hkey1', 'key2')
  1974. ->hdel('hkey1', 'key2')
  1975. ->hexists('hkey1', 'key2')
  1976. ->hkeys('hkey1')
  1977. ->hvals('hkey1')
  1978. ->hgetall('hkey1')
  1979. ->hset('hkey1', 'valn', 1)
  1980. ->hincrby('hkey1', 'valn', 4)
  1981. ->hincrby('hkey1', 'val-fail', 42)
  1982. ->hset('hkey1', 'val-fail', 'non-string')
  1983. ->hget('hkey1', 'val-fail')
  1984. ->hincrby('hkey1', 'val-fail', 42)
  1985. ->exec();
  1986. $i = 0;
  1987. $this->assertTrue(is_array($ret));
  1988. $this->assertTrue($ret[$i++] <= 1); // delete
  1989. $this->assertTrue($ret[$i++] === 1); // added 1 element
  1990. $this->assertTrue($ret[$i++] === 1); // added 1 element
  1991. $this->assertTrue($ret[$i++] === 1); // added 1 element
  1992. $this->assertTrue($ret[$i++] === array('key1' => 'value1', 'key2' => 'value2', 'key3' => 'value3')); // hmget, 3 elements
  1993. $this->assertTrue($ret[$i++] === 'value1'); // hget
  1994. $this->assertTrue($ret[$i++] === 3); // hlen
  1995. $this->assertTrue($ret[$i++] === TRUE); // hdel succeeded
  1996. $this->assertTrue($ret[$i++] === FALSE); // hdel failed
  1997. $this->assertTrue($ret[$i++] === FALSE); // hexists didn't find the deleted key
  1998. $this->assertTrue($ret[$i] === array('key1', 'key3') || $ret[$i] === array('key3', 'key1')); $i++; // hkeys
  1999. $this->assertTrue($ret[$i] === array('value1', 'value3') || $ret[$i] === array('value3', 'value1')); $i++; // hvals
  2000. $this->assertTrue($ret[$i] === array('key1' => 'value1', 'key3' => 'value3') || $ret[$i] === array('key3' => 'value3', 'key1' => 'value1')); $i++; // hgetall
  2001. $this->assertTrue($ret[$i++] === 1); // added 1 element
  2002. $this->assertTrue($ret[$i++] === 5); // added 4 to value 1 → 5
  2003. $this->assertTrue($ret[$i++] === 42); // member doesn't exist → assume 0, and then add.
  2004. $this->assertTrue($ret[$i++] === 0); // didn't add the element, already present, so 0.
  2005. $this->assertTrue($ret[$i++] === 'non-string'); // hset succeeded
  2006. $this->assertTrue($ret[$i++] === FALSE); // member isn't a number → fail.
  2007. $this->assertTrue(count($ret) === $i);
  2008. $this->redis->setOption(Redis::OPT_SERIALIZER, $serializer);
  2009. $ret = $this->redis->multi() // default to MULTI, not PIPELINE.
  2010. ->delete('test')
  2011. ->set('test', 'xyz')
  2012. ->get('test')
  2013. ->exec();
  2014. $i = 0;
  2015. $this->assertTrue(is_array($ret));
  2016. $this->assertTrue($ret[$i++] <= 1); // delete
  2017. $this->assertTrue($ret[$i++] === TRUE); // added 1 element
  2018. $this->assertTrue($ret[$i++] === 'xyz');
  2019. $this->assertTrue(count($ret) === $i);
  2020. }
  2021. public function testSerializerPHP() {
  2022. $this->checkSerializer(Redis::SERIALIZER_PHP);
  2023. // with prefix
  2024. $this->redis->setOption(Redis::OPT_PREFIX, "test:");
  2025. $this->checkSerializer(Redis::SERIALIZER_PHP);
  2026. $this->redis->setOption(Redis::OPT_PREFIX, "");
  2027. }
  2028. public function testSerializerIGBinary() {
  2029. $this->checkSerializer(Redis::SERIALIZER_IGBINARY);
  2030. // with prefix
  2031. $this->redis->setOption(Redis::OPT_PREFIX, "test:");
  2032. $this->checkSerializer(Redis::SERIALIZER_IGBINARY);
  2033. $this->redis->setOption(Redis::OPT_PREFIX, "");
  2034. }
  2035. private function checkSerializer($mode) {
  2036. $this->redis->delete('key');
  2037. $this->assertTrue($this->redis->getOption(Redis::OPT_SERIALIZER) === Redis::SERIALIZER_NONE); // default
  2038. $this->assertTrue($this->redis->setOption(Redis::OPT_SERIALIZER, $mode) === TRUE); // set ok
  2039. $this->assertTrue($this->redis->getOption(Redis::OPT_SERIALIZER) === $mode); // get ok
  2040. // lPush, rPush
  2041. $a = array('hello world', 42, TRUE, array('<tag>' => 1729));
  2042. $this->redis->delete('key');
  2043. $this->redis->lPush('key', $a[0]);
  2044. $this->redis->rPush('key', $a[1]);
  2045. $this->redis->rPush('key', $a[2]);
  2046. $this->redis->rPush('key', $a[3]);
  2047. // lGetRange
  2048. $this->assertTrue($a === $this->redis->lGetRange('key', 0, -1));
  2049. // lGet
  2050. $this->assertTrue($a[0] === $this->redis->lGet('key', 0));
  2051. $this->assertTrue($a[1] === $this->redis->lGet('key', 1));
  2052. $this->assertTrue($a[2] === $this->redis->lGet('key', 2));
  2053. $this->assertTrue($a[3] === $this->redis->lGet('key', 3));
  2054. // lRemove
  2055. $this->assertTrue($this->redis->lRemove('key', $a[3]) === 1);
  2056. $this->assertTrue(array_slice($a, 0, 3) === $this->redis->lGetRange('key', 0, -1));
  2057. // lSet
  2058. $a[0] = array('k' => 'v'); // update
  2059. $this->assertTrue(TRUE === $this->redis->lSet('key', 0, $a[0]));
  2060. $this->assertTrue($a[0] === $this->redis->lGet('key', 0));
  2061. // lInsert
  2062. $this->assertTrue($this->redis->lInsert('key', Redis::BEFORE, $a[0], array(1,2,3)) === 4);
  2063. $this->assertTrue($this->redis->lInsert('key', Redis::AFTER, $a[0], array(4,5,6)) === 5);
  2064. $a = array(array(1,2,3), $a[0], array(4,5,6), $a[1], $a[2]);
  2065. $this->assertTrue($a === $this->redis->lGetRange('key', 0, -1));
  2066. // sAdd
  2067. $this->redis->delete('key');
  2068. $s = array(1,'a', array(1,2,3), array('k' => 'v'));
  2069. $this->assertTrue(TRUE === $this->redis->sAdd('key', $s[0]));
  2070. $this->assertTrue(TRUE === $this->redis->sAdd('key', $s[1]));
  2071. $this->assertTrue(TRUE === $this->redis->sAdd('key', $s[2]));
  2072. $this->assertTrue(TRUE === $this->redis->sAdd('key', $s[3]));
  2073. // sRemove
  2074. $this->assertTrue(TRUE === $this->redis->sRemove('key', $s[3]));
  2075. $this->assertTrue(FALSE === $this->redis->sRemove('key', $s[3]));
  2076. // sContains
  2077. $this->assertTrue(TRUE === $this->redis->sContains('key', $s[0]));
  2078. $this->assertTrue(TRUE === $this->redis->sContains('key', $s[1]));
  2079. $this->assertTrue(TRUE === $this->redis->sContains('key', $s[2]));
  2080. $this->assertTrue(FALSE === $this->redis->sContains('key', $s[3]));
  2081. unset($s[3]);
  2082. // sMove
  2083. $this->redis->delete('tmp');
  2084. $this->redis->sMove('key', 'tmp', $s[0]);
  2085. $this->assertTrue(FALSE === $this->redis->sContains('key', $s[0]));
  2086. $this->assertTrue(TRUE === $this->redis->sContains('tmp', $s[0]));
  2087. unset($s[0]);
  2088. // sorted sets
  2089. $z = array('z0', array('k' => 'v'), FALSE, NULL);
  2090. $this->redis->delete('key');
  2091. // zAdd
  2092. $this->assertTrue(1 === $this->redis->zAdd('key', 0, $z[0]));
  2093. $this->assertTrue(1 === $this->redis->zAdd('key', 1, $z[1]));
  2094. $this->assertTrue(1 === $this->redis->zAdd('key', 2, $z[2]));
  2095. $this->assertTrue(1 === $this->redis->zAdd('key', 3, $z[3]));
  2096. // zDelete
  2097. $this->assertTrue(1 === $this->redis->zDelete('key', $z[3]));
  2098. $this->assertTrue(0 === $this->redis->zDelete('key', $z[3]));
  2099. unset($z[3]);
  2100. // zRange
  2101. $this->assertTrue($z === $this->redis->zRange('key', 0, -1));
  2102. // zScore
  2103. $this->assertTrue(0.0 === $this->redis->zScore('key', $z[0]));
  2104. $this->assertTrue(1.0 === $this->redis->zScore('key', $z[1]));
  2105. $this->assertTrue(2.0 === $this->redis->zScore('key', $z[2]));
  2106. // zRank
  2107. $this->assertTrue(0 === $this->redis->zRank('key', $z[0]));
  2108. $this->assertTrue(1 === $this->redis->zRank('key', $z[1]));
  2109. $this->assertTrue(2 === $this->redis->zRank('key', $z[2]));
  2110. // zRevRank
  2111. $this->assertTrue(2 === $this->redis->zRevRank('key', $z[0]));
  2112. $this->assertTrue(1 === $this->redis->zRevRank('key', $z[1]));
  2113. $this->assertTrue(0 === $this->redis->zRevRank('key', $z[2]));
  2114. // zIncrBy
  2115. $this->assertTrue(3.0 === $this->redis->zIncrBy('key', 1.0, $z[2]));
  2116. $this->assertTrue(3.0 === $this->redis->zScore('key', $z[2]));
  2117. $this->assertTrue(5.0 === $this->redis->zIncrBy('key', 2.0, $z[2]));
  2118. $this->assertTrue(5.0 === $this->redis->zScore('key', $z[2]));
  2119. $this->assertTrue(2.0 === $this->redis->zIncrBy('key', -3.0, $z[2]));
  2120. $this->assertTrue(2.0 === $this->redis->zScore('key', $z[2]));
  2121. // mset
  2122. $a = array('k0' => 1, 'k1' => 42, 'k2' => NULL, 'k3' => FALSE, 'k4' => array('a' => 'b'));
  2123. $this->assertTrue(TRUE === $this->redis->mset($a));
  2124. foreach($a as $k => $v) {
  2125. $this->assertTrue($this->redis->get($k) === $v);
  2126. }
  2127. $a = array('k0' => 1, 'k1' => 42, 'k2' => NULL, 'k3' => FALSE, 'k4' => array('a' => 'b'));
  2128. // hSet
  2129. $this->redis->delete('key');
  2130. foreach($a as $k => $v) {
  2131. $this->assertTrue(1 === $this->redis->hSet('key', $k, $v));
  2132. }
  2133. // hGet
  2134. foreach($a as $k => $v) {
  2135. $this->assertTrue($v === $this->redis->hGet('key', $k));
  2136. }
  2137. // hGetAll
  2138. $this->assertTrue($a === $this->redis->hGetAll('key'));
  2139. $this->assertTrue(TRUE === $this->redis->hExists('key', 'k0'));
  2140. $this->assertTrue(TRUE === $this->redis->hExists('key', 'k1'));
  2141. $this->assertTrue(TRUE === $this->redis->hExists('key', 'k2'));
  2142. $this->assertTrue(TRUE === $this->redis->hExists('key', 'k3'));
  2143. $this->assertTrue(TRUE === $this->redis->hExists('key', 'k4'));
  2144. // hMSet
  2145. $this->redis->delete('key');
  2146. $this->redis->hMSet('key', $a);
  2147. foreach($a as $k => $v) {
  2148. $this->assertTrue($v === $this->redis->hGet('key', $k));
  2149. }
  2150. // hMget
  2151. $hmget = $this->redis->hMget('key', array_keys($a));
  2152. foreach($hmget as $k => $v) {
  2153. $this->assertTrue($v === $a[$k]);
  2154. }
  2155. // getMultiple
  2156. $this->redis->set('a', NULL);
  2157. $this->redis->set('b', FALSE);
  2158. $this->redis->set('c', 42);
  2159. $this->redis->set('d', array('x' => 'y'));
  2160. $this->assertTrue(array(NULL, FALSE, 42, array('x' => 'y')) === $this->redis->getMultiple(array('a', 'b', 'c', 'd')));
  2161. // pipeline
  2162. $this->sequence(Redis::PIPELINE);
  2163. // multi-exec
  2164. $this->sequence(Redis::MULTI);
  2165. // keys
  2166. $this->assertTrue(is_array($this->redis->keys('*')));
  2167. // revert
  2168. $this->assertTrue($this->redis->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_NONE) === TRUE); // set ok
  2169. $this->assertTrue($this->redis->getOption(Redis::OPT_SERIALIZER) === Redis::SERIALIZER_NONE); // get ok
  2170. }
  2171. }
  2172. ?>