RedisCommandsTest.php 48 KB

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