RedisCommandsTest.php 76 KB

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