RedisCommandsTest.php 71 KB

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