RedisCommandsTest.php 74 KB

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