RedisCommandsTest.php 74 KB

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