RedisCommandsTest.php 77 KB

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