/tests/TestRedis.php

https://github.com/esimionato/phpredis · PHP · 1456 lines · 1043 code · 311 blank · 102 comment · 9 complexity · 244cd0e0d5a89c86387e875b78768618 MD5 · raw file

  1. <?php
  2. require_once 'PHPUnit.php';
  3. echo "Note: these tests might take up to a minute. Don't worry :-)\n";
  4. class Redis_Test extends PHPUnit_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. // variadic
  168. $this->redis->delete('key0');
  169. $this->assertTrue(3 === $this->redis->lPush('key0', 'val0', 'val1', 'val2'));
  170. $this->assertTrue(array('val2', 'val1', 'val0') === $this->redis->lrange('key0', 0, -1));
  171. $this->redis->delete('key0');
  172. $this->assertTrue(3 === $this->redis->rPush('key0', 'val0', 'val1', 'val2'));
  173. $this->assertTrue(array('val0', 'val1', 'val2') === $this->redis->lrange('key0', 0, -1));
  174. }
  175. public function testRenameNx() {
  176. // strings
  177. $this->redis->delete('key0', 'key1');
  178. $this->redis->set('key0', 'val0');
  179. $this->redis->set('key1', 'val1');
  180. $this->assertTrue($this->redis->renameNx('key0', 'key1') === FALSE);
  181. $this->assertTrue($this->redis->get('key0') === 'val0');
  182. $this->assertTrue($this->redis->get('key1') === 'val1');
  183. // lists
  184. $this->redis->delete('key0');
  185. $this->redis->delete('key1');
  186. $this->redis->lPush('key0', 'val0');
  187. $this->redis->lPush('key0', 'val1');
  188. $this->redis->lPush('key1', 'val1-0');
  189. $this->redis->lPush('key1', 'val1-1');
  190. $this->assertTrue($this->redis->renameNx('key0', 'key1') === FALSE);
  191. $this->assertTrue($this->redis->lGetRange('key0', 0, -1) === array('val1', 'val0'));
  192. $this->assertTrue($this->redis->lGetRange('key1', 0, -1) === array('val1-1', 'val1-0'));
  193. $this->redis->delete('key2');
  194. $this->assertTrue($this->redis->renameNx('key0', 'key2') === TRUE);
  195. $this->assertTrue($this->redis->lGetRange('key0', 0, -1) === array());
  196. $this->assertTrue($this->redis->lGetRange('key2', 0, -1) === array('val1', 'val0'));
  197. }
  198. public function testMultiple() {
  199. $this->redis->delete('k1');
  200. $this->redis->delete('k2');
  201. $this->redis->delete('k3');
  202. $this->redis->set('k1', 'v1');
  203. $this->redis->set('k2', 'v2');
  204. $this->redis->set('k3', 'v3');
  205. $this->redis->set(1, 'test');
  206. $this->assertEquals(array('v1'), $this->redis->getMultiple(array('k1')));
  207. $this->assertEquals(array('v1', 'v3', false), $this->redis->getMultiple(array('k1', 'k3', 'NoKey')));
  208. $this->assertEquals(array('v1', 'v2', 'v3'), $this->redis->getMultiple(array('k1', 'k2', 'k3')));
  209. $this->assertEquals(array('v1', 'v2', 'v3'), $this->redis->getMultiple(array('k1', 'k2', 'k3')));
  210. $this->redis->set('k5', '$1111111111');
  211. $this->assertEquals(array(0 => '$1111111111'), $this->redis->getMultiple(array('k5')));
  212. $this->assertEquals(array(0 => 'test'), $this->redis->getMultiple(array(1))); // non-string
  213. }
  214. public function testMultipleBin() {
  215. $this->redis->delete('k1');
  216. $this->redis->delete('k2');
  217. $this->redis->delete('k3');
  218. $this->redis->set('k1', gzcompress('v1'));
  219. $this->redis->set('k2', gzcompress('v2'));
  220. $this->redis->set('k3', gzcompress('v3'));
  221. $this->assertEquals(array(gzcompress('v1'), gzcompress('v2'), gzcompress('v3')), $this->redis->getMultiple(array('k1', 'k2', 'k3')));
  222. $this->assertEquals(array(gzcompress('v1'), gzcompress('v2'), gzcompress('v3')), $this->redis->getMultiple(array('k1', 'k2', 'k3')));
  223. }
  224. public function testSetTimeout() {
  225. $this->redis->delete('key');
  226. $this->redis->set('key', 'value');
  227. $this->assertEquals('value', $this->redis->get('key'));
  228. $this->redis->setTimeout('key', 1);
  229. $this->assertEquals('value', $this->redis->get('key'));
  230. sleep(2);
  231. $this->assertEquals(False, $this->redis->get('key'));
  232. }
  233. public function testExpireAt() {
  234. $this->redis->delete('key');
  235. $this->redis->set('key', 'value');
  236. $now = time(NULL);
  237. $this->redis->expireAt('key', $now + 1);
  238. $this->assertEquals('value', $this->redis->get('key'));
  239. sleep(2);
  240. $this->assertEquals(FALSE, $this->redis->get('key'));
  241. }
  242. public function testSetEx() {
  243. $this->redis->delete('key');
  244. $this->assertTrue($this->redis->setex('key', 7, 'val') === TRUE);
  245. $this->assertTrue($this->redis->ttl('key') ===7);
  246. $this->assertTrue($this->redis->get('key') === 'val');
  247. }
  248. public function testSetNX() {
  249. $this->redis->set('key', 42);
  250. $this->assertTrue($this->redis->setnx('key', 'err') === FALSE);
  251. $this->assertTrue($this->redis->get('key') === '42');
  252. $this->redis->delete('key');
  253. $this->assertTrue($this->redis->setnx('key', '42') === TRUE);
  254. $this->assertTrue($this->redis->get('key') === '42');
  255. }
  256. public function testIncr()
  257. {
  258. $this->redis->set('key', 0);
  259. $this->redis->incr('key');
  260. $this->assertEquals(1, $this->redis->get('key'));
  261. $this->redis->incr('key');
  262. $this->assertEquals(2, $this->redis->get('key'));
  263. $this->redis->incr('key', 3);
  264. $this->assertEquals(5, $this->redis->get('key'));
  265. $this->redis->incrBy('key', 3);
  266. $this->assertEquals(8, $this->redis->get('key'));
  267. $this->redis->incrBy('key', 1);
  268. $this->assertEquals(9, $this->redis->get('key'));
  269. $this->redis->incrBy('key', -1);
  270. $this->assertEquals(8, $this->redis->get('key'));
  271. $this->redis->delete('key');
  272. $this->redis->set('key', 'abc');
  273. $this->redis->incr('key');
  274. $this->assertTrue("abc" === $this->redis->get('key'));
  275. $this->redis->incr('key');
  276. $this->assertTrue("abc" === $this->redis->get('key'));
  277. }
  278. public function testDecr()
  279. {
  280. $this->redis->set('key', 5);
  281. $this->redis->decr('key');
  282. $this->assertEquals(4, $this->redis->get('key'));
  283. $this->redis->decr('key');
  284. $this->assertEquals(3, $this->redis->get('key'));
  285. $this->redis->decr('key', 2);
  286. $this->assertEquals(1, $this->redis->get('key'));
  287. $this->redis->decr('key', 2);
  288. $this->assertEquals(-1, $this->redis->get('key'));
  289. $this->redis->decrBy('key', 2);
  290. $this->assertEquals(-3, $this->redis->get('key'));
  291. $this->redis->decrBy('key', 1);
  292. $this->assertEquals(-4, $this->redis->get('key'));
  293. $this->redis->decr('key', -10);
  294. $this->assertEquals(6, $this->redis->get('key'));
  295. }
  296. public function testExists()
  297. {
  298. $this->redis->delete('key');
  299. $this->assertFalse($this->redis->exists('key'));
  300. $this->redis->set('key', 'val');
  301. $this->assertEquals(True, $this->redis->exists('key'));
  302. }
  303. public function testGetKeys()
  304. {
  305. $pattern = 'getKeys-test-';
  306. for($i = 1; $i < 10; $i++) {
  307. $this->redis->set($pattern.$i, $i);
  308. }
  309. $this->redis->delete($pattern.'3');
  310. $keys = $this->redis->getKeys($pattern.'*');
  311. $this->redis->set($pattern.'3', 'something');
  312. $keys2 = $this->redis->getKeys($pattern.'*');
  313. $this->assertEquals((count($keys) + 1), count($keys2));
  314. // empty array when no key matches
  315. $this->assertEquals(array(), $this->redis->getKeys(rand().rand().rand().'*'));
  316. }
  317. public function testDelete()
  318. {
  319. $key = 'key' . rand();
  320. $this->redis->set($key, 'val');
  321. $this->assertEquals('val', $this->redis->get($key));
  322. $this->assertEquals(1, $this->redis->delete($key));
  323. $this->assertEquals(false, $this->redis->get($key));
  324. // multiple, all existing
  325. $this->redis->set('x', 0);
  326. $this->redis->set('y', 1);
  327. $this->redis->set('z', 2);
  328. $this->assertEquals(3, $this->redis->delete('x', 'y', 'z'));
  329. $this->assertEquals(false, $this->redis->get('x'));
  330. $this->assertEquals(false, $this->redis->get('y'));
  331. $this->assertEquals(false, $this->redis->get('z'));
  332. // multiple, none existing
  333. $this->assertEquals(0, $this->redis->delete('x', 'y', 'z'));
  334. $this->assertEquals(false, $this->redis->get('x'));
  335. $this->assertEquals(false, $this->redis->get('y'));
  336. $this->assertEquals(false, $this->redis->get('z'));
  337. // multiple, some existing
  338. $this->redis->set('y', 1);
  339. $this->assertEquals(1, $this->redis->delete('x', 'y', 'z'));
  340. $this->assertEquals(false, $this->redis->get('y'));
  341. $this->redis->set('x', 0);
  342. $this->redis->set('y', 1);
  343. $this->assertEquals(2, $this->redis->delete(array('x', 'y')));
  344. }
  345. public function testType()
  346. {
  347. // 0 => none, (key didn't exist)
  348. // 1=> string,
  349. // 2 => set,
  350. // 3 => list,
  351. // 4 => zset,
  352. // 5 => hash
  353. // string
  354. $this->redis->set('key', 'val');
  355. $this->assertEquals(Redis::REDIS_STRING, $this->redis->type('key'));
  356. // list
  357. $this->redis->lPush('keyList', 'val0');
  358. $this->redis->lPush('keyList', 'val1');
  359. $this->assertEquals(Redis::REDIS_LIST, $this->redis->type('keyList'));
  360. // set
  361. $this->redis->delete('keySet');
  362. $this->redis->sAdd('keySet', 'val0');
  363. $this->redis->sAdd('keySet', 'val1');
  364. $this->assertEquals(Redis::REDIS_SET, $this->redis->type('keySet'));
  365. // sadd with numeric key
  366. $this->redis->delete(123);
  367. $this->assertTrue(1 === $this->redis->sAdd(123, 'val0'));
  368. $this->assertTrue(array('val0') === $this->redis->sMembers(123));
  369. // zset
  370. $this->redis->delete('keyZSet');
  371. $this->redis->zAdd('keyZSet', 0, 'val0');
  372. $this->redis->zAdd('keyZSet', 1, 'val1');
  373. $this->assertEquals(Redis::REDIS_ZSET, $this->redis->type('keyZSet'));
  374. // hash
  375. $this->redis->delete('keyHash');
  376. $this->redis->hSet('keyHash', 'key0', 'val0');
  377. $this->redis->hSet('keyHash', 'key1', 'val1');
  378. $this->assertEquals(Redis::REDIS_HASH, $this->redis->type('keyHash'));
  379. //None
  380. $this->assertEquals(Redis::REDIS_NOT_FOUND, $this->redis->type('keyNotExists'));
  381. }
  382. public function testStr() {
  383. $this->redis->set('key', 'val1');
  384. $this->assertTrue($this->redis->append('key', 'val2') === 8);
  385. $this->assertTrue($this->redis->get('key') === 'val1val2');
  386. $this->assertTrue($this->redis->append('keyNotExist', 'value') === 5);
  387. $this->assertTrue($this->redis->get('keyNotExist') === 'value');
  388. $this->redis->set('key', 'This is a string') ;
  389. $this->assertTrue($this->redis->getRange('key', 0, 3) === 'This');
  390. $this->assertTrue($this->redis->getRange('key', -6, -1) === 'string');
  391. $this->assertTrue($this->redis->getRange('key', -6, 100000) === 'string');
  392. $this->assertTrue($this->redis->get('key') === 'This is a string');
  393. $this->redis->set('key', 'This is a string') ;
  394. $this->assertTrue($this->redis->strlen('key') === 16);
  395. $this->redis->set('key', 10) ;
  396. $this->assertTrue($this->redis->strlen('key') === 2);
  397. $this->redis->set('key', '') ;
  398. $this->assertTrue($this->redis->strlen('key') === 0);
  399. $this->redis->set('key', '000') ;
  400. $this->assertTrue($this->redis->strlen('key') === 3);
  401. }
  402. // PUSH, POP : LPUSH, LPOP
  403. public function testlPop()
  404. {
  405. // rpush => tail
  406. // lpush => head
  407. $this->redis->delete('list');
  408. $this->redis->lPush('list', 'val');
  409. $this->redis->lPush('list', 'val2');
  410. $this->redis->rPush('list', 'val3');
  411. // 'list' = [ 'val2', 'val', 'val3']
  412. $this->assertEquals('val2', $this->redis->lPop('list'));
  413. $this->assertEquals('val', $this->redis->lPop('list'));
  414. $this->assertEquals('val3', $this->redis->lPop('list'));
  415. $this->assertEquals(FALSE, $this->redis->lPop('list'));
  416. // testing binary data
  417. $this->redis->delete('list');
  418. $this->assertEquals(1, $this->redis->lPush('list', gzcompress('val1')));
  419. $this->assertEquals(2, $this->redis->lPush('list', gzcompress('val2')));
  420. $this->assertEquals(3, $this->redis->lPush('list', gzcompress('val3')));
  421. $this->assertEquals('val3', gzuncompress($this->redis->lPop('list')));
  422. $this->assertEquals('val2', gzuncompress($this->redis->lPop('list')));
  423. $this->assertEquals('val1', gzuncompress($this->redis->lPop('list')));
  424. }
  425. // PUSH, POP : RPUSH, RPOP
  426. public function testrPop()
  427. {
  428. // rpush => tail
  429. // lpush => head
  430. $this->redis->delete('list');
  431. $this->redis->rPush('list', 'val');
  432. $this->redis->rPush('list', 'val2');
  433. $this->redis->lPush('list', 'val3');
  434. // 'list' = [ 'val3', 'val', 'val2']
  435. $this->assertEquals('val2', $this->redis->rPop('list'));
  436. $this->assertEquals('val', $this->redis->rPop('list'));
  437. $this->assertEquals('val3', $this->redis->rPop('list'));
  438. $this->assertEquals(FALSE, $this->redis->rPop('list'));
  439. // testing binary data
  440. $this->redis->delete('list');
  441. $this->assertEquals(1, $this->redis->rPush('list', gzcompress('val1')));
  442. $this->assertEquals(2, $this->redis->rPush('list', gzcompress('val2')));
  443. $this->assertEquals(3, $this->redis->rPush('list', gzcompress('val3')));
  444. $this->assertEquals('val3', gzuncompress($this->redis->rPop('list')));
  445. $this->assertEquals('val2', gzuncompress($this->redis->rPop('list')));
  446. $this->assertEquals('val1', gzuncompress($this->redis->rPop('list')));
  447. }
  448. public function testblockingPop() {
  449. // non blocking blPop, brPop
  450. $this->redis->delete('list');
  451. $this->redis->lPush('list', 'val1');
  452. $this->redis->lPush('list', 'val2');
  453. $this->assertTrue($this->redis->blPop(array('list'), 2) === array('list', 'val2'));
  454. $this->assertTrue($this->redis->blPop(array('list'), 2) === array('list', 'val1'));
  455. $this->redis->delete('list');
  456. $this->redis->lPush('list', 'val1');
  457. $this->redis->lPush('list', 'val2');
  458. $this->assertTrue($this->redis->brPop(array('list'), 1) === array('list', 'val1'));
  459. $this->assertTrue($this->redis->brPop(array('list'), 1) === array('list', 'val2'));
  460. // blocking blpop, brpop
  461. $this->redis->delete('list');
  462. $this->assertTrue($this->redis->blPop(array('list'), 1) === array());
  463. $this->assertTrue($this->redis->brPop(array('list'), 1) === array());
  464. // TODO: fix this broken test.
  465. // $this->redis->delete('list');
  466. // $params = array(
  467. // 0 => array("pipe", "r"),
  468. // 1 => array("pipe", "w"),
  469. // 2 => array("file", "/dev/null", "w")
  470. // );
  471. // if(function_exists('proc_open')) {
  472. // $env = array('PHPREDIS_key' =>'list', 'PHPREDIS_value' => 'value');
  473. // $process = proc_open('php', $params, $pipes, '/tmp', $env);
  474. //
  475. // if (is_resource($process)) {
  476. // fwrite($pipes[0], '<?php
  477. // sleep(2);
  478. // $r = new Redis;
  479. // $r->connect("'.self::HOST.'", '.self::PORT.');
  480. // if("'.addslashes(self::AUTH).'") {
  481. // $r->auth("'.addslashes(self::AUTH).'");
  482. // }
  483. // $r->lPush($_ENV["PHPREDIS_key"], $_ENV["PHPREDIS_value"]);
  484. // ?' . '>');
  485. //
  486. // fclose($pipes[0]);
  487. // fclose($pipes[1]);
  488. // $re = proc_close($process);
  489. //
  490. // $this->assertTrue($this->redis->blPop(array('list'), 5) === array("list", "value"));
  491. // }
  492. // }
  493. }
  494. public function testlSize()
  495. {
  496. $this->redis->delete('list');
  497. $this->redis->lPush('list', 'val');
  498. $this->assertEquals(1, $this->redis->lSize('list'));
  499. $this->redis->lPush('list', 'val2');
  500. $this->assertEquals(2, $this->redis->lSize('list'));
  501. $this->assertEquals('val2', $this->redis->lPop('list'));
  502. $this->assertEquals(1, $this->redis->lSize('list'));
  503. $this->assertEquals('val', $this->redis->lPop('list'));
  504. $this->assertEquals(0, $this->redis->lSize('list'));
  505. $this->assertEquals(FALSE, $this->redis->lPop('list'));
  506. $this->assertEquals(0, $this->redis->lSize('list')); // empty returns 0
  507. $this->redis->delete('list');
  508. $this->assertEquals(0, $this->redis->lSize('list')); // non-existent returns 0
  509. $this->redis->set('list', 'actually not a list');
  510. $this->assertEquals(FALSE, $this->redis->lSize('list'));// not a list returns FALSE
  511. }
  512. //lInsert, lPopx, rPopx
  513. public function testlPopx() {
  514. //test lPushx/rPushx
  515. $this->redis->delete('keyNotExists');
  516. $this->assertTrue($this->redis->lPushx('keyNotExists', 'value') === 0);
  517. $this->assertTrue($this->redis->rPushx('keyNotExists', 'value') === 0);
  518. $this->redis->delete('key');
  519. $this->redis->lPush('key', 'val0');
  520. $this->assertTrue($this->redis->lPushx('key', 'val1') === 2);
  521. $this->assertTrue($this->redis->rPushx('key', 'val2') === 3);
  522. $this->assertTrue($this->redis->lGetRange('key', 0, -1) === array('val1', 'val0', 'val2'));
  523. //test linsert
  524. $this->redis->delete('key');
  525. $this->redis->lPush('key', 'val0');
  526. $this->assertTrue($this->redis->lInsert('keyNotExists', Redis::AFTER, 'val1', 'val2') === 0);
  527. $this->assertTrue($this->redis->lInsert('key', Redis::BEFORE, 'valX', 'val2') === -1);
  528. $this->assertTrue($this->redis->lInsert('key', Redis::AFTER, 'val0', 'val1') === 2);
  529. $this->assertTrue($this->redis->lInsert('key', Redis::BEFORE, 'val0', 'val2') === 3);
  530. $this->assertTrue($this->redis->lGetRange('key', 0, -1) === array('val2', 'val0', 'val1'));
  531. }
  532. // ltrim, lsize, lpop
  533. public function testlistTrim()
  534. {
  535. $this->redis->delete('list');
  536. $this->redis->lPush('list', 'val');
  537. $this->redis->lPush('list', 'val2');
  538. $this->redis->lPush('list', 'val3');
  539. $this->redis->lPush('list', 'val4');
  540. $this->assertEquals(TRUE, $this->redis->listTrim('list', 0, 2));
  541. $this->assertEquals(3, $this->redis->lSize('list'));
  542. $this->redis->listTrim('list', 0, 0);
  543. $this->assertEquals(1, $this->redis->lSize('list'));
  544. $this->assertEquals('val4', $this->redis->lPop('list'));
  545. $this->assertEquals(TRUE, $this->redis->listTrim('list', 10, 10000));
  546. $this->assertEquals(TRUE, $this->redis->listTrim('list', 10000, 10));
  547. // test invalid type
  548. $this->redis->set('list', 'not a list...');
  549. $this->assertEquals(FALSE, $this->redis->listTrim('list', 0, 2));
  550. }
  551. public function setupSort() {
  552. // people with name, age, salary
  553. $this->redis->set('person:name_1', 'Alice');
  554. $this->redis->set('person:age_1', 27);
  555. $this->redis->set('person:salary_1', 2500);
  556. $this->redis->set('person:name_2', 'Bob');
  557. $this->redis->set('person:age_2', 34);
  558. $this->redis->set('person:salary_2', 2000);
  559. $this->redis->set('person:name_3', 'Carol');
  560. $this->redis->set('person:age_3', 25);
  561. $this->redis->set('person:salary_3', 2800);
  562. $this->redis->set('person:name_4', 'Dave');
  563. $this->redis->set('person:age_4', 41);
  564. $this->redis->set('person:salary_4', 3100);
  565. // set-up
  566. $this->redis->delete('person:id');
  567. foreach(array(1,2,3,4) as $id) {
  568. $this->redis->lPush('person:id', $id);
  569. }
  570. }
  571. public function testSortAsc() {
  572. $this->setupSort();
  573. $this->assertTrue(FALSE === $this->redis->sortAsc(NULL));
  574. // sort by age and get IDs
  575. $byAgeAsc = array('3','1','2','4');
  576. $this->assertEquals($byAgeAsc, $this->redis->sortAsc('person:id', 'person:age_*'));
  577. $this->assertEquals($byAgeAsc, $this->redis->sort('person:id', array('by' => 'person:age_*', 'sort' => 'asc')));
  578. $this->assertEquals(array('1', '2', '3', '4'), $this->redis->sortAsc('person:id', NULL)); // check that NULL works.
  579. $this->assertEquals(array('1', '2', '3', '4'), $this->redis->sortAsc('person:id', NULL, NULL)); // for all fields.
  580. $this->assertEquals(array('1', '2', '3', '4'), $this->redis->sort('person:id', array('sort' => 'asc')));
  581. // sort by age and get names
  582. $byAgeAsc = array('Carol','Alice','Bob','Dave');
  583. $this->assertEquals($byAgeAsc, $this->redis->sortAsc('person:id', 'person:age_*', 'person:name_*'));
  584. $this->assertEquals($byAgeAsc, $this->redis->sort('person:id', array('by' => 'person:age_*', 'get' => 'person:name_*', 'sort' => 'asc')));
  585. $this->assertEquals(array_slice($byAgeAsc, 0, 2), $this->redis->sortAsc('person:id', 'person:age_*', 'person:name_*', 0, 2));
  586. $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')));
  587. $this->assertEquals(array_slice($byAgeAsc, 1, 2), $this->redis->sortAsc('person:id', 'person:age_*', 'person:name_*', 1, 2));
  588. $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')));
  589. $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.
  590. $this->assertEquals($byAgeAsc, $this->redis->sortAsc('person:id', 'person:age_*', 'person:name_*', 0, 4));
  591. $this->assertEquals($byAgeAsc, $this->redis->sort('person:id', array('by' => 'person:age_*', 'get' => 'person:name_*', 'limit' => array(0, 4))));
  592. $this->assertEquals($byAgeAsc, $this->redis->sort('person:id', array('by' => 'person:age_*', 'get' => 'person:name_*', 'limit' => array(0, "4")))); // with strings
  593. $this->assertEquals($byAgeAsc, $this->redis->sort('person:id', array('by' => 'person:age_*', 'get' => 'person:name_*', 'limit' => array("0", 4))));
  594. $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.
  595. // sort by salary and get ages
  596. $agesBySalaryAsc = array('34', '27', '25', '41');
  597. $this->assertEquals($agesBySalaryAsc, $this->redis->sortAsc('person:id', 'person:salary_*', 'person:age_*'));
  598. $this->assertEquals($agesBySalaryAsc, $this->redis->sort('person:id', array('by' => 'person:salary_*', 'get' => 'person:age_*', 'sort' => 'asc')));
  599. $agesAndSalaries = $this->redis->sort('person:id', array('by' => 'person:salary_*', 'get' => array('person:age_*', 'person:salary_*'), 'sort' => 'asc'));
  600. $this->assertEquals(array('34', '2000', '27', '2500', '25', '2800', '41', '3100'), $agesAndSalaries);
  601. // sort non-alpha doesn't change all-string lists
  602. // list → [ghi, def, abc]
  603. $list = array('abc', 'def', 'ghi');
  604. $this->redis->delete('list');
  605. foreach($list as $i) {
  606. $this->redis->lPush('list', $i);
  607. }
  608. // SORT list → [ghi, def, abc]
  609. $this->assertEquals(array_reverse($list), $this->redis->sortAsc('list'));
  610. $this->assertEquals(array_reverse($list), $this->redis->sort('list', array('sort' => 'asc')));
  611. // SORT list ALPHA → [abc, def, ghi]
  612. $this->assertEquals($list, $this->redis->sortAscAlpha('list'));
  613. $this->assertEquals($list, $this->redis->sort('list', array('sort' => 'asc', 'alpha' => TRUE)));
  614. }
  615. public function testSortDesc() {
  616. $this->setupSort();
  617. // sort by age and get IDs
  618. $byAgeDesc = array('4','2','1','3');
  619. $this->assertEquals($byAgeDesc, $this->redis->sortDesc('person:id', 'person:age_*'));
  620. // sort by age and get names
  621. $byAgeDesc = array('Dave', 'Bob', 'Alice', 'Carol');
  622. $this->assertEquals($byAgeDesc, $this->redis->sortDesc('person:id', 'person:age_*', 'person:name_*'));
  623. $this->assertEquals(array_slice($byAgeDesc, 0, 2), $this->redis->sortDesc('person:id', 'person:age_*', 'person:name_*', 0, 2));
  624. $this->assertEquals(array_slice($byAgeDesc, 1, 2), $this->redis->sortDesc('person:id', 'person:age_*', 'person:name_*', 1, 2));
  625. // sort by salary and get ages
  626. $agesBySalaryDesc = array('41', '25', '27', '34');
  627. $this->assertEquals($agesBySalaryDesc, $this->redis->sortDesc('person:id', 'person:salary_*', 'person:age_*'));
  628. // sort non-alpha doesn't change all-string lists
  629. $list = array('def', 'abc', 'ghi');
  630. $this->redis->delete('list');
  631. foreach($list as $i) {
  632. $this->redis->lPush('list', $i);
  633. }
  634. // SORT list → [ghi, abc, def]
  635. $this->assertEquals(array_reverse($list), $this->redis->sortDesc('list'));
  636. // SORT list ALPHA → [abc, def, ghi]
  637. $this->assertEquals(array('ghi', 'def', 'abc'), $this->redis->sortDescAlpha('list'));
  638. }
  639. // LINDEX
  640. public function testlGet() {
  641. $this->redis->delete('list');
  642. $this->redis->lPush('list', 'val');
  643. $this->redis->lPush('list', 'val2');
  644. $this->redis->lPush('list', 'val3');
  645. $this->assertEquals('val3', $this->redis->lGet('list', 0));
  646. $this->assertEquals('val2', $this->redis->lGet('list', 1));
  647. $this->assertEquals('val', $this->redis->lGet('list', 2));
  648. $this->assertEquals('val', $this->redis->lGet('list', -1));
  649. $this->assertEquals('val2', $this->redis->lGet('list', -2));
  650. $this->assertEquals('val3', $this->redis->lGet('list', -3));
  651. $this->assertEquals(FALSE, $this->redis->lGet('list', -4));
  652. $this->redis->rPush('list', 'val4');
  653. $this->assertEquals('val4', $this->redis->lGet('list', 3));
  654. $this->assertEquals('val4', $this->redis->lGet('list', -1));
  655. }
  656. // lRem testing
  657. public function testlRemove() {
  658. $this->redis->delete('list');
  659. $this->redis->lPush('list', 'a');
  660. $this->redis->lPush('list', 'b');
  661. $this->redis->lPush('list', 'c');
  662. $this->redis->lPush('list', 'c');
  663. $this->redis->lPush('list', 'b');
  664. $this->redis->lPush('list', 'c');
  665. // ['c', 'b', 'c', 'c', 'b', 'a']
  666. $return = $this->redis->lRemove('list', 'b', 2);
  667. // ['c', 'c', 'c', 'a']
  668. $this->assertEquals(2, $return);
  669. $this->assertEquals('c', $this->redis->lGET('list', 0));
  670. $this->assertEquals('c', $this->redis->lGET('list', 1));
  671. $this->assertEquals('c', $this->redis->lGET('list', 2));
  672. $this->assertEquals('a', $this->redis->lGET('list', 3));
  673. $this->redis->delete('list');
  674. $this->redis->lPush('list', 'a');
  675. $this->redis->lPush('list', 'b');
  676. $this->redis->lPush('list', 'c');
  677. $this->redis->lPush('list', 'c');
  678. $this->redis->lPush('list', 'b');
  679. $this->redis->lPush('list', 'c');
  680. // ['c', 'b', 'c', 'c', 'b', 'a']
  681. $this->redis->lRemove('list', 'c', -2);
  682. // ['c', 'b', 'b', 'a']
  683. $this->assertEquals(2, $return);
  684. $this->assertEquals('c', $this->redis->lGET('list', 0));
  685. $this->assertEquals('b', $this->redis->lGET('list', 1));
  686. $this->assertEquals('b', $this->redis->lGET('list', 2));
  687. $this->assertEquals('a', $this->redis->lGET('list', 3));
  688. // remove each element
  689. $this->assertEquals(1, $this->redis->lRemove('list', 'a', 0));
  690. $this->assertEquals(0, $this->redis->lRemove('list', 'x', 0));
  691. $this->assertEquals(2, $this->redis->lRemove('list', 'b', 0));
  692. $this->assertEquals(1, $this->redis->lRemove('list', 'c', 0));
  693. $this->assertEquals(FALSE, $this->redis->get('list'));
  694. $this->redis->set('list', 'actually not a list');
  695. $this->assertEquals(FALSE, $this->redis->lRemove('list', 'x'));
  696. }
  697. public function testsAdd()
  698. {
  699. $this->redis->delete('set');
  700. $this->assertEquals(1, $this->redis->sAdd('set', 'val'));
  701. $this->assertEquals(0, $this->redis->sAdd('set', 'val'));
  702. $this->assertTrue($this->redis->sContains('set', 'val'));
  703. $this->assertFalse($this->redis->sContains('set', 'val2'));
  704. $this->assertEquals(1, $this->redis->sAdd('set', 'val2'));
  705. $this->assertTrue($this->redis->sContains('set', 'val2'));
  706. }
  707. public function testsSize()
  708. {
  709. $this->redis->delete('set');
  710. $this->assertEquals(1, $this->redis->sAdd('set', 'val'));
  711. $this->assertEquals(1, $this->redis->sSize('set'));
  712. $this->assertEquals(1, $this->redis->sAdd('set', 'val2'));
  713. $this->assertEquals(2, $this->redis->sSize('set'));
  714. }
  715. public function testsRemove()
  716. {
  717. $this->redis->delete('set');
  718. $this->redis->sAdd('set', 'val');
  719. $this->redis->sAdd('set', 'val2');
  720. $this->redis->sRemove('set', 'val');
  721. $this->assertEquals(1, $this->redis->sSize('set'));
  722. $this->redis->sRemove('set', 'val2');
  723. $this->assertEquals(0, $this->redis->sSize('set'));
  724. }
  725. public function testsMove()
  726. {
  727. $this->redis->delete('set0');
  728. $this->redis->delete('set1');
  729. $this->redis->sAdd('set0', 'val');
  730. $this->redis->sAdd('set0', 'val2');
  731. $this->assertTrue($this->redis->sMove('set0', 'set1', 'val'));
  732. $this->assertFalse($this->redis->sMove('set0', 'set1', 'val'));
  733. $this->assertFalse($this->redis->sMove('set0', 'set1', 'val-what'));
  734. $this->assertEquals(1, $this->redis->sSize('set0'));
  735. $this->assertEquals(1, $this->redis->sSize('set1'));
  736. $this->assertEquals(array('val2'), $this->redis->sGetMembers('set0'));
  737. $this->assertEquals(array('val'), $this->redis->sGetMembers('set1'));
  738. }
  739. public function testsPop()
  740. {
  741. $this->redis->delete('set0');
  742. $this->assertTrue($this->redis->sPop('set0') === FALSE);
  743. $this->redis->sAdd('set0', 'val');
  744. $this->redis->sAdd('set0', 'val2');
  745. $v0 = $this->redis->sPop('set0');
  746. $this->assertTrue(1 === $this->redis->sSize('set0'));
  747. $this->assertTrue($v0 === 'val' || $v0 === 'val2');
  748. $v1 = $this->redis->sPop('set0');
  749. $this->assertTrue(0 === $this->redis->sSize('set0'));
  750. $this->assertTrue(($v0 === 'val' && $v1 === 'val2') || ($v1 === 'val' && $v0 === 'val2'));
  751. $this->assertTrue($this->redis->sPop('set0') === FALSE);
  752. }
  753. public function testsRandMember() {
  754. $this->redis->delete('set0');
  755. $this->assertTrue($this->redis->sRandMember('set0') === FALSE);
  756. $this->redis->sAdd('set0', 'val');
  757. $this->redis->sAdd('set0', 'val2');
  758. $got = array();
  759. while(true) {
  760. $v = $this->redis->sRandMember('set0');
  761. $this->assertTrue(2 === $this->redis->sSize('set0')); // no change.
  762. $this->assertTrue($v === 'val' || $v === 'val2');
  763. $got[$v] = $v;
  764. if(count($got) == 2) {
  765. break;
  766. }
  767. }
  768. }
  769. public function testsContains()
  770. {
  771. $this->redis->delete('set');
  772. $this->redis->sAdd('set', 'val');
  773. $this->assertTrue($this->redis->sContains('set', 'val'));
  774. $this->assertFalse($this->redis->sContains('set', 'val2'));
  775. }
  776. public function testsGetMembers()
  777. {
  778. $this->redis->delete('set');
  779. $this->redis->sAdd('set', 'val');
  780. $this->redis->sAdd('set', 'val2');
  781. $this->redis->sAdd('set', 'val3');
  782. $array = array('val', 'val2', 'val3');
  783. $this->assertEquals($array, $this->redis->sGetMembers('set'));
  784. $this->assertEquals($array, $this->redis->sMembers('set')); // test alias
  785. }
  786. public function testlSet() {
  787. $this->redis->delete('list');
  788. $this->redis->lPush('list', 'val');
  789. $this->redis->lPush('list', 'val2');
  790. $this->redis->lPush('list', 'val3');
  791. $this->assertEquals($this->redis->lGet('list', 0), 'val3');
  792. $this->assertEquals($this->redis->lGet('list', 1), 'val2');
  793. $this->assertEquals($this->redis->lGet('list', 2), 'val');
  794. $this->assertEquals(TRUE, $this->redis->lSet('list', 1, 'valx'));
  795. $this->assertEquals($this->redis->lGet('list', 0), 'val3');
  796. $this->assertEquals($this->redis->lGet('list', 1), 'valx');
  797. $this->assertEquals($this->redis->lGet('list', 2), 'val');
  798. }
  799. public function testsInter() {
  800. $this->redis->delete('x'); // set of odd numbers
  801. $this->redis->delete('y'); // set of prime numbers
  802. $this->redis->delete('z'); // set of squares
  803. $this->redis->delete('t'); // set of numbers of the form n^2 - 1
  804. $x = array(1,3,5,7,9,11,13,15,17,19,21,23,25);
  805. foreach($x as $i) {
  806. $this->redis->sAdd('x', $i);
  807. }
  808. $y = array(1,2,3,5,7,11,13,17,19,23);
  809. foreach($y as $i) {
  810. $this->redis->sAdd('y', $i);
  811. }
  812. $z = array(1,4,9,16,25);
  813. foreach($z as $i) {
  814. $this->redis->sAdd('z', $i);
  815. }
  816. $t = array(2,5,10,17,26);
  817. foreach($t as $i) {
  818. $this->redis->sAdd('t', $i);
  819. }
  820. $xy = $this->redis->sInter('x', 'y'); // odd prime numbers
  821. foreach($xy as $i) {
  822. $i = (int)$i;
  823. $this->assertTrue(in_array($i, array_intersect($x, $y)));
  824. }
  825. $xy = $this->redis->sInter(array('x', 'y')); // odd prime numbers, as array.
  826. foreach($xy as $i) {
  827. $i = (int)$i;
  828. $this->assertTrue(in_array($i, array_intersect($x, $y)));
  829. }
  830. $yz = $this->redis->sInter('y', 'z'); // set of odd squares
  831. foreach($yz as $i) {
  832. $i = (int)$i;
  833. $this->assertTrue(in_array($i, array_intersect($y, $z)));
  834. }
  835. $yz = $this->redis->sInter(array('y', 'z')); // set of odd squares, as array
  836. foreach($yz as $i) {
  837. $i = (int)$i;
  838. $this->assertTrue(in_array($i, array_intersect($y, $z)));
  839. }
  840. $zt = $this->redis->sInter('z', 't'); // prime squares
  841. $this->assertTrue($zt === array());
  842. $zt = $this->redis->sInter(array('z', 't')); // prime squares, as array
  843. $this->assertTrue($zt === array());
  844. $xyz = $this->redis->sInter('x', 'y', 'z');// odd prime squares
  845. $this->assertTrue($xyz === array('1'));
  846. $xyz = $this->redis->sInter(array('x', 'y', 'z'));// odd prime squares, with an array as a parameter
  847. $this->assertTrue($xyz === array('1'));
  848. $nil = $this->redis->sInter();
  849. $this->assertTrue($nil === FALSE);
  850. $nil = $this->redis->sInter(array());
  851. $this->assertTrue($nil === FALSE);
  852. }
  853. public function testsInterStore() {
  854. $this->redis->delete('x'); // set of odd numbers
  855. $this->redis->delete('y'); // set of prime numbers
  856. $this->redis->delete('z'); // set of squares
  857. $this->redis->delete('t'); // set of numbers of the form n^2 - 1
  858. $x = array(1,3,5,7,9,11,13,15,17,19,21,23,25);
  859. foreach($x as $i) {
  860. $this->redis->sAdd('x', $i);
  861. }
  862. $y = array(1,2,3,5,7,11,13,17,19,23);
  863. foreach($y as $i) {
  864. $this->redis->sAdd('y', $i);
  865. }
  866. $z = array(1,4,9,16,25);
  867. foreach($z as $i) {
  868. $this->redis->sAdd('z', $i);
  869. }
  870. $t = array(2,5,10,17,26);
  871. foreach($t as $i) {
  872. $this->redis->sAdd('t', $i);
  873. }
  874. $count = $this->redis->sInterStore('k', 'x', 'y'); // odd prime numbers
  875. $this->assertEquals($count, $this->redis->sSize('k'));
  876. foreach(array_intersect($x, $y) as $i) {
  877. $this->assertTrue($this->redis->sContains('k', $i));
  878. }
  879. $count = $this->redis->sInterStore('k', 'y', 'z'); // set of odd squares
  880. $this->assertEquals($count, $this->redis->sSize('k'));
  881. foreach(array_intersect($y, $z) as $i) {
  882. $this->assertTrue($this->redis->sContains('k', $i));
  883. }
  884. $count = $this->redis->sInterStore('k', 'z', 't'); // squares of the form n^2 + 1
  885. $this->assertEquals($count, 0);
  886. $this->assertEquals($count, $this->redis->sSize('k'));
  887. $this->redis->delete('z');
  888. $xyz = $this->redis->sInterStore('k', 'x', 'y', 'z'); // only z missing, expect 0.
  889. $this->assertTrue($xyz === 0);
  890. $this->redis->delete('y');
  891. $xyz = $this->redis->sInterStore('k', 'x', 'y', 'z'); // y and z missing, expect 0.
  892. $this->assertTrue($xyz === 0);
  893. $this->redis->delete('x');
  894. $xyz = $this->redis->sInterStore('k', 'x', 'y', 'z'); // x y and z ALL missing, expect 0.
  895. $this->assertTrue($xyz === 0);
  896. $o = $this->redis->sInterStore('k');
  897. $this->assertTrue($o === FALSE); // error, wrong parameter count
  898. }
  899. public function testsUnion() {
  900. $this->redis->delete('x'); // set of odd numbers
  901. $this->redis->delete('y'); // set of prime numbers
  902. $this->redis->delete('z'); // set of squares
  903. $this->redis->delete('t'); // set of numbers of the form n^2 - 1
  904. $x = array(1,3,5,7,9,11,13,15,17,19,21,23,25);
  905. foreach($x as $i) {
  906. $this->redis->sAdd('x', $i);
  907. }
  908. $y = array(1,2,3,5,7,11,13,17,19,23);
  909. foreach($y as $i) {
  910. $this->redis->sAdd('y', $i);
  911. }
  912. $z = array(1,4,9,16,25);
  913. foreach($z as $i) {
  914. $this->redis->sAdd('z', $i);
  915. }
  916. $t = array(2,5,10,17,26);
  917. foreach($t as $i) {
  918. $this->redis->sAdd('t', $i);
  919. }
  920. $xy = $this->redis->sUnion('x', 'y'); // x U y
  921. foreach($xy as $i) {
  922. $i = (int)$i;
  923. $this->assertTrue(in_array($i, array_merge($x, $y)));
  924. }
  925. $yz = $this->redis->sUnion('y', 'z'); // y U Z
  926. foreach($yz as $i) {
  927. $i = (int)$i;
  928. $this->assertTrue(in_array($i, array_merge($y, $z)));
  929. }
  930. $zt = $this->redis->sUnion('z', 't'); // z U t
  931. foreach($zt as $i) {
  932. $i = (int)$i;
  933. $this->assertTrue(in_array($i, array_merge($z, $t)));
  934. }
  935. $xyz = $this->redis->sUnion('x', 'y', 'z'); // x U y U z
  936. foreach($xyz as $i) {
  937. $i = (int)$i;
  938. $this->assertTrue(in_array($i, array_merge($x, $y, $z)));
  939. }
  940. $nil = $this->redis->sUnion();
  941. $this->assertTrue($nil === FALSE);
  942. }
  943. public function testsUnionStore() {
  944. $this->redis->delete('x'); // set of odd numbers
  945. $this->redis->delete('y'); // set of prime numbers
  946. $this->redis->delete('z'); // set of squares
  947. $this->redis->delete('t'); // set of numbers of the form n^2 - 1
  948. $x = array(1,3,5,7,9,11,13,15,17,19,21,23,25);
  949. foreach($x as $i) {
  950. $this->redis->sAdd('x', $i);
  951. }
  952. $y = array(1,2,3,5,7,11,13,17,19,23);
  953. foreach($y as $i) {
  954. $this->redis->sAdd('y', $i);
  955. }
  956. $z = array(1,4,9,16,25);
  957. foreach($z as $i) {
  958. $this->redis->sAdd('z', $i);
  959. }
  960. $t = array(2,5,10,17,26);
  961. foreach($t as $i) {
  962. $this->redis->sAdd('t', $i);
  963. }
  964. $count = $this->redis->sUnionStore('k', 'x', 'y'); // x U y
  965. $xy = array_unique(array_merge($x, $y));
  966. $this->assertEquals($count, count($xy));
  967. foreach($xy as $i) {
  968. $i = (int)$i;
  969. $this->assertTrue($this->redis->sContains('k', $i));
  970. }
  971. $count = $this->redis->sUnionStore('k', 'y', 'z'); // y U z
  972. $yz = array_unique(array_merge($y, $z));
  973. $this->assertEquals($count, count($yz));
  974. foreach($yz as $i) {
  975. $i = (int)$i;
  976. $this->assertTrue($this->redis->sContains('k', $i));
  977. }
  978. $count = $this->redis->sUnionStore('k', 'z', 't'); // z U t
  979. $zt = array_unique(array_merge($z, $t));
  980. $this->assertEquals($count, count($zt));
  981. foreach($zt as $i) {
  982. $i = (int)$i;
  983. $this->assertTrue($this->redis->sContains('k', $i));
  984. }
  985. $count = $this->redis->sUnionStore('k', 'x', 'y', 'z'); // x U y U z
  986. $xyz = array_unique(array_merge($x, $y, $z));
  987. $this->assertEquals($count, count($xyz));
  988. foreach($xyz as $i) {
  989. $i = (int)$i;
  990. $this->assertTrue($this->redis->sContains('k', $i));
  991. }
  992. $this->redis->delete('x'); // x missing now
  993. $count = $this->redis->sUnionStore('k', 'x', 'y', 'z'); // x U y U z
  994. $this->assertTrue($count === count(array_unique(array_merge($y, $z))));
  995. $this->redis->delete('y'); // x and y missing
  996. $count = $this->redis->sUnionStore('k', 'x', 'y', 'z'); // x U y U z
  997. $this->assertTrue($count === count(array_unique($z)));
  998. $this->redis->delete('z'); // x, y, and z ALL missing
  999. $count = $this->redis->sUnionStore('k', 'x', 'y', 'z'); // x U y U z
  1000. $this->assertTrue($count === 0);
  1001. $count = $this->redis->sUnionStore('k'); // Union on nothing...
  1002. $this->assertTrue($count === FALSE);
  1003. }
  1004. public function testsDiff() {
  1005. $this->redis->delete('x'); // set of odd numbers
  1006. $this->redis->delete('y'); // set of prime numbers
  1007. $this->redis->delete('z'); // set of squares
  1008. $this->redis->delete('t'); // set of numbers of the form n^2 - 1
  1009. $x = array(1,3,5,7,9,11,13,15,17,19,21,23,25);
  1010. foreach($x as $i) {
  1011. $this->redis->sAdd('x', $i);
  1012. }
  1013. $y = array(1,2,3,5,7,11,13,17,19,23);
  1014. foreach($y as $i) {
  1015. $this->redis->sAdd('y', $i);
  1016. }
  1017. $z = array(1,4,9,16,25);
  1018. foreach($z as $i) {
  1019. $this->redis->sAdd('z', $i);
  1020. }
  1021. $t = array(2,5,10,17,26);
  1022. foreach($t as $i) {
  1023. $this->redis->sAdd('t', $i);
  1024. }
  1025. $xy = $this->redis->sDiff('x', 'y'); // x U y
  1026. foreach($xy as $i) {
  1027. $i = (int)$i;
  1028. $this->assertTrue(in_array($i, array_diff($x, $y)));
  1029. }
  1030. $yz = $this->redis->sDiff('y', 'z'); // y U Z
  1031. foreach($yz as $i) {
  1032. $i = (int)$i;
  1033. $this->assertTrue(in_array($i, array_diff($y, $z)));
  1034. }
  1035. $zt = $this->redis->sDiff('z', 't'); // z U t
  1036. foreach($zt as $i) {
  1037. $i = (int)$i;
  1038. $this->assertTrue(in_array($i, array_diff($z, $t)));
  1039. }
  1040. $xyz = $this->redis->sDiff('x', 'y', 'z'); // x U y U z
  1041. foreach($xyz as $i) {
  1042. $i = (int)$i;
  1043. $this->assertTrue(in_array($i, array_diff($x, $y, $z)));
  1044. }
  1045. $nil = $this->redis->sDiff();
  1046. $this->assertTrue($nil === FALSE);
  1047. }
  1048. public function testsDiffStore() {
  1049. $this->redis->delete('x'); // set of odd numbers
  1050. $this->redis->delete('y'); // set of prime numbers
  1051. $this->redis->delete('z'); // set of squares
  1052. $this->redis->delete('t'); // set of numbers of the form n^2 - 1
  1053. $x = array(1,3,5,7,9,11,13,15,17,19,21,23,25);
  1054. foreach($x as $i) {
  1055. $this->redis->sAdd('x', $i);
  1056. }
  1057. $y = array(1,2,3,5,7,11,13,17,19,23);
  1058. foreach($y as $i) {
  1059. $this->redis->sAdd('y', $i);
  1060. }
  1061. $z = array(1,4,9,16,25);
  1062. foreach($z as $i) {
  1063. $this->redis->sAdd('z', $i);
  1064. }
  1065. $t = array(2,5,10,17,26);
  1066. foreach($t as $i) {
  1067. $this->redis->sAdd('t', $i);
  1068. }
  1069. $count = $this->redis->sDiffStore('k', 'x', 'y'); // x - y
  1070. $xy = array_unique(array_diff($x, $y));
  1071. $this->assertEquals($count, count($xy));
  1072. foreach($xy as $i) {
  1073. $i = (int)$i;
  1074. $this->assertTrue($this->redis->sContains('k', $i));
  1075. }
  1076. $count = $this->redis->sDiffStore('k', 'y', 'z'); // y - z
  1077. $yz = array_unique(array_diff($y, $z));
  1078. $this->assertEquals($count, count($yz));
  1079. foreach($yz as $i) {
  1080. $i = (int)$i;
  1081. $this->assertTrue($this->redis->sContains('k', $i));
  1082. }
  1083. $count = $this->redis->sDiffStore('k', 'z', 't'); // z - t
  1084. $zt = array_unique(array_diff($z, $t));
  1085. $this->assertEquals($count, count($zt));
  1086. foreach($zt as $i) {
  1087. $i = (int)$i;
  1088. $this->assertTrue($this->redis->sContains('k', $i));
  1089. }
  1090. $count = $this->redis->sDiffStore('k', 'x', 'y', 'z'); // x - y - z
  1091. $xyz = array_unique(array_diff($x, $y, $z));
  1092. $this->assertEquals($count, count($xyz));
  1093. foreach($xyz as $i) {
  1094. $i = (int)$i;
  1095. $this->assertTrue($this->redis->sContains('k', $i));
  1096. }
  1097. $this->redis->delete('x'); // x missing now
  1098. $count = $this->redis->sDiffStore('k', 'x', 'y', 'z'); // x - y - z
  1099. $this->assertTrue($count === 0);
  1100. $this->redis->delete('y'); // x and y missing
  1101. $count = $this->redis->sDiffStore('k', 'x', 'y', 'z'); // x - y - z
  1102. $this->assertTrue($count === 0);
  1103. $this->redis->delete('z'); // x, y, and z ALL missing
  1104. $count = $this->redis->sDiffStore('k', 'x', 'y', 'z'); // x - y - z
  1105. $this->assertTrue($count === 0);
  1106. $count = $this->redis->sDiffStore('k'); // diff on nothing...
  1107. $this->assertTrue($count === FALSE);
  1108. }
  1109. public function testlGetRange() {
  1110. $this->redis->delete('list');
  1111. $this->redis->lPush('list', 'val');
  1112. $this->redis->lPush('list', 'val2');
  1113. $this->redis->lPush('list', 'val3');
  1114. // pos : 0 1 2
  1115. // pos : -3 -2 -1
  1116. // list: [val3, val2, val]
  1117. $this->assertEquals($this->redis->lGetRange('list', 0, 0), array('val3'));
  1118. $this->assertEquals($this->redis->lGetRange('list', 0, 1), array('val3', 'val2'));
  1119. $this->assertEquals($this->redis->lGetRange('list', 0, 2), array('val3', 'val2', 'val'));
  1120. $this->assertEquals($this->redis->lGetRange('list', 0, 3), array('val3', 'val2', 'val'));
  1121. $this->assertEquals($this->redis->lGetRange('list', 0, -1), array('val3', 'val2', 'val'));
  1122. $this->assertEquals($this->redis->lGetRange('list', 0, -2), array('val3', 'val2'));
  1123. $this->assertEquals($this->redis->lGetRange('list', -2, -1), array('val2', 'val'));
  1124. $this->redis->delete('list');
  1125. $this->assertEquals($this->redis->lGetRange('list', 0, -1), array());
  1126. }
  1127. // public function testsave() {
  1128. // $this->assertTrue($this->redis->save() === TRUE); // don't really know how else to test this...
  1129. // }
  1130. // public function testbgSave() {
  1131. // // let's try to