RedisCommandsTest.php 69 KB

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