RedisCommandsTest.php 76 KB

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