SortedSetKeyTest.php 18 KB

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