RedisCommandsTest.php 76 KB

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