RedisCommandsTest.php 52 KB

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