RedisCommandsTest.php 74 KB

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