RedisCommandsTest.php 72 KB

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