PageRenderTime 81ms CodeModel.GetById 32ms 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

Large files files are truncated, but you can click here to view the full file

  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->asseā€¦

Large files files are truncated, but you can click here to view the full file