RedisCommandsTest.php 76 KB

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