RedisCommandsTest.php 67 KB

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