RedisCommandsTest.php 49 KB

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