RedisCommandsTest.php 65 KB

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