RedisCommandsTest.php 66 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781
  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. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, p_anon("\$test", "
  871. \$test->redis->set('foo', 'bar');
  872. \$test->redis->zrange('foo', 0, -1);
  873. "));
  874. }
  875. function testZsetReverseRange() {
  876. $zset = RC::zsetAddAndReturn($this->redis, 'zset', RC::getZSetArray());
  877. $this->assertEquals(
  878. array_slice(array_reverse(array_keys($zset)), 0, 4),
  879. $this->redis->zrevrange('zset', 0, 3)
  880. );
  881. $this->assertEquals(
  882. array_slice(array_reverse(array_keys($zset)), 0, 1),
  883. $this->redis->zrevrange('zset', 0, 0)
  884. );
  885. $this->assertEquals(
  886. array(),
  887. $this->redis->zrevrange('zset', 1, 0)
  888. );
  889. $this->assertEquals(
  890. array_values(array_reverse(array_keys($zset))),
  891. $this->redis->zrevrange('zset', 0, -1)
  892. );
  893. $this->assertEquals(
  894. array_slice(array_reverse(array_keys($zset)), 3, 1),
  895. $this->redis->zrevrange('zset', 3, -3)
  896. );
  897. $this->assertEquals(
  898. array(),
  899. $this->redis->zrevrange('zset', 5, -3)
  900. );
  901. $this->assertEquals(
  902. array_slice(array_reverse(array_keys($zset)), -5, -1),
  903. $this->redis->zrevrange('zset', -5, -2)
  904. );
  905. $this->assertEquals(
  906. array_values(array_reverse(array_keys($zset))),
  907. $this->redis->zrevrange('zset', -100, 100)
  908. );
  909. $this->assertEquals(
  910. array(array('f', 30), array('e', 20), array('d', 20)),
  911. $this->redis->zrevrange('zset', 0, 2, 'withscores')
  912. );
  913. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, p_anon("\$test", "
  914. \$test->redis->set('foo', 'bar');
  915. \$test->redis->zrevrange('foo', 0, -1);
  916. "));
  917. }
  918. function testZsetRangeByScore() {
  919. $zset = RC::zsetAddAndReturn($this->redis, 'zset', RC::getZSetArray());
  920. $this->assertEquals(
  921. array('a'),
  922. $this->redis->zrangebyscore('zset', -10, -10)
  923. );
  924. $this->assertEquals(
  925. array('c', 'd', 'e', 'f'),
  926. $this->redis->zrangebyscore('zset', 10, 30)
  927. );
  928. $this->assertEquals(
  929. array('d', 'e'),
  930. $this->redis->zrangebyscore('zset', 20, 20)
  931. );
  932. $this->assertEquals(
  933. array(),
  934. $this->redis->zrangebyscore('zset', 30, 0)
  935. );
  936. $this->assertEquals(
  937. array(array('c', 10), array('d', 20), array('e', 20)),
  938. $this->redis->zrangebyscore('zset', 10, 20, 'withscores')
  939. );
  940. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, p_anon("\$test", "
  941. \$test->redis->set('foo', 'bar');
  942. \$test->redis->zrangebyscore('foo', 0, 0);
  943. "));
  944. }
  945. function testZsetUnionStore() {
  946. $zsetA = RC::zsetAddAndReturn($this->redis, 'zseta', array('a' => 1, 'b' => 2, 'c' => 3));
  947. $zsetB = RC::zsetAddAndReturn($this->redis, 'zsetb', array('b' => 1, 'c' => 2, 'd' => 3));
  948. // basic ZUNIONSTORE
  949. $this->assertEquals(4, $this->redis->zunionstore('zsetc', 2, 'zseta', 'zsetb'));
  950. $this->assertEquals(
  951. array(array('a', 1), array('b', 3), array('d', 3), array('c', 5)),
  952. $this->redis->zrange('zsetc', 0, -1, 'withscores')
  953. );
  954. $this->assertEquals(3, $this->redis->zunionstore('zsetc', 2, 'zseta', 'zsetbNull'));
  955. $this->assertEquals(
  956. array(array('a', 1), array('b', 2), array('c', 3)),
  957. $this->redis->zrange('zsetc', 0, -1, 'withscores')
  958. );
  959. $this->assertEquals(3, $this->redis->zunionstore('zsetc', 2, 'zsetaNull', 'zsetb'));
  960. $this->assertEquals(
  961. array(array('b', 1), array('c', 2), array('d', 3)),
  962. $this->redis->zrange('zsetc', 0, -1, 'withscores')
  963. );
  964. $this->assertEquals(0, $this->redis->zunionstore('zsetc', 2, 'zsetaNull', 'zsetbNull'));
  965. // with WEIGHTS
  966. $options = array('weights' => array(2, 3));
  967. $this->assertEquals(4, $this->redis->zunionstore('zsetc', 2, 'zseta', 'zsetb', $options));
  968. $this->assertEquals(
  969. array(array('a', 2), array('b', 7), array('d', 9), array('c', 12)),
  970. $this->redis->zrange('zsetc', 0, -1, 'withscores')
  971. );
  972. // with AGGREGATE (min)
  973. $options = array('aggregate' => 'min');
  974. $this->assertEquals(4, $this->redis->zunionstore('zsetc', 2, 'zseta', 'zsetb', $options));
  975. $this->assertEquals(
  976. array(array('a', 1), array('b', 1), array('c', 2), array('d', 3)),
  977. $this->redis->zrange('zsetc', 0, -1, 'withscores')
  978. );
  979. // with AGGREGATE (max)
  980. $options = array('aggregate' => 'max');
  981. $this->assertEquals(4, $this->redis->zunionstore('zsetc', 2, 'zseta', 'zsetb', $options));
  982. $this->assertEquals(
  983. array(array('a', 1), array('b', 2), array('c', 3), array('d', 3)),
  984. $this->redis->zrange('zsetc', 0, -1, 'withscores')
  985. );
  986. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, p_anon("\$test", "
  987. \$test->redis->set('zsetFake', 'fake');
  988. \$test->redis->zunionstore('zsetc', 2, 'zseta', 'zsetFake');
  989. "));
  990. }
  991. function testZsetIntersectionStore() {
  992. $zsetA = RC::zsetAddAndReturn($this->redis, 'zseta', array('a' => 1, 'b' => 2, 'c' => 3));
  993. $zsetB = RC::zsetAddAndReturn($this->redis, 'zsetb', array('b' => 1, 'c' => 2, 'd' => 3));
  994. // basic ZINTERSTORE
  995. $this->assertEquals(2, $this->redis->zinterstore('zsetc', 2, 'zseta', 'zsetb'));
  996. $this->assertEquals(
  997. array(array('b', 3), array('c', 5)),
  998. $this->redis->zrange('zsetc', 0, -1, 'withscores')
  999. );
  1000. $this->assertEquals(0, $this->redis->zinterstore('zsetc', 2, 'zseta', 'zsetbNull'));
  1001. $this->assertEquals(0, $this->redis->zinterstore('zsetc', 2, 'zsetaNull', 'zsetb'));
  1002. $this->assertEquals(0, $this->redis->zinterstore('zsetc', 2, 'zsetaNull', 'zsetbNull'));
  1003. // with WEIGHTS
  1004. $options = array('weights' => array(2, 3));
  1005. $this->assertEquals(2, $this->redis->zinterstore('zsetc', 2, 'zseta', 'zsetb', $options));
  1006. $this->assertEquals(
  1007. array(array('b', 7), array('c', 12)),
  1008. $this->redis->zrange('zsetc', 0, -1, 'withscores')
  1009. );
  1010. // with AGGREGATE (min)
  1011. $options = array('aggregate' => 'min');
  1012. $this->assertEquals(2, $this->redis->zinterstore('zsetc', 2, 'zseta', 'zsetb', $options));
  1013. $this->assertEquals(
  1014. array(array('b', 1), array('c', 2)),
  1015. $this->redis->zrange('zsetc', 0, -1, 'withscores')
  1016. );
  1017. // with AGGREGATE (max)
  1018. $options = array('aggregate' => 'max');
  1019. $this->assertEquals(2, $this->redis->zinterstore('zsetc', 2, 'zseta', 'zsetb', $options));
  1020. $this->assertEquals(
  1021. array(array('b', 2), array('c', 3)),
  1022. $this->redis->zrange('zsetc', 0, -1, 'withscores')
  1023. );
  1024. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, p_anon("\$test", "
  1025. \$test->redis->set('zsetFake', 'fake');
  1026. \$test->redis->zinterstore('zsetc', 2, 'zseta', 'zsetFake');
  1027. "));
  1028. }
  1029. function testZsetCount() {
  1030. $zset = RC::zsetAddAndReturn($this->redis, 'zset', RC::getZSetArray());
  1031. $this->assertEquals(0, $this->redis->zcount('zset', 50, 100));
  1032. $this->assertEquals(6, $this->redis->zcount('zset', -100, 100));
  1033. $this->assertEquals(3, $this->redis->zcount('zset', 10, 20));
  1034. $this->assertEquals(2, $this->redis->zcount('zset', "(10", 20));
  1035. $this->assertEquals(1, $this->redis->zcount('zset', 10, "(20"));
  1036. $this->assertEquals(0, $this->redis->zcount('zset', "(10", "(20"));
  1037. $this->assertEquals(3, $this->redis->zcount('zset', "(0", "(30"));
  1038. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, p_anon("\$test", "
  1039. \$test->redis->set('foo', 'bar');
  1040. \$test->redis->zcount('foo', 0, 0);
  1041. "));
  1042. }
  1043. function testZsetCardinality() {
  1044. $zset = RC::zsetAddAndReturn($this->redis, 'zset', RC::getZSetArray());
  1045. $this->assertEquals(count($zset), $this->redis->zcard('zset'));
  1046. $this->redis->zrem('zset', 'a');
  1047. $this->assertEquals(count($zset) - 1, $this->redis->zcard('zset'));
  1048. // empty zset
  1049. $this->redis->zadd('zsetB', 0, 'a');
  1050. $this->redis->zrem('zsetB', 'a');
  1051. $this->assertEquals(0, $this->redis->zcard('setB'));
  1052. // non-existing zset
  1053. $this->assertEquals(0, $this->redis->zcard('zsetDoesNotExist'));
  1054. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, p_anon("\$test", "
  1055. \$test->redis->set('foo', 'bar');
  1056. \$test->redis->zcard('foo');
  1057. "));
  1058. }
  1059. function testZsetScore() {
  1060. $zset = RC::zsetAddAndReturn($this->redis, 'zset', RC::getZSetArray());
  1061. $this->assertEquals(-10, $this->redis->zscore('zset', 'a'));
  1062. $this->assertEquals(10, $this->redis->zscore('zset', 'c'));
  1063. $this->assertEquals(20, $this->redis->zscore('zset', 'e'));
  1064. $this->assertNull($this->redis->zscore('zset', 'x'));
  1065. $this->assertNull($this->redis->zscore('zsetDoesNotExist', 'a'));
  1066. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, p_anon("\$test", "
  1067. \$test->redis->set('foo', 'bar');
  1068. \$test->redis->zscore('foo', 'bar');
  1069. "));
  1070. }
  1071. function testZsetRemoveRangeByScore() {
  1072. $zset = RC::zsetAddAndReturn($this->redis, 'zset', RC::getZSetArray());
  1073. $this->assertEquals(2, $this->redis->zremrangebyscore('zset', -10, 0));
  1074. $this->assertEquals(
  1075. array('c', 'd', 'e', 'f'),
  1076. $this->redis->zrange('zset', 0, -1)
  1077. );
  1078. $this->assertEquals(1, $this->redis->zremrangebyscore('zset', 10, 10));
  1079. $this->assertEquals(
  1080. array('d', 'e', 'f'),
  1081. $this->redis->zrange('zset', 0, -1)
  1082. );
  1083. $this->assertEquals(0, $this->redis->zremrangebyscore('zset', 100, 100));
  1084. $this->assertEquals(3, $this->redis->zremrangebyscore('zset', 0, 100));
  1085. $this->assertEquals(0, $this->redis->zremrangebyscore('zset', 0, 100));
  1086. $this->assertEquals(0, $this->redis->zremrangebyscore('zsetDoesNotExist', 0, 100));
  1087. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, p_anon("\$test", "
  1088. \$test->redis->set('foo', 'bar');
  1089. \$test->redis->zremrangebyscore('foo', 0, 0);
  1090. "));
  1091. }
  1092. function testZsetRank() {
  1093. $zset = RC::zsetAddAndReturn($this->redis, 'zset', RC::getZSetArray());
  1094. $this->assertEquals(0, $this->redis->zrank('zset', 'a'));
  1095. $this->assertEquals(1, $this->redis->zrank('zset', 'b'));
  1096. $this->assertEquals(4, $this->redis->zrank('zset', 'e'));
  1097. $this->redis->zrem('zset', 'd');
  1098. $this->assertEquals(3, $this->redis->zrank('zset', 'e'));
  1099. $this->assertNull($this->redis->zrank('zset', 'x'));
  1100. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, p_anon("\$test", "
  1101. \$test->redis->set('foo', 'bar');
  1102. \$test->redis->zrank('foo', 'a');
  1103. "));
  1104. }
  1105. function testZsetReverseRank() {
  1106. $zset = RC::zsetAddAndReturn($this->redis, 'zset', RC::getZSetArray());
  1107. $this->assertEquals(5, $this->redis->zrevrank('zset', 'a'));
  1108. $this->assertEquals(4, $this->redis->zrevrank('zset', 'b'));
  1109. $this->assertEquals(1, $this->redis->zrevrank('zset', 'e'));
  1110. $this->redis->zrem('zset', 'e');
  1111. $this->assertEquals(1, $this->redis->zrevrank('zset', 'd'));
  1112. $this->assertNull($this->redis->zrevrank('zset', 'x'));
  1113. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, p_anon("\$test", "
  1114. \$test->redis->set('foo', 'bar');
  1115. \$test->redis->zrevrank('foo', 'a');
  1116. "));
  1117. }
  1118. function testZsetRemoveRangeByRank() {
  1119. RC::zsetAddAndReturn($this->redis, 'zseta', RC::getZSetArray());
  1120. $this->assertEquals(3, $this->redis->zremrangebyrank('zseta', 0, 2));
  1121. $this->assertEquals(
  1122. array('d', 'e', 'f'),
  1123. $this->redis->zrange('zseta', 0, -1)
  1124. );
  1125. $this->assertEquals(1, $this->redis->zremrangebyrank('zseta', 0, 0));
  1126. $this->assertEquals(
  1127. array('e', 'f'),
  1128. $this->redis->zrange('zseta', 0, -1)
  1129. );
  1130. RC::zsetAddAndReturn($this->redis, 'zsetb', RC::getZSetArray());
  1131. $this->assertEquals(3, $this->redis->zremrangebyrank('zsetb', -3, -1));
  1132. $this->assertEquals(
  1133. array('a', 'b', 'c'),
  1134. $this->redis->zrange('zsetb', 0, -1)
  1135. );
  1136. $this->assertEquals(1, $this->redis->zremrangebyrank('zsetb', -1, -1));
  1137. $this->assertEquals(
  1138. array('a', 'b'),
  1139. $this->redis->zrange('zsetb', 0, -1)
  1140. );
  1141. $this->assertEquals(2, $this->redis->zremrangebyrank('zsetb', -2, 1));
  1142. $this->assertEquals(
  1143. array(),
  1144. $this->redis->zrange('zsetb', 0, -1)
  1145. );
  1146. $this->assertFalse($this->redis->exists('zsetb'));
  1147. $this->assertEquals(0, $this->redis->zremrangebyrank('zsetc', 0, 0));
  1148. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, p_anon("\$test", "
  1149. \$test->redis->set('foo', 'bar');
  1150. \$test->redis->zremrangebyrank('foo', 0, 1);
  1151. "));
  1152. }
  1153. /* commands operating on hashes */
  1154. function testHashSet() {
  1155. $this->assertTrue($this->redis->hset('metavars', 'foo', 'bar'));
  1156. $this->assertTrue($this->redis->hset('metavars', 'hoge', 'piyo'));
  1157. $this->assertEquals('bar', $this->redis->hget('metavars', 'foo'));
  1158. $this->assertEquals('piyo', $this->redis->hget('metavars', 'hoge'));
  1159. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, p_anon("\$test", "
  1160. \$test->redis->set('test', 'foobar');
  1161. \$test->redis->hset('test', 'hoge', 'piyo');
  1162. "));
  1163. }
  1164. function testHashGet() {
  1165. $this->assertTrue($this->redis->hset('metavars', 'foo', 'bar'));
  1166. $this->assertEquals('bar', $this->redis->hget('metavars', 'foo'));
  1167. $this->assertEquals('bar', $this->redis->hget('metavars', 'foo'));
  1168. $this->assertNull($this->redis->hget('metavars', 'hoge'));
  1169. $this->assertNull($this->redis->hget('hashDoesNotExist', 'field'));
  1170. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, p_anon("\$test", "
  1171. \$test->redis->rpush('metavars', 'foo');
  1172. \$test->redis->hget('metavars', 'foo');
  1173. "));
  1174. }
  1175. function testHashExists() {
  1176. $this->assertTrue($this->redis->hset('metavars', 'foo', 'bar'));
  1177. $this->assertTrue($this->redis->hexists('metavars', 'foo'));
  1178. $this->assertFalse($this->redis->hexists('metavars', 'hoge'));
  1179. $this->assertFalse($this->redis->hexists('hashDoesNotExist', 'field'));
  1180. }
  1181. function testHashDelete() {
  1182. $this->assertTrue($this->redis->hset('metavars', 'foo', 'bar'));
  1183. $this->assertTrue($this->redis->hexists('metavars', 'foo'));
  1184. $this->assertTrue($this->redis->hdel('metavars', 'foo'));
  1185. $this->assertFalse($this->redis->hexists('metavars', 'foo'));
  1186. $this->assertFalse($this->redis->hdel('metavars', 'hoge'));
  1187. $this->assertFalse($this->redis->hdel('hashDoesNotExist', 'field'));
  1188. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, p_anon("\$test", "
  1189. \$test->redis->set('foo', 'bar');
  1190. \$test->redis->hdel('foo', 'field');
  1191. "));
  1192. }
  1193. function testHashLength() {
  1194. $this->assertTrue($this->redis->hset('metavars', 'foo', 'bar'));
  1195. $this->assertTrue($this->redis->hset('metavars', 'hoge', 'piyo'));
  1196. $this->assertTrue($this->redis->hset('metavars', 'foofoo', 'barbar'));
  1197. $this->assertTrue($this->redis->hset('metavars', 'hogehoge', 'piyopiyo'));
  1198. $this->assertEquals(4, $this->redis->hlen('metavars'));
  1199. $this->assertEquals(0, $this->redis->hlen('hashDoesNotExist'));
  1200. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, p_anon("\$test", "
  1201. \$test->redis->set('foo', 'bar');
  1202. \$test->redis->hlen('foo');
  1203. "));
  1204. }
  1205. function testHashSetPreserve() {
  1206. $this->assertTrue($this->redis->hsetnx('metavars', 'foo', 'bar'));
  1207. $this->assertFalse($this->redis->hsetnx('metavars', 'foo', 'barbar'));
  1208. $this->assertEquals('bar', $this->redis->hget('metavars', 'foo'));
  1209. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, p_anon("\$test", "
  1210. \$test->redis->set('test', 'foobar');
  1211. \$test->redis->hsetnx('test', 'hoge', 'piyo');
  1212. "));
  1213. }
  1214. function testHashSetAndGetMultiple() {
  1215. $hashKVs = array('foo' => 'bar', 'hoge' => 'piyo');
  1216. // key=>value pairs via array instance
  1217. $this->assertTrue($this->redis->hmset('metavars', $hashKVs));
  1218. $multiRet = $this->redis->hmget('metavars', array_keys($hashKVs));
  1219. $this->assertEquals($hashKVs, array_combine(array_keys($hashKVs), array_values($multiRet)));
  1220. // key=>value pairs via function arguments
  1221. $this->redis->del('metavars');
  1222. $this->assertTrue($this->redis->hmset('metavars', 'foo', 'bar', 'hoge', 'piyo'));
  1223. $this->assertEquals(array('bar', 'piyo'), $this->redis->hmget('metavars', 'foo', 'hoge'));
  1224. }
  1225. function testHashIncrementBy() {
  1226. // test subsequent increment commands
  1227. $this->assertEquals(10, $this->redis->hincrby('hash', 'counter', 10));
  1228. $this->assertEquals(20, $this->redis->hincrby('hash', 'counter', 10));
  1229. $this->assertEquals(0, $this->redis->hincrby('hash', 'counter', -20));
  1230. RC::testForServerException($this, RC::EXCEPTION_HASH_VALNOTINT, p_anon("\$test", "
  1231. \$test->redis->hset('hash', 'field', 'stringvalue');
  1232. \$test->redis->hincrby('hash', 'field', 10);
  1233. "));
  1234. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, p_anon("\$test", "
  1235. \$test->redis->set('foo', 'bar');
  1236. \$test->redis->hincrby('foo', 'bar', 1);
  1237. "));
  1238. }
  1239. function testHashKeys() {
  1240. $hashKVs = array('foo' => 'bar', 'hoge' => 'piyo');
  1241. $this->assertTrue($this->redis->hmset('metavars', $hashKVs));
  1242. $this->assertEquals(array_keys($hashKVs), $this->redis->hkeys('metavars'));
  1243. $this->assertEquals(array(), $this->redis->hkeys('hashDoesNotExist'));
  1244. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, p_anon("\$test", "
  1245. \$test->redis->set('foo', 'bar');
  1246. \$test->redis->hkeys('foo');
  1247. "));
  1248. }
  1249. function testHashValues() {
  1250. $hashKVs = array('foo' => 'bar', 'hoge' => 'piyo');
  1251. $this->assertTrue($this->redis->hmset('metavars', $hashKVs));
  1252. $this->assertEquals(array_values($hashKVs), $this->redis->hvals('metavars'));
  1253. $this->assertEquals(array(), $this->redis->hvals('hashDoesNotExist'));
  1254. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, p_anon("\$test", "
  1255. \$test->redis->set('foo', 'bar');
  1256. \$test->redis->hvals('foo');
  1257. "));
  1258. }
  1259. function testHashGetAll() {
  1260. $hashKVs = array('foo' => 'bar', 'hoge' => 'piyo');
  1261. $this->assertTrue($this->redis->hmset('metavars', $hashKVs));
  1262. $this->assertEquals($hashKVs, $this->redis->hgetall('metavars'));
  1263. $this->assertEquals(array(), $this->redis->hgetall('hashDoesNotExist'));
  1264. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, p_anon("\$test", "
  1265. \$test->redis->set('foo', 'bar');
  1266. \$test->redis->hgetall('foo');
  1267. "));
  1268. }
  1269. /* multiple databases handling commands */
  1270. function testSelectDatabase() {
  1271. $this->assertTrue($this->redis->select(0));
  1272. $this->assertTrue($this->redis->select(RC::DEFAULT_DATABASE));
  1273. RC::testForServerException($this, RC::EXCEPTION_INVALID_DB_IDX, p_anon("\$test", "
  1274. \$test->redis->select(32);
  1275. "));
  1276. RC::testForServerException($this, RC::EXCEPTION_INVALID_DB_IDX, p_anon("\$test", "
  1277. \$test->redis->select(-1);
  1278. "));
  1279. }
  1280. function testMove() {
  1281. // TODO: This test sucks big time. Period.
  1282. $otherDb = 5;
  1283. $this->redis->set('foo', 'bar');
  1284. $this->redis->select($otherDb);
  1285. $this->redis->flushdb();
  1286. $this->redis->select(RC::DEFAULT_DATABASE);
  1287. $this->assertTrue($this->redis->move('foo', $otherDb));
  1288. $this->assertFalse($this->redis->move('foo', $otherDb));
  1289. $this->assertFalse($this->redis->move('keyDoesNotExist', $otherDb));
  1290. $this->redis->set('hoge', 'piyo');
  1291. // TODO: shouldn't Redis send an EXCEPTION_INVALID_DB_IDX instead of EXCEPTION_OUT_OF_RANGE?
  1292. RC::testForServerException($this, RC::EXCEPTION_OUT_OF_RANGE, p_anon("\$test", "
  1293. \$test->redis->move('hoge', 32);
  1294. "));
  1295. }
  1296. function testFlushdb() {
  1297. $this->assertTrue($this->redis->flushdb());
  1298. }
  1299. /* sorting */
  1300. function testSort() {
  1301. $unorderedList = RC::pushTailAndReturn($this->redis, 'unordered', array(2, 100, 3, 1, 30, 10));
  1302. // without parameters
  1303. $this->assertEquals(array(1, 2, 3, 10, 30, 100), $this->redis->sort('unordered'));
  1304. // with parameter ASC/DESC
  1305. $this->assertEquals(
  1306. array(100, 30, 10, 3, 2, 1),
  1307. $this->redis->sort('unordered', array(
  1308. 'sort' => 'desc'
  1309. ))
  1310. );
  1311. // with parameter LIMIT
  1312. $this->assertEquals(
  1313. array(1, 2, 3),
  1314. $this->redis->sort('unordered', array(
  1315. 'limit' => array(0, 3)
  1316. ))
  1317. );
  1318. $this->assertEquals(
  1319. array(10, 30),
  1320. $this->redis->sort('unordered', array(
  1321. 'limit' => array(3, 2)
  1322. ))
  1323. );
  1324. // with parameter ALPHA
  1325. $this->assertEquals(
  1326. array(1, 10, 100, 2, 3, 30),
  1327. $this->redis->sort('unordered', array(
  1328. 'alpha' => true
  1329. ))
  1330. );
  1331. // with combined parameters
  1332. $this->assertEquals(
  1333. array(30, 10, 3, 2),
  1334. $this->redis->sort('unordered', array(
  1335. 'alpha' => false,
  1336. 'sort' => 'desc',
  1337. 'limit' => array(1, 4)
  1338. ))
  1339. );
  1340. // with parameter ALPHA
  1341. $this->assertEquals(
  1342. array(1, 10, 100, 2, 3, 30),
  1343. $this->redis->sort('unordered', array(
  1344. 'alpha' => true
  1345. ))
  1346. );
  1347. // with parameter STORE
  1348. $this->assertEquals(
  1349. count($unorderedList),
  1350. $this->redis->sort('unordered', array(
  1351. 'store' => 'ordered'
  1352. ))
  1353. );
  1354. $this->assertEquals(array(1, 2, 3, 10, 30, 100), $this->redis->lrange('ordered', 0, -1));
  1355. // with parameter GET
  1356. $this->redis->rpush('uids', 1003);
  1357. $this->redis->rpush('uids', 1001);
  1358. $this->redis->rpush('uids', 1002);
  1359. $this->redis->rpush('uids', 1000);
  1360. $sortget = array(
  1361. 'uid:1000' => 'foo', 'uid:1001' => 'bar',
  1362. 'uid:1002' => 'hoge', 'uid:1003' => 'piyo'
  1363. );
  1364. $this->redis->mset($sortget);
  1365. $this->assertEquals(
  1366. array_values($sortget),
  1367. $this->redis->sort('uids', array('get' => 'uid:*'))
  1368. );
  1369. // wrong type
  1370. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, p_anon("\$test", "
  1371. \$test->redis->set('foo', 'bar');
  1372. \$test->redis->sort('foo');
  1373. "));
  1374. }
  1375. /* remote server control commands */
  1376. function testInfo() {
  1377. $serverInfo = $this->redis->info();
  1378. $this->assertType('array', $serverInfo);
  1379. $this->assertNotNull($serverInfo['redis_version']);
  1380. $this->assertGreaterThan(0, $serverInfo['uptime_in_seconds']);
  1381. $this->assertGreaterThan(0, $serverInfo['total_connections_received']);
  1382. }
  1383. function testSlaveOf() {
  1384. $masterHost = 'www.google.com';
  1385. $masterPort = 80;
  1386. $this->assertTrue($this->redis->slaveof($masterHost, $masterPort));
  1387. $serverInfo = $this->redis->info();
  1388. $this->assertEquals('slave', $serverInfo['role']);
  1389. $this->assertEquals($masterHost, $serverInfo['master_host']);
  1390. $this->assertEquals($masterPort, $serverInfo['master_port']);
  1391. // slave of NO ONE, the implicit way
  1392. $this->assertTrue($this->redis->slaveof());
  1393. $serverInfo = $this->redis->info();
  1394. $this->assertEquals('master', $serverInfo['role']);
  1395. // slave of NO ONE, the explicit way
  1396. $this->assertTrue($this->redis->slaveof('NO ONE'));
  1397. }
  1398. /* persistence control commands */
  1399. function testSave() {
  1400. $this->assertTrue($this->redis->save());
  1401. }
  1402. function testBackgroundSave() {
  1403. $this->assertTrue($this->redis->bgsave());
  1404. }
  1405. function testBackgroundRewriteAppendOnlyFile() {
  1406. $this->assertTrue($this->redis->bgrewriteaof());
  1407. }
  1408. function testLastSave() {
  1409. $this->assertGreaterThan(0, $this->redis->lastsave());
  1410. }
  1411. }
  1412. ?>