RedisCommandsTest.php 51 KB

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