RedisCommandsTest.php 67 KB

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