RedisCommandsTest.php 77 KB

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