RedisCommandsTest.php 74 KB

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