RedisCommandsTest.php 78 KB

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