RedisCommandsTest.php 66 KB

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