RedisCommandsTest.php 78 KB

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