RedisCommandsTest.php 76 KB

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