RedisCommandsTest.php 54 KB

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