SortedSetKeyTest.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. <?php
  2. /*
  3. * This file is part of the Predis package.
  4. *
  5. * (c) Daniele Alessandri <suppakilla@gmail.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Predis\Collection\Iterator;
  11. use PredisTestCase;
  12. /**
  13. * @group realm-iterators
  14. */
  15. class SortedSetKeyTest extends PredisTestCase
  16. {
  17. /**
  18. * @group disconnected
  19. * @expectedException \Predis\NotSupportedException
  20. * @expectedExceptionMessage 'ZSCAN' is not supported by the current command factory.
  21. */
  22. public function testThrowsExceptionOnMissingCommand()
  23. {
  24. $commands = $this->getMock('Predis\Command\FactoryInterface');
  25. $commands->expects($this->any())
  26. ->method('supportsCommand')
  27. ->will($this->returnValue(false));
  28. $client = $this->getMock('Predis\ClientInterface');
  29. $client->expects($this->any())
  30. ->method('getCommandFactory')
  31. ->will($this->returnValue($commands));
  32. new SortedSetKey($client, 'key:zset');
  33. }
  34. /**
  35. * @group disconnected
  36. */
  37. public function testIterationWithNoResults()
  38. {
  39. $client = $this->getMock('Predis\Client', array('getCommandFactory', 'zscan'));
  40. $client->expects($this->any())
  41. ->method('getCommandFactory')
  42. ->will($this->returnValue($this->getCommandFactory()));
  43. $client->expects($this->once())
  44. ->method('zscan')
  45. ->with('key:zset', 0, array())
  46. ->will($this->returnValue(array(0, array())));
  47. $iterator = new SortedSetKey($client, 'key:zset');
  48. $iterator->rewind();
  49. $this->assertFalse($iterator->valid());
  50. }
  51. /**
  52. * @link https://github.com/nrk/predis/issues/216
  53. * @group disconnected
  54. */
  55. public function testIterationWithIntegerMembers()
  56. {
  57. $client = $this->getMock('Predis\Client', array('getCommandFactory', 'zscan'));
  58. $client->expects($this->any())
  59. ->method('getCommandFactory')
  60. ->will($this->returnValue($this->getCommandFactory()));
  61. $client->expects($this->once())
  62. ->method('zscan')
  63. ->with('key:zset', 0, array())
  64. ->will($this->returnValue(array(0, array(
  65. 0 => 0, 101 => 1, 102 => 2,
  66. ))));
  67. $iterator = new SortedSetKey($client, 'key:zset');
  68. $iterator->rewind();
  69. $this->assertTrue($iterator->valid());
  70. $this->assertSame(0, $iterator->current());
  71. $this->assertSame(0, $iterator->key());
  72. $iterator->next();
  73. $this->assertTrue($iterator->valid());
  74. $this->assertSame(1, $iterator->current());
  75. $this->assertSame(101, $iterator->key());
  76. $iterator->next();
  77. $this->assertTrue($iterator->valid());
  78. $this->assertSame(2, $iterator->current());
  79. $this->assertSame(102, $iterator->key());
  80. $iterator->next();
  81. $this->assertFalse($iterator->valid());
  82. }
  83. /**
  84. * @group disconnected
  85. */
  86. public function testIterationOnSingleFetch()
  87. {
  88. $client = $this->getMock('Predis\Client', array('getCommandFactory', 'zscan'));
  89. $client->expects($this->any())
  90. ->method('getCommandFactory')
  91. ->will($this->returnValue($this->getCommandFactory()));
  92. $client->expects($this->once())
  93. ->method('zscan')
  94. ->with('key:zset', 0, array())
  95. ->will($this->returnValue(array(0, array(
  96. 'member:1st' => 1.0, 'member:2nd' => 2.0, 'member:3rd' => 3.0,
  97. ))));
  98. $iterator = new SortedSetKey($client, 'key:zset');
  99. $iterator->rewind();
  100. $this->assertTrue($iterator->valid());
  101. $this->assertSame(1.0, $iterator->current());
  102. $this->assertSame('member:1st', $iterator->key());
  103. $iterator->next();
  104. $this->assertTrue($iterator->valid());
  105. $this->assertSame(2.0, $iterator->current());
  106. $this->assertSame('member:2nd', $iterator->key());
  107. $iterator->next();
  108. $this->assertTrue($iterator->valid());
  109. $this->assertSame(3.0, $iterator->current());
  110. $this->assertSame('member:3rd', $iterator->key());
  111. $iterator->next();
  112. $this->assertFalse($iterator->valid());
  113. }
  114. /**
  115. * @group disconnected
  116. */
  117. public function testIterationOnMultipleFetches()
  118. {
  119. $client = $this->getMock('Predis\Client', array('getCommandFactory', 'zscan'));
  120. $client->expects($this->any())
  121. ->method('getCommandFactory')
  122. ->will($this->returnValue($this->getCommandFactory()));
  123. $client->expects($this->at(1))
  124. ->method('zscan')
  125. ->with('key:zset', 0, array())
  126. ->will($this->returnValue(array(2, array(
  127. 'member:1st' => 1.0, 'member:2nd' => 2.0,
  128. ))));
  129. $client->expects($this->at(2))
  130. ->method('zscan')
  131. ->with('key:zset', 2, array())
  132. ->will($this->returnValue(array(0, array(
  133. 'member:3rd' => 3.0,
  134. ))));
  135. $iterator = new SortedSetKey($client, 'key:zset');
  136. $iterator->rewind();
  137. $this->assertTrue($iterator->valid());
  138. $this->assertSame(1.0, $iterator->current());
  139. $this->assertSame('member:1st', $iterator->key());
  140. $iterator->next();
  141. $this->assertTrue($iterator->valid());
  142. $this->assertSame(2.0, $iterator->current());
  143. $this->assertSame('member:2nd', $iterator->key());
  144. $iterator->next();
  145. $this->assertTrue($iterator->valid());
  146. $this->assertSame(3.0, $iterator->current());
  147. $this->assertSame('member:3rd', $iterator->key());
  148. $iterator->next();
  149. $this->assertFalse($iterator->valid());
  150. }
  151. /**
  152. * @group disconnected
  153. */
  154. public function testIterationOnMultipleFetchesAndHoleInFirstFetch()
  155. {
  156. $client = $this->getMock('Predis\Client', array('getCommandFactory', 'zscan'));
  157. $client->expects($this->any())
  158. ->method('getCommandFactory')
  159. ->will($this->returnValue($this->getCommandFactory()));
  160. $client->expects($this->at(1))
  161. ->method('zscan')
  162. ->with('key:zset', 0, array())
  163. ->will($this->returnValue(array(4, array())));
  164. $client->expects($this->at(2))
  165. ->method('zscan')
  166. ->with('key:zset', 4, array())
  167. ->will($this->returnValue(array(0, array(
  168. 'member:1st' => 1.0, 'member:2nd' => 2.0,
  169. ))));
  170. $iterator = new SortedSetKey($client, 'key:zset');
  171. $iterator->rewind();
  172. $this->assertTrue($iterator->valid());
  173. $this->assertSame(1.0, $iterator->current());
  174. $this->assertSame('member:1st', $iterator->key());
  175. $iterator->next();
  176. $this->assertTrue($iterator->valid());
  177. $this->assertSame(2.0, $iterator->current());
  178. $this->assertSame('member:2nd', $iterator->key());
  179. $iterator->next();
  180. $this->assertFalse($iterator->valid());
  181. }
  182. /**
  183. * @group disconnected
  184. */
  185. public function testIterationOnMultipleFetchesAndHoleInMidFetch()
  186. {
  187. $client = $this->getMock('Predis\Client', array('getCommandFactory', 'zscan'));
  188. $client->expects($this->any())
  189. ->method('getCommandFactory')
  190. ->will($this->returnValue($this->getCommandFactory()));
  191. $client->expects($this->at(1))
  192. ->method('zscan')
  193. ->with('key:zset', 0, array())
  194. ->will($this->returnValue(array(2, array(
  195. 'member:1st' => 1.0, 'member:2nd' => 2.0,
  196. ))));
  197. $client->expects($this->at(2))
  198. ->method('zscan')
  199. ->with('key:zset', 2, array())
  200. ->will($this->returnValue(array(5, array())));
  201. $client->expects($this->at(3))
  202. ->method('zscan')
  203. ->with('key:zset', 5, array())
  204. ->will($this->returnValue(array(0, array(
  205. 'member:3rd' => 3.0,
  206. ))));
  207. $iterator = new SortedSetKey($client, 'key:zset');
  208. $iterator->rewind();
  209. $this->assertTrue($iterator->valid());
  210. $this->assertSame(1.0, $iterator->current());
  211. $this->assertSame('member:1st', $iterator->key());
  212. $iterator->next();
  213. $this->assertTrue($iterator->valid());
  214. $this->assertSame(2.0, $iterator->current());
  215. $this->assertSame('member:2nd', $iterator->key());
  216. $iterator->next();
  217. $this->assertTrue($iterator->valid());
  218. $this->assertSame(3.0, $iterator->current());
  219. $this->assertSame('member:3rd', $iterator->key());
  220. $iterator->next();
  221. $this->assertFalse($iterator->valid());
  222. }
  223. /**
  224. * @group disconnected
  225. */
  226. public function testIterationWithOptionMatch()
  227. {
  228. $client = $this->getMock('Predis\Client', array('getCommandFactory', 'zscan'));
  229. $client->expects($this->any())
  230. ->method('getCommandFactory')
  231. ->will($this->returnValue($this->getCommandFactory()));
  232. $client->expects($this->at(1))
  233. ->method('zscan')
  234. ->with('key:zset', 0, array('MATCH' => 'member:*'))
  235. ->will($this->returnValue(array(2, array(
  236. 'member:1st' => 1.0, 'member:2nd' => 2.0,
  237. ))));
  238. $iterator = new SortedSetKey($client, 'key:zset', 'member:*');
  239. $iterator->rewind();
  240. $this->assertTrue($iterator->valid());
  241. $this->assertSame(1.0, $iterator->current());
  242. $this->assertSame('member:1st', $iterator->key());
  243. $iterator->next();
  244. $this->assertTrue($iterator->valid());
  245. $this->assertSame(2.0, $iterator->current());
  246. $this->assertSame('member:2nd', $iterator->key());
  247. $iterator->next();
  248. $this->assertFalse($iterator->valid());
  249. }
  250. /**
  251. * @group disconnected
  252. */
  253. public function testIterationWithOptionMatchOnMultipleFetches()
  254. {
  255. $client = $this->getMock('Predis\Client', array('getCommandFactory', 'zscan'));
  256. $client->expects($this->any())
  257. ->method('getCommandFactory')
  258. ->will($this->returnValue($this->getCommandFactory()));
  259. $client->expects($this->at(1))
  260. ->method('zscan')
  261. ->with('key:zset', 0, array('MATCH' => 'member:*'))
  262. ->will($this->returnValue(array(1, array(
  263. 'member:1st' => 1.0,
  264. ))));
  265. $client->expects($this->at(2))
  266. ->method('zscan')
  267. ->with('key:zset', 1, array('MATCH' => 'member:*'))
  268. ->will($this->returnValue(array(0, array(
  269. 'member:2nd' => 2.0,
  270. ))));
  271. $iterator = new SortedSetKey($client, 'key:zset', 'member:*');
  272. $iterator->rewind();
  273. $this->assertTrue($iterator->valid());
  274. $this->assertSame(1.0, $iterator->current());
  275. $this->assertSame('member:1st', $iterator->key());
  276. $iterator->next();
  277. $this->assertTrue($iterator->valid());
  278. $this->assertSame(2.0, $iterator->current());
  279. $this->assertSame('member:2nd', $iterator->key());
  280. $iterator->next();
  281. $this->assertFalse($iterator->valid());
  282. }
  283. /**
  284. * @group disconnected
  285. */
  286. public function testIterationWithOptionCount()
  287. {
  288. $client = $this->getMock('Predis\Client', array('getCommandFactory', 'zscan'));
  289. $client->expects($this->any())
  290. ->method('getCommandFactory')
  291. ->will($this->returnValue($this->getCommandFactory()));
  292. $client->expects($this->at(1))
  293. ->method('zscan')
  294. ->with('key:zset', 0, array('COUNT' => 2))
  295. ->will($this->returnValue(array(0, array(
  296. 'member:1st' => 1.0, 'member:2nd' => 2.0,
  297. ))));
  298. $iterator = new SortedSetKey($client, 'key:zset', null, 2);
  299. $iterator->rewind();
  300. $this->assertTrue($iterator->valid());
  301. $this->assertSame(1.0, $iterator->current());
  302. $this->assertSame('member:1st', $iterator->key());
  303. $iterator->next();
  304. $this->assertTrue($iterator->valid());
  305. $this->assertSame(2.0, $iterator->current());
  306. $this->assertSame('member:2nd', $iterator->key());
  307. $iterator->next();
  308. $this->assertFalse($iterator->valid());
  309. }
  310. /**
  311. * @group disconnected
  312. */
  313. public function testIterationWithOptionCountOnMultipleFetches()
  314. {
  315. $client = $this->getMock('Predis\Client', array('getCommandFactory', 'zscan'));
  316. $client->expects($this->any())
  317. ->method('getCommandFactory')
  318. ->will($this->returnValue($this->getCommandFactory()));
  319. $client->expects($this->at(1))
  320. ->method('zscan')
  321. ->with('key:zset', 0, array('COUNT' => 1))
  322. ->will($this->returnValue(array(1, array(
  323. 'member:1st' => 1.0,
  324. ))));
  325. $client->expects($this->at(2))
  326. ->method('zscan')
  327. ->with('key:zset', 1, array('COUNT' => 1))
  328. ->will($this->returnValue(array(0, array(
  329. 'member:2nd' => 2.0,
  330. ))));
  331. $iterator = new SortedSetKey($client, 'key:zset', null, 1);
  332. $iterator->rewind();
  333. $this->assertTrue($iterator->valid());
  334. $this->assertSame(1.0, $iterator->current());
  335. $this->assertSame('member:1st', $iterator->key());
  336. $iterator->next();
  337. $this->assertTrue($iterator->valid());
  338. $this->assertSame(2.0, $iterator->current());
  339. $this->assertSame('member:2nd', $iterator->key());
  340. $iterator->next();
  341. $this->assertFalse($iterator->valid());
  342. }
  343. /**
  344. * @group disconnected
  345. */
  346. public function testIterationWithOptionsMatchAndCount()
  347. {
  348. $client = $this->getMock('Predis\Client', array('getCommandFactory', 'zscan'));
  349. $client->expects($this->any())
  350. ->method('getCommandFactory')
  351. ->will($this->returnValue($this->getCommandFactory()));
  352. $client->expects($this->at(1))
  353. ->method('zscan')
  354. ->with('key:zset', 0, array('MATCH' => 'member:*', 'COUNT' => 2))
  355. ->will($this->returnValue(array(0, array(
  356. 'member:1st' => 1.0, 'member:2nd' => 2.0,
  357. ))));
  358. $iterator = new SortedSetKey($client, 'key:zset', 'member:*', 2);
  359. $iterator->rewind();
  360. $this->assertTrue($iterator->valid());
  361. $this->assertSame(1.0, $iterator->current());
  362. $this->assertSame('member:1st', $iterator->key());
  363. $iterator->next();
  364. $this->assertTrue($iterator->valid());
  365. $this->assertSame(2.0, $iterator->current());
  366. $this->assertSame('member:2nd', $iterator->key());
  367. $iterator->next();
  368. $this->assertFalse($iterator->valid());
  369. }
  370. /**
  371. * @group disconnected
  372. */
  373. public function testIterationWithOptionsMatchAndCountOnMultipleFetches()
  374. {
  375. $client = $this->getMock('Predis\Client', array('getCommandFactory', 'zscan'));
  376. $client->expects($this->any())
  377. ->method('getCommandFactory')
  378. ->will($this->returnValue($this->getCommandFactory()));
  379. $client->expects($this->at(1))
  380. ->method('zscan')
  381. ->with('key:zset', 0, array('MATCH' => 'member:*', 'COUNT' => 1))
  382. ->will($this->returnValue(array(1, array(
  383. 'member:1st' => 1.0,
  384. ))));
  385. $client->expects($this->at(2))
  386. ->method('zscan')
  387. ->with('key:zset', 1, array('MATCH' => 'member:*', 'COUNT' => 1))
  388. ->will($this->returnValue(array(0, array(
  389. 'member:2nd' => 2.0,
  390. ))));
  391. $iterator = new SortedSetKey($client, 'key:zset', 'member:*', 1);
  392. $iterator->rewind();
  393. $this->assertTrue($iterator->valid());
  394. $this->assertSame(1.0, $iterator->current());
  395. $this->assertSame('member:1st', $iterator->key());
  396. $iterator->next();
  397. $this->assertTrue($iterator->valid());
  398. $this->assertSame(2.0, $iterator->current());
  399. $this->assertSame('member:2nd', $iterator->key());
  400. $iterator->next();
  401. $this->assertFalse($iterator->valid());
  402. }
  403. /**
  404. * @group disconnected
  405. */
  406. public function testIterationRewindable()
  407. {
  408. $client = $this->getMock('Predis\Client', array('getCommandFactory', 'zscan'));
  409. $client->expects($this->any())
  410. ->method('getCommandFactory')
  411. ->will($this->returnValue($this->getCommandFactory()));
  412. $client->expects($this->exactly(2))
  413. ->method('zscan')
  414. ->with('key:zset', 0, array())
  415. ->will($this->returnValue(array(0, array(
  416. 'member:1st' => 1.0, 'member:2nd' => 2.0,
  417. ))));
  418. $iterator = new SortedSetKey($client, 'key:zset');
  419. $iterator->rewind();
  420. $this->assertTrue($iterator->valid());
  421. $this->assertSame(1.0, $iterator->current());
  422. $this->assertSame('member:1st', $iterator->key());
  423. $iterator->rewind();
  424. $this->assertTrue($iterator->valid());
  425. $this->assertSame(1.0, $iterator->current());
  426. $this->assertSame('member:1st', $iterator->key());
  427. $iterator->next();
  428. $this->assertTrue($iterator->valid());
  429. $this->assertSame(2.0, $iterator->current());
  430. $this->assertSame('member:2nd', $iterator->key());
  431. $iterator->next();
  432. $this->assertFalse($iterator->valid());
  433. }
  434. }