RedisCommandsTest.php 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457
  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, 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->pushTail('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->setPreserve('foo', 'bar'));
  88. $this->assertFalse($this->redis->setPreserve('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->setMultiple($multi));
  95. $multiRet = $this->redis->getMultiple(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->setMultiple('a', 1, 'b', 2, 'c', 3));
  99. $this->assertEquals(array(1, 2, 3), $this->redis->getMultiple('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->setMultiple($multi);
  108. $this->assertTrue($this->redis->setMultiplePreserve($newpair));
  109. $this->assertEquals(
  110. array_values($expectedResult),
  111. $this->redis->getMultiple(array_keys($expectedResult))
  112. );
  113. $this->redis->flushDatabase();
  114. // unsuccessful set
  115. $expectedResult = array_merge($multi, array('hogehoge' => null));
  116. $this->redis->setMultiple($multi);
  117. $this->assertFalse($this->redis->setMultiplePreserve(array_merge($newpair, $hijacked)));
  118. $this->assertEquals(
  119. array_values($expectedResult),
  120. $this->redis->getMultiple(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->increment('foo'));
  131. $this->assertEquals(2, $this->redis->increment('foo'));
  132. // test subsequent incrementBy commands
  133. $this->assertEquals(22, $this->redis->incrementBy('foo', 20));
  134. $this->assertEquals(10, $this->redis->incrementBy('foo', -12));
  135. $this->assertEquals(-100, $this->redis->incrementBy('foo', -110));
  136. }
  137. function testDecrementAndDecrementBy() {
  138. // test subsequent decrement commands
  139. $this->assertEquals(-1, $this->redis->decrement('foo'));
  140. $this->assertEquals(-2, $this->redis->decrement('foo'));
  141. // test subsequent decrementBy commands
  142. $this->assertEquals(-22, $this->redis->decrementBy('foo', 20));
  143. $this->assertEquals(-10, $this->redis->decrementBy('foo', -12));
  144. $this->assertEquals(100, $this->redis->decrementBy('foo', -110));
  145. }
  146. function testDelete() {
  147. $this->redis->set('foo', 'bar');
  148. $this->assertTrue($this->redis->delete('foo'));
  149. $this->assertFalse($this->redis->exists('foo'));
  150. $this->assertFalse($this->redis->delete('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->pushTail('fooList', 'bar');
  157. $this->assertEquals('list', $this->redis->type('fooList'));
  158. $this->redis->setAdd('fooSet', 'bar');
  159. $this->assertEquals('set', $this->redis->type('fooSet'));
  160. $this->redis->zsetAdd('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->pushTail('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->pushTail('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->setMultiple($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->setMultiple($keyvals);
  206. $this->assertTrue(in_array($this->redis->randomKey(), array_keys($keyvals)));
  207. }
  208. function testRename() {
  209. $this->redis->setMultiple(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->setMultiple(array('foo' => 'bar', 'hoge' => 'piyo', 'hogehoge' => 'piyopiyo'));
  221. $this->assertTrue($this->redis->renamePreserve('foo', 'foofoo'));
  222. $this->assertFalse($this->redis->exists('foo'));
  223. $this->assertEquals('bar', $this->redis->get('foofoo'));
  224. $this->assertFalse($this->redis->renamePreserve('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->renamePreserve('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 testDatabaseSize() {
  252. // TODO: is this really OK?
  253. $this->assertEquals(0, $this->redis->databaseSize());
  254. $this->redis->setMultiple(RC::getKeyValueArray());
  255. $this->assertGreaterThan(0, $this->redis->databaseSize());
  256. }
  257. /* commands operating on lists */
  258. function testPushTail() {
  259. // NOTE: List push operations return the list length since Redis commit 520b5a3
  260. $this->assertEquals(1, $this->redis->pushTail('metavars', 'foo'));
  261. $this->assertTrue($this->redis->exists('metavars'));
  262. $this->assertEquals(2, $this->redis->pushTail('metavars', 'hoge'));
  263. // should throw an exception when trying to do a RPUSH on non-list types
  264. // should throw an exception when trying to do a LPUSH on non-list types
  265. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
  266. $test->redis->set('foo', 'bar');
  267. $test->redis->pushTail('foo', 'bar');
  268. });
  269. }
  270. function testPushHead() {
  271. // NOTE: List push operations return the list length since Redis commit 520b5a3
  272. $this->assertEquals(1, $this->redis->pushHead('metavars', 'foo'));
  273. $this->assertTrue($this->redis->exists('metavars'));
  274. $this->assertEquals(2, $this->redis->pushHead('metavars', 'hoge'));
  275. // should throw an exception when trying to do a LPUSH on non-list types
  276. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
  277. $test->redis->set('foo', 'bar');
  278. $test->redis->pushHead('foo', 'bar');
  279. });
  280. }
  281. function testListLength() {
  282. $this->assertEquals(1, $this->redis->pushTail('metavars', 'foo'));
  283. $this->assertEquals(2, $this->redis->pushTail('metavars', 'hoge'));
  284. $this->assertEquals(2, $this->redis->listLength('metavars'));
  285. $this->assertEquals(0, $this->redis->listLength('doesnotexist'));
  286. // should throw an exception when trying to do a LLEN on non-list types
  287. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
  288. $test->redis->set('foo', 'bar');
  289. $test->redis->listLength('foo');
  290. });
  291. }
  292. function testListRange() {
  293. $numbers = RC::pushTailAndReturn($this->redis, 'numbers', RC::getArrayOfNumbers());
  294. $this->assertEquals(
  295. array_slice($numbers, 0, 4),
  296. $this->redis->listRange('numbers', 0, 3)
  297. );
  298. $this->assertEquals(
  299. array_slice($numbers, 4, 5),
  300. $this->redis->listRange('numbers', 4, 8)
  301. );
  302. $this->assertEquals(
  303. array_slice($numbers, 0, 1),
  304. $this->redis->listRange('numbers', 0, 0)
  305. );
  306. $this->assertEquals(
  307. array(),
  308. $this->redis->listRange('numbers', 1, 0)
  309. );
  310. $this->assertEquals(
  311. $numbers,
  312. $this->redis->listRange('numbers', 0, -1)
  313. );
  314. $this->assertEquals(
  315. array(5),
  316. $this->redis->listRange('numbers', 5, -5)
  317. );
  318. $this->assertEquals(
  319. array(),
  320. $this->redis->listRange('numbers', 7, -5)
  321. );
  322. $this->assertEquals(
  323. array_slice($numbers, -5, -1),
  324. $this->redis->listRange('numbers', -5, -2)
  325. );
  326. $this->assertEquals(
  327. $numbers,
  328. $this->redis->listRange('numbers', -100, 100)
  329. );
  330. $this->assertEquals(
  331. array(),
  332. $this->redis->listRange('keyDoesNotExist', 0, 1)
  333. );
  334. // should throw an exception when trying to do a LRANGE on non-list types
  335. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
  336. $test->redis->set('foo', 'bar');
  337. $test->redis->listRange('foo', 0, -1);
  338. });
  339. }
  340. function testListTrim() {
  341. $numbers = RC::pushTailAndReturn($this->redis, 'numbers', RC::getArrayOfNumbers());
  342. $this->assertTrue($this->redis->listTrim('numbers', 0, 2));
  343. $this->assertEquals(
  344. array_slice($numbers, 0, 3),
  345. $this->redis->listRange('numbers', 0, -1)
  346. );
  347. $numbers = RC::pushTailAndReturn($this->redis, 'numbers', RC::getArrayOfNumbers(), RC::WIPE_OUT);
  348. $this->assertTrue($this->redis->listTrim('numbers', 5, 9));
  349. $this->assertEquals(
  350. array_slice($numbers, 5, 5),
  351. $this->redis->listRange('numbers', 0, -1)
  352. );
  353. $numbers = RC::pushTailAndReturn($this->redis, 'numbers', RC::getArrayOfNumbers(), RC::WIPE_OUT);
  354. $this->assertTrue($this->redis->listTrim('numbers', 0, -6));
  355. $this->assertEquals(
  356. array_slice($numbers, 0, -5),
  357. $this->redis->listRange('numbers', 0, -1)
  358. );
  359. $numbers = RC::pushTailAndReturn($this->redis, 'numbers', RC::getArrayOfNumbers(), RC::WIPE_OUT);
  360. $this->assertTrue($this->redis->listTrim('numbers', -5, -3));
  361. $this->assertEquals(
  362. array_slice($numbers, 5, 3),
  363. $this->redis->listRange('numbers', 0, -1)
  364. );
  365. $numbers = RC::pushTailAndReturn($this->redis, 'numbers', RC::getArrayOfNumbers(), RC::WIPE_OUT);
  366. $this->assertTrue($this->redis->listTrim('numbers', -100, 100));
  367. $this->assertEquals(
  368. $numbers,
  369. $this->redis->listRange('numbers', 0, -1)
  370. );
  371. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
  372. $test->redis->set('foo', 'bar');
  373. $test->redis->listTrim('foo', 0, 1);
  374. });
  375. }
  376. function testListIndex() {
  377. $numbers = RC::pushTailAndReturn($this->redis, 'numbers', RC::getArrayOfNumbers());
  378. $this->assertEquals(0, $this->redis->listIndex('numbers', 0));
  379. $this->assertEquals(5, $this->redis->listIndex('numbers', 5));
  380. $this->assertEquals(9, $this->redis->listIndex('numbers', 9));
  381. $this->assertNull($this->redis->listIndex('numbers', 100));
  382. $this->assertEquals(0, $this->redis->listIndex('numbers', -0));
  383. $this->assertEquals(9, $this->redis->listIndex('numbers', -1));
  384. $this->assertEquals(7, $this->redis->listIndex('numbers', -3));
  385. $this->assertNull($this->redis->listIndex('numbers', -100));
  386. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
  387. $test->redis->set('foo', 'bar');
  388. $test->redis->listIndex('foo', 0);
  389. });
  390. }
  391. function testListSet() {
  392. $numbers = RC::pushTailAndReturn($this->redis, 'numbers', RC::getArrayOfNumbers());
  393. $this->assertTrue($this->redis->listSet('numbers', 5, -5));
  394. $this->assertEquals(-5, $this->redis->listIndex('numbers', 5));
  395. RC::testForServerException($this, RC::EXCEPTION_OUT_OF_RANGE, function($test) {
  396. $test->redis->listSet('numbers', 99, 99);
  397. });
  398. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
  399. $test->redis->set('foo', 'bar');
  400. $test->redis->listSet('foo', 0, 0);
  401. });
  402. }
  403. function testListRemove() {
  404. $mixed = array(0, '_', 2, '_', 4, '_', 6, '_');
  405. RC::pushTailAndReturn($this->redis, 'mixed', $mixed);
  406. $this->assertEquals(2, $this->redis->listRemove('mixed', 2, '_'));
  407. $this->assertEquals(array(0, 2, 4, '_', 6, '_'), $this->redis->listRange('mixed', 0, -1));
  408. RC::pushTailAndReturn($this->redis, 'mixed', $mixed, RC::WIPE_OUT);
  409. $this->assertEquals(4, $this->redis->listRemove('mixed', 0, '_'));
  410. $this->assertEquals(array(0, 2, 4, 6), $this->redis->listRange('mixed', 0, -1));
  411. RC::pushTailAndReturn($this->redis, 'mixed', $mixed, RC::WIPE_OUT);
  412. $this->assertEquals(2, $this->redis->listRemove('mixed', -2, '_'));
  413. $this->assertEquals(array(0, '_', 2, '_', 4, 6), $this->redis->listRange('mixed', 0, -1));
  414. RC::pushTailAndReturn($this->redis, 'mixed', $mixed, RC::WIPE_OUT);
  415. $this->assertEquals(0, $this->redis->listRemove('mixed', 2, '|'));
  416. $this->assertEquals($mixed, $this->redis->listRange('mixed', 0, -1));
  417. $this->assertEquals(0, $this->redis->listRemove('listDoesNotExist', 2, '_'));
  418. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
  419. $test->redis->set('foo', 'bar');
  420. $test->redis->listRemove('foo', 0, 0);
  421. });
  422. }
  423. function testListPopFirst() {
  424. $numbers = RC::pushTailAndReturn($this->redis, 'numbers', array(0, 1, 2, 3, 4));
  425. $this->assertEquals(0, $this->redis->popFirst('numbers'));
  426. $this->assertEquals(1, $this->redis->popFirst('numbers'));
  427. $this->assertEquals(2, $this->redis->popFirst('numbers'));
  428. $this->assertEquals(array(3, 4), $this->redis->listRange('numbers', 0, -1));
  429. $this->redis->popFirst('numbers');
  430. $this->redis->popFirst('numbers');
  431. $this->assertNull($this->redis->popFirst('numbers'));
  432. $this->assertNull($this->redis->popFirst('numbers'));
  433. $this->assertNull($this->redis->popFirst('listDoesNotExist'));
  434. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
  435. $test->redis->set('foo', 'bar');
  436. $test->redis->popFirst('foo');
  437. });
  438. }
  439. function testListPopLast() {
  440. $numbers = RC::pushTailAndReturn($this->redis, 'numbers', array(0, 1, 2, 3, 4));
  441. $this->assertEquals(4, $this->redis->popLast('numbers'));
  442. $this->assertEquals(3, $this->redis->popLast('numbers'));
  443. $this->assertEquals(2, $this->redis->popLast('numbers'));
  444. $this->assertEquals(array(0, 1), $this->redis->listRange('numbers', 0, -1));
  445. $this->redis->popLast('numbers');
  446. $this->redis->popLast('numbers');
  447. $this->assertNull($this->redis->popLast('numbers'));
  448. $this->assertNull($this->redis->popLast('numbers'));
  449. $this->assertNull($this->redis->popLast('listDoesNotExist'));
  450. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
  451. $test->redis->set('foo', 'bar');
  452. $test->redis->popLast('foo');
  453. });
  454. }
  455. function testListPopLastPushHead() {
  456. $numbers = RC::pushTailAndReturn($this->redis, 'numbers', array(0, 1, 2));
  457. $this->assertEquals(0, $this->redis->listLength('temporary'));
  458. $this->assertEquals(2, $this->redis->listPopLastPushHead('numbers', 'temporary'));
  459. $this->assertEquals(1, $this->redis->listPopLastPushHead('numbers', 'temporary'));
  460. $this->assertEquals(0, $this->redis->listPopLastPushHead('numbers', 'temporary'));
  461. $this->assertEquals(0, $this->redis->listLength('numbers'));
  462. $this->assertEquals(3, $this->redis->listLength('temporary'));
  463. $this->assertEquals(array(), $this->redis->listRange('numbers', 0, -1));
  464. $this->assertEquals($numbers, $this->redis->listRange('temporary', 0, -1));
  465. $numbers = RC::pushTailAndReturn($this->redis, 'numbers', array(0, 1, 2));
  466. $this->redis->listPopLastPushHead('numbers', 'numbers');
  467. $this->redis->listPopLastPushHead('numbers', 'numbers');
  468. $this->redis->listPopLastPushHead('numbers', 'numbers');
  469. $this->assertEquals($numbers, $this->redis->listRange('numbers', 0, -1));
  470. $this->assertNull($this->redis->listPopLastPushHead('listDoesNotExist1', 'listDoesNotExist2'));
  471. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
  472. $test->redis->set('foo', 'bar');
  473. $test->redis->listPopLastPushHead('foo', 'hoge');
  474. });
  475. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
  476. $test->redis->set('foo', 'bar');
  477. $test->redis->listPopLastPushHead('temporary', 'foo');
  478. });
  479. }
  480. function testListBlockingPopFirst() {
  481. // TODO: this test does not cover all the aspects of BLPOP/BRPOP as it
  482. // does not run with a concurrent client pushing items on lists.
  483. RC::helperForBlockingPops('blpop');
  484. // BLPOP on one key
  485. $start = time();
  486. $item = $this->redis->blpop('blpop3', 5);
  487. $this->assertEquals((float)(time() - $start), 0, '', 1);
  488. $this->assertEquals($item, array('blpop3', 'c'));
  489. // BLPOP on more than one key
  490. $poppedItems = array();
  491. while ($item = $this->redis->blpop('blpop1', 'blpop2', 1)) {
  492. $poppedItems[] = $item;
  493. }
  494. $this->assertEquals(
  495. array(array('blpop1', 'a'), array('blpop1', 'd'), array('blpop2', 'b')),
  496. $poppedItems
  497. );
  498. // check if BLPOP timeouts as expected on empty lists
  499. $start = time();
  500. $this->redis->blpop('blpop4', 2);
  501. $this->assertEquals((float)(time() - $start), 2, '', 1);
  502. }
  503. function testListBlockingPopLast() {
  504. // TODO: this test does not cover all the aspects of BLPOP/BRPOP as it
  505. // does not run with a concurrent client pushing items on lists.
  506. RC::helperForBlockingPops('brpop');
  507. // BRPOP on one key
  508. $start = time();
  509. $item = $this->redis->brpop('brpop3', 5);
  510. $this->assertEquals((float)(time() - $start), 0, '', 1);
  511. $this->assertEquals($item, array('brpop3', 'c'));
  512. // BRPOP on more than one key
  513. $poppedItems = array();
  514. while ($item = $this->redis->brpop('brpop1', 'brpop2', 1)) {
  515. $poppedItems[] = $item;
  516. }
  517. $this->assertEquals(
  518. array(array('brpop1', 'd'), array('brpop1', 'a'), array('brpop2', 'b')),
  519. $poppedItems
  520. );
  521. // check if BRPOP timeouts as expected on empty lists
  522. $start = time();
  523. $this->redis->brpop('brpop4', 2);
  524. $this->assertEquals((float)(time() - $start), 2, '', 1);
  525. }
  526. /* commands operating on sets */
  527. function testSetAdd() {
  528. $this->assertTrue($this->redis->setAdd('set', 0));
  529. $this->assertTrue($this->redis->setAdd('set', 1));
  530. $this->assertFalse($this->redis->setAdd('set', 0));
  531. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
  532. $test->redis->set('foo', 'bar');
  533. $test->redis->setAdd('foo', 0);
  534. });
  535. }
  536. function testSetRemove() {
  537. $set = RC::setAddAndReturn($this->redis, 'set', array(0, 1, 2, 3, 4));
  538. $this->assertTrue($this->redis->setRemove('set', 0));
  539. $this->assertTrue($this->redis->setRemove('set', 4));
  540. $this->assertFalse($this->redis->setRemove('set', 10));
  541. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
  542. $test->redis->set('foo', 'bar');
  543. $test->redis->setRemove('foo', 0);
  544. });
  545. }
  546. function testSetPop() {
  547. $set = RC::setAddAndReturn($this->redis, 'set', array(0, 1, 2, 3, 4));
  548. $this->assertTrue(in_array($this->redis->setPop('set'), $set));
  549. $this->assertNull($this->redis->setPop('setDoesNotExist'));
  550. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
  551. $test->redis->set('foo', 'bar');
  552. $test->redis->setPop('foo');
  553. });
  554. }
  555. function testSetMove() {
  556. $setA = RC::setAddAndReturn($this->redis, 'setA', array(0, 1, 2, 3, 4, 5));
  557. $setB = RC::setAddAndReturn($this->redis, 'setB', array(5, 6, 7, 8, 9, 10));
  558. $this->assertTrue($this->redis->setMove('setA', 'setB', 0));
  559. $this->assertFalse($this->redis->setRemove('setA', 0));
  560. $this->assertTrue($this->redis->setRemove('setB', 0));
  561. $this->assertTrue($this->redis->setMove('setA', 'setB', 5));
  562. $this->assertFalse($this->redis->setMove('setA', 'setB', 100));
  563. // wrong type
  564. $this->redis->set('foo', 'bar');
  565. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
  566. $test->redis->setMove('foo', 'setB', 5);
  567. });
  568. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
  569. $test->redis->setMove('setA', 'foo', 5);
  570. });
  571. }
  572. function testSetCardinality() {
  573. RC::setAddAndReturn($this->redis, 'setA', array(0, 1, 2, 3, 4, 5));
  574. $this->assertEquals(6, $this->redis->setCardinality('setA'));
  575. // empty set
  576. $this->redis->setAdd('setB', 0);
  577. $this->redis->setPop('setB');
  578. $this->assertEquals(0, $this->redis->setCardinality('setB'));
  579. // non-existing set
  580. $this->assertEquals(0, $this->redis->setCardinality('setDoesNotExist'));
  581. // wrong type
  582. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
  583. $test->redis->set('foo', 'bar');
  584. $test->redis->setCardinality('foo');
  585. });
  586. }
  587. function testSetIsMember() {
  588. RC::setAddAndReturn($this->redis, 'set', array(0, 1, 2, 3, 4, 5));
  589. $this->assertTrue($this->redis->setIsMember('set', 3));
  590. $this->assertFalse($this->redis->setIsMember('set', 100));
  591. $this->assertFalse($this->redis->setIsMember('setDoesNotExist', 0));
  592. // wrong type
  593. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
  594. $test->redis->set('foo', 'bar');
  595. $test->redis->setIsMember('foo', 0);
  596. });
  597. }
  598. function testSetMembers() {
  599. $set = RC::setAddAndReturn($this->redis, 'set', array(0, 1, 2, 3, 4, 5, 6));
  600. $this->assertTrue(RC::sameValuesInArrays($set, $this->redis->setMembers('set')));
  601. $this->assertEquals(array(), $this->redis->setMembers('setDoesNotExist'));
  602. // wrong type
  603. $this->redis->set('foo', 'bar');
  604. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
  605. $test->redis->setMembers('foo');
  606. });
  607. }
  608. function testSetIntersection() {
  609. $setA = RC::setAddAndReturn($this->redis, 'setA', array(0, 1, 2, 3, 4, 5, 6));
  610. $setB = RC::setAddAndReturn($this->redis, 'setB', array(1, 3, 4, 6, 9, 10));
  611. $this->assertTrue(RC::sameValuesInArrays(
  612. $setA,
  613. $this->redis->setIntersection('setA')
  614. ));
  615. $this->assertTrue(RC::sameValuesInArrays(
  616. array_intersect($setA, $setB),
  617. $this->redis->setIntersection('setA', 'setB')
  618. ));
  619. $this->assertEquals(array(), $this->redis->setIntersection('setA', 'setDoesNotExist'));
  620. // wrong type
  621. $this->redis->set('foo', 'bar');
  622. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
  623. $test->redis->setIntersection('foo');
  624. });
  625. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
  626. $test->redis->setIntersection('setA', 'foo');
  627. });
  628. }
  629. function testSetIntersectionStore() {
  630. $setA = RC::setAddAndReturn($this->redis, 'setA', array(0, 1, 2, 3, 4, 5, 6));
  631. $setB = RC::setAddAndReturn($this->redis, 'setB', array(1, 3, 4, 6, 9, 10));
  632. $this->assertEquals(count($setA), $this->redis->setIntersectionStore('setC', 'setA'));
  633. $this->assertTrue(RC::sameValuesInArrays(
  634. $setA,
  635. $this->redis->setMembers('setC')
  636. ));
  637. $this->redis->delete('setC');
  638. $this->assertEquals(4, $this->redis->setIntersectionStore('setC', 'setA', 'setB'));
  639. $this->assertTrue(RC::sameValuesInArrays(
  640. array(1, 3, 4, 6),
  641. $this->redis->setMembers('setC')
  642. ));
  643. $this->redis->delete('setC');
  644. $this->assertEquals(array(), $this->redis->setIntersection('setC', 'setDoesNotExist'));
  645. $this->assertFalse($this->redis->exists('setC'));
  646. // existing keys are replaced by SINTERSTORE
  647. $this->redis->set('foo', 'bar');
  648. $this->assertEquals(count($setA), $this->redis->setIntersectionStore('foo', 'setA'));
  649. // wrong type
  650. $this->redis->set('foo', 'bar');
  651. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
  652. $test->redis->setIntersectionStore('setA', 'foo');
  653. });
  654. }
  655. function testSetUnion() {
  656. $setA = RC::setAddAndReturn($this->redis, 'setA', array(0, 1, 2, 3, 4, 5, 6));
  657. $setB = RC::setAddAndReturn($this->redis, 'setB', array(1, 3, 4, 6, 9, 10));
  658. $this->assertTrue(RC::sameValuesInArrays(
  659. $setA,
  660. $this->redis->setUnion('setA')
  661. ));
  662. $this->assertTrue(RC::sameValuesInArrays(
  663. array_union($setA, $setB),
  664. $this->redis->setUnion('setA', 'setB')
  665. ));
  666. $this->assertTrue(RC::sameValuesInArrays(
  667. $setA,
  668. $this->redis->setUnion('setA', 'setDoesNotExist')
  669. ));
  670. // wrong type
  671. $this->redis->set('foo', 'bar');
  672. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
  673. $test->redis->setUnion('foo');
  674. });
  675. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
  676. $test->redis->setUnion('setA', 'foo');
  677. });
  678. }
  679. function testSetUnionStore() {
  680. $setA = RC::setAddAndReturn($this->redis, 'setA', array(0, 1, 2, 3, 4, 5, 6));
  681. $setB = RC::setAddAndReturn($this->redis, 'setB', array(1, 3, 4, 6, 9, 10));
  682. $this->assertEquals(count($setA), $this->redis->setUnionStore('setC', 'setA'));
  683. $this->assertTrue(RC::sameValuesInArrays(
  684. $setA,
  685. $this->redis->setMembers('setC')
  686. ));
  687. $this->redis->delete('setC');
  688. $this->assertEquals(9, $this->redis->setUnionStore('setC', 'setA', 'setB'));
  689. $this->assertTrue(RC::sameValuesInArrays(
  690. array_union($setA, $setB),
  691. $this->redis->setMembers('setC')
  692. ));
  693. // non-existing keys are considered empty sets
  694. $this->redis->delete('setC');
  695. $this->assertEquals(0, $this->redis->setUnionStore('setC', 'setDoesNotExist'));
  696. $this->assertFalse($this->redis->exists('setC'));
  697. $this->assertEquals(0, $this->redis->setCardinality('setC'));
  698. // existing keys are replaced by SUNIONSTORE
  699. $this->redis->set('foo', 'bar');
  700. $this->assertEquals(count($setA), $this->redis->setUnionStore('foo', 'setA'));
  701. // wrong type
  702. $this->redis->set('foo', 'bar');
  703. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
  704. $test->redis->setUnionStore('setA', 'foo');
  705. });
  706. }
  707. function testSetDifference() {
  708. $setA = RC::setAddAndReturn($this->redis, 'setA', array(0, 1, 2, 3, 4, 5, 6));
  709. $setB = RC::setAddAndReturn($this->redis, 'setB', array(1, 3, 4, 6, 9, 10));
  710. $this->assertTrue(RC::sameValuesInArrays(
  711. $setA,
  712. $this->redis->setDifference('setA')
  713. ));
  714. $this->assertTrue(RC::sameValuesInArrays(
  715. array_diff($setA, $setB),
  716. $this->redis->setDifference('setA', 'setB')
  717. ));
  718. $this->assertTrue(RC::sameValuesInArrays(
  719. $setA,
  720. $this->redis->setDifference('setA', 'setDoesNotExist')
  721. ));
  722. // wrong type
  723. $this->redis->set('foo', 'bar');
  724. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
  725. $test->redis->setDifference('foo');
  726. });
  727. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
  728. $test->redis->setDifference('setA', 'foo');
  729. });
  730. }
  731. function testSetDifferenceStore() {
  732. $setA = RC::setAddAndReturn($this->redis, 'setA', array(0, 1, 2, 3, 4, 5, 6));
  733. $setB = RC::setAddAndReturn($this->redis, 'setB', array(1, 3, 4, 6, 9, 10));
  734. $this->assertEquals(count($setA), $this->redis->setDifferenceStore('setC', 'setA'));
  735. $this->assertTrue(RC::sameValuesInArrays(
  736. $setA,
  737. $this->redis->setMembers('setC')
  738. ));
  739. $this->redis->delete('setC');
  740. $this->assertEquals(3, $this->redis->setDifferenceStore('setC', 'setA', 'setB'));
  741. $this->assertTrue(RC::sameValuesInArrays(
  742. array_diff($setA, $setB),
  743. $this->redis->setMembers('setC')
  744. ));
  745. // non-existing keys are considered empty sets
  746. $this->redis->delete('setC');
  747. $this->assertEquals(0, $this->redis->setDifferenceStore('setC', 'setDoesNotExist'));
  748. $this->assertFalse($this->redis->exists('setC'));
  749. $this->assertEquals(0, $this->redis->setCardinality('setC'));
  750. // existing keys are replaced by SDIFFSTORE
  751. $this->redis->set('foo', 'bar');
  752. $this->assertEquals(count($setA), $this->redis->setDifferenceStore('foo', 'setA'));
  753. // wrong type
  754. $this->redis->set('foo', 'bar');
  755. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
  756. $test->redis->setDifferenceStore('setA', 'foo');
  757. });
  758. }
  759. function testRandomMember() {
  760. $set = RC::setAddAndReturn($this->redis, 'set', array(0, 1, 2, 3, 4, 5, 6));
  761. $this->assertTrue(in_array($this->redis->setRandomMember('set'), $set));
  762. $this->assertNull($this->redis->setRandomMember('setDoesNotExist'));
  763. // wrong type
  764. $this->redis->set('foo', 'bar');
  765. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
  766. $test->redis->setRandomMember('foo');
  767. });
  768. }
  769. /* commands operating on sorted sets */
  770. function testZsetAdd() {
  771. $this->assertTrue($this->redis->zsetAdd('zset', 0, 'a'));
  772. $this->assertTrue($this->redis->zsetAdd('zset', 1, 'b'));
  773. $this->assertTrue($this->redis->zsetAdd('zset', -1, 'c'));
  774. // TODO: floats?
  775. //$this->assertTrue($this->redis->zsetAdd('zset', -1.0, 'c'));
  776. //$this->assertTrue($this->redis->zsetAdd('zset', -1.0, 'c'));
  777. $this->assertFalse($this->redis->zsetAdd('zset', 2, 'b'));
  778. $this->assertFalse($this->redis->zsetAdd('zset', -2, 'b'));
  779. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
  780. $test->redis->set('foo', 'bar');
  781. $test->redis->zsetAdd('foo', 0, 'a');
  782. });
  783. }
  784. function testZsetIncrementBy() {
  785. $this->assertEquals(1, $this->redis->zsetIncrementBy('zsetDoesNotExist', 1, 'foo'));
  786. $this->assertEquals('zset', $this->redis->type('zsetDoesNotExist'));
  787. RC::zsetAddAndReturn($this->redis, 'zset', RC::getZSetArray());
  788. $this->assertEquals(-5, $this->redis->zsetIncrementBy('zset', 5, 'a'));
  789. $this->assertEquals(1, $this->redis->zsetIncrementBy('zset', 1, 'b'));
  790. $this->assertEquals(10, $this->redis->zsetIncrementBy('zset', 0, 'c'));
  791. $this->assertEquals(0, $this->redis->zsetIncrementBy('zset', -20, 'd'));
  792. $this->assertEquals(2, $this->redis->zsetIncrementBy('zset', 2, 'd'));
  793. $this->assertEquals(-10, $this->redis->zsetIncrementBy('zset', -30, 'e'));
  794. $this->assertEquals(1, $this->redis->zsetIncrementBy('zset', 1, 'x'));
  795. // wrong type
  796. $this->redis->set('foo', 'bar');
  797. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
  798. $test->redis->zsetIncrementBy('foo', 1, 'a');
  799. });
  800. }
  801. function testZsetRemove() {
  802. RC::zsetAddAndReturn($this->redis, 'zset', RC::getZSetArray());
  803. $this->assertTrue($this->redis->zsetRemove('zset', 'a'));
  804. $this->assertFalse($this->redis->zsetRemove('zset', 'x'));
  805. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
  806. $test->redis->set('foo', 'bar');
  807. $test->redis->zsetRemove('foo', 'bar');
  808. });
  809. }
  810. function testZsetRange() {
  811. $zset = RC::zsetAddAndReturn($this->redis, 'zset', RC::getZSetArray());
  812. $this->assertEquals(
  813. array_slice(array_keys($zset), 0, 4),
  814. $this->redis->zsetRange('zset', 0, 3)
  815. );
  816. $this->assertEquals(
  817. array_slice(array_keys($zset), 0, 1),
  818. $this->redis->zsetRange('zset', 0, 0)
  819. );
  820. $this->assertEquals(
  821. array(),
  822. $this->redis->zsetRange('zset', 1, 0)
  823. );
  824. $this->assertEquals(
  825. array_values(array_keys($zset)),
  826. $this->redis->zsetRange('zset', 0, -1)
  827. );
  828. $this->assertEquals(
  829. array_slice(array_keys($zset), 3, 1),
  830. $this->redis->zsetRange('zset', 3, -3)
  831. );
  832. $this->assertEquals(
  833. array(),
  834. $this->redis->zsetRange('zset', 5, -3)
  835. );
  836. $this->assertEquals(
  837. array_slice(array_keys($zset), -5, -1),
  838. $this->redis->zsetRange('zset', -5, -2)
  839. );
  840. $this->assertEquals(
  841. array_values(array_keys($zset)),
  842. $this->redis->zsetRange('zset', -100, 100)
  843. );
  844. $this->assertEquals(
  845. array_values(array_keys($zset)),
  846. $this->redis->zsetRange('zset', -100, 100)
  847. );
  848. $this->assertEquals(
  849. array(array('a', -10), array('b', 0), array('c', 10)),
  850. $this->redis->zsetRange('zset', 0, 2, 'withscores')
  851. );
  852. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
  853. $test->redis->set('foo', 'bar');
  854. $test->redis->zsetRange('foo', 0, -1);
  855. });
  856. }
  857. function testZsetReverseRange() {
  858. $zset = RC::zsetAddAndReturn($this->redis, 'zset', RC::getZSetArray());
  859. $this->assertEquals(
  860. array_slice(array_reverse(array_keys($zset)), 0, 4),
  861. $this->redis->zsetReverseRange('zset', 0, 3)
  862. );
  863. $this->assertEquals(
  864. array_slice(array_reverse(array_keys($zset)), 0, 1),
  865. $this->redis->zsetReverseRange('zset', 0, 0)
  866. );
  867. $this->assertEquals(
  868. array(),
  869. $this->redis->zsetReverseRange('zset', 1, 0)
  870. );
  871. $this->assertEquals(
  872. array_values(array_reverse(array_keys($zset))),
  873. $this->redis->zsetReverseRange('zset', 0, -1)
  874. );
  875. $this->assertEquals(
  876. array_slice(array_reverse(array_keys($zset)), 3, 1),
  877. $this->redis->zsetReverseRange('zset', 3, -3)
  878. );
  879. $this->assertEquals(
  880. array(),
  881. $this->redis->zsetReverseRange('zset', 5, -3)
  882. );
  883. $this->assertEquals(
  884. array_slice(array_reverse(array_keys($zset)), -5, -1),
  885. $this->redis->zsetReverseRange('zset', -5, -2)
  886. );
  887. $this->assertEquals(
  888. array_values(array_reverse(array_keys($zset))),
  889. $this->redis->zsetReverseRange('zset', -100, 100)
  890. );
  891. $this->assertEquals(
  892. array(array('f', 30), array('e', 20), array('d', 20)),
  893. $this->redis->zsetReverseRange('zset', 0, 2, 'withscores')
  894. );
  895. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
  896. $test->redis->set('foo', 'bar');
  897. $test->redis->zsetReverseRange('foo', 0, -1);
  898. });
  899. }
  900. function testZsetRangeByScore() {
  901. $zset = RC::zsetAddAndReturn($this->redis, 'zset', RC::getZSetArray());
  902. $this->assertEquals(
  903. array('a'),
  904. $this->redis->zsetRangeByScore('zset', -10, -10)
  905. );
  906. $this->assertEquals(
  907. array('c', 'd', 'e', 'f'),
  908. $this->redis->zsetRangeByScore('zset', 10, 30)
  909. );
  910. $this->assertEquals(
  911. array('d', 'e'),
  912. $this->redis->zsetRangeByScore('zset', 20, 20)
  913. );
  914. $this->assertEquals(
  915. array(),
  916. $this->redis->zsetRangeByScore('zset', 30, 0)
  917. );
  918. $this->assertEquals(
  919. array(array('c', 10), array('d', 20), array('e', 20)),
  920. $this->redis->zsetRangeByScore('zset', 10, 20, 'withscores')
  921. );
  922. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
  923. $test->redis->set('foo', 'bar');
  924. $test->redis->zsetRangeByScore('foo', 0, 0);
  925. });
  926. }
  927. function testZsetCount() {
  928. $zset = RC::zsetAddAndReturn($this->redis, 'zset', RC::getZSetArray());
  929. $this->assertEquals(0, $this->redis->zsetCount('zset', 50, 100));
  930. $this->assertEquals(6, $this->redis->zsetCount('zset', -100, 100));
  931. $this->assertEquals(3, $this->redis->zsetCount('zset', 10, 20));
  932. $this->assertEquals(2, $this->redis->zsetCount('zset', "(10", 20));
  933. $this->assertEquals(1, $this->redis->zsetCount('zset', 10, "(20"));
  934. $this->assertEquals(0, $this->redis->zsetCount('zset', "(10", "(20"));
  935. $this->assertEquals(3, $this->redis->zsetCount('zset', "(0", "(30"));
  936. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
  937. $test->redis->set('foo', 'bar');
  938. $test->redis->zsetCount('foo', 0, 0);
  939. });
  940. }
  941. function testZsetCardinality() {
  942. $zset = RC::zsetAddAndReturn($this->redis, 'zset', RC::getZSetArray());
  943. $this->assertEquals(count($zset), $this->redis->zsetCardinality('zset'));
  944. $this->redis->zsetRemove('zset', 'a');
  945. $this->assertEquals(count($zset) - 1, $this->redis->zsetCardinality('zset'));
  946. // empty zset
  947. $this->redis->zsetAdd('zsetB', 0, 'a');
  948. $this->redis->zsetRemove('zsetB', 'a');
  949. $this->assertEquals(0, $this->redis->zsetCardinality('setB'));
  950. // non-existing zset
  951. $this->assertEquals(0, $this->redis->zsetCardinality('zsetDoesNotExist'));
  952. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
  953. $test->redis->set('foo', 'bar');
  954. $test->redis->zsetCardinality('foo');
  955. });
  956. }
  957. function testZsetScore() {
  958. $zset = RC::zsetAddAndReturn($this->redis, 'zset', RC::getZSetArray());
  959. $this->assertEquals(-10, $this->redis->zsetScore('zset', 'a'));
  960. $this->assertEquals(10, $this->redis->zsetScore('zset', 'c'));
  961. $this->assertEquals(20, $this->redis->zsetScore('zset', 'e'));
  962. $this->assertNull($this->redis->zsetScore('zset', 'x'));
  963. $this->assertNull($this->redis->zsetScore('zsetDoesNotExist', 'a'));
  964. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
  965. $test->redis->set('foo', 'bar');
  966. $test->redis->zsetScore('foo', 'bar');
  967. });
  968. }
  969. function testZsetRemoveRangeByScore() {
  970. $zset = RC::zsetAddAndReturn($this->redis, 'zset', RC::getZSetArray());
  971. $this->assertEquals(2, $this->redis->zsetRemoveRangeByScore('zset', -10, 0));
  972. $this->assertEquals(
  973. array('c', 'd', 'e', 'f'),
  974. $this->redis->zsetRange('zset', 0, -1)
  975. );
  976. $this->assertEquals(1, $this->redis->zsetRemoveRangeByScore('zset', 10, 10));
  977. $this->assertEquals(
  978. array('d', 'e', 'f'),
  979. $this->redis->zsetRange('zset', 0, -1)
  980. );
  981. $this->assertEquals(0, $this->redis->zsetRemoveRangeByScore('zset', 100, 100));
  982. $this->assertEquals(3, $this->redis->zsetRemoveRangeByScore('zset', 0, 100));
  983. $this->assertEquals(0, $this->redis->zsetRemoveRangeByScore('zset', 0, 100));
  984. $this->assertEquals(0, $this->redis->zsetRemoveRangeByScore('zsetDoesNotExist', 0, 100));
  985. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
  986. $test->redis->set('foo', 'bar');
  987. $test->redis->zsetRemoveRangeByScore('foo', 0, 0);
  988. });
  989. }
  990. function testZrank() {
  991. $zset = RC::zsetAddAndReturn($this->redis, 'zset', RC::getZSetArray());
  992. $this->assertEquals(0, $this->redis->zsetRank('zset', 'a'));
  993. $this->assertEquals(1, $this->redis->zsetRank('zset', 'b'));
  994. $this->assertEquals(4, $this->redis->zsetRank('zset', 'e'));
  995. $this->redis->zsetRemove('zset', 'd');
  996. $this->assertEquals(3, $this->redis->zsetRank('zset', 'e'));
  997. $this->assertNull($this->redis->zsetRank('zset', 'x'));
  998. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
  999. $test->redis->set('foo', 'bar');
  1000. $test->redis->zsetRank('foo', 'a');
  1001. });
  1002. }
  1003. /* multiple databases handling commands */
  1004. function testSelectDatabase() {
  1005. $this->assertTrue($this->redis->selectDatabase(0));
  1006. $this->assertTrue($this->redis->selectDatabase(RC::DEFAULT_DATABASE));
  1007. RC::testForServerException($this, RC::EXCEPTION_INVALID_DB_IDX, function($test) {
  1008. $test->redis->selectDatabase(32);
  1009. });
  1010. RC::testForServerException($this, RC::EXCEPTION_INVALID_DB_IDX, function($test) {
  1011. $test->redis->selectDatabase(-1);
  1012. });
  1013. }
  1014. function testMove() {
  1015. // TODO: This test sucks big time. Period.
  1016. $otherDb = 5;
  1017. $this->redis->set('foo', 'bar');
  1018. $this->redis->selectDatabase($otherDb);
  1019. $this->redis->flushDatabase();
  1020. $this->redis->selectDatabase(RC::DEFAULT_DATABASE);
  1021. $this->assertTrue($this->redis->move('foo', $otherDb));
  1022. $this->assertFalse($this->redis->move('foo', $otherDb));
  1023. $this->assertFalse($this->redis->move('keyDoesNotExist', $otherDb));
  1024. $this->redis->set('hoge', 'piyo');
  1025. // TODO: shouldn't Redis send an EXCEPTION_INVALID_DB_IDX instead of EXCEPTION_OUT_OF_RANGE?
  1026. RC::testForServerException($this, RC::EXCEPTION_OUT_OF_RANGE, function($test) {
  1027. $test->redis->move('hoge', 32);
  1028. });
  1029. }
  1030. function testFlushDatabase() {
  1031. $this->assertTrue($this->redis->flushDatabase());
  1032. }
  1033. /* sorting */
  1034. function testSort() {
  1035. $unorderedList = RC::pushTailAndReturn($this->redis, 'unordered', array(2, 100, 3, 1, 30, 10));
  1036. // without parameters
  1037. $this->assertEquals(array(1, 2, 3, 10, 30, 100), $this->redis->sort('unordered'));
  1038. // with parameter ASC/DESC
  1039. $this->assertEquals(
  1040. array(100, 30, 10, 3, 2, 1),
  1041. $this->redis->sort('unordered', array(
  1042. 'sort' => 'desc'
  1043. ))
  1044. );
  1045. // with parameter LIMIT
  1046. $this->assertEquals(
  1047. array(1, 2, 3),
  1048. $this->redis->sort('unordered', array(
  1049. 'limit' => array(0, 3)
  1050. ))
  1051. );
  1052. $this->assertEquals(
  1053. array(10, 30),
  1054. $this->redis->sort('unordered', array(
  1055. 'limit' => array(3, 2)
  1056. ))
  1057. );
  1058. // with parameter ALPHA
  1059. $this->assertEquals(
  1060. array(1, 10, 100, 2, 3, 30),
  1061. $this->redis->sort('unordered', array(
  1062. 'alpha' => true
  1063. ))
  1064. );
  1065. // with combined parameters
  1066. $this->assertEquals(
  1067. array(30, 10, 3, 2),
  1068. $this->redis->sort('unordered', array(
  1069. 'alpha' => false,
  1070. 'sort' => 'desc',
  1071. 'limit' => array(1, 4)
  1072. ))
  1073. );
  1074. // with parameter ALPHA
  1075. $this->assertEquals(
  1076. array(1, 10, 100, 2, 3, 30),
  1077. $this->redis->sort('unordered', array(
  1078. 'alpha' => true
  1079. ))
  1080. );
  1081. // with parameter STORE
  1082. $this->assertEquals(
  1083. count($unorderedList),
  1084. $this->redis->sort('unordered', array(
  1085. 'store' => 'ordered'
  1086. ))
  1087. );
  1088. $this->assertEquals(array(1, 2, 3, 10, 30, 100), $this->redis->listRange('ordered', 0, -1));
  1089. // with parameter GET
  1090. $this->redis->pushTail('uids', 1003);
  1091. $this->redis->pushTail('uids', 1001);
  1092. $this->redis->pushTail('uids', 1002);
  1093. $this->redis->pushTail('uids', 1000);
  1094. $sortget = array(
  1095. 'uid:1000' => 'foo', 'uid:1001' => 'bar',
  1096. 'uid:1002' => 'hoge', 'uid:1003' => 'piyo'
  1097. );
  1098. $this->redis->setMultiple($sortget);
  1099. $this->assertEquals(
  1100. array_values($sortget),
  1101. $this->redis->sort('uids', array('get' => 'uid:*'))
  1102. );
  1103. // wrong type
  1104. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
  1105. $test->redis->set('foo', 'bar');
  1106. $test->redis->sort('foo');
  1107. });
  1108. }
  1109. /* remote server control commands */
  1110. function testInfo() {
  1111. $serverInfo = $this->redis->info();
  1112. $this->assertType('array', $serverInfo);
  1113. $this->assertNotNull($serverInfo['redis_version']);
  1114. $this->assertGreaterThan(0, $serverInfo['uptime_in_seconds']);
  1115. $this->assertGreaterThan(0, $serverInfo['total_connections_received']);
  1116. }
  1117. function testSlaveOf() {
  1118. $masterHost = 'www.google.com';
  1119. $masterPort = 80;
  1120. $this->assertTrue($this->redis->slaveOf($masterHost, $masterPort));
  1121. $serverInfo = $this->redis->info();
  1122. $this->assertEquals('slave', $serverInfo['role']);
  1123. $this->assertEquals($masterHost, $serverInfo['master_host']);
  1124. $this->assertEquals($masterPort, $serverInfo['master_port']);
  1125. // slave of NO ONE, the implicit way
  1126. $this->assertTrue($this->redis->slaveOf());
  1127. $serverInfo = $this->redis->info();
  1128. $this->assertEquals('master', $serverInfo['role']);
  1129. // slave of NO ONE, the explicit way
  1130. $this->assertTrue($this->redis->slaveOf('NO ONE'));
  1131. }
  1132. /* persistence control commands */
  1133. function testSave() {
  1134. $this->assertTrue($this->redis->save());
  1135. }
  1136. function testBackgroundSave() {
  1137. $this->assertTrue($this->redis->backgroundSave());
  1138. }
  1139. function testBackgroundRewriteAppendOnlyFile() {
  1140. $this->assertTrue($this->redis->backgroundRewriteAppendOnlyFile());
  1141. }
  1142. function testLastSave() {
  1143. $this->assertGreaterThan(0, $this->redis->lastSave());
  1144. }
  1145. function testShutdown() {
  1146. // TODO: seriously, we even test shutdown?
  1147. /*
  1148. $this->assertNull($this->redis->shutdown());
  1149. sleep(1);
  1150. $this->assertFalse($this->redis->isConnected());
  1151. */
  1152. }
  1153. }
  1154. ?>