SortedSetKeyTest.php 17 KB

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