RedisCommandsTest.php 53 KB

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