RedisCommandsTest.php 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231
  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 testListPopPush() {
  365. $numbers = RC::pushTailAndReturn($this->redis, 'numbers', array(0, 1, 2));
  366. $this->assertEquals(0, $this->redis->listLength('temporary'));
  367. $this->assertEquals(2, $this->redis->listPopPush('numbers', 'temporary'));
  368. $this->assertEquals(1, $this->redis->listPopPush('numbers', 'temporary'));
  369. $this->assertEquals(0, $this->redis->listPopPush('numbers', 'temporary'));
  370. $this->assertEquals(0, $this->redis->listLength('numbers'));
  371. $this->assertEquals(3, $this->redis->listLength('temporary'));
  372. $this->assertEquals(array(), $this->redis->listRange('numbers', 0, -1));
  373. $this->assertEquals($numbers, $this->redis->listRange('temporary', 0, -1));
  374. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
  375. $test->redis->set('foo', 'bar');
  376. $test->redis->listPopPush('foo', 'hoge');
  377. });
  378. }
  379. function testListPopFirst() {
  380. $numbers = RC::pushTailAndReturn($this->redis, 'numbers', array(0, 1, 2, 3, 4));
  381. $this->assertEquals(0, $this->redis->popFirst('numbers'));
  382. $this->assertEquals(1, $this->redis->popFirst('numbers'));
  383. $this->assertEquals(2, $this->redis->popFirst('numbers'));
  384. $this->assertEquals(array(3, 4), $this->redis->listRange('numbers', 0, -1));
  385. $this->redis->popFirst('numbers');
  386. $this->redis->popFirst('numbers');
  387. $this->assertNull($this->redis->popFirst('numbers'));
  388. $this->assertNull($this->redis->popFirst('numbers'));
  389. $this->assertNull($this->redis->popFirst('listDoesNotExist'));
  390. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
  391. $test->redis->set('foo', 'bar');
  392. $test->redis->popFirst('foo');
  393. });
  394. }
  395. function testListPopLast() {
  396. $numbers = RC::pushTailAndReturn($this->redis, 'numbers', array(0, 1, 2, 3, 4));
  397. $this->assertEquals(4, $this->redis->popLast('numbers'));
  398. $this->assertEquals(3, $this->redis->popLast('numbers'));
  399. $this->assertEquals(2, $this->redis->popLast('numbers'));
  400. $this->assertEquals(array(0, 1), $this->redis->listRange('numbers', 0, -1));
  401. $this->redis->popLast('numbers');
  402. $this->redis->popLast('numbers');
  403. $this->assertNull($this->redis->popLast('numbers'));
  404. $this->assertNull($this->redis->popLast('numbers'));
  405. $this->assertNull($this->redis->popLast('listDoesNotExist'));
  406. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
  407. $test->redis->set('foo', 'bar');
  408. $test->redis->popLast('foo');
  409. });
  410. }
  411. /* commands operating on sets */
  412. function testSetAdd() {
  413. $this->assertTrue($this->redis->setAdd('set', 0));
  414. $this->assertTrue($this->redis->setAdd('set', 1));
  415. $this->assertFalse($this->redis->setAdd('set', 0));
  416. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
  417. $test->redis->set('foo', 'bar');
  418. $test->redis->setAdd('foo', 0);
  419. });
  420. }
  421. function testSetRemove() {
  422. $set = RC::setAddAndReturn($this->redis, 'set', array(0, 1, 2, 3, 4));
  423. $this->assertTrue($this->redis->setRemove('set', 0));
  424. $this->assertTrue($this->redis->setRemove('set', 4));
  425. $this->assertFalse($this->redis->setRemove('set', 10));
  426. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
  427. $test->redis->set('foo', 'bar');
  428. $test->redis->setRemove('foo', 0);
  429. });
  430. }
  431. function testSetPop() {
  432. $set = RC::setAddAndReturn($this->redis, 'set', array(0, 1, 2, 3, 4));
  433. $this->assertTrue(in_array($this->redis->setPop('set'), $set));
  434. $this->assertNull($this->redis->setPop('setDoesNotExist'));
  435. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
  436. $test->redis->set('foo', 'bar');
  437. $test->redis->setPop('foo');
  438. });
  439. }
  440. function testSetMove() {
  441. $setA = RC::setAddAndReturn($this->redis, 'setA', array(0, 1, 2, 3, 4, 5));
  442. $setB = RC::setAddAndReturn($this->redis, 'setB', array(5, 6, 7, 8, 9, 10));
  443. $this->assertTrue($this->redis->setMove('setA', 'setB', 0));
  444. $this->assertFalse($this->redis->setRemove('setA', 0));
  445. $this->assertTrue($this->redis->setRemove('setB', 0));
  446. $this->assertTrue($this->redis->setMove('setA', 'setB', 5));
  447. $this->assertFalse($this->redis->setMove('setA', 'setB', 100));
  448. // wrong type
  449. $this->redis->set('foo', 'bar');
  450. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
  451. $test->redis->setMove('foo', 'setB', 5);
  452. });
  453. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
  454. $test->redis->setMove('setA', 'foo', 5);
  455. });
  456. }
  457. function testSetCardinality() {
  458. RC::setAddAndReturn($this->redis, 'setA', array(0, 1, 2, 3, 4, 5));
  459. $this->assertEquals(6, $this->redis->setCardinality('setA'));
  460. // empty set
  461. $this->redis->setAdd('setB', 0);
  462. $this->redis->setPop('setB');
  463. $this->assertEquals(0, $this->redis->setCardinality('setB'));
  464. // non-existing set
  465. $this->assertEquals(0, $this->redis->setCardinality('setDoesNotExist'));
  466. // wrong type
  467. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
  468. $test->redis->set('foo', 'bar');
  469. $test->redis->setCardinality('foo');
  470. });
  471. }
  472. function testSetIsMember() {
  473. RC::setAddAndReturn($this->redis, 'set', array(0, 1, 2, 3, 4, 5));
  474. $this->assertTrue($this->redis->setIsMember('set', 3));
  475. $this->assertFalse($this->redis->setIsMember('set', 100));
  476. $this->assertFalse($this->redis->setIsMember('setDoesNotExist', 0));
  477. // wrong type
  478. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
  479. $test->redis->set('foo', 'bar');
  480. $test->redis->setIsMember('foo', 0);
  481. });
  482. }
  483. function testSetMembers() {
  484. $set = RC::setAddAndReturn($this->redis, 'set', array(0, 1, 2, 3, 4, 5, 6));
  485. $this->assertTrue(RC::sameValuesInArrays($set, $this->redis->setMembers('set')));
  486. $this->assertNull($this->redis->setMembers('setDoesNotExist'));
  487. // wrong type
  488. $this->redis->set('foo', 'bar');
  489. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
  490. $test->redis->setMembers('foo');
  491. });
  492. }
  493. function testSetIntersection() {
  494. $setA = RC::setAddAndReturn($this->redis, 'setA', array(0, 1, 2, 3, 4, 5, 6));
  495. $setB = RC::setAddAndReturn($this->redis, 'setB', array(1, 3, 4, 6, 9, 10));
  496. $this->assertTrue(RC::sameValuesInArrays(
  497. $setA,
  498. $this->redis->setIntersection('setA')
  499. ));
  500. $this->assertTrue(RC::sameValuesInArrays(
  501. array_intersect($setA, $setB),
  502. $this->redis->setIntersection('setA', 'setB')
  503. ));
  504. // TODO: should nil really be considered an empty set?
  505. $this->assertNull($this->redis->setIntersection('setA', 'setDoesNotExist'));
  506. // wrong type
  507. $this->redis->set('foo', 'bar');
  508. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
  509. $test->redis->setIntersection('foo');
  510. });
  511. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
  512. $test->redis->setIntersection('setA', 'foo');
  513. });
  514. }
  515. function testSetIntersectionStore() {
  516. $setA = RC::setAddAndReturn($this->redis, 'setA', array(0, 1, 2, 3, 4, 5, 6));
  517. $setB = RC::setAddAndReturn($this->redis, 'setB', array(1, 3, 4, 6, 9, 10));
  518. $this->assertEquals(count($setA), $this->redis->setIntersectionStore('setC', 'setA'));
  519. $this->assertTrue(RC::sameValuesInArrays(
  520. $setA,
  521. $this->redis->setMembers('setC')
  522. ));
  523. $this->redis->delete('setC');
  524. $this->assertEquals(4, $this->redis->setIntersectionStore('setC', 'setA', 'setB'));
  525. $this->assertTrue(RC::sameValuesInArrays(
  526. array(1, 3, 4, 6),
  527. $this->redis->setMembers('setC')
  528. ));
  529. $this->redis->delete('setC');
  530. $this->assertNull($this->redis->setIntersection('setC', 'setDoesNotExist'));
  531. $this->assertFalse($this->redis->exists('setC'));
  532. // existing keys are replaced by SINTERSTORE
  533. $this->redis->set('foo', 'bar');
  534. $this->assertEquals(count($setA), $this->redis->setIntersectionStore('foo', 'setA'));
  535. // wrong type
  536. $this->redis->set('foo', 'bar');
  537. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
  538. $test->redis->setIntersectionStore('setA', 'foo');
  539. });
  540. }
  541. function testSetUnion() {
  542. $setA = RC::setAddAndReturn($this->redis, 'setA', array(0, 1, 2, 3, 4, 5, 6));
  543. $setB = RC::setAddAndReturn($this->redis, 'setB', array(1, 3, 4, 6, 9, 10));
  544. $this->assertTrue(RC::sameValuesInArrays(
  545. $setA,
  546. $this->redis->setUnion('setA')
  547. ));
  548. $this->assertTrue(RC::sameValuesInArrays(
  549. array_union($setA, $setB),
  550. $this->redis->setUnion('setA', 'setB')
  551. ));
  552. $this->assertTrue(RC::sameValuesInArrays(
  553. $setA,
  554. $this->redis->setUnion('setA', 'setDoesNotExist')
  555. ));
  556. // wrong type
  557. $this->redis->set('foo', 'bar');
  558. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
  559. $test->redis->setUnion('foo');
  560. });
  561. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
  562. $test->redis->setUnion('setA', 'foo');
  563. });
  564. }
  565. function testSetUnionStore() {
  566. $setA = RC::setAddAndReturn($this->redis, 'setA', array(0, 1, 2, 3, 4, 5, 6));
  567. $setB = RC::setAddAndReturn($this->redis, 'setB', array(1, 3, 4, 6, 9, 10));
  568. $this->assertEquals(count($setA), $this->redis->setUnionStore('setC', 'setA'));
  569. $this->assertTrue(RC::sameValuesInArrays(
  570. $setA,
  571. $this->redis->setMembers('setC')
  572. ));
  573. $this->redis->delete('setC');
  574. $this->assertEquals(9, $this->redis->setUnionStore('setC', 'setA', 'setB'));
  575. $this->assertTrue(RC::sameValuesInArrays(
  576. array_union($setA, $setB),
  577. $this->redis->setMembers('setC')
  578. ));
  579. // non-existing keys are considered empty sets
  580. $this->redis->delete('setC');
  581. $this->assertEquals(0, $this->redis->setUnionStore('setC', 'setDoesNotExist'));
  582. $this->assertTrue($this->redis->exists('setC'));
  583. $this->assertEquals(0, $this->redis->setCardinality('setC'));
  584. // existing keys are replaced by SUNIONSTORE
  585. $this->redis->set('foo', 'bar');
  586. $this->assertEquals(count($setA), $this->redis->setUnionStore('foo', 'setA'));
  587. // wrong type
  588. $this->redis->set('foo', 'bar');
  589. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
  590. $test->redis->setUnionStore('setA', 'foo');
  591. });
  592. }
  593. function testSetDifference() {
  594. $setA = RC::setAddAndReturn($this->redis, 'setA', array(0, 1, 2, 3, 4, 5, 6));
  595. $setB = RC::setAddAndReturn($this->redis, 'setB', array(1, 3, 4, 6, 9, 10));
  596. $this->assertTrue(RC::sameValuesInArrays(
  597. $setA,
  598. $this->redis->setDifference('setA')
  599. ));
  600. $this->assertTrue(RC::sameValuesInArrays(
  601. array_diff($setA, $setB),
  602. $this->redis->setDifference('setA', 'setB')
  603. ));
  604. $this->assertTrue(RC::sameValuesInArrays(
  605. $setA,
  606. $this->redis->setDifference('setA', 'setDoesNotExist')
  607. ));
  608. // wrong type
  609. $this->redis->set('foo', 'bar');
  610. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
  611. $test->redis->setDifference('foo');
  612. });
  613. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
  614. $test->redis->setDifference('setA', 'foo');
  615. });
  616. }
  617. function testSetDifferenceStore() {
  618. $setA = RC::setAddAndReturn($this->redis, 'setA', array(0, 1, 2, 3, 4, 5, 6));
  619. $setB = RC::setAddAndReturn($this->redis, 'setB', array(1, 3, 4, 6, 9, 10));
  620. $this->assertEquals(count($setA), $this->redis->setDifferenceStore('setC', 'setA'));
  621. $this->assertTrue(RC::sameValuesInArrays(
  622. $setA,
  623. $this->redis->setMembers('setC')
  624. ));
  625. $this->redis->delete('setC');
  626. $this->assertEquals(3, $this->redis->setDifferenceStore('setC', 'setA', 'setB'));
  627. $this->assertTrue(RC::sameValuesInArrays(
  628. array_diff($setA, $setB),
  629. $this->redis->setMembers('setC')
  630. ));
  631. // non-existing keys are considered empty sets
  632. $this->redis->delete('setC');
  633. $this->assertEquals(0, $this->redis->setDifferenceStore('setC', 'setDoesNotExist'));
  634. $this->assertTrue($this->redis->exists('setC'));
  635. $this->assertEquals(0, $this->redis->setCardinality('setC'));
  636. // existing keys are replaced by SDIFFSTORE
  637. $this->redis->set('foo', 'bar');
  638. $this->assertEquals(count($setA), $this->redis->setDifferenceStore('foo', 'setA'));
  639. // wrong type
  640. $this->redis->set('foo', 'bar');
  641. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
  642. $test->redis->setDifferenceStore('setA', 'foo');
  643. });
  644. }
  645. function testRandomMember() {
  646. $set = RC::setAddAndReturn($this->redis, 'set', array(0, 1, 2, 3, 4, 5, 6));
  647. $this->assertTrue(in_array($this->redis->setRandomMember('set'), $set));
  648. $this->assertNull($this->redis->setRandomMember('setDoesNotExist'));
  649. // wrong type
  650. $this->redis->set('foo', 'bar');
  651. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
  652. $test->redis->setRandomMember('foo');
  653. });
  654. }
  655. /* commands operating on sorted sets */
  656. function testZsetAdd() {
  657. $this->assertTrue($this->redis->zsetAdd('zset', 0, 'a'));
  658. $this->assertTrue($this->redis->zsetAdd('zset', 1, 'b'));
  659. $this->assertTrue($this->redis->zsetAdd('zset', -1, 'c'));
  660. // TODO: floats?
  661. //$this->assertTrue($this->redis->zsetAdd('zset', -1.0, 'c'));
  662. //$this->assertTrue($this->redis->zsetAdd('zset', -1.0, 'c'));
  663. $this->assertFalse($this->redis->zsetAdd('zset', 2, 'b'));
  664. $this->assertFalse($this->redis->zsetAdd('zset', -2, 'b'));
  665. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
  666. $test->redis->set('foo', 'bar');
  667. $test->redis->zsetAdd('foo', 0, 'a');
  668. });
  669. }
  670. function testZsetRemove() {
  671. RC::zsetAddAndReturn($this->redis, 'zset', RC::getZSetArray());
  672. $this->assertTrue($this->redis->zsetRemove('zset', 'a'));
  673. $this->assertFalse($this->redis->zsetRemove('zset', 'x'));
  674. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
  675. $test->redis->set('foo', 'bar');
  676. $test->redis->zsetRemove('foo', 'bar');
  677. });
  678. }
  679. function testZsetRange() {
  680. $zset = RC::zsetAddAndReturn($this->redis, 'zset', RC::getZSetArray());
  681. $this->assertEquals(
  682. array_slice(array_keys($zset), 0, 4),
  683. $this->redis->zsetRange('zset', 0, 3)
  684. );
  685. $this->assertEquals(
  686. array_slice(array_keys($zset), 0, 1),
  687. $this->redis->zsetRange('zset', 0, 0)
  688. );
  689. $this->assertEquals(
  690. array(),
  691. $this->redis->zsetRange('zset', 1, 0)
  692. );
  693. $this->assertEquals(
  694. array_values(array_keys($zset)),
  695. $this->redis->zsetRange('zset', 0, -1)
  696. );
  697. $this->assertEquals(
  698. array_slice(array_keys($zset), 3, 1),
  699. $this->redis->zsetRange('zset', 3, -3)
  700. );
  701. $this->assertEquals(
  702. array(),
  703. $this->redis->zsetRange('zset', 5, -3)
  704. );
  705. $this->assertEquals(
  706. array_slice(array_keys($zset), -5, -1),
  707. $this->redis->zsetRange('zset', -5, -2)
  708. );
  709. $this->assertEquals(
  710. array_values(array_keys($zset)),
  711. $this->redis->zsetRange('zset', -100, 100)
  712. );
  713. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
  714. $test->redis->set('foo', 'bar');
  715. $test->redis->zsetRange('foo', 0, -1);
  716. });
  717. }
  718. function testZsetReverseRange() {
  719. $zset = RC::zsetAddAndReturn($this->redis, 'zset', RC::getZSetArray());
  720. $this->assertEquals(
  721. array_slice(array_reverse(array_keys($zset)), 0, 4),
  722. $this->redis->zsetReverseRange('zset', 0, 3)
  723. );
  724. $this->assertEquals(
  725. array_slice(array_reverse(array_keys($zset)), 0, 1),
  726. $this->redis->zsetReverseRange('zset', 0, 0)
  727. );
  728. $this->assertEquals(
  729. array(),
  730. $this->redis->zsetReverseRange('zset', 1, 0)
  731. );
  732. $this->assertEquals(
  733. array_values(array_reverse(array_keys($zset))),
  734. $this->redis->zsetReverseRange('zset', 0, -1)
  735. );
  736. $this->assertEquals(
  737. array_slice(array_reverse(array_keys($zset)), 3, 1),
  738. $this->redis->zsetReverseRange('zset', 3, -3)
  739. );
  740. $this->assertEquals(
  741. array(),
  742. $this->redis->zsetReverseRange('zset', 5, -3)
  743. );
  744. $this->assertEquals(
  745. array_slice(array_reverse(array_keys($zset)), -5, -1),
  746. $this->redis->zsetReverseRange('zset', -5, -2)
  747. );
  748. $this->assertEquals(
  749. array_values(array_reverse(array_keys($zset))),
  750. $this->redis->zsetReverseRange('zset', -100, 100)
  751. );
  752. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
  753. $test->redis->set('foo', 'bar');
  754. $test->redis->zsetReverseRange('foo', 0, -1);
  755. });
  756. }
  757. function testZsetRangeByScore() {
  758. $zset = RC::zsetAddAndReturn($this->redis, 'zset', RC::getZSetArray());
  759. $this->assertEquals(
  760. array('a'),
  761. $this->redis->zsetRangeByScore('zset', -10, -10)
  762. );
  763. $this->assertEquals(
  764. array('c', 'd', 'e', 'f'),
  765. $this->redis->zsetRangeByScore('zset', 10, 30)
  766. );
  767. $this->assertEquals(
  768. array('d', 'e'),
  769. $this->redis->zsetRangeByScore('zset', 20, 20)
  770. );
  771. $this->assertEquals(
  772. array(),
  773. $this->redis->zsetRangeByScore('zset', 30, 0)
  774. );
  775. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
  776. $test->redis->set('foo', 'bar');
  777. $test->redis->zsetRangeByScore('foo', 0, 0);
  778. });
  779. }
  780. function testZsetCardinality() {
  781. $zset = RC::zsetAddAndReturn($this->redis, 'zset', RC::getZSetArray());
  782. $this->assertEquals(count($zset), $this->redis->zsetCardinality('zset'));
  783. $this->redis->zsetRemove('zset', 'a');
  784. $this->assertEquals(count($zset) - 1, $this->redis->zsetCardinality('zset'));
  785. // empty zset
  786. $this->redis->zsetAdd('zsetB', 0, 'a');
  787. $this->redis->zsetRemove('zsetB', 'a');
  788. $this->assertEquals(0, $this->redis->zsetCardinality('setB'));
  789. // non-existing zset
  790. $this->assertEquals(0, $this->redis->zsetCardinality('zsetDoesNotExist'));
  791. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
  792. $test->redis->set('foo', 'bar');
  793. $test->redis->zsetCardinality('foo');
  794. });
  795. }
  796. function testZsetScore() {
  797. $zset = RC::zsetAddAndReturn($this->redis, 'zset', RC::getZSetArray());
  798. $this->assertEquals(-10, $this->redis->zsetScore('zset', 'a'));
  799. $this->assertEquals(10, $this->redis->zsetScore('zset', 'c'));
  800. $this->assertEquals(20, $this->redis->zsetScore('zset', 'e'));
  801. $this->assertNull($this->redis->zsetScore('zset', 'x'));
  802. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
  803. $test->redis->set('foo', 'bar');
  804. $test->redis->zsetScore('foo', 'bar');
  805. });
  806. }
  807. function testZsetRemoveRangeByScore() {
  808. $zset = RC::zsetAddAndReturn($this->redis, 'zset', RC::getZSetArray());
  809. $this->assertEquals(2, $this->redis->zsetRemoveRangeByScore('zset', -10, 0));
  810. $this->assertEquals(
  811. array('c', 'd', 'e', 'f'),
  812. $this->redis->zsetRange('zset', 0, -1)
  813. );
  814. $this->assertEquals(1, $this->redis->zsetRemoveRangeByScore('zset', 10, 10));
  815. $this->assertEquals(
  816. array('d', 'e', 'f'),
  817. $this->redis->zsetRange('zset', 0, -1)
  818. );
  819. $this->assertEquals(0, $this->redis->zsetRemoveRangeByScore('zset', 100, 100));
  820. $this->assertEquals(3, $this->redis->zsetRemoveRangeByScore('zset', 0, 100));
  821. $this->assertEquals(0, $this->redis->zsetRemoveRangeByScore('zset', 0, 100));
  822. $this->assertEquals(0, $this->redis->zsetRemoveRangeByScore('zsetDoesNotExist', 0, 100));
  823. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
  824. $test->redis->set('foo', 'bar');
  825. $test->redis->zsetRemoveRangeByScore('foo', 0, 0);
  826. });
  827. }
  828. /* multiple databases handling commands */
  829. function testSelectDatabase() {
  830. $this->assertTrue($this->redis->selectDatabase(0));
  831. $this->assertTrue($this->redis->selectDatabase(RC::DEFAULT_DATABASE));
  832. RC::testForServerException($this, RC::EXCEPTION_INVALID_DB_IDX, function($test) {
  833. $test->redis->selectDatabase(32);
  834. });
  835. RC::testForServerException($this, RC::EXCEPTION_INVALID_DB_IDX, function($test) {
  836. $test->redis->selectDatabase(-1);
  837. });
  838. }
  839. function testMove() {
  840. // TODO: This test sucks big time. Period.
  841. $otherDb = 5;
  842. $this->redis->set('foo', 'bar');
  843. $this->redis->selectDatabase($otherDb);
  844. $this->redis->flushDatabase();
  845. $this->redis->selectDatabase(RC::DEFAULT_DATABASE);
  846. $this->assertTrue($this->redis->move('foo', $otherDb));
  847. $this->assertFalse($this->redis->move('foo', $otherDb));
  848. $this->assertFalse($this->redis->move('keyDoesNotExist', $otherDb));
  849. $this->redis->set('hoge', 'piyo');
  850. // TODO: shouldn't Redis send an EXCEPTION_INVALID_DB_IDX instead of EXCEPTION_OUT_OF_RANGE?
  851. RC::testForServerException($this, RC::EXCEPTION_OUT_OF_RANGE, function($test) {
  852. $test->redis->move('hoge', 32);
  853. });
  854. }
  855. function testFlushDatabase() {
  856. $this->assertTrue($this->redis->flushDatabase());
  857. }
  858. function testFlushDatabases() {
  859. $this->assertTrue($this->redis->flushDatabases());
  860. }
  861. /* sorting */
  862. function testSort() {
  863. $unorderedList = RC::pushTailAndReturn($this->redis, 'unordered', array(2, 100, 3, 1, 30, 10));
  864. // without parameters
  865. $this->assertEquals(array(1, 2, 3, 10, 30, 100), $this->redis->sort('unordered'));
  866. // with parameter ASC/DESC
  867. $this->assertEquals(
  868. array(100, 30, 10, 3, 2, 1),
  869. $this->redis->sort('unordered', array(
  870. 'sort' => 'desc'
  871. ))
  872. );
  873. // with parameter LIMIT
  874. $this->assertEquals(
  875. array(1, 2, 3),
  876. $this->redis->sort('unordered', array(
  877. 'limit' => array(0, 3)
  878. ))
  879. );
  880. $this->assertEquals(
  881. array(10, 30),
  882. $this->redis->sort('unordered', array(
  883. 'limit' => array(3, 2)
  884. ))
  885. );
  886. // with parameter ALPHA
  887. $this->assertEquals(
  888. array(1, 10, 100, 2, 3, 30),
  889. $this->redis->sort('unordered', array(
  890. 'alpha' => true
  891. ))
  892. );
  893. // with combined parameters
  894. $this->assertEquals(
  895. array(30, 10, 3, 2),
  896. $this->redis->sort('unordered', array(
  897. 'alpha' => false,
  898. 'sort' => 'desc',
  899. 'limit' => array(1, 4)
  900. ))
  901. );
  902. // with parameter ALPHA
  903. $this->assertEquals(
  904. array(1, 10, 100, 2, 3, 30),
  905. $this->redis->sort('unordered', array(
  906. 'alpha' => true
  907. ))
  908. );
  909. // with parameter STORE
  910. $this->assertEquals(
  911. count($unorderedList),
  912. $this->redis->sort('unordered', array(
  913. 'store' => 'ordered'
  914. ))
  915. );
  916. $this->assertEquals(array(1, 2, 3, 10, 30, 100), $this->redis->listRange('ordered', 0, -1));
  917. // wront type
  918. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
  919. $test->redis->set('foo', 'bar');
  920. $test->redis->sort('foo');
  921. });
  922. }
  923. /* remote server control commands */
  924. function testInfo() {
  925. $serverInfo = $this->redis->info();
  926. $this->assertType('array', $serverInfo);
  927. $this->assertNotNull($serverInfo['redis_version']);
  928. $this->assertGreaterThan(0, $serverInfo['uptime_in_seconds']);
  929. $this->assertGreaterThan(0, $serverInfo['total_connections_received']);
  930. }
  931. function testSlaveOf() {
  932. $masterHost = 'www.google.com';
  933. $masterPort = 80;
  934. $this->assertTrue($this->redis->slaveOf($masterHost, $masterPort));
  935. $serverInfo = $this->redis->info();
  936. $this->assertEquals('slave', $serverInfo['role']);
  937. $this->assertEquals($masterHost, $serverInfo['master_host']);
  938. $this->assertEquals($masterPort, $serverInfo['master_port']);
  939. // slave of NO ONE, the implicit way
  940. $this->assertTrue($this->redis->slaveOf());
  941. $serverInfo = $this->redis->info();
  942. $this->assertEquals('master', $serverInfo['role']);
  943. // slave of NO ONE, the explicit way
  944. $this->assertTrue($this->redis->slaveOf('NO ONE'));
  945. }
  946. /* persistence control commands */
  947. function testSave() {
  948. $this->assertTrue($this->redis->save());
  949. }
  950. function testBackgroundSave() {
  951. $this->assertTrue($this->redis->backgroundSave());
  952. }
  953. function testLastSave() {
  954. $this->assertGreaterThan(0, $this->redis->lastSave());
  955. }
  956. function testShutdown() {
  957. // TODO: seriously, we even test shutdown?
  958. /*
  959. $this->assertNull($this->redis->shutdown());
  960. sleep(1);
  961. $this->assertFalse($this->redis->isConnected());
  962. */
  963. }
  964. }
  965. ?>