RedisCommandsTest.php 71 KB

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