RedisCommandsTest.php 77 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072
  1. <?php
  2. class RedisCommandsTestSuite extends PHPUnit_Framework_TestCase {
  3. public $redis;
  4. // TODO: instead of an boolean assertion against the return value
  5. // of RC::sameValuesInArrays, we should extend PHPUnit with
  6. // a new assertion, e.g. $this->assertSameValues();
  7. // TODO: an option to skip certain tests such as testflushdbs
  8. // should be provided.
  9. // TODO: missing test with float values for a few commands
  10. protected function setUp() {
  11. $this->redis = RC::getConnection();
  12. $this->redis->flushdb();
  13. }
  14. protected function tearDown() {
  15. }
  16. protected function onNotSuccessfulTest(Exception $exception) {
  17. // drops and reconnect to a redis server on uncaught exceptions
  18. RC::resetConnection();
  19. parent::onNotSuccessfulTest($exception);
  20. }
  21. /* miscellaneous commands */
  22. function testPing() {
  23. $this->assertTrue($this->redis->ping());
  24. }
  25. function testEcho() {
  26. $string = 'This is an echo test!';
  27. $this->assertEquals($string, $this->redis->echo($string));
  28. }
  29. function testQuit() {
  30. $this->redis->quit();
  31. $this->assertFalse($this->redis->isConnected());
  32. }
  33. function testMultiExec() {
  34. // NOTE: due to a limitation in the current implementation of Predis_Client,
  35. // the replies returned by Predis_Command_Exec are not parsed by their
  36. // respective Predis_Command::parseResponse methods. If you need that
  37. // kind of behaviour, you should use an instance of Predis_MultiExecBlock.
  38. $this->assertTrue($this->redis->multi());
  39. $this->assertInstanceOf('Predis_ResponseQueued', $this->redis->ping());
  40. $this->assertInstanceOf('Predis_ResponseQueued', $this->redis->echo('hello'));
  41. $this->assertInstanceOf('Predis_ResponseQueued', $this->redis->echo('redis'));
  42. $this->assertEquals(array('PONG', 'hello', 'redis'), $this->redis->exec());
  43. $this->assertTrue($this->redis->multi());
  44. $this->assertEquals(array(), $this->redis->exec());
  45. // should throw an exception when trying to EXEC without having previously issued MULTI
  46. RC::testForServerException($this, RC::EXCEPTION_EXEC_NO_MULTI, p_anon("\$test", "
  47. \$test->redis->exec();
  48. "));
  49. }
  50. function testDiscard() {
  51. $this->assertTrue($this->redis->multi());
  52. $this->assertInstanceOf('Predis_ResponseQueued', $this->redis->set('foo', 'bar'));
  53. $this->assertInstanceOf('Predis_ResponseQueued', $this->redis->set('hoge', 'piyo'));
  54. $this->assertEquals(true, $this->redis->discard());
  55. // should throw an exception when trying to EXEC after a DISCARD
  56. RC::testForServerException($this, RC::EXCEPTION_EXEC_NO_MULTI, p_anon("\$test", "
  57. \$test->redis->exec();
  58. "));
  59. $this->assertFalse($this->redis->exists('foo'));
  60. $this->assertFalse($this->redis->exists('hoge'));
  61. }
  62. /* commands operating on string values */
  63. function testSet() {
  64. $this->assertTrue($this->redis->set('foo', 'bar'));
  65. $this->assertEquals('bar', $this->redis->get('foo'));
  66. }
  67. function testGet() {
  68. $this->redis->set('foo', 'bar');
  69. $this->assertEquals('bar', $this->redis->get('foo'));
  70. $this->assertTrue($this->redis->set('foo', ''));
  71. $this->assertEquals('', $this->redis->get('foo'));
  72. $this->assertNull($this->redis->get('fooDoesNotExist'));
  73. // should throw an exception when trying to do a GET on non-string types
  74. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, p_anon("\$test", "
  75. \$test->redis->rpush('metavars', 'foo');
  76. \$test->redis->get('metavars');
  77. "));
  78. }
  79. function testExists() {
  80. $this->redis->set('foo', 'bar');
  81. $this->assertTrue($this->redis->exists('foo'));
  82. $this->assertFalse($this->redis->exists('key_does_not_exist'));
  83. }
  84. function testSetPreserve() {
  85. $multi = RC::getKeyValueArray();
  86. $this->assertTrue($this->redis->setnx('foo', 'bar'));
  87. $this->assertFalse($this->redis->setnx('foo', 'rab'));
  88. $this->assertEquals('bar', $this->redis->get('foo'));
  89. }
  90. function testMultipleSetAndGet() {
  91. $multi = RC::getKeyValueArray();
  92. // key=>value pairs via array instance
  93. $this->assertTrue($this->redis->mset($multi));
  94. $multiRet = $this->redis->mget(array_keys($multi));
  95. $this->assertEquals($multi, array_combine(array_keys($multi), array_values($multiRet)));
  96. // key=>value pairs via function arguments
  97. $this->assertTrue($this->redis->mset('a', 1, 'b', 2, 'c', 3));
  98. $this->assertEquals(array(1, 2, 3), $this->redis->mget('a', 'b', 'c'));
  99. }
  100. function testSetMultiplePreserve() {
  101. $multi = RC::getKeyValueArray();
  102. $newpair = array('hogehoge' => 'piyopiyo');
  103. $hijacked = array('foo' => 'baz', 'hoge' => 'fuga');
  104. // successful set
  105. $expectedResult = array_merge($multi, $newpair);
  106. $this->redis->mset($multi);
  107. $this->assertTrue($this->redis->msetnx($newpair));
  108. $this->assertEquals(
  109. array_values($expectedResult),
  110. $this->redis->mget(array_keys($expectedResult))
  111. );
  112. $this->redis->flushdb();
  113. // unsuccessful set
  114. $expectedResult = array_merge($multi, array('hogehoge' => null));
  115. $this->redis->mset($multi);
  116. $this->assertFalse($this->redis->msetnx(array_merge($newpair, $hijacked)));
  117. $this->assertEquals(
  118. array_values($expectedResult),
  119. $this->redis->mget(array_keys($expectedResult))
  120. );
  121. }
  122. function testGetSet() {
  123. $this->assertNull($this->redis->getset('foo', 'bar'));
  124. $this->assertEquals('bar', $this->redis->getset('foo', 'barbar'));
  125. $this->assertEquals('barbar', $this->redis->getset('foo', 'baz'));
  126. }
  127. function testIncrementAndIncrementBy() {
  128. // test subsequent increment commands
  129. $this->assertEquals(1, $this->redis->incr('foo'));
  130. $this->assertEquals(2, $this->redis->incr('foo'));
  131. // test subsequent incrementBy commands
  132. $this->assertEquals(22, $this->redis->incrby('foo', 20));
  133. $this->assertEquals(10, $this->redis->incrby('foo', -12));
  134. $this->assertEquals(-100, $this->redis->incrby('foo', -110));
  135. }
  136. function testDecrementAndDecrementBy() {
  137. // test subsequent decrement commands
  138. $this->assertEquals(-1, $this->redis->decr('foo'));
  139. $this->assertEquals(-2, $this->redis->decr('foo'));
  140. // test subsequent decrementBy commands
  141. $this->assertEquals(-22, $this->redis->decrby('foo', 20));
  142. $this->assertEquals(-10, $this->redis->decrby('foo', -12));
  143. $this->assertEquals(100, $this->redis->decrby('foo', -110));
  144. }
  145. function testDelete() {
  146. $this->redis->set('foo', 'bar');
  147. $this->assertEquals(1, $this->redis->del('foo'));
  148. $this->assertFalse($this->redis->exists('foo'));
  149. $this->assertEquals(0, $this->redis->del('foo'));
  150. }
  151. function testType() {
  152. $this->assertEquals('none', $this->redis->type('fooDoesNotExist'));
  153. $this->redis->set('fooString', 'bar');
  154. $this->assertEquals('string', $this->redis->type('fooString'));
  155. $this->redis->rpush('fooList', 'bar');
  156. $this->assertEquals('list', $this->redis->type('fooList'));
  157. $this->redis->sadd('fooSet', 'bar');
  158. $this->assertEquals('set', $this->redis->type('fooSet'));
  159. $this->redis->zadd('fooZSet', 0, 'bar');
  160. $this->assertEquals('zset', $this->redis->type('fooZSet'));
  161. $this->redis->hset('fooHash', 'value', 'bar');
  162. $this->assertEquals('hash', $this->redis->type('fooHash'));
  163. }
  164. function testAppend() {
  165. $this->redis->set('foo', 'bar');
  166. $this->assertEquals(5, $this->redis->append('foo', '__'));
  167. $this->assertEquals(8, $this->redis->append('foo', 'bar'));
  168. $this->assertEquals('bar__bar', $this->redis->get('foo'));
  169. $this->assertEquals(4, $this->redis->append('hoge', 'piyo'));
  170. $this->assertEquals('piyo', $this->redis->get('hoge'));
  171. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, p_anon("\$test", "
  172. \$test->redis->rpush('metavars', 'foo');
  173. \$test->redis->append('metavars', 'bar');
  174. "));
  175. }
  176. function testSetRange() {
  177. $this->assertEquals(6, $this->redis->setrange('var', 0, 'foobar'));
  178. $this->assertEquals('foobar', $this->redis->get('var'));
  179. $this->assertEquals(6, $this->redis->setrange('var', 3, 'foo'));
  180. $this->assertEquals('foofoo', $this->redis->get('var'));
  181. $this->assertEquals(16, $this->redis->setrange('var', 10, 'barbar'));
  182. $this->assertEquals("foofoo\x00\x00\x00\x00barbar", $this->redis->get('var'));
  183. $this->assertEquals(4, $this->redis->setrange('binary', 0, pack('l', -2147483648)));
  184. list($unpacked) = array_values(unpack('l', $this->redis->get('binary')));
  185. $this->assertEquals(-2147483648, $unpacked);
  186. RC::testForServerException($this, RC::EXCEPTION_OFFSET_RANGE, p_anon("\$test", "
  187. \$test->redis->setrange('var', -1, 'bogus');
  188. "));
  189. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, p_anon("\$test", "
  190. \$test->redis->rpush('metavars', 'foo');
  191. \$test->redis->setrange('metavars', 0, 'hoge');
  192. "));
  193. }
  194. function testSubstr() {
  195. $this->redis->set('var', 'foobar');
  196. $this->assertEquals('foo', $this->redis->substr('var', 0, 2));
  197. $this->assertEquals('bar', $this->redis->substr('var', 3, 5));
  198. $this->assertEquals('bar', $this->redis->substr('var', -3, -1));
  199. $this->assertEquals($this->redis->substr('var', 5, 0), '');
  200. $this->redis->set('numeric', 123456789);
  201. $this->assertEquals(12345, $this->redis->substr('numeric', 0, 4));
  202. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, p_anon("\$test", "
  203. \$test->redis->rpush('metavars', 'foo');
  204. \$test->redis->substr('metavars', 0, 3);
  205. "));
  206. }
  207. function testStrlen() {
  208. $this->redis->set('var', 'foobar');
  209. $this->assertEquals(6, $this->redis->strlen('var'));
  210. $this->assertEquals(9, $this->redis->append('var', '___'));
  211. $this->assertEquals(9, $this->redis->strlen('var'));
  212. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, p_anon("\$test", "
  213. \$test->redis->rpush('metavars', 'foo');
  214. \$test->redis->strlen('metavars');
  215. "));
  216. }
  217. function testSetBit() {
  218. $this->assertEquals(0, $this->redis->setbit('binary', 31, 1));
  219. $this->assertEquals(0, $this->redis->setbit('binary', 0, 1));
  220. $this->assertEquals(4, $this->redis->strlen('binary'));
  221. $this->assertEquals("\x80\x00\00\x01", $this->redis->get('binary'));
  222. $this->assertEquals(1, $this->redis->setbit('binary', 0, 0));
  223. $this->assertEquals(0, $this->redis->setbit('binary', 0, 0));
  224. $this->assertEquals("\x00\x00\00\x01", $this->redis->get('binary'));
  225. RC::testForServerException($this, RC::EXCEPTION_BIT_OFFSET, p_anon("\$test", "
  226. \$test->redis->setbit('binary', -1, 1);
  227. "));
  228. RC::testForServerException($this, RC::EXCEPTION_BIT_OFFSET, p_anon("\$test", "
  229. \$test->redis->setbit('binary', 'invalid', 1);
  230. "));
  231. RC::testForServerException($this, RC::EXCEPTION_BIT_VALUE, p_anon("\$test", "
  232. \$test->redis->setbit('binary', 15, 255);
  233. "));
  234. RC::testForServerException($this, RC::EXCEPTION_BIT_VALUE, p_anon("\$test", "
  235. \$test->redis->setbit('binary', 15, 'invalid');
  236. "));
  237. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, p_anon("\$test", "
  238. \$test->redis->rpush('metavars', 'foo');
  239. \$test->redis->setbit('metavars', 0, 1);
  240. "));
  241. }
  242. function testGetBit() {
  243. $this->redis->set('binary', "\x80\x00\00\x01");
  244. $this->assertEquals(1, $this->redis->getbit('binary', 0));
  245. $this->assertEquals(0, $this->redis->getbit('binary', 15));
  246. $this->assertEquals(1, $this->redis->getbit('binary', 31));
  247. $this->assertEquals(0, $this->redis->getbit('binary', 63));
  248. RC::testForServerException($this, RC::EXCEPTION_BIT_OFFSET, p_anon("\$test", "
  249. \$test->redis->getbit('binary', -1);
  250. "));
  251. RC::testForServerException($this, RC::EXCEPTION_BIT_OFFSET, p_anon("\$test", "
  252. \$test->redis->getbit('binary', 'invalid');
  253. "));
  254. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, p_anon("\$test", "
  255. \$test->redis->rpush('metavars', 'foo');
  256. \$test->redis->getbit('metavars', 0);
  257. "));
  258. }
  259. /* commands operating on the key space */
  260. function testKeys() {
  261. $keyValsNs = RC::getNamespacedKeyValueArray();
  262. $keyValsOthers = array('aaa' => 1, 'aba' => 2, 'aca' => 3);
  263. $allKeyVals = array_merge($keyValsNs, $keyValsOthers);
  264. $this->redis->mset($allKeyVals);
  265. $this->assertEquals(array(), $this->redis->keys('nokeys:*'));
  266. $keysFromRedis = $this->redis->keys('metavar:*');
  267. $this->assertEquals(array(), array_diff(array_keys($keyValsNs), $keysFromRedis));
  268. $keysFromRedis = $this->redis->keys('*');
  269. $this->assertEquals(array(), array_diff(array_keys($allKeyVals), $keysFromRedis));
  270. $keysFromRedis = $this->redis->keys('a?a');
  271. $this->assertEquals(array(), array_diff(array_keys($keyValsOthers), $keysFromRedis));
  272. }
  273. function testRandomKey() {
  274. $keyvals = RC::getKeyValueArray();
  275. $this->assertNull($this->redis->randomkey());
  276. $this->redis->mset($keyvals);
  277. $this->assertTrue(in_array($this->redis->randomkey(), array_keys($keyvals)));
  278. }
  279. function testRename() {
  280. $this->redis->mset(array('foo' => 'bar', 'foofoo' => 'barbar'));
  281. // rename existing keys
  282. $this->assertTrue($this->redis->rename('foo', 'foofoo'));
  283. $this->assertFalse($this->redis->exists('foo'));
  284. $this->assertEquals('bar', $this->redis->get('foofoo'));
  285. // should throw an excepion then trying to rename non-existing keys
  286. RC::testForServerException($this, RC::EXCEPTION_NO_SUCH_KEY, p_anon("\$test", "
  287. \$test->redis->rename('hoge', 'hogehoge');
  288. "));
  289. }
  290. function testRenamePreserve() {
  291. $this->redis->mset(array('foo' => 'bar', 'hoge' => 'piyo', 'hogehoge' => 'piyopiyo'));
  292. $this->assertTrue($this->redis->renamenx('foo', 'foofoo'));
  293. $this->assertFalse($this->redis->exists('foo'));
  294. $this->assertEquals('bar', $this->redis->get('foofoo'));
  295. $this->assertFalse($this->redis->renamenx('hoge', 'hogehoge'));
  296. $this->assertTrue($this->redis->exists('hoge'));
  297. // should throw an excepion then trying to rename non-existing keys
  298. RC::testForServerException($this, RC::EXCEPTION_NO_SUCH_KEY, p_anon("\$test", "
  299. \$test->redis->renamenx('fuga', 'baz');
  300. "));
  301. }
  302. function testExpirationAndTTL() {
  303. $this->redis->set('foo', 'bar');
  304. // check for key expiration
  305. $this->assertTrue($this->redis->expire('foo', 1));
  306. $this->assertEquals(1, $this->redis->ttl('foo'));
  307. $this->assertTrue($this->redis->exists('foo'));
  308. sleep(2);
  309. $this->assertFalse($this->redis->exists('foo'));
  310. $this->assertEquals(-1, $this->redis->ttl('foo'));
  311. // check for consistent TTL values
  312. $this->redis->set('foo', 'bar');
  313. $this->assertTrue($this->redis->expire('foo', 100));
  314. sleep(3);
  315. $this->assertEquals(97, $this->redis->ttl('foo'));
  316. // delete key on negative TTL
  317. $this->redis->set('foo', 'bar');
  318. $this->assertTrue($this->redis->expire('foo', -100));
  319. $this->assertFalse($this->redis->exists('foo'));
  320. $this->assertEquals(-1, $this->redis->ttl('foo'));
  321. }
  322. function testSetExpire() {
  323. $this->assertTrue($this->redis->setex('foo', 10, 'bar'));
  324. $this->assertTrue($this->redis->exists('foo'));
  325. $this->assertEquals(10, $this->redis->ttl('foo'));
  326. $this->assertTrue($this->redis->setex('hoge', 1, 'piyo'));
  327. sleep(2);
  328. $this->assertFalse($this->redis->exists('hoge'));
  329. // TODO: do not check the error message RC::EXCEPTION_VALUE_NOT_INT for now
  330. RC::testForServerException($this, null, p_anon("\$test", "
  331. \$test->redis->setex('hoge', 2.5, 'piyo');
  332. "));
  333. RC::testForServerException($this, RC::EXCEPTION_SETEX_TTL, p_anon("\$test", "
  334. \$test->redis->setex('hoge', 0, 'piyo');
  335. "));
  336. RC::testForServerException($this, RC::EXCEPTION_SETEX_TTL, p_anon("\$test", "
  337. \$test->redis->setex('hoge', -10, 'piyo');
  338. "));
  339. }
  340. function testDatabaseSize() {
  341. // TODO: is this really OK?
  342. $this->assertEquals(0, $this->redis->dbsize());
  343. $this->redis->mset(RC::getKeyValueArray());
  344. $this->assertGreaterThan(0, $this->redis->dbsize());
  345. }
  346. /* commands operating on lists */
  347. function testPushTail() {
  348. // NOTE: List push operations return the list length since Redis commit 520b5a3
  349. $this->assertEquals(1, $this->redis->rpush('metavars', 'foo'));
  350. $this->assertTrue($this->redis->exists('metavars'));
  351. $this->assertEquals(2, $this->redis->rpush('metavars', 'hoge'));
  352. // should throw an exception when trying to do a RPUSH on non-list types
  353. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, p_anon("\$test", "
  354. \$test->redis->set('foo', 'bar');
  355. \$test->redis->rpush('foo', 'bar');
  356. "));
  357. }
  358. function testPushTailX() {
  359. $this->assertEquals(0, $this->redis->rpushx('numbers', 1));
  360. $this->assertEquals(1, $this->redis->rpush('numbers', 2));
  361. $this->assertEquals(2, $this->redis->rpushx('numbers', 3));
  362. $this->assertEquals(2, $this->redis->llen('numbers'));
  363. $this->assertEquals(array(2, 3), $this->redis->lrange('numbers', 0, -1));
  364. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, p_anon("\$test", "
  365. \$test->redis->set('foo', 'bar');
  366. \$test->redis->rpushx('foo', 'bar');
  367. "));
  368. }
  369. function testPushHead() {
  370. // NOTE: List push operations return the list length since Redis commit 520b5a3
  371. $this->assertEquals(1, $this->redis->lpush('metavars', 'foo'));
  372. $this->assertTrue($this->redis->exists('metavars'));
  373. $this->assertEquals(2, $this->redis->lpush('metavars', 'hoge'));
  374. // should throw an exception when trying to do a LPUSH on non-list types
  375. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, p_anon("\$test", "
  376. \$test->redis->set('foo', 'bar');
  377. \$test->redis->lpush('foo', 'bar');
  378. "));
  379. }
  380. function testPushHeadX() {
  381. $this->assertEquals(0, $this->redis->lpushx('numbers', 1));
  382. $this->assertEquals(1, $this->redis->lpush('numbers', 2));
  383. $this->assertEquals(2, $this->redis->lpushx('numbers', 3));
  384. $this->assertEquals(2, $this->redis->llen('numbers'));
  385. $this->assertEquals(array(3, 2), $this->redis->lrange('numbers', 0, -1));
  386. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, p_anon("\$test", "
  387. \$test->redis->set('foo', 'bar');
  388. \$test->redis->lpushx('foo', 'bar');
  389. "));
  390. }
  391. function testListLength() {
  392. $this->assertEquals(1, $this->redis->rpush('metavars', 'foo'));
  393. $this->assertEquals(2, $this->redis->rpush('metavars', 'hoge'));
  394. $this->assertEquals(2, $this->redis->llen('metavars'));
  395. $this->assertEquals(0, $this->redis->llen('doesnotexist'));
  396. // should throw an exception when trying to do a LLEN on non-list types
  397. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, p_anon("\$test", "
  398. \$test->redis->set('foo', 'bar');
  399. \$test->redis->llen('foo');
  400. "));
  401. }
  402. function testListRange() {
  403. $numbers = RC::pushTailAndReturn($this->redis, 'numbers', RC::getArrayOfNumbers());
  404. $this->assertEquals(
  405. array_slice($numbers, 0, 4),
  406. $this->redis->lrange('numbers', 0, 3)
  407. );
  408. $this->assertEquals(
  409. array_slice($numbers, 4, 5),
  410. $this->redis->lrange('numbers', 4, 8)
  411. );
  412. $this->assertEquals(
  413. array_slice($numbers, 0, 1),
  414. $this->redis->lrange('numbers', 0, 0)
  415. );
  416. $this->assertEquals(
  417. array(),
  418. $this->redis->lrange('numbers', 1, 0)
  419. );
  420. $this->assertEquals(
  421. $numbers,
  422. $this->redis->lrange('numbers', 0, -1)
  423. );
  424. $this->assertEquals(
  425. array(5),
  426. $this->redis->lrange('numbers', 5, -5)
  427. );
  428. $this->assertEquals(
  429. array(),
  430. $this->redis->lrange('numbers', 7, -5)
  431. );
  432. $this->assertEquals(
  433. array_slice($numbers, -5, -1),
  434. $this->redis->lrange('numbers', -5, -2)
  435. );
  436. $this->assertEquals(
  437. $numbers,
  438. $this->redis->lrange('numbers', -100, 100)
  439. );
  440. $this->assertEquals(
  441. array(),
  442. $this->redis->lrange('keyDoesNotExist', 0, 1)
  443. );
  444. // should throw an exception when trying to do a LRANGE on non-list types
  445. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, p_anon("\$test", "
  446. \$test->redis->set('foo', 'bar');
  447. \$test->redis->lrange('foo', 0, -1);
  448. "));
  449. }
  450. function testListTrim() {
  451. $numbers = RC::pushTailAndReturn($this->redis, 'numbers', RC::getArrayOfNumbers());
  452. $this->assertTrue($this->redis->ltrim('numbers', 0, 2));
  453. $this->assertEquals(
  454. array_slice($numbers, 0, 3),
  455. $this->redis->lrange('numbers', 0, -1)
  456. );
  457. $numbers = RC::pushTailAndReturn($this->redis, 'numbers', RC::getArrayOfNumbers(), RC::WIPE_OUT);
  458. $this->assertTrue($this->redis->ltrim('numbers', 5, 9));
  459. $this->assertEquals(
  460. array_slice($numbers, 5, 5),
  461. $this->redis->lrange('numbers', 0, -1)
  462. );
  463. $numbers = RC::pushTailAndReturn($this->redis, 'numbers', RC::getArrayOfNumbers(), RC::WIPE_OUT);
  464. $this->assertTrue($this->redis->ltrim('numbers', 0, -6));
  465. $this->assertEquals(
  466. array_slice($numbers, 0, -5),
  467. $this->redis->lrange('numbers', 0, -1)
  468. );
  469. $numbers = RC::pushTailAndReturn($this->redis, 'numbers', RC::getArrayOfNumbers(), RC::WIPE_OUT);
  470. $this->assertTrue($this->redis->ltrim('numbers', -5, -3));
  471. $this->assertEquals(
  472. array_slice($numbers, 5, 3),
  473. $this->redis->lrange('numbers', 0, -1)
  474. );
  475. $numbers = RC::pushTailAndReturn($this->redis, 'numbers', RC::getArrayOfNumbers(), RC::WIPE_OUT);
  476. $this->assertTrue($this->redis->ltrim('numbers', -100, 100));
  477. $this->assertEquals(
  478. $numbers,
  479. $this->redis->lrange('numbers', 0, -1)
  480. );
  481. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, p_anon("\$test", "
  482. \$test->redis->set('foo', 'bar');
  483. \$test->redis->ltrim('foo', 0, 1);
  484. "));
  485. }
  486. function testListIndex() {
  487. $numbers = RC::pushTailAndReturn($this->redis, 'numbers', RC::getArrayOfNumbers());
  488. $this->assertEquals(0, $this->redis->lindex('numbers', 0));
  489. $this->assertEquals(5, $this->redis->lindex('numbers', 5));
  490. $this->assertEquals(9, $this->redis->lindex('numbers', 9));
  491. $this->assertNull($this->redis->lindex('numbers', 100));
  492. $this->assertEquals(0, $this->redis->lindex('numbers', -0));
  493. $this->assertEquals(9, $this->redis->lindex('numbers', -1));
  494. $this->assertEquals(7, $this->redis->lindex('numbers', -3));
  495. $this->assertNull($this->redis->lindex('numbers', -100));
  496. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, p_anon("\$test", "
  497. \$test->redis->set('foo', 'bar');
  498. \$test->redis->lindex('foo', 0);
  499. "));
  500. }
  501. function testListSet() {
  502. $numbers = RC::pushTailAndReturn($this->redis, 'numbers', RC::getArrayOfNumbers());
  503. $this->assertTrue($this->redis->lset('numbers', 5, -5));
  504. $this->assertEquals(-5, $this->redis->lindex('numbers', 5));
  505. RC::testForServerException($this, RC::EXCEPTION_OUT_OF_RANGE, p_anon("\$test", "
  506. \$test->redis->lset('numbers', 99, 99);
  507. "));
  508. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, p_anon("\$test", "
  509. \$test->redis->set('foo', 'bar');
  510. \$test->redis->lset('foo', 0, 0);
  511. "));
  512. }
  513. function testListRemove() {
  514. $mixed = array(0, '_', 2, '_', 4, '_', 6, '_');
  515. RC::pushTailAndReturn($this->redis, 'mixed', $mixed);
  516. $this->assertEquals(2, $this->redis->lrem('mixed', 2, '_'));
  517. $this->assertEquals(array(0, 2, 4, '_', 6, '_'), $this->redis->lrange('mixed', 0, -1));
  518. RC::pushTailAndReturn($this->redis, 'mixed', $mixed, RC::WIPE_OUT);
  519. $this->assertEquals(4, $this->redis->lrem('mixed', 0, '_'));
  520. $this->assertEquals(array(0, 2, 4, 6), $this->redis->lrange('mixed', 0, -1));
  521. RC::pushTailAndReturn($this->redis, 'mixed', $mixed, RC::WIPE_OUT);
  522. $this->assertEquals(2, $this->redis->lrem('mixed', -2, '_'));
  523. $this->assertEquals(array(0, '_', 2, '_', 4, 6), $this->redis->lrange('mixed', 0, -1));
  524. RC::pushTailAndReturn($this->redis, 'mixed', $mixed, RC::WIPE_OUT);
  525. $this->assertEquals(0, $this->redis->lrem('mixed', 2, '|'));
  526. $this->assertEquals($mixed, $this->redis->lrange('mixed', 0, -1));
  527. $this->assertEquals(0, $this->redis->lrem('listDoesNotExist', 2, '_'));
  528. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, p_anon("\$test", "
  529. \$test->redis->set('foo', 'bar');
  530. \$test->redis->lrem('foo', 0, 0);
  531. "));
  532. }
  533. function testListPopFirst() {
  534. $numbers = RC::pushTailAndReturn($this->redis, 'numbers', array(0, 1, 2, 3, 4));
  535. $this->assertEquals(0, $this->redis->lpop('numbers'));
  536. $this->assertEquals(1, $this->redis->lpop('numbers'));
  537. $this->assertEquals(2, $this->redis->lpop('numbers'));
  538. $this->assertEquals(array(3, 4), $this->redis->lrange('numbers', 0, -1));
  539. $this->redis->lpop('numbers');
  540. $this->redis->lpop('numbers');
  541. $this->assertNull($this->redis->lpop('numbers'));
  542. $this->assertNull($this->redis->lpop('numbers'));
  543. $this->assertNull($this->redis->lpop('listDoesNotExist'));
  544. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, p_anon("\$test", "
  545. \$test->redis->set('foo', 'bar');
  546. \$test->redis->lpop('foo');
  547. "));
  548. }
  549. function testListPopLast() {
  550. $numbers = RC::pushTailAndReturn($this->redis, 'numbers', array(0, 1, 2, 3, 4));
  551. $this->assertEquals(4, $this->redis->rpop('numbers'));
  552. $this->assertEquals(3, $this->redis->rpop('numbers'));
  553. $this->assertEquals(2, $this->redis->rpop('numbers'));
  554. $this->assertEquals(array(0, 1), $this->redis->lrange('numbers', 0, -1));
  555. $this->redis->rpop('numbers');
  556. $this->redis->rpop('numbers');
  557. $this->assertNull($this->redis->rpop('numbers'));
  558. $this->assertNull($this->redis->rpop('numbers'));
  559. $this->assertNull($this->redis->rpop('listDoesNotExist'));
  560. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, p_anon("\$test", "
  561. \$test->redis->set('foo', 'bar');
  562. \$test->redis->rpop('foo');
  563. "));
  564. }
  565. function testListPopLastPushHead() {
  566. $numbers = RC::pushTailAndReturn($this->redis, 'numbers', array(0, 1, 2));
  567. $this->assertEquals(0, $this->redis->llen('temporary'));
  568. $this->assertEquals(2, $this->redis->rpoplpush('numbers', 'temporary'));
  569. $this->assertEquals(1, $this->redis->rpoplpush('numbers', 'temporary'));
  570. $this->assertEquals(0, $this->redis->rpoplpush('numbers', 'temporary'));
  571. $this->assertEquals(0, $this->redis->llen('numbers'));
  572. $this->assertEquals(3, $this->redis->llen('temporary'));
  573. $this->assertEquals(array(), $this->redis->lrange('numbers', 0, -1));
  574. $this->assertEquals($numbers, $this->redis->lrange('temporary', 0, -1));
  575. $numbers = RC::pushTailAndReturn($this->redis, 'numbers', array(0, 1, 2));
  576. $this->redis->rpoplpush('numbers', 'numbers');
  577. $this->redis->rpoplpush('numbers', 'numbers');
  578. $this->redis->rpoplpush('numbers', 'numbers');
  579. $this->assertEquals($numbers, $this->redis->lrange('numbers', 0, -1));
  580. $this->assertNull($this->redis->rpoplpush('listDoesNotExist1', 'listDoesNotExist2'));
  581. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, p_anon("\$test", "
  582. \$test->redis->set('foo', 'bar');
  583. \$test->redis->rpoplpush('foo', 'hoge');
  584. "));
  585. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, p_anon("\$test", "
  586. \$test->redis->set('foo', 'bar');
  587. \$test->redis->rpoplpush('temporary', 'foo');
  588. "));
  589. }
  590. function testListBlockingPopFirst() {
  591. // TODO: this test does not cover all the aspects of BLPOP/BRPOP as it
  592. // does not run with a concurrent client pushing items on lists.
  593. RC::helperForBlockingPops('blpop');
  594. // BLPOP on one key
  595. $start = time();
  596. $item = $this->redis->blpop('blpop3', 5);
  597. $this->assertEquals((float)(time() - $start), 0, '', 1);
  598. $this->assertEquals($item, array('blpop3', 'c'));
  599. // BLPOP on more than one key
  600. $poppedItems = array();
  601. while ($item = $this->redis->blpop('blpop1', 'blpop2', 1)) {
  602. $poppedItems[] = $item;
  603. }
  604. $this->assertEquals(
  605. array(array('blpop1', 'a'), array('blpop1', 'd'), array('blpop2', 'b')),
  606. $poppedItems
  607. );
  608. // check if BLPOP timeouts as expected on empty lists
  609. $start = time();
  610. $this->redis->blpop('blpop4', 2);
  611. $this->assertEquals((float)(time() - $start), 2, '', 1);
  612. }
  613. function testListBlockingPopLast() {
  614. // TODO: this test does not cover all the aspects of BLPOP/BRPOP as it
  615. // does not run with a concurrent client pushing items on lists.
  616. RC::helperForBlockingPops('brpop');
  617. // BRPOP on one key
  618. $start = time();
  619. $item = $this->redis->brpop('brpop3', 5);
  620. $this->assertEquals((float)(time() - $start), 0, '', 1);
  621. $this->assertEquals($item, array('brpop3', 'c'));
  622. // BRPOP on more than one key
  623. $poppedItems = array();
  624. while ($item = $this->redis->brpop('brpop1', 'brpop2', 1)) {
  625. $poppedItems[] = $item;
  626. }
  627. $this->assertEquals(
  628. array(array('brpop1', 'd'), array('brpop1', 'a'), array('brpop2', 'b')),
  629. $poppedItems
  630. );
  631. // check if BRPOP timeouts as expected on empty lists
  632. $start = time();
  633. $this->redis->brpop('brpop4', 2);
  634. $this->assertEquals((float)(time() - $start), 2, '', 1);
  635. }
  636. function testListBlockingPopLastPushHead() {
  637. // TODO: this test does not cover all the aspects of BLPOP/BRPOP as it
  638. // does not run with a concurrent client pushing items on lists.
  639. $numbers = RC::pushTailAndReturn($this->redis, 'numbers', array(1, 2, 3));
  640. $src_count = count($numbers);
  641. $dst_count = 0;
  642. while ($item = $this->redis->brpoplpush('numbers', 'temporary', 1)) {
  643. $this->assertEquals(--$src_count, $this->redis->llen('numbers'));
  644. $this->assertEquals(++$dst_count, $this->redis->llen('temporary'));
  645. $this->assertEquals(array_pop($numbers), $this->redis->lindex('temporary', 0));
  646. }
  647. $start = time();
  648. $this->assertNull($this->redis->brpoplpush('numbers', 'temporary', 2));
  649. $this->assertEquals(2, (float)(time() - $start), '', 1);
  650. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, p_anon("\$test", "
  651. \$test->redis->del('numbers');
  652. \$test->redis->del('temporary');
  653. \$test->redis->set('numbers', 'foobar');
  654. \$test->redis->brpoplpush('numbers', 'temporary', 1);
  655. "));
  656. }
  657. function testListInsert() {
  658. $numbers = RC::pushTailAndReturn($this->redis, 'numbers', RC::getArrayOfNumbers());
  659. $this->assertEquals(11, $this->redis->linsert('numbers', 'before', 0, -2));
  660. $this->assertEquals(12, $this->redis->linsert('numbers', 'after', -2, -1));
  661. $this->assertEquals(array(-2, -1, 0, 1), $this->redis->lrange('numbers', 0, 3));
  662. $this->assertEquals(-1, $this->redis->linsert('numbers', 'after', 100, 200));
  663. $this->assertEquals(-1, $this->redis->linsert('numbers', 'before', 100, 50));
  664. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, p_anon("\$test", "
  665. \$test->redis->set('foo', 'bar');
  666. \$test->redis->linsert('foo', 'before', 0, 0);
  667. "));
  668. }
  669. /* commands operating on sets */
  670. function testSetAdd() {
  671. $this->assertTrue($this->redis->sadd('set', 0));
  672. $this->assertTrue($this->redis->sadd('set', 1));
  673. $this->assertFalse($this->redis->sadd('set', 0));
  674. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, p_anon("\$test", "
  675. \$test->redis->set('foo', 'bar');
  676. \$test->redis->sadd('foo', 0);
  677. "));
  678. }
  679. function testSetRemove() {
  680. $set = RC::setAddAndReturn($this->redis, 'set', array(0, 1, 2, 3, 4));
  681. $this->assertTrue($this->redis->srem('set', 0));
  682. $this->assertTrue($this->redis->srem('set', 4));
  683. $this->assertFalse($this->redis->srem('set', 10));
  684. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, p_anon("\$test", "
  685. \$test->redis->set('foo', 'bar');
  686. \$test->redis->srem('foo', 0);
  687. "));
  688. }
  689. function testSetPop() {
  690. $set = RC::setAddAndReturn($this->redis, 'set', array(0, 1, 2, 3, 4));
  691. $this->assertTrue(in_array($this->redis->spop('set'), $set));
  692. $this->assertNull($this->redis->spop('setDoesNotExist'));
  693. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, p_anon("\$test", "
  694. \$test->redis->set('foo', 'bar');
  695. \$test->redis->spop('foo');
  696. "));
  697. }
  698. function testSetMove() {
  699. $setA = RC::setAddAndReturn($this->redis, 'setA', array(0, 1, 2, 3, 4, 5));
  700. $setB = RC::setAddAndReturn($this->redis, 'setB', array(5, 6, 7, 8, 9, 10));
  701. $this->assertTrue($this->redis->smove('setA', 'setB', 0));
  702. $this->assertFalse($this->redis->srem('setA', 0));
  703. $this->assertTrue($this->redis->srem('setB', 0));
  704. $this->assertTrue($this->redis->smove('setA', 'setB', 5));
  705. $this->assertFalse($this->redis->smove('setA', 'setB', 100));
  706. // wrong type
  707. $this->redis->set('foo', 'bar');
  708. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, p_anon("\$test", "
  709. \$test->redis->smove('foo', 'setB', 5);
  710. "));
  711. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, p_anon("\$test", "
  712. \$test->redis->smove('setA', 'foo', 5);
  713. "));
  714. }
  715. function testSetCardinality() {
  716. RC::setAddAndReturn($this->redis, 'setA', array(0, 1, 2, 3, 4, 5));
  717. $this->assertEquals(6, $this->redis->scard('setA'));
  718. // empty set
  719. $this->redis->sadd('setB', 0);
  720. $this->redis->spop('setB');
  721. $this->assertEquals(0, $this->redis->scard('setB'));
  722. // non-existing set
  723. $this->assertEquals(0, $this->redis->scard('setDoesNotExist'));
  724. // wrong type
  725. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, p_anon("\$test", "
  726. \$test->redis->set('foo', 'bar');
  727. \$test->redis->scard('foo');
  728. "));
  729. }
  730. function testSetIsMember() {
  731. RC::setAddAndReturn($this->redis, 'set', array(0, 1, 2, 3, 4, 5));
  732. $this->assertTrue($this->redis->sismember('set', 3));
  733. $this->assertFalse($this->redis->sismember('set', 100));
  734. $this->assertFalse($this->redis->sismember('setDoesNotExist', 0));
  735. // wrong type
  736. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, p_anon("\$test", "
  737. \$test->redis->set('foo', 'bar');
  738. \$test->redis->sismember('foo', 0);
  739. "));
  740. }
  741. function testSetMembers() {
  742. $set = RC::setAddAndReturn($this->redis, 'set', array(0, 1, 2, 3, 4, 5, 6));
  743. $this->assertTrue(RC::sameValuesInArrays($set, $this->redis->smembers('set')));
  744. $this->assertEquals(array(), $this->redis->smembers('setDoesNotExist'));
  745. // wrong type
  746. $this->redis->set('foo', 'bar');
  747. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, p_anon("\$test", "
  748. \$test->redis->smembers('foo');
  749. "));
  750. }
  751. function testSetIntersection() {
  752. $setA = RC::setAddAndReturn($this->redis, 'setA', array(0, 1, 2, 3, 4, 5, 6));
  753. $setB = RC::setAddAndReturn($this->redis, 'setB', array(1, 3, 4, 6, 9, 10));
  754. $this->assertTrue(RC::sameValuesInArrays(
  755. $setA,
  756. $this->redis->sinter('setA')
  757. ));
  758. $this->assertTrue(RC::sameValuesInArrays(
  759. array_intersect($setA, $setB),
  760. $this->redis->sinter('setA', 'setB')
  761. ));
  762. $this->assertEquals(array(), $this->redis->sinter('setA', 'setDoesNotExist'));
  763. // wrong type
  764. $this->redis->set('foo', 'bar');
  765. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, p_anon("\$test", "
  766. \$test->redis->sinter('foo');
  767. "));
  768. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, p_anon("\$test", "
  769. \$test->redis->sinter('setA', 'foo');
  770. "));
  771. }
  772. function testSetIntersectionStore() {
  773. $setA = RC::setAddAndReturn($this->redis, 'setA', array(0, 1, 2, 3, 4, 5, 6));
  774. $setB = RC::setAddAndReturn($this->redis, 'setB', array(1, 3, 4, 6, 9, 10));
  775. $this->assertEquals(count($setA), $this->redis->sinterstore('setC', 'setA'));
  776. $this->assertTrue(RC::sameValuesInArrays(
  777. $setA,
  778. $this->redis->smembers('setC')
  779. ));
  780. $this->redis->del('setC');
  781. $this->assertEquals(4, $this->redis->sinterstore('setC', 'setA', 'setB'));
  782. $this->assertTrue(RC::sameValuesInArrays(
  783. array(1, 3, 4, 6),
  784. $this->redis->smembers('setC')
  785. ));
  786. $this->redis->del('setC');
  787. $this->assertEquals(array(), $this->redis->sinter('setC', 'setDoesNotExist'));
  788. $this->assertFalse($this->redis->exists('setC'));
  789. // existing keys are replaced by SINTERSTORE
  790. $this->redis->set('foo', 'bar');
  791. $this->assertEquals(count($setA), $this->redis->sinterstore('foo', 'setA'));
  792. // accepts an array for the list of source keys
  793. $this->assertEquals(4, $this->redis->sinterstore('setC', array('setA', 'setB')));
  794. // wrong type
  795. $this->redis->set('foo', 'bar');
  796. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, p_anon("\$test", "
  797. \$test->redis->sinterstore('setA', 'foo');
  798. "));
  799. }
  800. function testSetUnion() {
  801. $setA = RC::setAddAndReturn($this->redis, 'setA', array(0, 1, 2, 3, 4, 5, 6));
  802. $setB = RC::setAddAndReturn($this->redis, 'setB', array(1, 3, 4, 6, 9, 10));
  803. $this->assertTrue(RC::sameValuesInArrays(
  804. $setA,
  805. $this->redis->sunion('setA')
  806. ));
  807. $this->assertTrue(RC::sameValuesInArrays(
  808. array_union($setA, $setB),
  809. $this->redis->sunion('setA', 'setB')
  810. ));
  811. $this->assertTrue(RC::sameValuesInArrays(
  812. $setA,
  813. $this->redis->sunion('setA', 'setDoesNotExist')
  814. ));
  815. // wrong type
  816. $this->redis->set('foo', 'bar');
  817. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, p_anon("\$test", "
  818. \$test->redis->sunion('foo');
  819. "));
  820. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, p_anon("\$test", "
  821. \$test->redis->sunion('setA', 'foo');
  822. "));
  823. }
  824. function testSetUnionStore() {
  825. $setA = RC::setAddAndReturn($this->redis, 'setA', array(0, 1, 2, 3, 4, 5, 6));
  826. $setB = RC::setAddAndReturn($this->redis, 'setB', array(1, 3, 4, 6, 9, 10));
  827. $this->assertEquals(count($setA), $this->redis->sunionstore('setC', 'setA'));
  828. $this->assertTrue(RC::sameValuesInArrays(
  829. $setA,
  830. $this->redis->smembers('setC')
  831. ));
  832. $this->redis->del('setC');
  833. $this->assertEquals(9, $this->redis->sunionstore('setC', 'setA', 'setB'));
  834. $this->assertTrue(RC::sameValuesInArrays(
  835. array_union($setA, $setB),
  836. $this->redis->smembers('setC')
  837. ));
  838. // non-existing keys are considered empty sets
  839. $this->redis->del('setC');
  840. $this->assertEquals(0, $this->redis->sunionstore('setC', 'setDoesNotExist'));
  841. $this->assertFalse($this->redis->exists('setC'));
  842. $this->assertEquals(0, $this->redis->scard('setC'));
  843. // existing keys are replaced by SUNIONSTORE
  844. $this->redis->set('foo', 'bar');
  845. $this->assertEquals(count($setA), $this->redis->sunionstore('foo', 'setA'));
  846. // accepts an array for the list of source keys
  847. $this->assertEquals(9, $this->redis->sunionstore('setC', array('setA', 'setB')));
  848. // wrong type
  849. $this->redis->set('foo', 'bar');
  850. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, p_anon("\$test", "
  851. \$test->redis->sunionstore('setA', 'foo');
  852. "));
  853. }
  854. function testSetDifference() {
  855. $setA = RC::setAddAndReturn($this->redis, 'setA', array(0, 1, 2, 3, 4, 5, 6));
  856. $setB = RC::setAddAndReturn($this->redis, 'setB', array(1, 3, 4, 6, 9, 10));
  857. $this->assertTrue(RC::sameValuesInArrays(
  858. $setA,
  859. $this->redis->sdiff('setA')
  860. ));
  861. $this->assertTrue(RC::sameValuesInArrays(
  862. array_diff($setA, $setB),
  863. $this->redis->sdiff('setA', 'setB')
  864. ));
  865. $this->assertTrue(RC::sameValuesInArrays(
  866. $setA,
  867. $this->redis->sdiff('setA', 'setDoesNotExist')
  868. ));
  869. // wrong type
  870. $this->redis->set('foo', 'bar');
  871. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, p_anon("\$test", "
  872. \$test->redis->sdiff('foo');
  873. "));
  874. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, p_anon("\$test", "
  875. \$test->redis->sdiff('setA', 'foo');
  876. "));
  877. }
  878. function testSetDifferenceStore() {
  879. $setA = RC::setAddAndReturn($this->redis, 'setA', array(0, 1, 2, 3, 4, 5, 6));
  880. $setB = RC::setAddAndReturn($this->redis, 'setB', array(1, 3, 4, 6, 9, 10));
  881. $this->assertEquals(count($setA), $this->redis->sdiffstore('setC', 'setA'));
  882. $this->assertTrue(RC::sameValuesInArrays(
  883. $setA,
  884. $this->redis->smembers('setC')
  885. ));
  886. $this->redis->del('setC');
  887. $this->assertEquals(3, $this->redis->sdiffstore('setC', 'setA', 'setB'));
  888. $this->assertTrue(RC::sameValuesInArrays(
  889. array_diff($setA, $setB),
  890. $this->redis->smembers('setC')
  891. ));
  892. // non-existing keys are considered empty sets
  893. $this->redis->del('setC');
  894. $this->assertEquals(0, $this->redis->sdiffstore('setC', 'setDoesNotExist'));
  895. $this->assertFalse($this->redis->exists('setC'));
  896. $this->assertEquals(0, $this->redis->scard('setC'));
  897. // existing keys are replaced by SDIFFSTORE
  898. $this->redis->set('foo', 'bar');
  899. $this->assertEquals(count($setA), $this->redis->sdiffstore('foo', 'setA'));
  900. // wrong type
  901. $this->redis->set('foo', 'bar');
  902. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, p_anon("\$test", "
  903. \$test->redis->sdiffstore('setA', 'foo');
  904. "));
  905. }
  906. function testRandomMember() {
  907. $set = RC::setAddAndReturn($this->redis, 'set', array(0, 1, 2, 3, 4, 5, 6));
  908. $this->assertTrue(in_array($this->redis->srandmember('set'), $set));
  909. $this->assertNull($this->redis->srandmember('setDoesNotExist'));
  910. // wrong type
  911. $this->redis->set('foo', 'bar');
  912. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, p_anon("\$test", "
  913. \$test->redis->srandmember('foo');
  914. "));
  915. }
  916. /* commands operating on sorted sets */
  917. function testZsetAdd() {
  918. $this->assertTrue($this->redis->zadd('zset', 0, 'a'));
  919. $this->assertTrue($this->redis->zadd('zset', 1, 'b'));
  920. $this->assertTrue($this->redis->zadd('zset', -1, 'c'));
  921. // TODO: floats?
  922. //$this->assertTrue($this->redis->zadd('zset', -1.0, 'c'));
  923. //$this->assertTrue($this->redis->zadd('zset', -1.0, 'c'));
  924. $this->assertFalse($this->redis->zadd('zset', 2, 'b'));
  925. $this->assertFalse($this->redis->zadd('zset', -2, 'b'));
  926. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, p_anon("\$test", "
  927. \$test->redis->set('foo', 'bar');
  928. \$test->redis->zadd('foo', 0, 'a');
  929. "));
  930. }
  931. function testZsetIncrementBy() {
  932. $this->assertEquals(1, $this->redis->zincrby('zsetDoesNotExist', 1, 'foo'));
  933. $this->assertEquals('zset', $this->redis->type('zsetDoesNotExist'));
  934. RC::zsetAddAndReturn($this->redis, 'zset', RC::getZSetArray());
  935. $this->assertEquals(-5, $this->redis->zincrby('zset', 5, 'a'));
  936. $this->assertEquals(1, $this->redis->zincrby('zset', 1, 'b'));
  937. $this->assertEquals(10, $this->redis->zincrby('zset', 0, 'c'));
  938. $this->assertEquals(0, $this->redis->zincrby('zset', -20, 'd'));
  939. $this->assertEquals(2, $this->redis->zincrby('zset', 2, 'd'));
  940. $this->assertEquals(-10, $this->redis->zincrby('zset', -30, 'e'));
  941. $this->assertEquals(1, $this->redis->zincrby('zset', 1, 'x'));
  942. // wrong type
  943. $this->redis->set('foo', 'bar');
  944. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, p_anon("\$test", "
  945. \$test->redis->zincrby('foo', 1, 'a');
  946. "));
  947. }
  948. function testZsetRemove() {
  949. RC::zsetAddAndReturn($this->redis, 'zset', RC::getZSetArray());
  950. $this->assertTrue($this->redis->zrem('zset', 'a'));
  951. $this->assertFalse($this->redis->zrem('zset', 'x'));
  952. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, p_anon("\$test", "
  953. \$test->redis->set('foo', 'bar');
  954. \$test->redis->zrem('foo', 'bar');
  955. "));
  956. }
  957. function testZsetRange() {
  958. $zset = RC::zsetAddAndReturn($this->redis, 'zset', RC::getZSetArray());
  959. $this->assertEquals(
  960. array_slice(array_keys($zset), 0, 4),
  961. $this->redis->zrange('zset', 0, 3)
  962. );
  963. $this->assertEquals(
  964. array_slice(array_keys($zset), 0, 1),
  965. $this->redis->zrange('zset', 0, 0)
  966. );
  967. $this->assertEquals(
  968. array(),
  969. $this->redis->zrange('zset', 1, 0)
  970. );
  971. $this->assertEquals(
  972. array_values(array_keys($zset)),
  973. $this->redis->zrange('zset', 0, -1)
  974. );
  975. $this->assertEquals(
  976. array_slice(array_keys($zset), 3, 1),
  977. $this->redis->zrange('zset', 3, -3)
  978. );
  979. $this->assertEquals(
  980. array(),
  981. $this->redis->zrange('zset', 5, -3)
  982. );
  983. $this->assertEquals(
  984. array_slice(array_keys($zset), -5, -1),
  985. $this->redis->zrange('zset', -5, -2)
  986. );
  987. $this->assertEquals(
  988. array_values(array_keys($zset)),
  989. $this->redis->zrange('zset', -100, 100)
  990. );
  991. $this->assertEquals(
  992. array_values(array_keys($zset)),
  993. $this->redis->zrange('zset', -100, 100)
  994. );
  995. $this->assertEquals(
  996. array(array('a', -10), array('b', 0), array('c', 10)),
  997. $this->redis->zrange('zset', 0, 2, 'withscores')
  998. );
  999. $this->assertEquals(
  1000. array(array('a', -10), array('b', 0), array('c', 10)),
  1001. $this->redis->zrange('zset', 0, 2, array('withscores' => true))
  1002. );
  1003. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, p_anon("\$test", "
  1004. \$test->redis->set('foo', 'bar');
  1005. \$test->redis->zrange('foo', 0, -1);
  1006. "));
  1007. }
  1008. function testZsetReverseRange() {
  1009. $zset = RC::zsetAddAndReturn($this->redis, 'zset', RC::getZSetArray());
  1010. $this->assertEquals(
  1011. array_slice(array_reverse(array_keys($zset)), 0, 4),
  1012. $this->redis->zrevrange('zset', 0, 3)
  1013. );
  1014. $this->assertEquals(
  1015. array_slice(array_reverse(array_keys($zset)), 0, 1),
  1016. $this->redis->zrevrange('zset', 0, 0)
  1017. );
  1018. $this->assertEquals(
  1019. array(),
  1020. $this->redis->zrevrange('zset', 1, 0)
  1021. );
  1022. $this->assertEquals(
  1023. array_values(array_reverse(array_keys($zset))),
  1024. $this->redis->zrevrange('zset', 0, -1)
  1025. );
  1026. $this->assertEquals(
  1027. array_slice(array_reverse(array_keys($zset)), 3, 1),
  1028. $this->redis->zrevrange('zset', 3, -3)
  1029. );
  1030. $this->assertEquals(
  1031. array(),
  1032. $this->redis->zrevrange('zset', 5, -3)
  1033. );
  1034. $this->assertEquals(
  1035. array_slice(array_reverse(array_keys($zset)), -5, -1),
  1036. $this->redis->zrevrange('zset', -5, -2)
  1037. );
  1038. $this->assertEquals(
  1039. array_values(array_reverse(array_keys($zset))),
  1040. $this->redis->zrevrange('zset', -100, 100)
  1041. );
  1042. $this->assertEquals(
  1043. array(array('f', 30), array('e', 20), array('d', 20)),
  1044. $this->redis->zrevrange('zset', 0, 2, 'withscores')
  1045. );
  1046. $this->assertEquals(
  1047. array(array('f', 30), array('e', 20), array('d', 20)),
  1048. $this->redis->zrevrange('zset', 0, 2, array('withscores' => true))
  1049. );
  1050. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, p_anon("\$test", "
  1051. \$test->redis->set('foo', 'bar');
  1052. \$test->redis->zrevrange('foo', 0, -1);
  1053. "));
  1054. }
  1055. function testZsetRangeByScore() {
  1056. $zset = RC::zsetAddAndReturn($this->redis, 'zset', RC::getZSetArray());
  1057. $this->assertEquals(
  1058. array('a'),
  1059. $this->redis->zrangebyscore('zset', -10, -10)
  1060. );
  1061. $this->assertEquals(
  1062. array('c', 'd', 'e', 'f'),
  1063. $this->redis->zrangebyscore('zset', 10, 30)
  1064. );
  1065. $this->assertEquals(
  1066. array('d', 'e'),
  1067. $this->redis->zrangebyscore('zset', 20, 20)
  1068. );
  1069. $this->assertEquals(
  1070. array(),
  1071. $this->redis->zrangebyscore('zset', 30, 0)
  1072. );
  1073. $this->assertEquals(
  1074. array(array('c', 10), array('d', 20), array('e', 20)),
  1075. $this->redis->zrangebyscore('zset', 10, 20, 'withscores')
  1076. );
  1077. $this->assertEquals(
  1078. array(array('c', 10), array('d', 20), array('e', 20)),
  1079. $this->redis->zrangebyscore('zset', 10, 20, array('withscores' => true))
  1080. );
  1081. $this->assertEquals(
  1082. array('d', 'e'),
  1083. $this->redis->zrangebyscore('zset', 10, 20, array('limit' => array(1, 2)))
  1084. );
  1085. $this->assertEquals(
  1086. array('d', 'e'),
  1087. $this->redis->zrangebyscore('zset', 10, 20, array(
  1088. 'limit' => array('offset' => 1, 'count' => 2)
  1089. ))
  1090. );
  1091. $this->assertEquals(
  1092. array(array('d', 20), array('e', 20)),
  1093. $this->redis->zrangebyscore('zset', 10, 20, array(
  1094. 'limit' => array(1, 2),
  1095. 'withscores' => true,
  1096. ))
  1097. );
  1098. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, p_anon("\$test", "
  1099. \$test->redis->set('foo', 'bar');
  1100. \$test->redis->zrangebyscore('foo', 0, 0);
  1101. "));
  1102. }
  1103. function testZsetReverseRangeByScore() {
  1104. $zset = RC::zsetAddAndReturn($this->redis, 'zset', RC::getZSetArray());
  1105. $this->assertEquals(
  1106. array('a'),
  1107. $this->redis->zrevrangebyscore('zset', -10, -10)
  1108. );
  1109. $this->assertEquals(
  1110. array('b', 'a'),
  1111. $this->redis->zrevrangebyscore('zset', 0, -10)
  1112. );
  1113. $this->assertEquals(
  1114. array('e', 'd'),
  1115. $this->redis->zrevrangebyscore('zset', 20, 20)
  1116. );
  1117. $this->assertEquals(
  1118. array('f', 'e', 'd', 'c', 'b'),
  1119. $this->redis->zrevrangebyscore('zset', 30, 0)
  1120. );
  1121. $this->assertEquals(
  1122. array(array('e', 20), array('d', 20), array('c', 10)),
  1123. $this->redis->zrevrangebyscore('zset', 20, 10, 'withscores')
  1124. );
  1125. $this->assertEquals(
  1126. array(array('e', 20), array('d', 20), array('c', 10)),
  1127. $this->redis->zrevrangebyscore('zset', 20, 10, array('withscores' => true))
  1128. );
  1129. $this->assertEquals(
  1130. array('d', 'c'),
  1131. $this->redis->zrevrangebyscore('zset', 20, 10, array('limit' => array(1, 2)))
  1132. );
  1133. $this->assertEquals(
  1134. array('d', 'c'),
  1135. $this->redis->zrevrangebyscore('zset', 20, 10, array(
  1136. 'limit' => array('offset' => 1, 'count' => 2)
  1137. ))
  1138. );
  1139. $this->assertEquals(
  1140. array(array('d', 20), array('c', 10)),
  1141. $this->redis->zrevrangebyscore('zset', 20, 10, array(
  1142. 'limit' => array(1, 2),
  1143. 'withscores' => true,
  1144. ))
  1145. );
  1146. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, p_anon("\$test", "
  1147. \$test->redis->set('foo', 'bar');
  1148. \$test->redis->zrevrangebyscore('foo', 0, 0);
  1149. "));
  1150. }
  1151. function testZsetUnionStore() {
  1152. $zsetA = RC::zsetAddAndReturn($this->redis, 'zseta', array('a' => 1, 'b' => 2, 'c' => 3));
  1153. $zsetB = RC::zsetAddAndReturn($this->redis, 'zsetb', array('b' => 1, 'c' => 2, 'd' => 3));
  1154. // basic ZUNIONSTORE
  1155. $this->assertEquals(4, $this->redis->zunionstore('zsetc', 2, 'zseta', 'zsetb'));
  1156. $this->assertEquals(
  1157. array(array('a', 1), array('b', 3), array('d', 3), array('c', 5)),
  1158. $this->redis->zrange('zsetc', 0, -1, 'withscores')
  1159. );
  1160. $this->assertEquals(3, $this->redis->zunionstore('zsetc', 2, 'zseta', 'zsetbNull'));
  1161. $this->assertEquals(
  1162. array(array('a', 1), array('b', 2), array('c', 3)),
  1163. $this->redis->zrange('zsetc', 0, -1, 'withscores')
  1164. );
  1165. $this->assertEquals(3, $this->redis->zunionstore('zsetc', 2, 'zsetaNull', 'zsetb'));
  1166. $this->assertEquals(
  1167. array(array('b', 1), array('c', 2), array('d', 3)),
  1168. $this->redis->zrange('zsetc', 0, -1, 'withscores')
  1169. );
  1170. $this->assertEquals(0, $this->redis->zunionstore('zsetc', 2, 'zsetaNull', 'zsetbNull'));
  1171. // with WEIGHTS
  1172. $options = array('weights' => array(2, 3));
  1173. $this->assertEquals(4, $this->redis->zunionstore('zsetc', 2, 'zseta', 'zsetb', $options));
  1174. $this->assertEquals(
  1175. array(array('a', 2), array('b', 7), array('d', 9), array('c', 12)),
  1176. $this->redis->zrange('zsetc', 0, -1, 'withscores')
  1177. );
  1178. // with AGGREGATE (min)
  1179. $options = array('aggregate' => 'min');
  1180. $this->assertEquals(4, $this->redis->zunionstore('zsetc', 2, 'zseta', 'zsetb', $options));
  1181. $this->assertEquals(
  1182. array(array('a', 1), array('b', 1), array('c', 2), array('d', 3)),
  1183. $this->redis->zrange('zsetc', 0, -1, 'withscores')
  1184. );
  1185. // with AGGREGATE (max)
  1186. $options = array('aggregate' => 'max');
  1187. $this->assertEquals(4, $this->redis->zunionstore('zsetc', 2, 'zseta', 'zsetb', $options));
  1188. $this->assertEquals(
  1189. array(array('a', 1), array('b', 2), array('c', 3), array('d', 3)),
  1190. $this->redis->zrange('zsetc', 0, -1, 'withscores')
  1191. );
  1192. // using an array to pass the list of source keys
  1193. $sourceKeys = array('zseta', 'zsetb');
  1194. $this->assertEquals(4, $this->redis->zunionstore('zsetc', $sourceKeys));
  1195. $this->assertEquals(
  1196. array(array('a', 1), array('b', 3), array('d', 3), array('c', 5)),
  1197. $this->redis->zrange('zsetc', 0, -1, 'withscores')
  1198. );
  1199. // using an array to pass the list of source keys + options array
  1200. $options = array('weights' => array(2, 3));
  1201. $this->assertEquals(4, $this->redis->zunionstore('zsetc', $sourceKeys, $options));
  1202. $this->assertEquals(
  1203. array(array('a', 2), array('b', 7), array('d', 9), array('c', 12)),
  1204. $this->redis->zrange('zsetc', 0, -1, 'withscores')
  1205. );
  1206. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, p_anon("\$test", "
  1207. \$test->redis->set('zsetFake', 'fake');
  1208. \$test->redis->zunionstore('zsetc', 2, 'zseta', 'zsetFake');
  1209. "));
  1210. }
  1211. function testZsetIntersectionStore() {
  1212. $zsetA = RC::zsetAddAndReturn($this->redis, 'zseta', array('a' => 1, 'b' => 2, 'c' => 3));
  1213. $zsetB = RC::zsetAddAndReturn($this->redis, 'zsetb', array('b' => 1, 'c' => 2, 'd' => 3));
  1214. // basic ZINTERSTORE
  1215. $this->assertEquals(2, $this->redis->zinterstore('zsetc', 2, 'zseta', 'zsetb'));
  1216. $this->assertEquals(
  1217. array(array('b', 3), array('c', 5)),
  1218. $this->redis->zrange('zsetc', 0, -1, 'withscores')
  1219. );
  1220. $this->assertEquals(0, $this->redis->zinterstore('zsetc', 2, 'zseta', 'zsetbNull'));
  1221. $this->assertEquals(0, $this->redis->zinterstore('zsetc', 2, 'zsetaNull', 'zsetb'));
  1222. $this->assertEquals(0, $this->redis->zinterstore('zsetc', 2, 'zsetaNull', 'zsetbNull'));
  1223. // with WEIGHTS
  1224. $options = array('weights' => array(2, 3));
  1225. $this->assertEquals(2, $this->redis->zinterstore('zsetc', 2, 'zseta', 'zsetb', $options));
  1226. $this->assertEquals(
  1227. array(array('b', 7), array('c', 12)),
  1228. $this->redis->zrange('zsetc', 0, -1, 'withscores')
  1229. );
  1230. // with AGGREGATE (min)
  1231. $options = array('aggregate' => 'min');
  1232. $this->assertEquals(2, $this->redis->zinterstore('zsetc', 2, 'zseta', 'zsetb', $options));
  1233. $this->assertEquals(
  1234. array(array('b', 1), array('c', 2)),
  1235. $this->redis->zrange('zsetc', 0, -1, 'withscores')
  1236. );
  1237. // with AGGREGATE (max)
  1238. $options = array('aggregate' => 'max');
  1239. $this->assertEquals(2, $this->redis->zinterstore('zsetc', 2, 'zseta', 'zsetb', $options));
  1240. $this->assertEquals(
  1241. array(array('b', 2), array('c', 3)),
  1242. $this->redis->zrange('zsetc', 0, -1, 'withscores')
  1243. );
  1244. // using an array to pass the list of source keys
  1245. $sourceKeys = array('zseta', 'zsetb');
  1246. $this->assertEquals(2, $this->redis->zinterstore('zsetc', $sourceKeys));
  1247. $this->assertEquals(
  1248. array(array('b', 3), array('c', 5)),
  1249. $this->redis->zrange('zsetc', 0, -1, 'withscores')
  1250. );
  1251. // using an array to pass the list of source keys + options array
  1252. $options = array('weights' => array(2, 3));
  1253. $this->assertEquals(2, $this->redis->zinterstore('zsetc', $sourceKeys, $options));
  1254. $this->assertEquals(
  1255. array(array('b', 7), array('c', 12)),
  1256. $this->redis->zrange('zsetc', 0, -1, 'withscores')
  1257. );
  1258. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, p_anon("\$test", "
  1259. \$test->redis->set('zsetFake', 'fake');
  1260. \$test->redis->zinterstore('zsetc', 2, 'zseta', 'zsetFake');
  1261. "));
  1262. }
  1263. function testZsetCount() {
  1264. $zset = RC::zsetAddAndReturn($this->redis, 'zset', RC::getZSetArray());
  1265. $this->assertEquals(0, $this->redis->zcount('zset', 50, 100));
  1266. $this->assertEquals(6, $this->redis->zcount('zset', -100, 100));
  1267. $this->assertEquals(3, $this->redis->zcount('zset', 10, 20));
  1268. $this->assertEquals(2, $this->redis->zcount('zset', "(10", 20));
  1269. $this->assertEquals(1, $this->redis->zcount('zset', 10, "(20"));
  1270. $this->assertEquals(0, $this->redis->zcount('zset', "(10", "(20"));
  1271. $this->assertEquals(3, $this->redis->zcount('zset', "(0", "(30"));
  1272. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, p_anon("\$test", "
  1273. \$test->redis->set('foo', 'bar');
  1274. \$test->redis->zcount('foo', 0, 0);
  1275. "));
  1276. }
  1277. function testZsetCardinality() {
  1278. $zset = RC::zsetAddAndReturn($this->redis, 'zset', RC::getZSetArray());
  1279. $this->assertEquals(count($zset), $this->redis->zcard('zset'));
  1280. $this->redis->zrem('zset', 'a');
  1281. $this->assertEquals(count($zset) - 1, $this->redis->zcard('zset'));
  1282. // empty zset
  1283. $this->redis->zadd('zsetB', 0, 'a');
  1284. $this->redis->zrem('zsetB', 'a');
  1285. $this->assertEquals(0, $this->redis->zcard('setB'));
  1286. // non-existing zset
  1287. $this->assertEquals(0, $this->redis->zcard('zsetDoesNotExist'));
  1288. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, p_anon("\$test", "
  1289. \$test->redis->set('foo', 'bar');
  1290. \$test->redis->zcard('foo');
  1291. "));
  1292. }
  1293. function testZsetScore() {
  1294. $zset = RC::zsetAddAndReturn($this->redis, 'zset', RC::getZSetArray());
  1295. $this->assertEquals(-10, $this->redis->zscore('zset', 'a'));
  1296. $this->assertEquals(10, $this->redis->zscore('zset', 'c'));
  1297. $this->assertEquals(20, $this->redis->zscore('zset', 'e'));
  1298. $this->assertNull($this->redis->zscore('zset', 'x'));
  1299. $this->assertNull($this->redis->zscore('zsetDoesNotExist', 'a'));
  1300. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, p_anon("\$test", "
  1301. \$test->redis->set('foo', 'bar');
  1302. \$test->redis->zscore('foo', 'bar');
  1303. "));
  1304. }
  1305. function testZsetRemoveRangeByScore() {
  1306. $zset = RC::zsetAddAndReturn($this->redis, 'zset', RC::getZSetArray());
  1307. $this->assertEquals(2, $this->redis->zremrangebyscore('zset', -10, 0));
  1308. $this->assertEquals(
  1309. array('c', 'd', 'e', 'f'),
  1310. $this->redis->zrange('zset', 0, -1)
  1311. );
  1312. $this->assertEquals(1, $this->redis->zremrangebyscore('zset', 10, 10));
  1313. $this->assertEquals(
  1314. array('d', 'e', 'f'),
  1315. $this->redis->zrange('zset', 0, -1)
  1316. );
  1317. $this->assertEquals(0, $this->redis->zremrangebyscore('zset', 100, 100));
  1318. $this->assertEquals(3, $this->redis->zremrangebyscore('zset', 0, 100));
  1319. $this->assertEquals(0, $this->redis->zremrangebyscore('zset', 0, 100));
  1320. $this->assertEquals(0, $this->redis->zremrangebyscore('zsetDoesNotExist', 0, 100));
  1321. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, p_anon("\$test", "
  1322. \$test->redis->set('foo', 'bar');
  1323. \$test->redis->zremrangebyscore('foo', 0, 0);
  1324. "));
  1325. }
  1326. function testZsetRank() {
  1327. $zset = RC::zsetAddAndReturn($this->redis, 'zset', RC::getZSetArray());
  1328. $this->assertEquals(0, $this->redis->zrank('zset', 'a'));
  1329. $this->assertEquals(1, $this->redis->zrank('zset', 'b'));
  1330. $this->assertEquals(4, $this->redis->zrank('zset', 'e'));
  1331. $this->redis->zrem('zset', 'd');
  1332. $this->assertEquals(3, $this->redis->zrank('zset', 'e'));
  1333. $this->assertNull($this->redis->zrank('zset', 'x'));
  1334. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, p_anon("\$test", "
  1335. \$test->redis->set('foo', 'bar');
  1336. \$test->redis->zrank('foo', 'a');
  1337. "));
  1338. }
  1339. function testZsetReverseRank() {
  1340. $zset = RC::zsetAddAndReturn($this->redis, 'zset', RC::getZSetArray());
  1341. $this->assertEquals(5, $this->redis->zrevrank('zset', 'a'));
  1342. $this->assertEquals(4, $this->redis->zrevrank('zset', 'b'));
  1343. $this->assertEquals(1, $this->redis->zrevrank('zset', 'e'));
  1344. $this->redis->zrem('zset', 'e');
  1345. $this->assertEquals(1, $this->redis->zrevrank('zset', 'd'));
  1346. $this->assertNull($this->redis->zrevrank('zset', 'x'));
  1347. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, p_anon("\$test", "
  1348. \$test->redis->set('foo', 'bar');
  1349. \$test->redis->zrevrank('foo', 'a');
  1350. "));
  1351. }
  1352. function testZsetRemoveRangeByRank() {
  1353. RC::zsetAddAndReturn($this->redis, 'zseta', RC::getZSetArray());
  1354. $this->assertEquals(3, $this->redis->zremrangebyrank('zseta', 0, 2));
  1355. $this->assertEquals(
  1356. array('d', 'e', 'f'),
  1357. $this->redis->zrange('zseta', 0, -1)
  1358. );
  1359. $this->assertEquals(1, $this->redis->zremrangebyrank('zseta', 0, 0));
  1360. $this->assertEquals(
  1361. array('e', 'f'),
  1362. $this->redis->zrange('zseta', 0, -1)
  1363. );
  1364. RC::zsetAddAndReturn($this->redis, 'zsetb', RC::getZSetArray());
  1365. $this->assertEquals(3, $this->redis->zremrangebyrank('zsetb', -3, -1));
  1366. $this->assertEquals(
  1367. array('a', 'b', 'c'),
  1368. $this->redis->zrange('zsetb', 0, -1)
  1369. );
  1370. $this->assertEquals(1, $this->redis->zremrangebyrank('zsetb', -1, -1));
  1371. $this->assertEquals(
  1372. array('a', 'b'),
  1373. $this->redis->zrange('zsetb', 0, -1)
  1374. );
  1375. $this->assertEquals(2, $this->redis->zremrangebyrank('zsetb', -2, 1));
  1376. $this->assertEquals(
  1377. array(),
  1378. $this->redis->zrange('zsetb', 0, -1)
  1379. );
  1380. $this->assertFalse($this->redis->exists('zsetb'));
  1381. $this->assertEquals(0, $this->redis->zremrangebyrank('zsetc', 0, 0));
  1382. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, p_anon("\$test", "
  1383. \$test->redis->set('foo', 'bar');
  1384. \$test->redis->zremrangebyrank('foo', 0, 1);
  1385. "));
  1386. }
  1387. /* commands operating on hashes */
  1388. function testHashSet() {
  1389. $this->assertTrue($this->redis->hset('metavars', 'foo', 'bar'));
  1390. $this->assertTrue($this->redis->hset('metavars', 'hoge', 'piyo'));
  1391. $this->assertEquals('bar', $this->redis->hget('metavars', 'foo'));
  1392. $this->assertEquals('piyo', $this->redis->hget('metavars', 'hoge'));
  1393. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, p_anon("\$test", "
  1394. \$test->redis->set('test', 'foobar');
  1395. \$test->redis->hset('test', 'hoge', 'piyo');
  1396. "));
  1397. }
  1398. function testHashGet() {
  1399. $this->assertTrue($this->redis->hset('metavars', 'foo', 'bar'));
  1400. $this->assertEquals('bar', $this->redis->hget('metavars', 'foo'));
  1401. $this->assertEquals('bar', $this->redis->hget('metavars', 'foo'));
  1402. $this->assertNull($this->redis->hget('metavars', 'hoge'));
  1403. $this->assertNull($this->redis->hget('hashDoesNotExist', 'field'));
  1404. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, p_anon("\$test", "
  1405. \$test->redis->rpush('metavars', 'foo');
  1406. \$test->redis->hget('metavars', 'foo');
  1407. "));
  1408. }
  1409. function testHashExists() {
  1410. $this->assertTrue($this->redis->hset('metavars', 'foo', 'bar'));
  1411. $this->assertTrue($this->redis->hexists('metavars', 'foo'));
  1412. $this->assertFalse($this->redis->hexists('metavars', 'hoge'));
  1413. $this->assertFalse($this->redis->hexists('hashDoesNotExist', 'field'));
  1414. }
  1415. function testHashDelete() {
  1416. $this->assertTrue($this->redis->hset('metavars', 'foo', 'bar'));
  1417. $this->assertTrue($this->redis->hexists('metavars', 'foo'));
  1418. $this->assertTrue($this->redis->hdel('metavars', 'foo'));
  1419. $this->assertFalse($this->redis->hexists('metavars', 'foo'));
  1420. $this->assertFalse($this->redis->hdel('metavars', 'hoge'));
  1421. $this->assertFalse($this->redis->hdel('hashDoesNotExist', 'field'));
  1422. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, p_anon("\$test", "
  1423. \$test->redis->set('foo', 'bar');
  1424. \$test->redis->hdel('foo', 'field');
  1425. "));
  1426. }
  1427. function testHashLength() {
  1428. $this->assertTrue($this->redis->hset('metavars', 'foo', 'bar'));
  1429. $this->assertTrue($this->redis->hset('metavars', 'hoge', 'piyo'));
  1430. $this->assertTrue($this->redis->hset('metavars', 'foofoo', 'barbar'));
  1431. $this->assertTrue($this->redis->hset('metavars', 'hogehoge', 'piyopiyo'));
  1432. $this->assertEquals(4, $this->redis->hlen('metavars'));
  1433. $this->assertEquals(0, $this->redis->hlen('hashDoesNotExist'));
  1434. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, p_anon("\$test", "
  1435. \$test->redis->set('foo', 'bar');
  1436. \$test->redis->hlen('foo');
  1437. "));
  1438. }
  1439. function testHashSetPreserve() {
  1440. $this->assertTrue($this->redis->hsetnx('metavars', 'foo', 'bar'));
  1441. $this->assertFalse($this->redis->hsetnx('metavars', 'foo', 'barbar'));
  1442. $this->assertEquals('bar', $this->redis->hget('metavars', 'foo'));
  1443. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, p_anon("\$test", "
  1444. \$test->redis->set('test', 'foobar');
  1445. \$test->redis->hsetnx('test', 'hoge', 'piyo');
  1446. "));
  1447. }
  1448. function testHashSetAndGetMultiple() {
  1449. $hashKVs = array('foo' => 'bar', 'hoge' => 'piyo');
  1450. // key=>value pairs via array instance
  1451. $this->assertTrue($this->redis->hmset('metavars', $hashKVs));
  1452. $multiRet = $this->redis->hmget('metavars', array_keys($hashKVs));
  1453. $this->assertEquals($hashKVs, array_combine(array_keys($hashKVs), array_values($multiRet)));
  1454. // key=>value pairs via function arguments
  1455. $this->redis->del('metavars');
  1456. $this->assertTrue($this->redis->hmset('metavars', 'foo', 'bar', 'hoge', 'piyo'));
  1457. $this->assertEquals(array('bar', 'piyo'), $this->redis->hmget('metavars', 'foo', 'hoge'));
  1458. }
  1459. function testHashIncrementBy() {
  1460. // test subsequent increment commands
  1461. $this->assertEquals(10, $this->redis->hincrby('hash', 'counter', 10));
  1462. $this->assertEquals(20, $this->redis->hincrby('hash', 'counter', 10));
  1463. $this->assertEquals(0, $this->redis->hincrby('hash', 'counter', -20));
  1464. RC::testForServerException($this, RC::EXCEPTION_HASH_VALNOTINT, p_anon("\$test", "
  1465. \$test->redis->hset('hash', 'field', 'stringvalue');
  1466. \$test->redis->hincrby('hash', 'field', 10);
  1467. "));
  1468. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, p_anon("\$test", "
  1469. \$test->redis->set('foo', 'bar');
  1470. \$test->redis->hincrby('foo', 'bar', 1);
  1471. "));
  1472. }
  1473. function testHashKeys() {
  1474. $hashKVs = array('foo' => 'bar', 'hoge' => 'piyo');
  1475. $this->assertTrue($this->redis->hmset('metavars', $hashKVs));
  1476. $this->assertEquals(array_keys($hashKVs), $this->redis->hkeys('metavars'));
  1477. $this->assertEquals(array(), $this->redis->hkeys('hashDoesNotExist'));
  1478. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, p_anon("\$test", "
  1479. \$test->redis->set('foo', 'bar');
  1480. \$test->redis->hkeys('foo');
  1481. "));
  1482. }
  1483. function testHashValues() {
  1484. $hashKVs = array('foo' => 'bar', 'hoge' => 'piyo');
  1485. $this->assertTrue($this->redis->hmset('metavars', $hashKVs));
  1486. $this->assertEquals(array_values($hashKVs), $this->redis->hvals('metavars'));
  1487. $this->assertEquals(array(), $this->redis->hvals('hashDoesNotExist'));
  1488. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, p_anon("\$test", "
  1489. \$test->redis->set('foo', 'bar');
  1490. \$test->redis->hvals('foo');
  1491. "));
  1492. }
  1493. function testHashGetAll() {
  1494. $hashKVs = array('foo' => 'bar', 'hoge' => 'piyo');
  1495. $this->assertTrue($this->redis->hmset('metavars', $hashKVs));
  1496. $this->assertEquals($hashKVs, $this->redis->hgetall('metavars'));
  1497. $this->assertEquals(array(), $this->redis->hgetall('hashDoesNotExist'));
  1498. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, p_anon("\$test", "
  1499. \$test->redis->set('foo', 'bar');
  1500. \$test->redis->hgetall('foo');
  1501. "));
  1502. }
  1503. /* multiple databases handling commands */
  1504. function testSelectDatabase() {
  1505. $this->assertTrue($this->redis->select(0));
  1506. $this->assertTrue($this->redis->select(RC::DEFAULT_DATABASE));
  1507. RC::testForServerException($this, RC::EXCEPTION_INVALID_DB_IDX, p_anon("\$test", "
  1508. \$test->redis->select(32);
  1509. "));
  1510. RC::testForServerException($this, RC::EXCEPTION_INVALID_DB_IDX, p_anon("\$test", "
  1511. \$test->redis->select(-1);
  1512. "));
  1513. }
  1514. function testMove() {
  1515. // TODO: This test sucks big time. Period.
  1516. $otherDb = 5;
  1517. $this->redis->set('foo', 'bar');
  1518. $this->redis->select($otherDb);
  1519. $this->redis->flushdb();
  1520. $this->redis->select(RC::DEFAULT_DATABASE);
  1521. $this->assertTrue($this->redis->move('foo', $otherDb));
  1522. $this->assertFalse($this->redis->move('foo', $otherDb));
  1523. $this->assertFalse($this->redis->move('keyDoesNotExist', $otherDb));
  1524. $this->redis->set('hoge', 'piyo');
  1525. // TODO: shouldn't Redis send an EXCEPTION_INVALID_DB_IDX instead of EXCEPTION_OUT_OF_RANGE?
  1526. RC::testForServerException($this, RC::EXCEPTION_OUT_OF_RANGE, p_anon("\$test", "
  1527. \$test->redis->move('hoge', 32);
  1528. "));
  1529. }
  1530. function testFlushdb() {
  1531. $this->assertTrue($this->redis->flushdb());
  1532. }
  1533. /* sorting */
  1534. function testSort() {
  1535. $unorderedList = RC::pushTailAndReturn($this->redis, 'unordered', array(2, 100, 3, 1, 30, 10));
  1536. // without parameters
  1537. $this->assertEquals(array(1, 2, 3, 10, 30, 100), $this->redis->sort('unordered'));
  1538. // with parameter ASC/DESC
  1539. $this->assertEquals(
  1540. array(100, 30, 10, 3, 2, 1),
  1541. $this->redis->sort('unordered', array(
  1542. 'sort' => 'desc'
  1543. ))
  1544. );
  1545. // with parameter LIMIT
  1546. $this->assertEquals(
  1547. array(1, 2, 3),
  1548. $this->redis->sort('unordered', array(
  1549. 'limit' => array(0, 3)
  1550. ))
  1551. );
  1552. $this->assertEquals(
  1553. array(10, 30),
  1554. $this->redis->sort('unordered', array(
  1555. 'limit' => array(3, 2)
  1556. ))
  1557. );
  1558. // with parameter ALPHA
  1559. $this->assertEquals(
  1560. array(1, 10, 100, 2, 3, 30),
  1561. $this->redis->sort('unordered', array(
  1562. 'alpha' => true
  1563. ))
  1564. );
  1565. // with combined parameters
  1566. $this->assertEquals(
  1567. array(30, 10, 3, 2),
  1568. $this->redis->sort('unordered', array(
  1569. 'alpha' => false,
  1570. 'sort' => 'desc',
  1571. 'limit' => array(1, 4)
  1572. ))
  1573. );
  1574. // with parameter ALPHA
  1575. $this->assertEquals(
  1576. array(1, 10, 100, 2, 3, 30),
  1577. $this->redis->sort('unordered', array(
  1578. 'alpha' => true
  1579. ))
  1580. );
  1581. // with parameter STORE
  1582. $this->assertEquals(
  1583. count($unorderedList),
  1584. $this->redis->sort('unordered', array(
  1585. 'store' => 'ordered'
  1586. ))
  1587. );
  1588. $this->assertEquals(array(1, 2, 3, 10, 30, 100), $this->redis->lrange('ordered', 0, -1));
  1589. // with parameter GET
  1590. $this->redis->rpush('uids', 1003);
  1591. $this->redis->rpush('uids', 1001);
  1592. $this->redis->rpush('uids', 1002);
  1593. $this->redis->rpush('uids', 1000);
  1594. $sortget = array(
  1595. 'uid:1000' => 'foo', 'uid:1001' => 'bar',
  1596. 'uid:1002' => 'hoge', 'uid:1003' => 'piyo'
  1597. );
  1598. $this->redis->mset($sortget);
  1599. $this->assertEquals(
  1600. array_values($sortget),
  1601. $this->redis->sort('uids', array('get' => 'uid:*'))
  1602. );
  1603. // wrong type
  1604. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, p_anon("\$test", "
  1605. \$test->redis->set('foo', 'bar');
  1606. \$test->redis->sort('foo');
  1607. "));
  1608. }
  1609. /* remote server control commands */
  1610. function testInfo() {
  1611. $serverInfo = $this->redis->info();
  1612. $this->assertInternalType('array', $serverInfo);
  1613. $this->assertNotNull($serverInfo['redis_version']);
  1614. $this->assertGreaterThan(0, $serverInfo['uptime_in_seconds']);
  1615. $this->assertGreaterThan(0, $serverInfo['total_connections_received']);
  1616. }
  1617. function testSlaveOf() {
  1618. $masterHost = 'www.google.com';
  1619. $masterPort = 80;
  1620. $this->assertTrue($this->redis->slaveof($masterHost, $masterPort));
  1621. $serverInfo = $this->redis->info();
  1622. $this->assertEquals('slave', $serverInfo['role']);
  1623. $this->assertEquals($masterHost, $serverInfo['master_host']);
  1624. $this->assertEquals($masterPort, $serverInfo['master_port']);
  1625. // slave of NO ONE, the implicit way
  1626. $this->assertTrue($this->redis->slaveof());
  1627. $serverInfo = $this->redis->info();
  1628. $this->assertEquals('master', $serverInfo['role']);
  1629. // slave of NO ONE, the explicit way
  1630. $this->assertTrue($this->redis->slaveof('NO ONE'));
  1631. }
  1632. /* persistence control commands */
  1633. function testSave() {
  1634. $this->assertTrue($this->redis->save());
  1635. }
  1636. function testBackgroundSave() {
  1637. $this->assertTrue($this->redis->bgsave());
  1638. }
  1639. function testBackgroundRewriteAppendOnlyFile() {
  1640. $this->assertTrue($this->redis->bgrewriteaof());
  1641. }
  1642. function testLastSave() {
  1643. $this->assertGreaterThan(0, $this->redis->lastsave());
  1644. }
  1645. }
  1646. ?>