RedisCommandsTest.php 65 KB

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