RedisCommandsTest.php 67 KB

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