RedisCommandsTest.php 76 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042
  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 testStrlen() {
  207. $this->redis->set('var', 'foobar');
  208. $this->assertEquals(6, $this->redis->strlen('var'));
  209. $this->assertEquals(9, $this->redis->append('var', '___'));
  210. $this->assertEquals(9, $this->redis->strlen('var'));
  211. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
  212. $test->redis->rpush('metavars', 'foo');
  213. $test->redis->strlen('metavars');
  214. });
  215. }
  216. function testSetBit() {
  217. $this->assertEquals(0, $this->redis->setbit('binary', 31, 1));
  218. $this->assertEquals(0, $this->redis->setbit('binary', 0, 1));
  219. $this->assertEquals(4, $this->redis->strlen('binary'));
  220. $this->assertEquals("\x80\x00\00\x01", $this->redis->get('binary'));
  221. $this->assertEquals(1, $this->redis->setbit('binary', 0, 0));
  222. $this->assertEquals(0, $this->redis->setbit('binary', 0, 0));
  223. $this->assertEquals("\x00\x00\00\x01", $this->redis->get('binary'));
  224. RC::testForServerException($this, RC::EXCEPTION_BIT_OFFSET, function($test) {
  225. $test->redis->setbit('binary', -1, 1);
  226. });
  227. RC::testForServerException($this, RC::EXCEPTION_BIT_OFFSET, function($test) {
  228. $test->redis->setbit('binary', 'invalid', 1);
  229. });
  230. RC::testForServerException($this, RC::EXCEPTION_BIT_VALUE, function($test) {
  231. $test->redis->setbit('binary', 15, 255);
  232. });
  233. RC::testForServerException($this, RC::EXCEPTION_BIT_VALUE, function($test) {
  234. $test->redis->setbit('binary', 15, 'invalid');
  235. });
  236. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
  237. $test->redis->rpush('metavars', 'foo');
  238. $test->redis->setbit('metavars', 0, 1);
  239. });
  240. }
  241. function testGetBit() {
  242. $this->redis->set('binary', "\x80\x00\00\x01");
  243. $this->assertEquals(1, $this->redis->getbit('binary', 0));
  244. $this->assertEquals(0, $this->redis->getbit('binary', 15));
  245. $this->assertEquals(1, $this->redis->getbit('binary', 31));
  246. $this->assertEquals(0, $this->redis->getbit('binary', 63));
  247. RC::testForServerException($this, RC::EXCEPTION_BIT_OFFSET, function($test) {
  248. $test->redis->getbit('binary', -1);
  249. });
  250. RC::testForServerException($this, RC::EXCEPTION_BIT_OFFSET, function($test) {
  251. $test->redis->getbit('binary', 'invalid');
  252. });
  253. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
  254. $test->redis->rpush('metavars', 'foo');
  255. $test->redis->getbit('metavars', 0);
  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. // TODO: do not check the error message RC::EXCEPTION_VALUE_NOT_INT for now
  338. RC::testForServerException($this, null, function($test) {
  339. $test->redis->setex('hoge', 2.5, 'piyo');
  340. });
  341. RC::testForServerException($this, RC::EXCEPTION_SETEX_TTL, function($test) {
  342. $test->redis->setex('hoge', 0, 'piyo');
  343. });
  344. RC::testForServerException($this, RC::EXCEPTION_SETEX_TTL, function($test) {
  345. $test->redis->setex('hoge', -10, 'piyo');
  346. });
  347. }
  348. function testDatabaseSize() {
  349. // TODO: is this really OK?
  350. $this->assertEquals(0, $this->redis->dbsize());
  351. $this->redis->mset(RC::getKeyValueArray());
  352. $this->assertGreaterThan(0, $this->redis->dbsize());
  353. }
  354. /* commands operating on lists */
  355. function testPushTail() {
  356. // NOTE: List push operations return the list length since Redis commit 520b5a3
  357. $this->assertEquals(1, $this->redis->rpush('metavars', 'foo'));
  358. $this->assertTrue($this->redis->exists('metavars'));
  359. $this->assertEquals(2, $this->redis->rpush('metavars', 'hoge'));
  360. // should throw an exception when trying to do a RPUSH on non-list types
  361. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
  362. $test->redis->set('foo', 'bar');
  363. $test->redis->rpush('foo', 'bar');
  364. });
  365. }
  366. function testPushTailX() {
  367. $this->assertEquals(0, $this->redis->rpushx('numbers', 1));
  368. $this->assertEquals(1, $this->redis->rpush('numbers', 2));
  369. $this->assertEquals(2, $this->redis->rpushx('numbers', 3));
  370. $this->assertEquals(2, $this->redis->llen('numbers'));
  371. $this->assertEquals(array(2, 3), $this->redis->lrange('numbers', 0, -1));
  372. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
  373. $test->redis->set('foo', 'bar');
  374. $test->redis->rpushx('foo', 'bar');
  375. });
  376. }
  377. function testPushHead() {
  378. // NOTE: List push operations return the list length since Redis commit 520b5a3
  379. $this->assertEquals(1, $this->redis->lpush('metavars', 'foo'));
  380. $this->assertTrue($this->redis->exists('metavars'));
  381. $this->assertEquals(2, $this->redis->lpush('metavars', 'hoge'));
  382. // should throw an exception when trying to do a LPUSH on non-list types
  383. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
  384. $test->redis->set('foo', 'bar');
  385. $test->redis->lpush('foo', 'bar');
  386. });
  387. }
  388. function testPushHeadX() {
  389. $this->assertEquals(0, $this->redis->lpushx('numbers', 1));
  390. $this->assertEquals(1, $this->redis->lpush('numbers', 2));
  391. $this->assertEquals(2, $this->redis->lpushx('numbers', 3));
  392. $this->assertEquals(2, $this->redis->llen('numbers'));
  393. $this->assertEquals(array(3, 2), $this->redis->lrange('numbers', 0, -1));
  394. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
  395. $test->redis->set('foo', 'bar');
  396. $test->redis->lpushx('foo', 'bar');
  397. });
  398. }
  399. function testListLength() {
  400. $this->assertEquals(1, $this->redis->rpush('metavars', 'foo'));
  401. $this->assertEquals(2, $this->redis->rpush('metavars', 'hoge'));
  402. $this->assertEquals(2, $this->redis->llen('metavars'));
  403. $this->assertEquals(0, $this->redis->llen('doesnotexist'));
  404. // should throw an exception when trying to do a LLEN on non-list types
  405. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
  406. $test->redis->set('foo', 'bar');
  407. $test->redis->llen('foo');
  408. });
  409. }
  410. function testListRange() {
  411. $numbers = RC::pushTailAndReturn($this->redis, 'numbers', RC::getArrayOfNumbers());
  412. $this->assertEquals(
  413. array_slice($numbers, 0, 4),
  414. $this->redis->lrange('numbers', 0, 3)
  415. );
  416. $this->assertEquals(
  417. array_slice($numbers, 4, 5),
  418. $this->redis->lrange('numbers', 4, 8)
  419. );
  420. $this->assertEquals(
  421. array_slice($numbers, 0, 1),
  422. $this->redis->lrange('numbers', 0, 0)
  423. );
  424. $this->assertEquals(
  425. array(),
  426. $this->redis->lrange('numbers', 1, 0)
  427. );
  428. $this->assertEquals(
  429. $numbers,
  430. $this->redis->lrange('numbers', 0, -1)
  431. );
  432. $this->assertEquals(
  433. array(5),
  434. $this->redis->lrange('numbers', 5, -5)
  435. );
  436. $this->assertEquals(
  437. array(),
  438. $this->redis->lrange('numbers', 7, -5)
  439. );
  440. $this->assertEquals(
  441. array_slice($numbers, -5, -1),
  442. $this->redis->lrange('numbers', -5, -2)
  443. );
  444. $this->assertEquals(
  445. $numbers,
  446. $this->redis->lrange('numbers', -100, 100)
  447. );
  448. $this->assertEquals(
  449. array(),
  450. $this->redis->lrange('keyDoesNotExist', 0, 1)
  451. );
  452. // should throw an exception when trying to do a LRANGE on non-list types
  453. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
  454. $test->redis->set('foo', 'bar');
  455. $test->redis->lrange('foo', 0, -1);
  456. });
  457. }
  458. function testListTrim() {
  459. $numbers = RC::pushTailAndReturn($this->redis, 'numbers', RC::getArrayOfNumbers());
  460. $this->assertTrue($this->redis->ltrim('numbers', 0, 2));
  461. $this->assertEquals(
  462. array_slice($numbers, 0, 3),
  463. $this->redis->lrange('numbers', 0, -1)
  464. );
  465. $numbers = RC::pushTailAndReturn($this->redis, 'numbers', RC::getArrayOfNumbers(), RC::WIPE_OUT);
  466. $this->assertTrue($this->redis->ltrim('numbers', 5, 9));
  467. $this->assertEquals(
  468. array_slice($numbers, 5, 5),
  469. $this->redis->lrange('numbers', 0, -1)
  470. );
  471. $numbers = RC::pushTailAndReturn($this->redis, 'numbers', RC::getArrayOfNumbers(), RC::WIPE_OUT);
  472. $this->assertTrue($this->redis->ltrim('numbers', 0, -6));
  473. $this->assertEquals(
  474. array_slice($numbers, 0, -5),
  475. $this->redis->lrange('numbers', 0, -1)
  476. );
  477. $numbers = RC::pushTailAndReturn($this->redis, 'numbers', RC::getArrayOfNumbers(), RC::WIPE_OUT);
  478. $this->assertTrue($this->redis->ltrim('numbers', -5, -3));
  479. $this->assertEquals(
  480. array_slice($numbers, 5, 3),
  481. $this->redis->lrange('numbers', 0, -1)
  482. );
  483. $numbers = RC::pushTailAndReturn($this->redis, 'numbers', RC::getArrayOfNumbers(), RC::WIPE_OUT);
  484. $this->assertTrue($this->redis->ltrim('numbers', -100, 100));
  485. $this->assertEquals(
  486. $numbers,
  487. $this->redis->lrange('numbers', 0, -1)
  488. );
  489. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
  490. $test->redis->set('foo', 'bar');
  491. $test->redis->ltrim('foo', 0, 1);
  492. });
  493. }
  494. function testListIndex() {
  495. $numbers = RC::pushTailAndReturn($this->redis, 'numbers', RC::getArrayOfNumbers());
  496. $this->assertEquals(0, $this->redis->lindex('numbers', 0));
  497. $this->assertEquals(5, $this->redis->lindex('numbers', 5));
  498. $this->assertEquals(9, $this->redis->lindex('numbers', 9));
  499. $this->assertNull($this->redis->lindex('numbers', 100));
  500. $this->assertEquals(0, $this->redis->lindex('numbers', -0));
  501. $this->assertEquals(9, $this->redis->lindex('numbers', -1));
  502. $this->assertEquals(7, $this->redis->lindex('numbers', -3));
  503. $this->assertNull($this->redis->lindex('numbers', -100));
  504. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
  505. $test->redis->set('foo', 'bar');
  506. $test->redis->lindex('foo', 0);
  507. });
  508. }
  509. function testListSet() {
  510. $numbers = RC::pushTailAndReturn($this->redis, 'numbers', RC::getArrayOfNumbers());
  511. $this->assertTrue($this->redis->lset('numbers', 5, -5));
  512. $this->assertEquals(-5, $this->redis->lindex('numbers', 5));
  513. RC::testForServerException($this, RC::EXCEPTION_OUT_OF_RANGE, function($test) {
  514. $test->redis->lset('numbers', 99, 99);
  515. });
  516. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
  517. $test->redis->set('foo', 'bar');
  518. $test->redis->lset('foo', 0, 0);
  519. });
  520. }
  521. function testListRemove() {
  522. $mixed = array(0, '_', 2, '_', 4, '_', 6, '_');
  523. RC::pushTailAndReturn($this->redis, 'mixed', $mixed);
  524. $this->assertEquals(2, $this->redis->lrem('mixed', 2, '_'));
  525. $this->assertEquals(array(0, 2, 4, '_', 6, '_'), $this->redis->lrange('mixed', 0, -1));
  526. RC::pushTailAndReturn($this->redis, 'mixed', $mixed, RC::WIPE_OUT);
  527. $this->assertEquals(4, $this->redis->lrem('mixed', 0, '_'));
  528. $this->assertEquals(array(0, 2, 4, 6), $this->redis->lrange('mixed', 0, -1));
  529. RC::pushTailAndReturn($this->redis, 'mixed', $mixed, RC::WIPE_OUT);
  530. $this->assertEquals(2, $this->redis->lrem('mixed', -2, '_'));
  531. $this->assertEquals(array(0, '_', 2, '_', 4, 6), $this->redis->lrange('mixed', 0, -1));
  532. RC::pushTailAndReturn($this->redis, 'mixed', $mixed, RC::WIPE_OUT);
  533. $this->assertEquals(0, $this->redis->lrem('mixed', 2, '|'));
  534. $this->assertEquals($mixed, $this->redis->lrange('mixed', 0, -1));
  535. $this->assertEquals(0, $this->redis->lrem('listDoesNotExist', 2, '_'));
  536. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
  537. $test->redis->set('foo', 'bar');
  538. $test->redis->lrem('foo', 0, 0);
  539. });
  540. }
  541. function testListPopFirst() {
  542. $numbers = RC::pushTailAndReturn($this->redis, 'numbers', array(0, 1, 2, 3, 4));
  543. $this->assertEquals(0, $this->redis->lpop('numbers'));
  544. $this->assertEquals(1, $this->redis->lpop('numbers'));
  545. $this->assertEquals(2, $this->redis->lpop('numbers'));
  546. $this->assertEquals(array(3, 4), $this->redis->lrange('numbers', 0, -1));
  547. $this->redis->lpop('numbers');
  548. $this->redis->lpop('numbers');
  549. $this->assertNull($this->redis->lpop('numbers'));
  550. $this->assertNull($this->redis->lpop('numbers'));
  551. $this->assertNull($this->redis->lpop('listDoesNotExist'));
  552. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
  553. $test->redis->set('foo', 'bar');
  554. $test->redis->lpop('foo');
  555. });
  556. }
  557. function testListPopLast() {
  558. $numbers = RC::pushTailAndReturn($this->redis, 'numbers', array(0, 1, 2, 3, 4));
  559. $this->assertEquals(4, $this->redis->rpop('numbers'));
  560. $this->assertEquals(3, $this->redis->rpop('numbers'));
  561. $this->assertEquals(2, $this->redis->rpop('numbers'));
  562. $this->assertEquals(array(0, 1), $this->redis->lrange('numbers', 0, -1));
  563. $this->redis->rpop('numbers');
  564. $this->redis->rpop('numbers');
  565. $this->assertNull($this->redis->rpop('numbers'));
  566. $this->assertNull($this->redis->rpop('numbers'));
  567. $this->assertNull($this->redis->rpop('listDoesNotExist'));
  568. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
  569. $test->redis->set('foo', 'bar');
  570. $test->redis->rpop('foo');
  571. });
  572. }
  573. function testListPopLastPushHead() {
  574. $numbers = RC::pushTailAndReturn($this->redis, 'numbers', array(0, 1, 2));
  575. $this->assertEquals(0, $this->redis->llen('temporary'));
  576. $this->assertEquals(2, $this->redis->rpoplpush('numbers', 'temporary'));
  577. $this->assertEquals(1, $this->redis->rpoplpush('numbers', 'temporary'));
  578. $this->assertEquals(0, $this->redis->rpoplpush('numbers', 'temporary'));
  579. $this->assertEquals(0, $this->redis->llen('numbers'));
  580. $this->assertEquals(3, $this->redis->llen('temporary'));
  581. $this->assertEquals(array(), $this->redis->lrange('numbers', 0, -1));
  582. $this->assertEquals($numbers, $this->redis->lrange('temporary', 0, -1));
  583. $numbers = RC::pushTailAndReturn($this->redis, 'numbers', array(0, 1, 2));
  584. $this->redis->rpoplpush('numbers', 'numbers');
  585. $this->redis->rpoplpush('numbers', 'numbers');
  586. $this->redis->rpoplpush('numbers', 'numbers');
  587. $this->assertEquals($numbers, $this->redis->lrange('numbers', 0, -1));
  588. $this->assertNull($this->redis->rpoplpush('listDoesNotExist1', 'listDoesNotExist2'));
  589. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
  590. $test->redis->set('foo', 'bar');
  591. $test->redis->rpoplpush('foo', 'hoge');
  592. });
  593. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
  594. $test->redis->set('foo', 'bar');
  595. $test->redis->rpoplpush('temporary', 'foo');
  596. });
  597. }
  598. function testListBlockingPopFirst() {
  599. // TODO: this test does not cover all the aspects of BLPOP/BRPOP as it
  600. // does not run with a concurrent client pushing items on lists.
  601. RC::helperForBlockingPops('blpop');
  602. // BLPOP on one key
  603. $start = time();
  604. $item = $this->redis->blpop('blpop3', 5);
  605. $this->assertEquals((float)(time() - $start), 0, '', 1);
  606. $this->assertEquals($item, array('blpop3', 'c'));
  607. // BLPOP on more than one key
  608. $poppedItems = array();
  609. while ($item = $this->redis->blpop('blpop1', 'blpop2', 1)) {
  610. $poppedItems[] = $item;
  611. }
  612. $this->assertEquals(
  613. array(array('blpop1', 'a'), array('blpop1', 'd'), array('blpop2', 'b')),
  614. $poppedItems
  615. );
  616. // check if BLPOP timeouts as expected on empty lists
  617. $start = time();
  618. $this->redis->blpop('blpop4', 2);
  619. $this->assertEquals((float)(time() - $start), 2, '', 1);
  620. }
  621. function testListBlockingPopLast() {
  622. // TODO: this test does not cover all the aspects of BLPOP/BRPOP as it
  623. // does not run with a concurrent client pushing items on lists.
  624. RC::helperForBlockingPops('brpop');
  625. // BRPOP on one key
  626. $start = time();
  627. $item = $this->redis->brpop('brpop3', 5);
  628. $this->assertEquals((float)(time() - $start), 0, '', 1);
  629. $this->assertEquals($item, array('brpop3', 'c'));
  630. // BRPOP on more than one key
  631. $poppedItems = array();
  632. while ($item = $this->redis->brpop('brpop1', 'brpop2', 1)) {
  633. $poppedItems[] = $item;
  634. }
  635. $this->assertEquals(
  636. array(array('brpop1', 'd'), array('brpop1', 'a'), array('brpop2', 'b')),
  637. $poppedItems
  638. );
  639. // check if BRPOP timeouts as expected on empty lists
  640. $start = time();
  641. $this->redis->brpop('brpop4', 2);
  642. $this->assertEquals((float)(time() - $start), 2, '', 1);
  643. }
  644. function testListBlockingPopLastPushHead() {
  645. // TODO: this test does not cover all the aspects of BLPOP/BRPOP as it
  646. // does not run with a concurrent client pushing items on lists.
  647. $numbers = RC::pushTailAndReturn($this->redis, 'numbers', array(1, 2, 3));
  648. $src_count = count($numbers);
  649. $dst_count = 0;
  650. while ($item = $this->redis->brpoplpush('numbers', 'temporary', 1)) {
  651. $this->assertEquals(--$src_count, $this->redis->llen('numbers'));
  652. $this->assertEquals(++$dst_count, $this->redis->llen('temporary'));
  653. $this->assertEquals(array_pop($numbers), $this->redis->lindex('temporary', 0));
  654. }
  655. $start = time();
  656. $this->assertNull($this->redis->brpoplpush('numbers', 'temporary', 2));
  657. $this->assertEquals(2, (float)(time() - $start), '', 1);
  658. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
  659. $test->redis->del('numbers');
  660. $test->redis->del('temporary');
  661. $test->redis->set('numbers', 'foobar');
  662. $test->redis->brpoplpush('numbers', 'temporary', 1);
  663. });
  664. }
  665. function testListInsert() {
  666. $numbers = RC::pushTailAndReturn($this->redis, 'numbers', RC::getArrayOfNumbers());
  667. $this->assertEquals(11, $this->redis->linsert('numbers', 'before', 0, -2));
  668. $this->assertEquals(12, $this->redis->linsert('numbers', 'after', -2, -1));
  669. $this->assertEquals(array(-2, -1, 0, 1), $this->redis->lrange('numbers', 0, 3));
  670. $this->assertEquals(-1, $this->redis->linsert('numbers', 'after', 100, 200));
  671. $this->assertEquals(-1, $this->redis->linsert('numbers', 'before', 100, 50));
  672. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
  673. $test->redis->set('foo', 'bar');
  674. $test->redis->linsert('foo', 'before', 0, 0);
  675. });
  676. }
  677. /* commands operating on sets */
  678. function testSetAdd() {
  679. $this->assertTrue($this->redis->sadd('set', 0));
  680. $this->assertTrue($this->redis->sadd('set', 1));
  681. $this->assertFalse($this->redis->sadd('set', 0));
  682. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
  683. $test->redis->set('foo', 'bar');
  684. $test->redis->sadd('foo', 0);
  685. });
  686. }
  687. function testSetRemove() {
  688. $set = RC::setAddAndReturn($this->redis, 'set', array(0, 1, 2, 3, 4));
  689. $this->assertTrue($this->redis->srem('set', 0));
  690. $this->assertTrue($this->redis->srem('set', 4));
  691. $this->assertFalse($this->redis->srem('set', 10));
  692. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
  693. $test->redis->set('foo', 'bar');
  694. $test->redis->srem('foo', 0);
  695. });
  696. }
  697. function testSetPop() {
  698. $set = RC::setAddAndReturn($this->redis, 'set', array(0, 1, 2, 3, 4));
  699. $this->assertTrue(in_array($this->redis->spop('set'), $set));
  700. $this->assertNull($this->redis->spop('setDoesNotExist'));
  701. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
  702. $test->redis->set('foo', 'bar');
  703. $test->redis->spop('foo');
  704. });
  705. }
  706. function testSetMove() {
  707. $setA = RC::setAddAndReturn($this->redis, 'setA', array(0, 1, 2, 3, 4, 5));
  708. $setB = RC::setAddAndReturn($this->redis, 'setB', array(5, 6, 7, 8, 9, 10));
  709. $this->assertTrue($this->redis->smove('setA', 'setB', 0));
  710. $this->assertFalse($this->redis->srem('setA', 0));
  711. $this->assertTrue($this->redis->srem('setB', 0));
  712. $this->assertTrue($this->redis->smove('setA', 'setB', 5));
  713. $this->assertFalse($this->redis->smove('setA', 'setB', 100));
  714. // wrong type
  715. $this->redis->set('foo', 'bar');
  716. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
  717. $test->redis->smove('foo', 'setB', 5);
  718. });
  719. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
  720. $test->redis->smove('setA', 'foo', 5);
  721. });
  722. }
  723. function testSetCardinality() {
  724. RC::setAddAndReturn($this->redis, 'setA', array(0, 1, 2, 3, 4, 5));
  725. $this->assertEquals(6, $this->redis->scard('setA'));
  726. // empty set
  727. $this->redis->sadd('setB', 0);
  728. $this->redis->spop('setB');
  729. $this->assertEquals(0, $this->redis->scard('setB'));
  730. // non-existing set
  731. $this->assertEquals(0, $this->redis->scard('setDoesNotExist'));
  732. // wrong type
  733. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
  734. $test->redis->set('foo', 'bar');
  735. $test->redis->scard('foo');
  736. });
  737. }
  738. function testSetIsMember() {
  739. RC::setAddAndReturn($this->redis, 'set', array(0, 1, 2, 3, 4, 5));
  740. $this->assertTrue($this->redis->sismember('set', 3));
  741. $this->assertFalse($this->redis->sismember('set', 100));
  742. $this->assertFalse($this->redis->sismember('setDoesNotExist', 0));
  743. // wrong type
  744. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
  745. $test->redis->set('foo', 'bar');
  746. $test->redis->sismember('foo', 0);
  747. });
  748. }
  749. function testSetMembers() {
  750. $set = RC::setAddAndReturn($this->redis, 'set', array(0, 1, 2, 3, 4, 5, 6));
  751. $this->assertTrue(RC::sameValuesInArrays($set, $this->redis->smembers('set')));
  752. $this->assertEquals(array(), $this->redis->smembers('setDoesNotExist'));
  753. // wrong type
  754. $this->redis->set('foo', 'bar');
  755. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
  756. $test->redis->smembers('foo');
  757. });
  758. }
  759. function testSetIntersection() {
  760. $setA = RC::setAddAndReturn($this->redis, 'setA', array(0, 1, 2, 3, 4, 5, 6));
  761. $setB = RC::setAddAndReturn($this->redis, 'setB', array(1, 3, 4, 6, 9, 10));
  762. $this->assertTrue(RC::sameValuesInArrays(
  763. $setA,
  764. $this->redis->sinter('setA')
  765. ));
  766. $this->assertTrue(RC::sameValuesInArrays(
  767. array_intersect($setA, $setB),
  768. $this->redis->sinter('setA', 'setB')
  769. ));
  770. $this->assertEquals(array(), $this->redis->sinter('setA', 'setDoesNotExist'));
  771. // wrong type
  772. $this->redis->set('foo', 'bar');
  773. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
  774. $test->redis->sinter('foo');
  775. });
  776. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
  777. $test->redis->sinter('setA', 'foo');
  778. });
  779. }
  780. function testSetIntersectionStore() {
  781. $setA = RC::setAddAndReturn($this->redis, 'setA', array(0, 1, 2, 3, 4, 5, 6));
  782. $setB = RC::setAddAndReturn($this->redis, 'setB', array(1, 3, 4, 6, 9, 10));
  783. $this->assertEquals(count($setA), $this->redis->sinterstore('setC', 'setA'));
  784. $this->assertTrue(RC::sameValuesInArrays(
  785. $setA,
  786. $this->redis->smembers('setC')
  787. ));
  788. $this->redis->del('setC');
  789. $this->assertEquals(4, $this->redis->sinterstore('setC', 'setA', 'setB'));
  790. $this->assertTrue(RC::sameValuesInArrays(
  791. array(1, 3, 4, 6),
  792. $this->redis->smembers('setC')
  793. ));
  794. $this->redis->del('setC');
  795. $this->assertEquals(array(), $this->redis->sinter('setC', 'setDoesNotExist'));
  796. $this->assertFalse($this->redis->exists('setC'));
  797. // existing keys are replaced by SINTERSTORE
  798. $this->redis->set('foo', 'bar');
  799. $this->assertEquals(count($setA), $this->redis->sinterstore('foo', 'setA'));
  800. // wrong type
  801. $this->redis->set('foo', 'bar');
  802. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
  803. $test->redis->sinterstore('setA', 'foo');
  804. });
  805. }
  806. function testSetUnion() {
  807. $setA = RC::setAddAndReturn($this->redis, 'setA', array(0, 1, 2, 3, 4, 5, 6));
  808. $setB = RC::setAddAndReturn($this->redis, 'setB', array(1, 3, 4, 6, 9, 10));
  809. $this->assertTrue(RC::sameValuesInArrays(
  810. $setA,
  811. $this->redis->sunion('setA')
  812. ));
  813. $this->assertTrue(RC::sameValuesInArrays(
  814. array_union($setA, $setB),
  815. $this->redis->sunion('setA', 'setB')
  816. ));
  817. $this->assertTrue(RC::sameValuesInArrays(
  818. $setA,
  819. $this->redis->sunion('setA', 'setDoesNotExist')
  820. ));
  821. // wrong type
  822. $this->redis->set('foo', 'bar');
  823. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
  824. $test->redis->sunion('foo');
  825. });
  826. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
  827. $test->redis->sunion('setA', 'foo');
  828. });
  829. }
  830. function testSetUnionStore() {
  831. $setA = RC::setAddAndReturn($this->redis, 'setA', array(0, 1, 2, 3, 4, 5, 6));
  832. $setB = RC::setAddAndReturn($this->redis, 'setB', array(1, 3, 4, 6, 9, 10));
  833. $this->assertEquals(count($setA), $this->redis->sunionstore('setC', 'setA'));
  834. $this->assertTrue(RC::sameValuesInArrays(
  835. $setA,
  836. $this->redis->smembers('setC')
  837. ));
  838. $this->redis->del('setC');
  839. $this->assertEquals(9, $this->redis->sunionstore('setC', 'setA', 'setB'));
  840. $this->assertTrue(RC::sameValuesInArrays(
  841. array_union($setA, $setB),
  842. $this->redis->smembers('setC')
  843. ));
  844. // non-existing keys are considered empty sets
  845. $this->redis->del('setC');
  846. $this->assertEquals(0, $this->redis->sunionstore('setC', 'setDoesNotExist'));
  847. $this->assertFalse($this->redis->exists('setC'));
  848. $this->assertEquals(0, $this->redis->scard('setC'));
  849. // existing keys are replaced by SUNIONSTORE
  850. $this->redis->set('foo', 'bar');
  851. $this->assertEquals(count($setA), $this->redis->sunionstore('foo', 'setA'));
  852. // wrong type
  853. $this->redis->set('foo', 'bar');
  854. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
  855. $test->redis->sunionstore('setA', 'foo');
  856. });
  857. }
  858. function testSetDifference() {
  859. $setA = RC::setAddAndReturn($this->redis, 'setA', array(0, 1, 2, 3, 4, 5, 6));
  860. $setB = RC::setAddAndReturn($this->redis, 'setB', array(1, 3, 4, 6, 9, 10));
  861. $this->assertTrue(RC::sameValuesInArrays(
  862. $setA,
  863. $this->redis->sdiff('setA')
  864. ));
  865. $this->assertTrue(RC::sameValuesInArrays(
  866. array_diff($setA, $setB),
  867. $this->redis->sdiff('setA', 'setB')
  868. ));
  869. $this->assertTrue(RC::sameValuesInArrays(
  870. $setA,
  871. $this->redis->sdiff('setA', 'setDoesNotExist')
  872. ));
  873. // wrong type
  874. $this->redis->set('foo', 'bar');
  875. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
  876. $test->redis->sdiff('foo');
  877. });
  878. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
  879. $test->redis->sdiff('setA', 'foo');
  880. });
  881. }
  882. function testSetDifferenceStore() {
  883. $setA = RC::setAddAndReturn($this->redis, 'setA', array(0, 1, 2, 3, 4, 5, 6));
  884. $setB = RC::setAddAndReturn($this->redis, 'setB', array(1, 3, 4, 6, 9, 10));
  885. $this->assertEquals(count($setA), $this->redis->sdiffstore('setC', 'setA'));
  886. $this->assertTrue(RC::sameValuesInArrays(
  887. $setA,
  888. $this->redis->smembers('setC')
  889. ));
  890. $this->redis->del('setC');
  891. $this->assertEquals(3, $this->redis->sdiffstore('setC', 'setA', 'setB'));
  892. $this->assertTrue(RC::sameValuesInArrays(
  893. array_diff($setA, $setB),
  894. $this->redis->smembers('setC')
  895. ));
  896. // non-existing keys are considered empty sets
  897. $this->redis->del('setC');
  898. $this->assertEquals(0, $this->redis->sdiffstore('setC', 'setDoesNotExist'));
  899. $this->assertFalse($this->redis->exists('setC'));
  900. $this->assertEquals(0, $this->redis->scard('setC'));
  901. // existing keys are replaced by SDIFFSTORE
  902. $this->redis->set('foo', 'bar');
  903. $this->assertEquals(count($setA), $this->redis->sdiffstore('foo', 'setA'));
  904. // wrong type
  905. $this->redis->set('foo', 'bar');
  906. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
  907. $test->redis->sdiffstore('setA', 'foo');
  908. });
  909. }
  910. function testRandomMember() {
  911. $set = RC::setAddAndReturn($this->redis, 'set', array(0, 1, 2, 3, 4, 5, 6));
  912. $this->assertTrue(in_array($this->redis->srandmember('set'), $set));
  913. $this->assertNull($this->redis->srandmember('setDoesNotExist'));
  914. // wrong type
  915. $this->redis->set('foo', 'bar');
  916. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
  917. $test->redis->srandmember('foo');
  918. });
  919. }
  920. /* commands operating on sorted sets */
  921. function testZsetAdd() {
  922. $this->assertTrue($this->redis->zadd('zset', 0, 'a'));
  923. $this->assertTrue($this->redis->zadd('zset', 1, 'b'));
  924. $this->assertTrue($this->redis->zadd('zset', -1, 'c'));
  925. // TODO: floats?
  926. //$this->assertTrue($this->redis->zadd('zset', -1.0, 'c'));
  927. //$this->assertTrue($this->redis->zadd('zset', -1.0, 'c'));
  928. $this->assertFalse($this->redis->zadd('zset', 2, 'b'));
  929. $this->assertFalse($this->redis->zadd('zset', -2, 'b'));
  930. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
  931. $test->redis->set('foo', 'bar');
  932. $test->redis->zadd('foo', 0, 'a');
  933. });
  934. }
  935. function testZsetIncrementBy() {
  936. $this->assertEquals(1, $this->redis->zincrby('zsetDoesNotExist', 1, 'foo'));
  937. $this->assertEquals('zset', $this->redis->type('zsetDoesNotExist'));
  938. RC::zsetAddAndReturn($this->redis, 'zset', RC::getZSetArray());
  939. $this->assertEquals(-5, $this->redis->zincrby('zset', 5, 'a'));
  940. $this->assertEquals(1, $this->redis->zincrby('zset', 1, 'b'));
  941. $this->assertEquals(10, $this->redis->zincrby('zset', 0, 'c'));
  942. $this->assertEquals(0, $this->redis->zincrby('zset', -20, 'd'));
  943. $this->assertEquals(2, $this->redis->zincrby('zset', 2, 'd'));
  944. $this->assertEquals(-10, $this->redis->zincrby('zset', -30, 'e'));
  945. $this->assertEquals(1, $this->redis->zincrby('zset', 1, 'x'));
  946. // wrong type
  947. $this->redis->set('foo', 'bar');
  948. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
  949. $test->redis->zincrby('foo', 1, 'a');
  950. });
  951. }
  952. function testZsetRemove() {
  953. RC::zsetAddAndReturn($this->redis, 'zset', RC::getZSetArray());
  954. $this->assertTrue($this->redis->zrem('zset', 'a'));
  955. $this->assertFalse($this->redis->zrem('zset', 'x'));
  956. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
  957. $test->redis->set('foo', 'bar');
  958. $test->redis->zrem('foo', 'bar');
  959. });
  960. }
  961. function testZsetRange() {
  962. $zset = RC::zsetAddAndReturn($this->redis, 'zset', RC::getZSetArray());
  963. $this->assertEquals(
  964. array_slice(array_keys($zset), 0, 4),
  965. $this->redis->zrange('zset', 0, 3)
  966. );
  967. $this->assertEquals(
  968. array_slice(array_keys($zset), 0, 1),
  969. $this->redis->zrange('zset', 0, 0)
  970. );
  971. $this->assertEquals(
  972. array(),
  973. $this->redis->zrange('zset', 1, 0)
  974. );
  975. $this->assertEquals(
  976. array_values(array_keys($zset)),
  977. $this->redis->zrange('zset', 0, -1)
  978. );
  979. $this->assertEquals(
  980. array_slice(array_keys($zset), 3, 1),
  981. $this->redis->zrange('zset', 3, -3)
  982. );
  983. $this->assertEquals(
  984. array(),
  985. $this->redis->zrange('zset', 5, -3)
  986. );
  987. $this->assertEquals(
  988. array_slice(array_keys($zset), -5, -1),
  989. $this->redis->zrange('zset', -5, -2)
  990. );
  991. $this->assertEquals(
  992. array_values(array_keys($zset)),
  993. $this->redis->zrange('zset', -100, 100)
  994. );
  995. $this->assertEquals(
  996. array_values(array_keys($zset)),
  997. $this->redis->zrange('zset', -100, 100)
  998. );
  999. $this->assertEquals(
  1000. array(array('a', -10), array('b', 0), array('c', 10)),
  1001. $this->redis->zrange('zset', 0, 2, 'withscores')
  1002. );
  1003. $this->assertEquals(
  1004. array(array('a', -10), array('b', 0), array('c', 10)),
  1005. $this->redis->zrange('zset', 0, 2, array('withscores' => true))
  1006. );
  1007. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
  1008. $test->redis->set('foo', 'bar');
  1009. $test->redis->zrange('foo', 0, -1);
  1010. });
  1011. }
  1012. function testZsetReverseRange() {
  1013. $zset = RC::zsetAddAndReturn($this->redis, 'zset', RC::getZSetArray());
  1014. $this->assertEquals(
  1015. array_slice(array_reverse(array_keys($zset)), 0, 4),
  1016. $this->redis->zrevrange('zset', 0, 3)
  1017. );
  1018. $this->assertEquals(
  1019. array_slice(array_reverse(array_keys($zset)), 0, 1),
  1020. $this->redis->zrevrange('zset', 0, 0)
  1021. );
  1022. $this->assertEquals(
  1023. array(),
  1024. $this->redis->zrevrange('zset', 1, 0)
  1025. );
  1026. $this->assertEquals(
  1027. array_values(array_reverse(array_keys($zset))),
  1028. $this->redis->zrevrange('zset', 0, -1)
  1029. );
  1030. $this->assertEquals(
  1031. array_slice(array_reverse(array_keys($zset)), 3, 1),
  1032. $this->redis->zrevrange('zset', 3, -3)
  1033. );
  1034. $this->assertEquals(
  1035. array(),
  1036. $this->redis->zrevrange('zset', 5, -3)
  1037. );
  1038. $this->assertEquals(
  1039. array_slice(array_reverse(array_keys($zset)), -5, -1),
  1040. $this->redis->zrevrange('zset', -5, -2)
  1041. );
  1042. $this->assertEquals(
  1043. array_values(array_reverse(array_keys($zset))),
  1044. $this->redis->zrevrange('zset', -100, 100)
  1045. );
  1046. $this->assertEquals(
  1047. array(array('f', 30), array('e', 20), array('d', 20)),
  1048. $this->redis->zrevrange('zset', 0, 2, 'withscores')
  1049. );
  1050. $this->assertEquals(
  1051. array(array('f', 30), array('e', 20), array('d', 20)),
  1052. $this->redis->zrevrange('zset', 0, 2, array('withscores' => true))
  1053. );
  1054. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
  1055. $test->redis->set('foo', 'bar');
  1056. $test->redis->zrevrange('foo', 0, -1);
  1057. });
  1058. }
  1059. function testZsetRangeByScore() {
  1060. $zset = RC::zsetAddAndReturn($this->redis, 'zset', RC::getZSetArray());
  1061. $this->assertEquals(
  1062. array('a'),
  1063. $this->redis->zrangebyscore('zset', -10, -10)
  1064. );
  1065. $this->assertEquals(
  1066. array('c', 'd', 'e', 'f'),
  1067. $this->redis->zrangebyscore('zset', 10, 30)
  1068. );
  1069. $this->assertEquals(
  1070. array('d', 'e'),
  1071. $this->redis->zrangebyscore('zset', 20, 20)
  1072. );
  1073. $this->assertEquals(
  1074. array(),
  1075. $this->redis->zrangebyscore('zset', 30, 0)
  1076. );
  1077. $this->assertEquals(
  1078. array(array('c', 10), array('d', 20), array('e', 20)),
  1079. $this->redis->zrangebyscore('zset', 10, 20, 'withscores')
  1080. );
  1081. $this->assertEquals(
  1082. array(array('c', 10), array('d', 20), array('e', 20)),
  1083. $this->redis->zrangebyscore('zset', 10, 20, array('withscores' => true))
  1084. );
  1085. $this->assertEquals(
  1086. array('d', 'e'),
  1087. $this->redis->zrangebyscore('zset', 10, 20, array('limit' => array(1, 2)))
  1088. );
  1089. $this->assertEquals(
  1090. array('d', 'e'),
  1091. $this->redis->zrangebyscore('zset', 10, 20, array(
  1092. 'limit' => array('offset' => 1, 'count' => 2)
  1093. ))
  1094. );
  1095. $this->assertEquals(
  1096. array(array('d', 20), array('e', 20)),
  1097. $this->redis->zrangebyscore('zset', 10, 20, array(
  1098. 'limit' => array(1, 2),
  1099. 'withscores' => true,
  1100. ))
  1101. );
  1102. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
  1103. $test->redis->set('foo', 'bar');
  1104. $test->redis->zrangebyscore('foo', 0, 0);
  1105. });
  1106. }
  1107. function testZsetReverseRangeByScore() {
  1108. $zset = RC::zsetAddAndReturn($this->redis, 'zset', RC::getZSetArray());
  1109. $this->assertEquals(
  1110. array('a'),
  1111. $this->redis->zrevrangebyscore('zset', -10, -10)
  1112. );
  1113. $this->assertEquals(
  1114. array('b', 'a'),
  1115. $this->redis->zrevrangebyscore('zset', 0, -10)
  1116. );
  1117. $this->assertEquals(
  1118. array('e', 'd'),
  1119. $this->redis->zrevrangebyscore('zset', 20, 20)
  1120. );
  1121. $this->assertEquals(
  1122. array('f', 'e', 'd', 'c', 'b'),
  1123. $this->redis->zrevrangebyscore('zset', 30, 0)
  1124. );
  1125. $this->assertEquals(
  1126. array(array('e', 20), array('d', 20), array('c', 10)),
  1127. $this->redis->zrevrangebyscore('zset', 20, 10, 'withscores')
  1128. );
  1129. $this->assertEquals(
  1130. array(array('e', 20), array('d', 20), array('c', 10)),
  1131. $this->redis->zrevrangebyscore('zset', 20, 10, array('withscores' => true))
  1132. );
  1133. $this->assertEquals(
  1134. array('d', 'c'),
  1135. $this->redis->zrevrangebyscore('zset', 20, 10, array('limit' => array(1, 2)))
  1136. );
  1137. $this->assertEquals(
  1138. array('d', 'c'),
  1139. $this->redis->zrevrangebyscore('zset', 20, 10, array(
  1140. 'limit' => array('offset' => 1, 'count' => 2)
  1141. ))
  1142. );
  1143. $this->assertEquals(
  1144. array(array('d', 20), array('c', 10)),
  1145. $this->redis->zrevrangebyscore('zset', 20, 10, array(
  1146. 'limit' => array(1, 2),
  1147. 'withscores' => true,
  1148. ))
  1149. );
  1150. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
  1151. $test->redis->set('foo', 'bar');
  1152. $test->redis->zrevrangebyscore('foo', 0, 0);
  1153. });
  1154. }
  1155. function testZsetUnionStore() {
  1156. $zsetA = RC::zsetAddAndReturn($this->redis, 'zseta', array('a' => 1, 'b' => 2, 'c' => 3));
  1157. $zsetB = RC::zsetAddAndReturn($this->redis, 'zsetb', array('b' => 1, 'c' => 2, 'd' => 3));
  1158. // basic ZUNIONSTORE
  1159. $this->assertEquals(4, $this->redis->zunionstore('zsetc', 2, 'zseta', 'zsetb'));
  1160. $this->assertEquals(
  1161. array(array('a', 1), array('b', 3), array('d', 3), array('c', 5)),
  1162. $this->redis->zrange('zsetc', 0, -1, 'withscores')
  1163. );
  1164. $this->assertEquals(3, $this->redis->zunionstore('zsetc', 2, 'zseta', 'zsetbNull'));
  1165. $this->assertEquals(
  1166. array(array('a', 1), array('b', 2), array('c', 3)),
  1167. $this->redis->zrange('zsetc', 0, -1, 'withscores')
  1168. );
  1169. $this->assertEquals(3, $this->redis->zunionstore('zsetc', 2, 'zsetaNull', 'zsetb'));
  1170. $this->assertEquals(
  1171. array(array('b', 1), array('c', 2), array('d', 3)),
  1172. $this->redis->zrange('zsetc', 0, -1, 'withscores')
  1173. );
  1174. $this->assertEquals(0, $this->redis->zunionstore('zsetc', 2, 'zsetaNull', 'zsetbNull'));
  1175. // with WEIGHTS
  1176. $options = array('weights' => array(2, 3));
  1177. $this->assertEquals(4, $this->redis->zunionstore('zsetc', 2, 'zseta', 'zsetb', $options));
  1178. $this->assertEquals(
  1179. array(array('a', 2), array('b', 7), array('d', 9), array('c', 12)),
  1180. $this->redis->zrange('zsetc', 0, -1, 'withscores')
  1181. );
  1182. // with AGGREGATE (min)
  1183. $options = array('aggregate' => 'min');
  1184. $this->assertEquals(4, $this->redis->zunionstore('zsetc', 2, 'zseta', 'zsetb', $options));
  1185. $this->assertEquals(
  1186. array(array('a', 1), array('b', 1), array('c', 2), array('d', 3)),
  1187. $this->redis->zrange('zsetc', 0, -1, 'withscores')
  1188. );
  1189. // with AGGREGATE (max)
  1190. $options = array('aggregate' => 'max');
  1191. $this->assertEquals(4, $this->redis->zunionstore('zsetc', 2, 'zseta', 'zsetb', $options));
  1192. $this->assertEquals(
  1193. array(array('a', 1), array('b', 2), array('c', 3), array('d', 3)),
  1194. $this->redis->zrange('zsetc', 0, -1, 'withscores')
  1195. );
  1196. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
  1197. $test->redis->set('zsetFake', 'fake');
  1198. $test->redis->zunionstore('zsetc', 2, 'zseta', 'zsetFake');
  1199. });
  1200. }
  1201. function testZsetIntersectionStore() {
  1202. $zsetA = RC::zsetAddAndReturn($this->redis, 'zseta', array('a' => 1, 'b' => 2, 'c' => 3));
  1203. $zsetB = RC::zsetAddAndReturn($this->redis, 'zsetb', array('b' => 1, 'c' => 2, 'd' => 3));
  1204. // basic ZINTERSTORE
  1205. $this->assertEquals(2, $this->redis->zinterstore('zsetc', 2, 'zseta', 'zsetb'));
  1206. $this->assertEquals(
  1207. array(array('b', 3), array('c', 5)),
  1208. $this->redis->zrange('zsetc', 0, -1, 'withscores')
  1209. );
  1210. $this->assertEquals(0, $this->redis->zinterstore('zsetc', 2, 'zseta', 'zsetbNull'));
  1211. $this->assertEquals(0, $this->redis->zinterstore('zsetc', 2, 'zsetaNull', 'zsetb'));
  1212. $this->assertEquals(0, $this->redis->zinterstore('zsetc', 2, 'zsetaNull', 'zsetbNull'));
  1213. // with WEIGHTS
  1214. $options = array('weights' => array(2, 3));
  1215. $this->assertEquals(2, $this->redis->zinterstore('zsetc', 2, 'zseta', 'zsetb', $options));
  1216. $this->assertEquals(
  1217. array(array('b', 7), array('c', 12)),
  1218. $this->redis->zrange('zsetc', 0, -1, 'withscores')
  1219. );
  1220. // with AGGREGATE (min)
  1221. $options = array('aggregate' => 'min');
  1222. $this->assertEquals(2, $this->redis->zinterstore('zsetc', 2, 'zseta', 'zsetb', $options));
  1223. $this->assertEquals(
  1224. array(array('b', 1), array('c', 2)),
  1225. $this->redis->zrange('zsetc', 0, -1, 'withscores')
  1226. );
  1227. // with AGGREGATE (max)
  1228. $options = array('aggregate' => 'max');
  1229. $this->assertEquals(2, $this->redis->zinterstore('zsetc', 2, 'zseta', 'zsetb', $options));
  1230. $this->assertEquals(
  1231. array(array('b', 2), array('c', 3)),
  1232. $this->redis->zrange('zsetc', 0, -1, 'withscores')
  1233. );
  1234. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
  1235. $test->redis->set('zsetFake', 'fake');
  1236. $test->redis->zinterstore('zsetc', 2, 'zseta', 'zsetFake');
  1237. });
  1238. }
  1239. function testZsetCount() {
  1240. $zset = RC::zsetAddAndReturn($this->redis, 'zset', RC::getZSetArray());
  1241. $this->assertEquals(0, $this->redis->zcount('zset', 50, 100));
  1242. $this->assertEquals(6, $this->redis->zcount('zset', -100, 100));
  1243. $this->assertEquals(3, $this->redis->zcount('zset', 10, 20));
  1244. $this->assertEquals(2, $this->redis->zcount('zset', "(10", 20));
  1245. $this->assertEquals(1, $this->redis->zcount('zset', 10, "(20"));
  1246. $this->assertEquals(0, $this->redis->zcount('zset', "(10", "(20"));
  1247. $this->assertEquals(3, $this->redis->zcount('zset', "(0", "(30"));
  1248. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
  1249. $test->redis->set('foo', 'bar');
  1250. $test->redis->zcount('foo', 0, 0);
  1251. });
  1252. }
  1253. function testZsetCardinality() {
  1254. $zset = RC::zsetAddAndReturn($this->redis, 'zset', RC::getZSetArray());
  1255. $this->assertEquals(count($zset), $this->redis->zcard('zset'));
  1256. $this->redis->zrem('zset', 'a');
  1257. $this->assertEquals(count($zset) - 1, $this->redis->zcard('zset'));
  1258. // empty zset
  1259. $this->redis->zadd('zsetB', 0, 'a');
  1260. $this->redis->zrem('zsetB', 'a');
  1261. $this->assertEquals(0, $this->redis->zcard('setB'));
  1262. // non-existing zset
  1263. $this->assertEquals(0, $this->redis->zcard('zsetDoesNotExist'));
  1264. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
  1265. $test->redis->set('foo', 'bar');
  1266. $test->redis->zcard('foo');
  1267. });
  1268. }
  1269. function testZsetScore() {
  1270. $zset = RC::zsetAddAndReturn($this->redis, 'zset', RC::getZSetArray());
  1271. $this->assertEquals(-10, $this->redis->zscore('zset', 'a'));
  1272. $this->assertEquals(10, $this->redis->zscore('zset', 'c'));
  1273. $this->assertEquals(20, $this->redis->zscore('zset', 'e'));
  1274. $this->assertNull($this->redis->zscore('zset', 'x'));
  1275. $this->assertNull($this->redis->zscore('zsetDoesNotExist', 'a'));
  1276. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
  1277. $test->redis->set('foo', 'bar');
  1278. $test->redis->zscore('foo', 'bar');
  1279. });
  1280. }
  1281. function testZsetRemoveRangeByScore() {
  1282. $zset = RC::zsetAddAndReturn($this->redis, 'zset', RC::getZSetArray());
  1283. $this->assertEquals(2, $this->redis->zremrangebyscore('zset', -10, 0));
  1284. $this->assertEquals(
  1285. array('c', 'd', 'e', 'f'),
  1286. $this->redis->zrange('zset', 0, -1)
  1287. );
  1288. $this->assertEquals(1, $this->redis->zremrangebyscore('zset', 10, 10));
  1289. $this->assertEquals(
  1290. array('d', 'e', 'f'),
  1291. $this->redis->zrange('zset', 0, -1)
  1292. );
  1293. $this->assertEquals(0, $this->redis->zremrangebyscore('zset', 100, 100));
  1294. $this->assertEquals(3, $this->redis->zremrangebyscore('zset', 0, 100));
  1295. $this->assertEquals(0, $this->redis->zremrangebyscore('zset', 0, 100));
  1296. $this->assertEquals(0, $this->redis->zremrangebyscore('zsetDoesNotExist', 0, 100));
  1297. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
  1298. $test->redis->set('foo', 'bar');
  1299. $test->redis->zremrangebyscore('foo', 0, 0);
  1300. });
  1301. }
  1302. function testZsetRank() {
  1303. $zset = RC::zsetAddAndReturn($this->redis, 'zset', RC::getZSetArray());
  1304. $this->assertEquals(0, $this->redis->zrank('zset', 'a'));
  1305. $this->assertEquals(1, $this->redis->zrank('zset', 'b'));
  1306. $this->assertEquals(4, $this->redis->zrank('zset', 'e'));
  1307. $this->redis->zrem('zset', 'd');
  1308. $this->assertEquals(3, $this->redis->zrank('zset', 'e'));
  1309. $this->assertNull($this->redis->zrank('zset', 'x'));
  1310. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
  1311. $test->redis->set('foo', 'bar');
  1312. $test->redis->zrank('foo', 'a');
  1313. });
  1314. }
  1315. function testZsetReverseRank() {
  1316. $zset = RC::zsetAddAndReturn($this->redis, 'zset', RC::getZSetArray());
  1317. $this->assertEquals(5, $this->redis->zrevrank('zset', 'a'));
  1318. $this->assertEquals(4, $this->redis->zrevrank('zset', 'b'));
  1319. $this->assertEquals(1, $this->redis->zrevrank('zset', 'e'));
  1320. $this->redis->zrem('zset', 'e');
  1321. $this->assertEquals(1, $this->redis->zrevrank('zset', 'd'));
  1322. $this->assertNull($this->redis->zrevrank('zset', 'x'));
  1323. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
  1324. $test->redis->set('foo', 'bar');
  1325. $test->redis->zrevrank('foo', 'a');
  1326. });
  1327. }
  1328. function testZsetRemoveRangeByRank() {
  1329. RC::zsetAddAndReturn($this->redis, 'zseta', RC::getZSetArray());
  1330. $this->assertEquals(3, $this->redis->zremrangebyrank('zseta', 0, 2));
  1331. $this->assertEquals(
  1332. array('d', 'e', 'f'),
  1333. $this->redis->zrange('zseta', 0, -1)
  1334. );
  1335. $this->assertEquals(1, $this->redis->zremrangebyrank('zseta', 0, 0));
  1336. $this->assertEquals(
  1337. array('e', 'f'),
  1338. $this->redis->zrange('zseta', 0, -1)
  1339. );
  1340. RC::zsetAddAndReturn($this->redis, 'zsetb', RC::getZSetArray());
  1341. $this->assertEquals(3, $this->redis->zremrangebyrank('zsetb', -3, -1));
  1342. $this->assertEquals(
  1343. array('a', 'b', 'c'),
  1344. $this->redis->zrange('zsetb', 0, -1)
  1345. );
  1346. $this->assertEquals(1, $this->redis->zremrangebyrank('zsetb', -1, -1));
  1347. $this->assertEquals(
  1348. array('a', 'b'),
  1349. $this->redis->zrange('zsetb', 0, -1)
  1350. );
  1351. $this->assertEquals(2, $this->redis->zremrangebyrank('zsetb', -2, 1));
  1352. $this->assertEquals(
  1353. array(),
  1354. $this->redis->zrange('zsetb', 0, -1)
  1355. );
  1356. $this->assertFalse($this->redis->exists('zsetb'));
  1357. $this->assertEquals(0, $this->redis->zremrangebyrank('zsetc', 0, 0));
  1358. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
  1359. $test->redis->set('foo', 'bar');
  1360. $test->redis->zremrangebyrank('foo', 0, 1);
  1361. });
  1362. }
  1363. /* commands operating on hashes */
  1364. function testHashSet() {
  1365. $this->assertTrue($this->redis->hset('metavars', 'foo', 'bar'));
  1366. $this->assertTrue($this->redis->hset('metavars', 'hoge', 'piyo'));
  1367. $this->assertEquals('bar', $this->redis->hget('metavars', 'foo'));
  1368. $this->assertEquals('piyo', $this->redis->hget('metavars', 'hoge'));
  1369. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
  1370. $test->redis->set('test', 'foobar');
  1371. $test->redis->hset('test', 'hoge', 'piyo');
  1372. });
  1373. }
  1374. function testHashGet() {
  1375. $this->assertTrue($this->redis->hset('metavars', 'foo', 'bar'));
  1376. $this->assertEquals('bar', $this->redis->hget('metavars', 'foo'));
  1377. $this->assertEquals('bar', $this->redis->hget('metavars', 'foo'));
  1378. $this->assertNull($this->redis->hget('metavars', 'hoge'));
  1379. $this->assertNull($this->redis->hget('hashDoesNotExist', 'field'));
  1380. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
  1381. $test->redis->rpush('metavars', 'foo');
  1382. $test->redis->hget('metavars', 'foo');
  1383. });
  1384. }
  1385. function testHashExists() {
  1386. $this->assertTrue($this->redis->hset('metavars', 'foo', 'bar'));
  1387. $this->assertTrue($this->redis->hexists('metavars', 'foo'));
  1388. $this->assertFalse($this->redis->hexists('metavars', 'hoge'));
  1389. $this->assertFalse($this->redis->hexists('hashDoesNotExist', 'field'));
  1390. }
  1391. function testHashDelete() {
  1392. $this->assertTrue($this->redis->hset('metavars', 'foo', 'bar'));
  1393. $this->assertTrue($this->redis->hexists('metavars', 'foo'));
  1394. $this->assertTrue($this->redis->hdel('metavars', 'foo'));
  1395. $this->assertFalse($this->redis->hexists('metavars', 'foo'));
  1396. $this->assertFalse($this->redis->hdel('metavars', 'hoge'));
  1397. $this->assertFalse($this->redis->hdel('hashDoesNotExist', 'field'));
  1398. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
  1399. $test->redis->set('foo', 'bar');
  1400. $test->redis->hdel('foo', 'field');
  1401. });
  1402. }
  1403. function testHashLength() {
  1404. $this->assertTrue($this->redis->hset('metavars', 'foo', 'bar'));
  1405. $this->assertTrue($this->redis->hset('metavars', 'hoge', 'piyo'));
  1406. $this->assertTrue($this->redis->hset('metavars', 'foofoo', 'barbar'));
  1407. $this->assertTrue($this->redis->hset('metavars', 'hogehoge', 'piyopiyo'));
  1408. $this->assertEquals(4, $this->redis->hlen('metavars'));
  1409. $this->assertEquals(0, $this->redis->hlen('hashDoesNotExist'));
  1410. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
  1411. $test->redis->set('foo', 'bar');
  1412. $test->redis->hlen('foo');
  1413. });
  1414. }
  1415. function testHashSetPreserve() {
  1416. $this->assertTrue($this->redis->hsetnx('metavars', 'foo', 'bar'));
  1417. $this->assertFalse($this->redis->hsetnx('metavars', 'foo', 'barbar'));
  1418. $this->assertEquals('bar', $this->redis->hget('metavars', 'foo'));
  1419. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
  1420. $test->redis->set('test', 'foobar');
  1421. $test->redis->hsetnx('test', 'hoge', 'piyo');
  1422. });
  1423. }
  1424. function testHashSetAndGetMultiple() {
  1425. $hashKVs = array('foo' => 'bar', 'hoge' => 'piyo');
  1426. // key=>value pairs via array instance
  1427. $this->assertTrue($this->redis->hmset('metavars', $hashKVs));
  1428. $multiRet = $this->redis->hmget('metavars', array_keys($hashKVs));
  1429. $this->assertEquals($hashKVs, array_combine(array_keys($hashKVs), array_values($multiRet)));
  1430. // key=>value pairs via function arguments
  1431. $this->redis->del('metavars');
  1432. $this->assertTrue($this->redis->hmset('metavars', 'foo', 'bar', 'hoge', 'piyo'));
  1433. $this->assertEquals(array('bar', 'piyo'), $this->redis->hmget('metavars', 'foo', 'hoge'));
  1434. }
  1435. function testHashIncrementBy() {
  1436. // test subsequent increment commands
  1437. $this->assertEquals(10, $this->redis->hincrby('hash', 'counter', 10));
  1438. $this->assertEquals(20, $this->redis->hincrby('hash', 'counter', 10));
  1439. $this->assertEquals(0, $this->redis->hincrby('hash', 'counter', -20));
  1440. RC::testForServerException($this, RC::EXCEPTION_HASH_VALNOTINT, function($test) {
  1441. $test->redis->hset('hash', 'field', 'stringvalue');
  1442. $test->redis->hincrby('hash', 'field', 10);
  1443. });
  1444. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
  1445. $test->redis->set('foo', 'bar');
  1446. $test->redis->hincrby('foo', 'bar', 1);
  1447. });
  1448. }
  1449. function testHashKeys() {
  1450. $hashKVs = array('foo' => 'bar', 'hoge' => 'piyo');
  1451. $this->assertTrue($this->redis->hmset('metavars', $hashKVs));
  1452. $this->assertEquals(array_keys($hashKVs), $this->redis->hkeys('metavars'));
  1453. $this->assertEquals(array(), $this->redis->hkeys('hashDoesNotExist'));
  1454. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
  1455. $test->redis->set('foo', 'bar');
  1456. $test->redis->hkeys('foo');
  1457. });
  1458. }
  1459. function testHashValues() {
  1460. $hashKVs = array('foo' => 'bar', 'hoge' => 'piyo');
  1461. $this->assertTrue($this->redis->hmset('metavars', $hashKVs));
  1462. $this->assertEquals(array_values($hashKVs), $this->redis->hvals('metavars'));
  1463. $this->assertEquals(array(), $this->redis->hvals('hashDoesNotExist'));
  1464. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
  1465. $test->redis->set('foo', 'bar');
  1466. $test->redis->hvals('foo');
  1467. });
  1468. }
  1469. function testHashGetAll() {
  1470. $hashKVs = array('foo' => 'bar', 'hoge' => 'piyo');
  1471. $this->assertTrue($this->redis->hmset('metavars', $hashKVs));
  1472. $this->assertEquals($hashKVs, $this->redis->hgetall('metavars'));
  1473. $this->assertEquals(array(), $this->redis->hgetall('hashDoesNotExist'));
  1474. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
  1475. $test->redis->set('foo', 'bar');
  1476. $test->redis->hgetall('foo');
  1477. });
  1478. }
  1479. /* multiple databases handling commands */
  1480. function testSelectDatabase() {
  1481. $this->assertTrue($this->redis->select(0));
  1482. $this->assertTrue($this->redis->select(RC::DEFAULT_DATABASE));
  1483. RC::testForServerException($this, RC::EXCEPTION_INVALID_DB_IDX, function($test) {
  1484. $test->redis->select(32);
  1485. });
  1486. RC::testForServerException($this, RC::EXCEPTION_INVALID_DB_IDX, function($test) {
  1487. $test->redis->select(-1);
  1488. });
  1489. }
  1490. function testMove() {
  1491. // TODO: This test sucks big time. Period.
  1492. $otherDb = 5;
  1493. $this->redis->set('foo', 'bar');
  1494. $this->redis->select($otherDb);
  1495. $this->redis->flushdb();
  1496. $this->redis->select(RC::DEFAULT_DATABASE);
  1497. $this->assertTrue($this->redis->move('foo', $otherDb));
  1498. $this->assertFalse($this->redis->move('foo', $otherDb));
  1499. $this->assertFalse($this->redis->move('keyDoesNotExist', $otherDb));
  1500. $this->redis->set('hoge', 'piyo');
  1501. // TODO: shouldn't Redis send an EXCEPTION_INVALID_DB_IDX instead of EXCEPTION_OUT_OF_RANGE?
  1502. RC::testForServerException($this, RC::EXCEPTION_OUT_OF_RANGE, function($test) {
  1503. $test->redis->move('hoge', 32);
  1504. });
  1505. }
  1506. function testFlushdb() {
  1507. $this->assertTrue($this->redis->flushdb());
  1508. }
  1509. /* sorting */
  1510. function testSort() {
  1511. $unorderedList = RC::pushTailAndReturn($this->redis, 'unordered', array(2, 100, 3, 1, 30, 10));
  1512. // without parameters
  1513. $this->assertEquals(array(1, 2, 3, 10, 30, 100), $this->redis->sort('unordered'));
  1514. // with parameter ASC/DESC
  1515. $this->assertEquals(
  1516. array(100, 30, 10, 3, 2, 1),
  1517. $this->redis->sort('unordered', array(
  1518. 'sort' => 'desc'
  1519. ))
  1520. );
  1521. // with parameter LIMIT
  1522. $this->assertEquals(
  1523. array(1, 2, 3),
  1524. $this->redis->sort('unordered', array(
  1525. 'limit' => array(0, 3)
  1526. ))
  1527. );
  1528. $this->assertEquals(
  1529. array(10, 30),
  1530. $this->redis->sort('unordered', array(
  1531. 'limit' => array(3, 2)
  1532. ))
  1533. );
  1534. // with parameter ALPHA
  1535. $this->assertEquals(
  1536. array(1, 10, 100, 2, 3, 30),
  1537. $this->redis->sort('unordered', array(
  1538. 'alpha' => true
  1539. ))
  1540. );
  1541. // with combined parameters
  1542. $this->assertEquals(
  1543. array(30, 10, 3, 2),
  1544. $this->redis->sort('unordered', array(
  1545. 'alpha' => false,
  1546. 'sort' => 'desc',
  1547. 'limit' => array(1, 4)
  1548. ))
  1549. );
  1550. // with parameter ALPHA
  1551. $this->assertEquals(
  1552. array(1, 10, 100, 2, 3, 30),
  1553. $this->redis->sort('unordered', array(
  1554. 'alpha' => true
  1555. ))
  1556. );
  1557. // with parameter STORE
  1558. $this->assertEquals(
  1559. count($unorderedList),
  1560. $this->redis->sort('unordered', array(
  1561. 'store' => 'ordered'
  1562. ))
  1563. );
  1564. $this->assertEquals(array(1, 2, 3, 10, 30, 100), $this->redis->lrange('ordered', 0, -1));
  1565. // with parameter GET
  1566. $this->redis->rpush('uids', 1003);
  1567. $this->redis->rpush('uids', 1001);
  1568. $this->redis->rpush('uids', 1002);
  1569. $this->redis->rpush('uids', 1000);
  1570. $sortget = array(
  1571. 'uid:1000' => 'foo', 'uid:1001' => 'bar',
  1572. 'uid:1002' => 'hoge', 'uid:1003' => 'piyo'
  1573. );
  1574. $this->redis->mset($sortget);
  1575. $this->assertEquals(
  1576. array_values($sortget),
  1577. $this->redis->sort('uids', array('get' => 'uid:*'))
  1578. );
  1579. // wrong type
  1580. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
  1581. $test->redis->set('foo', 'bar');
  1582. $test->redis->sort('foo');
  1583. });
  1584. }
  1585. /* remote server control commands */
  1586. function testInfo() {
  1587. $serverInfo = $this->redis->info();
  1588. $this->assertType('array', $serverInfo);
  1589. $this->assertNotNull($serverInfo['redis_version']);
  1590. $this->assertGreaterThan(0, $serverInfo['uptime_in_seconds']);
  1591. $this->assertGreaterThan(0, $serverInfo['total_connections_received']);
  1592. }
  1593. function testSlaveOf() {
  1594. $masterHost = 'www.google.com';
  1595. $masterPort = 80;
  1596. $this->assertTrue($this->redis->slaveof($masterHost, $masterPort));
  1597. $serverInfo = $this->redis->info();
  1598. $this->assertEquals('slave', $serverInfo['role']);
  1599. $this->assertEquals($masterHost, $serverInfo['master_host']);
  1600. $this->assertEquals($masterPort, $serverInfo['master_port']);
  1601. // slave of NO ONE, the implicit way
  1602. $this->assertTrue($this->redis->slaveof());
  1603. $serverInfo = $this->redis->info();
  1604. $this->assertEquals('master', $serverInfo['role']);
  1605. // slave of NO ONE, the explicit way
  1606. $this->assertTrue($this->redis->slaveof('NO ONE'));
  1607. }
  1608. /* persistence control commands */
  1609. function testSave() {
  1610. $this->assertTrue($this->redis->save());
  1611. }
  1612. function testBackgroundSave() {
  1613. $this->assertTrue($this->redis->bgsave());
  1614. }
  1615. function testBackgroundRewriteAppendOnlyFile() {
  1616. $this->assertTrue($this->redis->bgrewriteaof());
  1617. }
  1618. function testLastSave() {
  1619. $this->assertGreaterThan(0, $this->redis->lastsave());
  1620. }
  1621. }
  1622. ?>