RedisCommandsTest.php 53 KB

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