RedisCommandsTest.php 71 KB

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