RedisCommandsTest.php 51 KB

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