RedisCommandsTest.php 45 KB

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