RedisCommandsTest.php 77 KB

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