RedisCommandsTest.php 77 KB

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