RedisCommandsTest.php 66 KB

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