RedisCommandsTest.php 47 KB

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