RedisCommandsTest.php 69 KB

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