RedisCommandsTest.php 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298
  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 testFlushDatabases
  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->flushDatabase();
  16. }
  17. protected function tearDown() {
  18. }
  19. protected function onNotSuccessfulTest($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, p_anon("\$test", "
  50. \$test->redis->exec();
  51. "));
  52. }
  53. /* commands operating on string values */
  54. function testSet() {
  55. $this->assertTrue($this->redis->set('foo', 'bar'));
  56. $this->assertEquals('bar', $this->redis->get('foo'));
  57. }
  58. function testGet() {
  59. $this->redis->set('foo', 'bar');
  60. $this->assertEquals('bar', $this->redis->get('foo'));
  61. $this->assertNull($this->redis->get('fooDoesNotExist'));
  62. // should throw an exception when trying to do a GET on non-string types
  63. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, p_anon("\$test", "
  64. \$test->redis->pushTail('metavars', 'foo');
  65. \$test->redis->get('metavars');
  66. "));
  67. }
  68. function testExists() {
  69. $this->redis->set('foo', 'bar');
  70. $this->assertTrue($this->redis->exists('foo'));
  71. $this->assertFalse($this->redis->exists('key_does_not_exist'));
  72. }
  73. function testSetPreserve() {
  74. $multi = RC::getKeyValueArray();
  75. $this->assertTrue($this->redis->setPreserve('foo', 'bar'));
  76. $this->assertFalse($this->redis->setPreserve('foo', 'rab'));
  77. $this->assertEquals('bar', $this->redis->get('foo'));
  78. }
  79. function testMultipleSetAndGet() {
  80. $multi = RC::getKeyValueArray();
  81. // key=>value pairs via array instance
  82. $this->assertTrue($this->redis->setMultiple($multi));
  83. $multiRet = $this->redis->getMultiple(array_keys($multi));
  84. $this->assertEquals($multi, array_combine(array_keys($multi), array_values($multiRet)));
  85. // key=>value pairs via function arguments
  86. $this->assertTrue($this->redis->setMultiple('a', 1, 'b', 2, 'c', 3));
  87. $this->assertEquals(array(1, 2, 3), $this->redis->getMultiple('a', 'b', 'c'));
  88. }
  89. function testSetMultiplePreserve() {
  90. $multi = RC::getKeyValueArray();
  91. $newpair = array('hogehoge' => 'piyopiyo');
  92. $hijacked = array('foo' => 'baz', 'hoge' => 'fuga');
  93. // successful set
  94. $expectedResult = array_merge($multi, $newpair);
  95. $this->redis->setMultiple($multi);
  96. $this->assertTrue($this->redis->setMultiplePreserve($newpair));
  97. $this->assertEquals(
  98. array_values($expectedResult),
  99. $this->redis->getMultiple(array_keys($expectedResult))
  100. );
  101. $this->redis->flushDatabase();
  102. // unsuccessful set
  103. $expectedResult = array_merge($multi, array('hogehoge' => null));
  104. $this->redis->setMultiple($multi);
  105. $this->assertFalse($this->redis->setMultiplePreserve(array_merge($newpair, $hijacked)));
  106. $this->assertEquals(
  107. array_values($expectedResult),
  108. $this->redis->getMultiple(array_keys($expectedResult))
  109. );
  110. }
  111. function testGetSet() {
  112. $this->assertNull($this->redis->getSet('foo', 'bar'));
  113. $this->assertEquals('bar', $this->redis->getSet('foo', 'barbar'));
  114. $this->assertEquals('barbar', $this->redis->getSet('foo', 'baz'));
  115. }
  116. function testIncrementAndIncrementBy() {
  117. // test subsequent increment commands
  118. $this->assertEquals(1, $this->redis->increment('foo'));
  119. $this->assertEquals(2, $this->redis->increment('foo'));
  120. // test subsequent incrementBy commands
  121. $this->assertEquals(22, $this->redis->incrementBy('foo', 20));
  122. $this->assertEquals(10, $this->redis->incrementBy('foo', -12));
  123. $this->assertEquals(-100, $this->redis->incrementBy('foo', -110));
  124. }
  125. function testDecrementAndDecrementBy() {
  126. // test subsequent decrement commands
  127. $this->assertEquals(-1, $this->redis->decrement('foo'));
  128. $this->assertEquals(-2, $this->redis->decrement('foo'));
  129. // test subsequent decrementBy commands
  130. $this->assertEquals(-22, $this->redis->decrementBy('foo', 20));
  131. $this->assertEquals(-10, $this->redis->decrementBy('foo', -12));
  132. $this->assertEquals(100, $this->redis->decrementBy('foo', -110));
  133. }
  134. function testDelete() {
  135. $this->redis->set('foo', 'bar');
  136. $this->assertTrue($this->redis->delete('foo'));
  137. $this->assertFalse($this->redis->exists('foo'));
  138. $this->assertFalse($this->redis->delete('foo'));
  139. }
  140. function testType() {
  141. $this->assertEquals('none', $this->redis->type('fooDoesNotExist'));
  142. $this->redis->set('fooString', 'bar');
  143. $this->assertEquals('string', $this->redis->type('fooString'));
  144. $this->redis->pushTail('fooList', 'bar');
  145. $this->assertEquals('list', $this->redis->type('fooList'));
  146. $this->redis->setAdd('fooSet', 'bar');
  147. $this->assertEquals('set', $this->redis->type('fooSet'));
  148. $this->redis->zsetAdd('fooZSet', 'bar', 0);
  149. $this->assertEquals('zset', $this->redis->type('fooZSet'));
  150. }
  151. /* commands operating on the key space */
  152. function testKeys() {
  153. $keyValsNs = RC::getNamespacedKeyValueArray();
  154. $keyValsOthers = array('aaa' => 1, 'aba' => 2, 'aca' => 3);
  155. $allKeyVals = array_merge($keyValsNs, $keyValsOthers);
  156. $this->redis->setMultiple($allKeyVals);
  157. $this->assertEquals(array(), $this->redis->keys('nokeys:*'));
  158. $keysFromRedis = $this->redis->keys('metavar:*');
  159. $this->assertEquals(array(), array_diff(array_keys($keyValsNs), $keysFromRedis));
  160. $keysFromRedis = $this->redis->keys('*');
  161. $this->assertEquals(array(), array_diff(array_keys($allKeyVals), $keysFromRedis));
  162. $keysFromRedis = $this->redis->keys('a?a');
  163. $this->assertEquals(array(), array_diff(array_keys($keyValsOthers), $keysFromRedis));
  164. }
  165. function testRandomKey() {
  166. $keyvals = RC::getKeyValueArray();
  167. $this->assertNull($this->redis->randomKey());
  168. $this->redis->setMultiple($keyvals);
  169. $this->assertTrue(in_array($this->redis->randomKey(), array_keys($keyvals)));
  170. }
  171. function testRename() {
  172. $this->redis->setMultiple(array('foo' => 'bar', 'foofoo' => 'barbar'));
  173. // rename existing keys
  174. $this->assertTrue($this->redis->rename('foo', 'foofoo'));
  175. $this->assertFalse($this->redis->exists('foo'));
  176. $this->assertEquals('bar', $this->redis->get('foofoo'));
  177. // should throw an excepion then trying to rename non-existing keys
  178. RC::testForServerException($this, RC::EXCEPTION_NO_SUCH_KEY, p_anon("\$test", "
  179. \$test->redis->rename('hoge', 'hogehoge');
  180. "));
  181. }
  182. function testRenamePreserve() {
  183. $this->redis->setMultiple(array('foo' => 'bar', 'hoge' => 'piyo', 'hogehoge' => 'piyopiyo'));
  184. $this->assertTrue($this->redis->renamePreserve('foo', 'foofoo'));
  185. $this->assertFalse($this->redis->exists('foo'));
  186. $this->assertEquals('bar', $this->redis->get('foofoo'));
  187. $this->assertFalse($this->redis->renamePreserve('hoge', 'hogehoge'));
  188. $this->assertTrue($this->redis->exists('hoge'));
  189. // should throw an excepion then trying to rename non-existing keys
  190. RC::testForServerException($this, RC::EXCEPTION_NO_SUCH_KEY, p_anon("\$test", "
  191. \$test->redis->renamePreserve('fuga', 'baz');
  192. "));
  193. }
  194. function testExpirationAndTTL() {
  195. $this->redis->set('foo', 'bar');
  196. // check for key expiration
  197. $this->assertTrue($this->redis->expire('foo', 1));
  198. $this->assertEquals(1, $this->redis->ttl('foo'));
  199. $this->assertTrue($this->redis->exists('foo'));
  200. sleep(2);
  201. $this->assertFalse($this->redis->exists('foo'));
  202. $this->assertEquals(-1, $this->redis->ttl('foo'));
  203. // check for consistent TTL values
  204. $this->redis->set('foo', 'bar');
  205. $this->assertTrue($this->redis->expire('foo', 100));
  206. sleep(3);
  207. $this->assertEquals(97, $this->redis->ttl('foo'));
  208. // delete key on negative TTL
  209. $this->redis->set('foo', 'bar');
  210. $this->assertTrue($this->redis->expire('foo', -100));
  211. $this->assertFalse($this->redis->exists('foo'));
  212. $this->assertEquals(-1, $this->redis->ttl('foo'));
  213. }
  214. function testDatabaseSize() {
  215. // TODO: is this really OK?
  216. $this->assertEquals(0, $this->redis->databaseSize());
  217. $this->redis->setMultiple(RC::getKeyValueArray());
  218. $this->assertGreaterThan(0, $this->redis->databaseSize());
  219. }
  220. /* commands operating on lists */
  221. function testPushTail() {
  222. $this->assertTrue($this->redis->pushTail('metavars', 'foo'));
  223. $this->assertTrue($this->redis->exists('metavars'));
  224. $this->assertTrue($this->redis->pushTail('metavars', 'hoge'));
  225. // should throw an exception when trying to do a RPUSH on non-list types
  226. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, p_anon("\$test", "
  227. \$test->redis->set('foo', 'bar');
  228. \$test->redis->pushTail('foo', 'bar');
  229. "));
  230. }
  231. function testPushHead() {
  232. $this->assertTrue($this->redis->pushHead('metavars', 'foo'));
  233. $this->assertTrue($this->redis->exists('metavars'));
  234. $this->assertTrue($this->redis->pushHead('metavars', 'hoge'));
  235. // should throw an exception when trying to do a LPUSH on non-list types
  236. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, p_anon("\$test", "
  237. \$test->redis->set('foo', 'bar');
  238. \$test->redis->pushHead('foo', 'bar');
  239. "));
  240. }
  241. function testListLength() {
  242. $this->assertTrue($this->redis->pushTail('metavars', 'foo'));
  243. $this->assertTrue($this->redis->pushTail('metavars', 'hoge'));
  244. $this->assertEquals(2, $this->redis->listLength('metavars'));
  245. $this->assertEquals(0, $this->redis->listLength('doesnotexist'));
  246. // should throw an exception when trying to do a LLEN on non-list types
  247. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, p_anon("\$test", "
  248. \$test->redis->set('foo', 'bar');
  249. \$test->redis->listLength('foo');
  250. "));
  251. }
  252. function testListRange() {
  253. $numbers = RC::pushTailAndReturn($this->redis, 'numbers', RC::getArrayOfNumbers());
  254. $this->assertEquals(
  255. array_slice($numbers, 0, 4),
  256. $this->redis->listRange('numbers', 0, 3)
  257. );
  258. $this->assertEquals(
  259. array_slice($numbers, 4, 5),
  260. $this->redis->listRange('numbers', 4, 8)
  261. );
  262. $this->assertEquals(
  263. array_slice($numbers, 0, 1),
  264. $this->redis->listRange('numbers', 0, 0)
  265. );
  266. $this->assertEquals(
  267. array(),
  268. $this->redis->listRange('numbers', 1, 0)
  269. );
  270. $this->assertEquals(
  271. $numbers,
  272. $this->redis->listRange('numbers', 0, -1)
  273. );
  274. $this->assertEquals(
  275. array(5),
  276. $this->redis->listRange('numbers', 5, -5)
  277. );
  278. $this->assertEquals(
  279. array(),
  280. $this->redis->listRange('numbers', 7, -5)
  281. );
  282. $this->assertEquals(
  283. array_slice($numbers, -5, -1),
  284. $this->redis->listRange('numbers', -5, -2)
  285. );
  286. $this->assertEquals(
  287. $numbers,
  288. $this->redis->listRange('numbers', -100, 100)
  289. );
  290. $this->assertNull($this->redis->listRange('keyDoesNotExist', 0, 1));
  291. // should throw an exception when trying to do a LRANGE on non-list types
  292. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, p_anon("\$test", "
  293. \$test->redis->set('foo', 'bar');
  294. \$test->redis->listRange('foo', 0, -1);
  295. "));
  296. }
  297. function testListTrim() {
  298. $numbers = RC::pushTailAndReturn($this->redis, 'numbers', RC::getArrayOfNumbers());
  299. $this->assertTrue($this->redis->listTrim('numbers', 0, 2));
  300. $this->assertEquals(
  301. array_slice($numbers, 0, 3),
  302. $this->redis->listRange('numbers', 0, -1)
  303. );
  304. $numbers = RC::pushTailAndReturn($this->redis, 'numbers', RC::getArrayOfNumbers(), RC::WIPE_OUT);
  305. $this->assertTrue($this->redis->listTrim('numbers', 5, 9));
  306. $this->assertEquals(
  307. array_slice($numbers, 5, 5),
  308. $this->redis->listRange('numbers', 0, -1)
  309. );
  310. $numbers = RC::pushTailAndReturn($this->redis, 'numbers', RC::getArrayOfNumbers(), RC::WIPE_OUT);
  311. $this->assertTrue($this->redis->listTrim('numbers', 0, -6));
  312. $this->assertEquals(
  313. array_slice($numbers, 0, -5),
  314. $this->redis->listRange('numbers', 0, -1)
  315. );
  316. $numbers = RC::pushTailAndReturn($this->redis, 'numbers', RC::getArrayOfNumbers(), RC::WIPE_OUT);
  317. $this->assertTrue($this->redis->listTrim('numbers', -5, -3));
  318. $this->assertEquals(
  319. array_slice($numbers, 5, 3),
  320. $this->redis->listRange('numbers', 0, -1)
  321. );
  322. $numbers = RC::pushTailAndReturn($this->redis, 'numbers', RC::getArrayOfNumbers(), RC::WIPE_OUT);
  323. $this->assertTrue($this->redis->listTrim('numbers', -100, 100));
  324. $this->assertEquals(
  325. $numbers,
  326. $this->redis->listRange('numbers', 0, -1)
  327. );
  328. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, p_anon("\$test", "
  329. \$test->redis->set('foo', 'bar');
  330. \$test->redis->listTrim('foo', 0, 1);
  331. "));
  332. }
  333. function testListIndex() {
  334. $numbers = RC::pushTailAndReturn($this->redis, 'numbers', RC::getArrayOfNumbers());
  335. $this->assertEquals(0, $this->redis->listIndex('numbers', 0));
  336. $this->assertEquals(5, $this->redis->listIndex('numbers', 5));
  337. $this->assertEquals(9, $this->redis->listIndex('numbers', 9));
  338. $this->assertNull($this->redis->listIndex('numbers', 100));
  339. $this->assertEquals(0, $this->redis->listIndex('numbers', -0));
  340. $this->assertEquals(9, $this->redis->listIndex('numbers', -1));
  341. $this->assertEquals(7, $this->redis->listIndex('numbers', -3));
  342. $this->assertNull($this->redis->listIndex('numbers', -100));
  343. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, p_anon("\$test", "
  344. \$test->redis->set('foo', 'bar');
  345. \$test->redis->listIndex('foo', 0);
  346. "));
  347. }
  348. function testListSet() {
  349. $numbers = RC::pushTailAndReturn($this->redis, 'numbers', RC::getArrayOfNumbers());
  350. $this->assertTrue($this->redis->listSet('numbers', 5, -5));
  351. $this->assertEquals(-5, $this->redis->listIndex('numbers', 5));
  352. RC::testForServerException($this, RC::EXCEPTION_OUT_OF_RANGE, p_anon("\$test", "
  353. \$test->redis->listSet('numbers', 99, 99);
  354. "));
  355. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, p_anon("\$test", "
  356. \$test->redis->set('foo', 'bar');
  357. \$test->redis->listSet('foo', 0, 0);
  358. "));
  359. }
  360. function testListRemove() {
  361. $mixed = array(0, '_', 2, '_', 4, '_', 6, '_');
  362. RC::pushTailAndReturn($this->redis, 'mixed', $mixed);
  363. $this->assertEquals(2, $this->redis->listRemove('mixed', 2, '_'));
  364. $this->assertEquals(array(0, 2, 4, '_', 6, '_'), $this->redis->listRange('mixed', 0, -1));
  365. RC::pushTailAndReturn($this->redis, 'mixed', $mixed, RC::WIPE_OUT);
  366. $this->assertEquals(4, $this->redis->listRemove('mixed', 0, '_'));
  367. $this->assertEquals(array(0, 2, 4, 6), $this->redis->listRange('mixed', 0, -1));
  368. RC::pushTailAndReturn($this->redis, 'mixed', $mixed, RC::WIPE_OUT);
  369. $this->assertEquals(2, $this->redis->listRemove('mixed', -2, '_'));
  370. $this->assertEquals(array(0, '_', 2, '_', 4, 6), $this->redis->listRange('mixed', 0, -1));
  371. RC::pushTailAndReturn($this->redis, 'mixed', $mixed, RC::WIPE_OUT);
  372. $this->assertEquals(0, $this->redis->listRemove('mixed', 2, '|'));
  373. $this->assertEquals($mixed, $this->redis->listRange('mixed', 0, -1));
  374. $this->assertEquals(0, $this->redis->listRemove('listDoesNotExist', 2, '_'));
  375. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, p_anon("\$test", "
  376. \$test->redis->set('foo', 'bar');
  377. \$test->redis->listRemove('foo', 0, 0);
  378. "));
  379. }
  380. function testListPopFirst() {
  381. $numbers = RC::pushTailAndReturn($this->redis, 'numbers', array(0, 1, 2, 3, 4));
  382. $this->assertEquals(0, $this->redis->popFirst('numbers'));
  383. $this->assertEquals(1, $this->redis->popFirst('numbers'));
  384. $this->assertEquals(2, $this->redis->popFirst('numbers'));
  385. $this->assertEquals(array(3, 4), $this->redis->listRange('numbers', 0, -1));
  386. $this->redis->popFirst('numbers');
  387. $this->redis->popFirst('numbers');
  388. $this->assertNull($this->redis->popFirst('numbers'));
  389. $this->assertNull($this->redis->popFirst('numbers'));
  390. $this->assertNull($this->redis->popFirst('listDoesNotExist'));
  391. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, p_anon("\$test", "
  392. \$test->redis->set('foo', 'bar');
  393. \$test->redis->popFirst('foo');
  394. "));
  395. }
  396. function testListPopLast() {
  397. $numbers = RC::pushTailAndReturn($this->redis, 'numbers', array(0, 1, 2, 3, 4));
  398. $this->assertEquals(4, $this->redis->popLast('numbers'));
  399. $this->assertEquals(3, $this->redis->popLast('numbers'));
  400. $this->assertEquals(2, $this->redis->popLast('numbers'));
  401. $this->assertEquals(array(0, 1), $this->redis->listRange('numbers', 0, -1));
  402. $this->redis->popLast('numbers');
  403. $this->redis->popLast('numbers');
  404. $this->assertNull($this->redis->popLast('numbers'));
  405. $this->assertNull($this->redis->popLast('numbers'));
  406. $this->assertNull($this->redis->popLast('listDoesNotExist'));
  407. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, p_anon("\$test", "
  408. \$test->redis->set('foo', 'bar');
  409. \$test->redis->popLast('foo');
  410. "));
  411. }
  412. function testListPopLastPushHead() {
  413. $numbers = RC::pushTailAndReturn($this->redis, 'numbers', array(0, 1, 2));
  414. $this->assertEquals(0, $this->redis->listLength('temporary'));
  415. $this->assertEquals(2, $this->redis->listPopLastPushHead('numbers', 'temporary'));
  416. $this->assertEquals(1, $this->redis->listPopLastPushHead('numbers', 'temporary'));
  417. $this->assertEquals(0, $this->redis->listPopLastPushHead('numbers', 'temporary'));
  418. $this->assertEquals(0, $this->redis->listLength('numbers'));
  419. $this->assertEquals(3, $this->redis->listLength('temporary'));
  420. $this->assertEquals(array(), $this->redis->listRange('numbers', 0, -1));
  421. $this->assertEquals($numbers, $this->redis->listRange('temporary', 0, -1));
  422. $numbers = RC::pushTailAndReturn($this->redis, 'numbers', array(0, 1, 2));
  423. $this->redis->listPopLastPushHead('numbers', 'numbers');
  424. $this->redis->listPopLastPushHead('numbers', 'numbers');
  425. $this->redis->listPopLastPushHead('numbers', 'numbers');
  426. $this->assertEquals($numbers, $this->redis->listRange('numbers', 0, -1));
  427. $this->assertEquals(null, $this->redis->listPopLastPushHead('listDoesNotExist1', 'listDoesNotExist2'));
  428. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, p_anon("\$test", "
  429. \$test->redis->set('foo', 'bar');
  430. \$test->redis->listPopLastPushHead('foo', 'hoge');
  431. "));
  432. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, p_anon("\$test", "
  433. \$test->redis->set('foo', 'bar');
  434. \$test->redis->listPopLastPushHead('temporary', 'foo');
  435. "));
  436. }
  437. /* commands operating on sets */
  438. function testSetAdd() {
  439. $this->assertTrue($this->redis->setAdd('set', 0));
  440. $this->assertTrue($this->redis->setAdd('set', 1));
  441. $this->assertFalse($this->redis->setAdd('set', 0));
  442. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, p_anon("\$test", "
  443. \$test->redis->set('foo', 'bar');
  444. \$test->redis->setAdd('foo', 0);
  445. "));
  446. }
  447. function testSetRemove() {
  448. $set = RC::setAddAndReturn($this->redis, 'set', array(0, 1, 2, 3, 4));
  449. $this->assertTrue($this->redis->setRemove('set', 0));
  450. $this->assertTrue($this->redis->setRemove('set', 4));
  451. $this->assertFalse($this->redis->setRemove('set', 10));
  452. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, p_anon("\$test", "
  453. \$test->redis->set('foo', 'bar');
  454. \$test->redis->setRemove('foo', 0);
  455. "));
  456. }
  457. function testSetPop() {
  458. $set = RC::setAddAndReturn($this->redis, 'set', array(0, 1, 2, 3, 4));
  459. $this->assertTrue(in_array($this->redis->setPop('set'), $set));
  460. $this->assertNull($this->redis->setPop('setDoesNotExist'));
  461. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, p_anon("\$test", "
  462. \$test->redis->set('foo', 'bar');
  463. \$test->redis->setPop('foo');
  464. "));
  465. }
  466. function testSetMove() {
  467. $setA = RC::setAddAndReturn($this->redis, 'setA', array(0, 1, 2, 3, 4, 5));
  468. $setB = RC::setAddAndReturn($this->redis, 'setB', array(5, 6, 7, 8, 9, 10));
  469. $this->assertTrue($this->redis->setMove('setA', 'setB', 0));
  470. $this->assertFalse($this->redis->setRemove('setA', 0));
  471. $this->assertTrue($this->redis->setRemove('setB', 0));
  472. $this->assertTrue($this->redis->setMove('setA', 'setB', 5));
  473. $this->assertFalse($this->redis->setMove('setA', 'setB', 100));
  474. // wrong type
  475. $this->redis->set('foo', 'bar');
  476. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, p_anon("\$test", "
  477. \$test->redis->setMove('foo', 'setB', 5);
  478. "));
  479. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, p_anon("\$test", "
  480. \$test->redis->setMove('setA', 'foo', 5);
  481. "));
  482. }
  483. function testSetCardinality() {
  484. RC::setAddAndReturn($this->redis, 'setA', array(0, 1, 2, 3, 4, 5));
  485. $this->assertEquals(6, $this->redis->setCardinality('setA'));
  486. // empty set
  487. $this->redis->setAdd('setB', 0);
  488. $this->redis->setPop('setB');
  489. $this->assertEquals(0, $this->redis->setCardinality('setB'));
  490. // non-existing set
  491. $this->assertEquals(0, $this->redis->setCardinality('setDoesNotExist'));
  492. // wrong type
  493. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, p_anon("\$test", "
  494. \$test->redis->set('foo', 'bar');
  495. \$test->redis->setCardinality('foo');
  496. "));
  497. }
  498. function testSetIsMember() {
  499. RC::setAddAndReturn($this->redis, 'set', array(0, 1, 2, 3, 4, 5));
  500. $this->assertTrue($this->redis->setIsMember('set', 3));
  501. $this->assertFalse($this->redis->setIsMember('set', 100));
  502. $this->assertFalse($this->redis->setIsMember('setDoesNotExist', 0));
  503. // wrong type
  504. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, p_anon("\$test", "
  505. \$test->redis->set('foo', 'bar');
  506. \$test->redis->setIsMember('foo', 0);
  507. "));
  508. }
  509. function testSetMembers() {
  510. $set = RC::setAddAndReturn($this->redis, 'set', array(0, 1, 2, 3, 4, 5, 6));
  511. $this->assertTrue(RC::sameValuesInArrays($set, $this->redis->setMembers('set')));
  512. $this->assertNull($this->redis->setMembers('setDoesNotExist'));
  513. // wrong type
  514. $this->redis->set('foo', 'bar');
  515. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, p_anon("\$test", "
  516. \$test->redis->setMembers('foo');
  517. "));
  518. }
  519. function testSetIntersection() {
  520. $setA = RC::setAddAndReturn($this->redis, 'setA', array(0, 1, 2, 3, 4, 5, 6));
  521. $setB = RC::setAddAndReturn($this->redis, 'setB', array(1, 3, 4, 6, 9, 10));
  522. $this->assertTrue(RC::sameValuesInArrays(
  523. $setA,
  524. $this->redis->setIntersection('setA')
  525. ));
  526. $this->assertTrue(RC::sameValuesInArrays(
  527. array_intersect($setA, $setB),
  528. $this->redis->setIntersection('setA', 'setB')
  529. ));
  530. // TODO: should nil really be considered an empty set?
  531. $this->assertNull($this->redis->setIntersection('setA', 'setDoesNotExist'));
  532. // wrong type
  533. $this->redis->set('foo', 'bar');
  534. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, p_anon("\$test", "
  535. \$test->redis->setIntersection('foo');
  536. "));
  537. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, p_anon("\$test", "
  538. \$test->redis->setIntersection('setA', 'foo');
  539. "));
  540. }
  541. function testSetIntersectionStore() {
  542. $setA = RC::setAddAndReturn($this->redis, 'setA', array(0, 1, 2, 3, 4, 5, 6));
  543. $setB = RC::setAddAndReturn($this->redis, 'setB', array(1, 3, 4, 6, 9, 10));
  544. $this->assertEquals(count($setA), $this->redis->setIntersectionStore('setC', 'setA'));
  545. $this->assertTrue(RC::sameValuesInArrays(
  546. $setA,
  547. $this->redis->setMembers('setC')
  548. ));
  549. $this->redis->delete('setC');
  550. $this->assertEquals(4, $this->redis->setIntersectionStore('setC', 'setA', 'setB'));
  551. $this->assertTrue(RC::sameValuesInArrays(
  552. array(1, 3, 4, 6),
  553. $this->redis->setMembers('setC')
  554. ));
  555. $this->redis->delete('setC');
  556. $this->assertNull($this->redis->setIntersection('setC', 'setDoesNotExist'));
  557. $this->assertFalse($this->redis->exists('setC'));
  558. // existing keys are replaced by SINTERSTORE
  559. $this->redis->set('foo', 'bar');
  560. $this->assertEquals(count($setA), $this->redis->setIntersectionStore('foo', 'setA'));
  561. // wrong type
  562. $this->redis->set('foo', 'bar');
  563. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, p_anon("\$test", "
  564. \$test->redis->setIntersectionStore('setA', 'foo');
  565. "));
  566. }
  567. function testSetUnion() {
  568. $setA = RC::setAddAndReturn($this->redis, 'setA', array(0, 1, 2, 3, 4, 5, 6));
  569. $setB = RC::setAddAndReturn($this->redis, 'setB', array(1, 3, 4, 6, 9, 10));
  570. $this->assertTrue(RC::sameValuesInArrays(
  571. $setA,
  572. $this->redis->setUnion('setA')
  573. ));
  574. $this->assertTrue(RC::sameValuesInArrays(
  575. array_union($setA, $setB),
  576. $this->redis->setUnion('setA', 'setB')
  577. ));
  578. $this->assertTrue(RC::sameValuesInArrays(
  579. $setA,
  580. $this->redis->setUnion('setA', 'setDoesNotExist')
  581. ));
  582. // wrong type
  583. $this->redis->set('foo', 'bar');
  584. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, p_anon("\$test", "
  585. \$test->redis->setUnion('foo');
  586. "));
  587. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, p_anon("\$test", "
  588. \$test->redis->setUnion('setA', 'foo');
  589. "));
  590. }
  591. function testSetUnionStore() {
  592. $setA = RC::setAddAndReturn($this->redis, 'setA', array(0, 1, 2, 3, 4, 5, 6));
  593. $setB = RC::setAddAndReturn($this->redis, 'setB', array(1, 3, 4, 6, 9, 10));
  594. $this->assertEquals(count($setA), $this->redis->setUnionStore('setC', 'setA'));
  595. $this->assertTrue(RC::sameValuesInArrays(
  596. $setA,
  597. $this->redis->setMembers('setC')
  598. ));
  599. $this->redis->delete('setC');
  600. $this->assertEquals(9, $this->redis->setUnionStore('setC', 'setA', 'setB'));
  601. $this->assertTrue(RC::sameValuesInArrays(
  602. array_union($setA, $setB),
  603. $this->redis->setMembers('setC')
  604. ));
  605. // non-existing keys are considered empty sets
  606. $this->redis->delete('setC');
  607. $this->assertEquals(0, $this->redis->setUnionStore('setC', 'setDoesNotExist'));
  608. $this->assertTrue($this->redis->exists('setC'));
  609. $this->assertEquals(0, $this->redis->setCardinality('setC'));
  610. // existing keys are replaced by SUNIONSTORE
  611. $this->redis->set('foo', 'bar');
  612. $this->assertEquals(count($setA), $this->redis->setUnionStore('foo', 'setA'));
  613. // wrong type
  614. $this->redis->set('foo', 'bar');
  615. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, p_anon("\$test", "
  616. \$test->redis->setUnionStore('setA', 'foo');
  617. "));
  618. }
  619. function testSetDifference() {
  620. $setA = RC::setAddAndReturn($this->redis, 'setA', array(0, 1, 2, 3, 4, 5, 6));
  621. $setB = RC::setAddAndReturn($this->redis, 'setB', array(1, 3, 4, 6, 9, 10));
  622. $this->assertTrue(RC::sameValuesInArrays(
  623. $setA,
  624. $this->redis->setDifference('setA')
  625. ));
  626. $this->assertTrue(RC::sameValuesInArrays(
  627. array_diff($setA, $setB),
  628. $this->redis->setDifference('setA', 'setB')
  629. ));
  630. $this->assertTrue(RC::sameValuesInArrays(
  631. $setA,
  632. $this->redis->setDifference('setA', 'setDoesNotExist')
  633. ));
  634. // wrong type
  635. $this->redis->set('foo', 'bar');
  636. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, p_anon("\$test", "
  637. \$test->redis->setDifference('foo');
  638. "));
  639. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, p_anon("\$test", "
  640. \$test->redis->setDifference('setA', 'foo');
  641. "));
  642. }
  643. function testSetDifferenceStore() {
  644. $setA = RC::setAddAndReturn($this->redis, 'setA', array(0, 1, 2, 3, 4, 5, 6));
  645. $setB = RC::setAddAndReturn($this->redis, 'setB', array(1, 3, 4, 6, 9, 10));
  646. $this->assertEquals(count($setA), $this->redis->setDifferenceStore('setC', 'setA'));
  647. $this->assertTrue(RC::sameValuesInArrays(
  648. $setA,
  649. $this->redis->setMembers('setC')
  650. ));
  651. $this->redis->delete('setC');
  652. $this->assertEquals(3, $this->redis->setDifferenceStore('setC', 'setA', 'setB'));
  653. $this->assertTrue(RC::sameValuesInArrays(
  654. array_diff($setA, $setB),
  655. $this->redis->setMembers('setC')
  656. ));
  657. // non-existing keys are considered empty sets
  658. $this->redis->delete('setC');
  659. $this->assertEquals(0, $this->redis->setDifferenceStore('setC', 'setDoesNotExist'));
  660. $this->assertTrue($this->redis->exists('setC'));
  661. $this->assertEquals(0, $this->redis->setCardinality('setC'));
  662. // existing keys are replaced by SDIFFSTORE
  663. $this->redis->set('foo', 'bar');
  664. $this->assertEquals(count($setA), $this->redis->setDifferenceStore('foo', 'setA'));
  665. // wrong type
  666. $this->redis->set('foo', 'bar');
  667. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, p_anon("\$test", "
  668. \$test->redis->setDifferenceStore('setA', 'foo');
  669. "));
  670. }
  671. function testRandomMember() {
  672. $set = RC::setAddAndReturn($this->redis, 'set', array(0, 1, 2, 3, 4, 5, 6));
  673. $this->assertTrue(in_array($this->redis->setRandomMember('set'), $set));
  674. $this->assertNull($this->redis->setRandomMember('setDoesNotExist'));
  675. // wrong type
  676. $this->redis->set('foo', 'bar');
  677. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, p_anon("\$test", "
  678. \$test->redis->setRandomMember('foo');
  679. "));
  680. }
  681. /* commands operating on sorted sets */
  682. function testZsetAdd() {
  683. $this->assertTrue($this->redis->zsetAdd('zset', 0, 'a'));
  684. $this->assertTrue($this->redis->zsetAdd('zset', 1, 'b'));
  685. $this->assertTrue($this->redis->zsetAdd('zset', -1, 'c'));
  686. // TODO: floats?
  687. //$this->assertTrue($this->redis->zsetAdd('zset', -1.0, 'c'));
  688. //$this->assertTrue($this->redis->zsetAdd('zset', -1.0, 'c'));
  689. $this->assertFalse($this->redis->zsetAdd('zset', 2, 'b'));
  690. $this->assertFalse($this->redis->zsetAdd('zset', -2, 'b'));
  691. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, p_anon("\$test", "
  692. \$test->redis->set('foo', 'bar');
  693. \$test->redis->zsetAdd('foo', 0, 'a');
  694. "));
  695. }
  696. function testZsetIncrementBy() {
  697. $this->assertEquals(1, $this->redis->zsetIncrementBy('zsetDoesNotExist', 1, 'foo'));
  698. $this->assertEquals('zset', $this->redis->type('zsetDoesNotExist'));
  699. RC::zsetAddAndReturn($this->redis, 'zset', RC::getZSetArray());
  700. $this->assertEquals(-5, $this->redis->zsetIncrementBy('zset', 5, 'a'));
  701. $this->assertEquals(1, $this->redis->zsetIncrementBy('zset', 1, 'b'));
  702. $this->assertEquals(10, $this->redis->zsetIncrementBy('zset', 0, 'c'));
  703. $this->assertEquals(0, $this->redis->zsetIncrementBy('zset', -20, 'd'));
  704. $this->assertEquals(2, $this->redis->zsetIncrementBy('zset', 2, 'd'));
  705. $this->assertEquals(-10, $this->redis->zsetIncrementBy('zset', -30, 'e'));
  706. $this->assertEquals(1, $this->redis->zsetIncrementBy('zset', 1, 'x'));
  707. // wrong type
  708. $this->redis->set('foo', 'bar');
  709. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, p_anon("\$test", "
  710. \$test->redis->zsetIncrementBy('foo', 1, 'a');
  711. "));
  712. }
  713. function testZsetRemove() {
  714. RC::zsetAddAndReturn($this->redis, 'zset', RC::getZSetArray());
  715. $this->assertTrue($this->redis->zsetRemove('zset', 'a'));
  716. $this->assertFalse($this->redis->zsetRemove('zset', 'x'));
  717. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, p_anon("\$test", "
  718. \$test->redis->set('foo', 'bar');
  719. \$test->redis->zsetRemove('foo', 'bar');
  720. "));
  721. }
  722. function testZsetRange() {
  723. $zset = RC::zsetAddAndReturn($this->redis, 'zset', RC::getZSetArray());
  724. $this->assertEquals(
  725. array_slice(array_keys($zset), 0, 4),
  726. $this->redis->zsetRange('zset', 0, 3)
  727. );
  728. $this->assertEquals(
  729. array_slice(array_keys($zset), 0, 1),
  730. $this->redis->zsetRange('zset', 0, 0)
  731. );
  732. $this->assertEquals(
  733. array(),
  734. $this->redis->zsetRange('zset', 1, 0)
  735. );
  736. $this->assertEquals(
  737. array_values(array_keys($zset)),
  738. $this->redis->zsetRange('zset', 0, -1)
  739. );
  740. $this->assertEquals(
  741. array_slice(array_keys($zset), 3, 1),
  742. $this->redis->zsetRange('zset', 3, -3)
  743. );
  744. $this->assertEquals(
  745. array(),
  746. $this->redis->zsetRange('zset', 5, -3)
  747. );
  748. $this->assertEquals(
  749. array_slice(array_keys($zset), -5, -1),
  750. $this->redis->zsetRange('zset', -5, -2)
  751. );
  752. $this->assertEquals(
  753. array_values(array_keys($zset)),
  754. $this->redis->zsetRange('zset', -100, 100)
  755. );
  756. $this->assertEquals(
  757. array_values(array_keys($zset)),
  758. $this->redis->zsetRange('zset', -100, 100)
  759. );
  760. $this->assertEquals(
  761. array(array('a', -10), array('b', 0), array('c', 10)),
  762. $this->redis->zsetRange('zset', 0, 2, 'withscores')
  763. );
  764. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, p_anon("\$test", "
  765. \$test->redis->set('foo', 'bar');
  766. \$test->redis->zsetRange('foo', 0, -1);
  767. "));
  768. }
  769. function testZsetReverseRange() {
  770. $zset = RC::zsetAddAndReturn($this->redis, 'zset', RC::getZSetArray());
  771. $this->assertEquals(
  772. array_slice(array_reverse(array_keys($zset)), 0, 4),
  773. $this->redis->zsetReverseRange('zset', 0, 3)
  774. );
  775. $this->assertEquals(
  776. array_slice(array_reverse(array_keys($zset)), 0, 1),
  777. $this->redis->zsetReverseRange('zset', 0, 0)
  778. );
  779. $this->assertEquals(
  780. array(),
  781. $this->redis->zsetReverseRange('zset', 1, 0)
  782. );
  783. $this->assertEquals(
  784. array_values(array_reverse(array_keys($zset))),
  785. $this->redis->zsetReverseRange('zset', 0, -1)
  786. );
  787. $this->assertEquals(
  788. array_slice(array_reverse(array_keys($zset)), 3, 1),
  789. $this->redis->zsetReverseRange('zset', 3, -3)
  790. );
  791. $this->assertEquals(
  792. array(),
  793. $this->redis->zsetReverseRange('zset', 5, -3)
  794. );
  795. $this->assertEquals(
  796. array_slice(array_reverse(array_keys($zset)), -5, -1),
  797. $this->redis->zsetReverseRange('zset', -5, -2)
  798. );
  799. $this->assertEquals(
  800. array_values(array_reverse(array_keys($zset))),
  801. $this->redis->zsetReverseRange('zset', -100, 100)
  802. );
  803. $this->assertEquals(
  804. array(array('f', 30), array('e', 20), array('d', 20)),
  805. $this->redis->zsetReverseRange('zset', 0, 2, 'withscores')
  806. );
  807. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, p_anon("\$test", "
  808. \$test->redis->set('foo', 'bar');
  809. \$test->redis->zsetReverseRange('foo', 0, -1);
  810. "));
  811. }
  812. function testZsetRangeByScore() {
  813. $zset = RC::zsetAddAndReturn($this->redis, 'zset', RC::getZSetArray());
  814. $this->assertEquals(
  815. array('a'),
  816. $this->redis->zsetRangeByScore('zset', -10, -10)
  817. );
  818. $this->assertEquals(
  819. array('c', 'd', 'e', 'f'),
  820. $this->redis->zsetRangeByScore('zset', 10, 30)
  821. );
  822. $this->assertEquals(
  823. array('d', 'e'),
  824. $this->redis->zsetRangeByScore('zset', 20, 20)
  825. );
  826. $this->assertEquals(
  827. array(),
  828. $this->redis->zsetRangeByScore('zset', 30, 0)
  829. );
  830. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, p_anon("\$test", "
  831. \$test->redis->set('foo', 'bar');
  832. \$test->redis->zsetRangeByScore('foo', 0, 0);
  833. "));
  834. }
  835. function testZsetCardinality() {
  836. $zset = RC::zsetAddAndReturn($this->redis, 'zset', RC::getZSetArray());
  837. $this->assertEquals(count($zset), $this->redis->zsetCardinality('zset'));
  838. $this->redis->zsetRemove('zset', 'a');
  839. $this->assertEquals(count($zset) - 1, $this->redis->zsetCardinality('zset'));
  840. // empty zset
  841. $this->redis->zsetAdd('zsetB', 0, 'a');
  842. $this->redis->zsetRemove('zsetB', 'a');
  843. $this->assertEquals(0, $this->redis->zsetCardinality('setB'));
  844. // non-existing zset
  845. $this->assertEquals(0, $this->redis->zsetCardinality('zsetDoesNotExist'));
  846. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, p_anon("\$test", "
  847. \$test->redis->set('foo', 'bar');
  848. \$test->redis->zsetCardinality('foo');
  849. "));
  850. }
  851. function testZsetScore() {
  852. $zset = RC::zsetAddAndReturn($this->redis, 'zset', RC::getZSetArray());
  853. $this->assertEquals(-10, $this->redis->zsetScore('zset', 'a'));
  854. $this->assertEquals(10, $this->redis->zsetScore('zset', 'c'));
  855. $this->assertEquals(20, $this->redis->zsetScore('zset', 'e'));
  856. $this->assertNull($this->redis->zsetScore('zset', 'x'));
  857. $this->assertNull($this->redis->zsetScore('zsetDoesNotExist', 'a'));
  858. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, p_anon("\$test", "
  859. \$test->redis->set('foo', 'bar');
  860. \$test->redis->zsetScore('foo', 'bar');
  861. "));
  862. }
  863. function testZsetRemoveRangeByScore() {
  864. $zset = RC::zsetAddAndReturn($this->redis, 'zset', RC::getZSetArray());
  865. $this->assertEquals(2, $this->redis->zsetRemoveRangeByScore('zset', -10, 0));
  866. $this->assertEquals(
  867. array('c', 'd', 'e', 'f'),
  868. $this->redis->zsetRange('zset', 0, -1)
  869. );
  870. $this->assertEquals(1, $this->redis->zsetRemoveRangeByScore('zset', 10, 10));
  871. $this->assertEquals(
  872. array('d', 'e', 'f'),
  873. $this->redis->zsetRange('zset', 0, -1)
  874. );
  875. $this->assertEquals(0, $this->redis->zsetRemoveRangeByScore('zset', 100, 100));
  876. $this->assertEquals(3, $this->redis->zsetRemoveRangeByScore('zset', 0, 100));
  877. $this->assertEquals(0, $this->redis->zsetRemoveRangeByScore('zset', 0, 100));
  878. $this->assertEquals(0, $this->redis->zsetRemoveRangeByScore('zsetDoesNotExist', 0, 100));
  879. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, p_anon("\$test", "
  880. \$test->redis->set('foo', 'bar');
  881. \$test->redis->zsetRemoveRangeByScore('foo', 0, 0);
  882. "));
  883. }
  884. /* multiple databases handling commands */
  885. function testSelectDatabase() {
  886. $this->assertTrue($this->redis->selectDatabase(0));
  887. $this->assertTrue($this->redis->selectDatabase(RC::DEFAULT_DATABASE));
  888. RC::testForServerException($this, RC::EXCEPTION_INVALID_DB_IDX, p_anon("\$test", "
  889. \$test->redis->selectDatabase(32);
  890. "));
  891. RC::testForServerException($this, RC::EXCEPTION_INVALID_DB_IDX, p_anon("\$test", "
  892. \$test->redis->selectDatabase(-1);
  893. "));
  894. }
  895. function testMove() {
  896. // TODO: This test sucks big time. Period.
  897. $otherDb = 5;
  898. $this->redis->set('foo', 'bar');
  899. $this->redis->selectDatabase($otherDb);
  900. $this->redis->flushDatabase();
  901. $this->redis->selectDatabase(RC::DEFAULT_DATABASE);
  902. $this->assertTrue($this->redis->move('foo', $otherDb));
  903. $this->assertFalse($this->redis->move('foo', $otherDb));
  904. $this->assertFalse($this->redis->move('keyDoesNotExist', $otherDb));
  905. $this->redis->set('hoge', 'piyo');
  906. // TODO: shouldn't Redis send an EXCEPTION_INVALID_DB_IDX instead of EXCEPTION_OUT_OF_RANGE?
  907. RC::testForServerException($this, RC::EXCEPTION_OUT_OF_RANGE, p_anon("\$test", "
  908. \$test->redis->move('hoge', 32);
  909. "));
  910. }
  911. function testFlushDatabase() {
  912. $this->assertTrue($this->redis->flushDatabase());
  913. }
  914. function testFlushDatabases() {
  915. $this->assertTrue($this->redis->flushDatabases());
  916. }
  917. /* sorting */
  918. function testSort() {
  919. $unorderedList = RC::pushTailAndReturn($this->redis, 'unordered', array(2, 100, 3, 1, 30, 10));
  920. // without parameters
  921. $this->assertEquals(array(1, 2, 3, 10, 30, 100), $this->redis->sort('unordered'));
  922. // with parameter ASC/DESC
  923. $this->assertEquals(
  924. array(100, 30, 10, 3, 2, 1),
  925. $this->redis->sort('unordered', array(
  926. 'sort' => 'desc'
  927. ))
  928. );
  929. // with parameter LIMIT
  930. $this->assertEquals(
  931. array(1, 2, 3),
  932. $this->redis->sort('unordered', array(
  933. 'limit' => array(0, 3)
  934. ))
  935. );
  936. $this->assertEquals(
  937. array(10, 30),
  938. $this->redis->sort('unordered', array(
  939. 'limit' => array(3, 2)
  940. ))
  941. );
  942. // with parameter ALPHA
  943. $this->assertEquals(
  944. array(1, 10, 100, 2, 3, 30),
  945. $this->redis->sort('unordered', array(
  946. 'alpha' => true
  947. ))
  948. );
  949. // with combined parameters
  950. $this->assertEquals(
  951. array(30, 10, 3, 2),
  952. $this->redis->sort('unordered', array(
  953. 'alpha' => false,
  954. 'sort' => 'desc',
  955. 'limit' => array(1, 4)
  956. ))
  957. );
  958. // with parameter ALPHA
  959. $this->assertEquals(
  960. array(1, 10, 100, 2, 3, 30),
  961. $this->redis->sort('unordered', array(
  962. 'alpha' => true
  963. ))
  964. );
  965. // with parameter STORE
  966. $this->assertEquals(
  967. count($unorderedList),
  968. $this->redis->sort('unordered', array(
  969. 'store' => 'ordered'
  970. ))
  971. );
  972. $this->assertEquals(array(1, 2, 3, 10, 30, 100), $this->redis->listRange('ordered', 0, -1));
  973. // wront type
  974. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, p_anon("\$test", "
  975. \$test->redis->set('foo', 'bar');
  976. \$test->redis->sort('foo');
  977. "));
  978. }
  979. /* remote server control commands */
  980. function testInfo() {
  981. $serverInfo = $this->redis->info();
  982. $this->assertType('array', $serverInfo);
  983. $this->assertNotNull($serverInfo['redis_version']);
  984. $this->assertGreaterThan(0, $serverInfo['uptime_in_seconds']);
  985. $this->assertGreaterThan(0, $serverInfo['total_connections_received']);
  986. }
  987. function testSlaveOf() {
  988. $masterHost = 'www.google.com';
  989. $masterPort = 80;
  990. $this->assertTrue($this->redis->slaveOf($masterHost, $masterPort));
  991. $serverInfo = $this->redis->info();
  992. $this->assertEquals('slave', $serverInfo['role']);
  993. $this->assertEquals($masterHost, $serverInfo['master_host']);
  994. $this->assertEquals($masterPort, $serverInfo['master_port']);
  995. // slave of NO ONE, the implicit way
  996. $this->assertTrue($this->redis->slaveOf());
  997. $serverInfo = $this->redis->info();
  998. $this->assertEquals('master', $serverInfo['role']);
  999. // slave of NO ONE, the explicit way
  1000. $this->assertTrue($this->redis->slaveOf('NO ONE'));
  1001. }
  1002. /* persistence control commands */
  1003. function testSave() {
  1004. $this->assertTrue($this->redis->save());
  1005. }
  1006. function testBackgroundSave() {
  1007. $this->assertTrue($this->redis->backgroundSave());
  1008. }
  1009. function testLastSave() {
  1010. $this->assertGreaterThan(0, $this->redis->lastSave());
  1011. }
  1012. function testShutdown() {
  1013. // TODO: seriously, we even test shutdown?
  1014. /*
  1015. $this->assertNull($this->redis->shutdown());
  1016. sleep(1);
  1017. $this->assertFalse($this->redis->isConnected());
  1018. */
  1019. }
  1020. }
  1021. ?>