RedisCommandsTest.php 77 KB

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