SortedSetKeyTest.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  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. use Predis\Profile;
  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. * @group disconnected
  50. */
  51. public function testIterationOnSingleFetch()
  52. {
  53. $client = $this->getMock('Predis\Client', array('getProfile', 'zscan'));
  54. $client->expects($this->any())
  55. ->method('getProfile')
  56. ->will($this->returnValue(Profile\Factory::get('2.8')));
  57. $client->expects($this->once())
  58. ->method('zscan')
  59. ->with('key:zset', 0, array())
  60. ->will($this->returnValue(array(0, array(
  61. 'member:1st' => 1.0, 'member:2nd' => 2.0, 'member:3rd' => 3.0,
  62. ))));
  63. $iterator = new SortedSetKey($client, 'key:zset');
  64. $iterator->rewind();
  65. $this->assertTrue($iterator->valid());
  66. $this->assertSame(1.0, $iterator->current());
  67. $this->assertSame('member:1st', $iterator->key());
  68. $iterator->next();
  69. $this->assertTrue($iterator->valid());
  70. $this->assertSame(2.0, $iterator->current());
  71. $this->assertSame('member:2nd', $iterator->key());
  72. $iterator->next();
  73. $this->assertTrue($iterator->valid());
  74. $this->assertSame(3.0, $iterator->current());
  75. $this->assertSame('member:3rd', $iterator->key());
  76. $iterator->next();
  77. $this->assertFalse($iterator->valid());
  78. }
  79. /**
  80. * @group disconnected
  81. */
  82. public function testIterationOnMultipleFetches()
  83. {
  84. $client = $this->getMock('Predis\Client', array('getProfile', 'zscan'));
  85. $client->expects($this->any())
  86. ->method('getProfile')
  87. ->will($this->returnValue(Profile\Factory::get('2.8')));
  88. $client->expects($this->at(1))
  89. ->method('zscan')
  90. ->with('key:zset', 0, array())
  91. ->will($this->returnValue(array(2, array(
  92. 'member:1st' => 1.0, 'member:2nd' => 2.0,
  93. ))));
  94. $client->expects($this->at(2))
  95. ->method('zscan')
  96. ->with('key:zset', 2, array())
  97. ->will($this->returnValue(array(0, array(
  98. 'member:3rd' => 3.0,
  99. ))));
  100. $iterator = new SortedSetKey($client, 'key:zset');
  101. $iterator->rewind();
  102. $this->assertTrue($iterator->valid());
  103. $this->assertSame(1.0, $iterator->current());
  104. $this->assertSame('member:1st', $iterator->key());
  105. $iterator->next();
  106. $this->assertTrue($iterator->valid());
  107. $this->assertSame(2.0, $iterator->current());
  108. $this->assertSame('member:2nd', $iterator->key());
  109. $iterator->next();
  110. $this->assertTrue($iterator->valid());
  111. $this->assertSame(3.0, $iterator->current());
  112. $this->assertSame('member:3rd', $iterator->key());
  113. $iterator->next();
  114. $this->assertFalse($iterator->valid());
  115. }
  116. /**
  117. * @group disconnected
  118. */
  119. public function testIterationOnMultipleFetchesAndHoleInFirstFetch()
  120. {
  121. $client = $this->getMock('Predis\Client', array('getProfile', 'zscan'));
  122. $client->expects($this->any())
  123. ->method('getProfile')
  124. ->will($this->returnValue(Profile\Factory::get('2.8')));
  125. $client->expects($this->at(1))
  126. ->method('zscan')
  127. ->with('key:zset', 0, array())
  128. ->will($this->returnValue(array(4, array())));
  129. $client->expects($this->at(2))
  130. ->method('zscan')
  131. ->with('key:zset', 4, array())
  132. ->will($this->returnValue(array(0, array(
  133. 'member:1st' => 1.0, 'member:2nd' => 2.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->assertFalse($iterator->valid());
  146. }
  147. /**
  148. * @group disconnected
  149. */
  150. public function testIterationOnMultipleFetchesAndHoleInMidFetch()
  151. {
  152. $client = $this->getMock('Predis\Client', array('getProfile', 'zscan'));
  153. $client->expects($this->any())
  154. ->method('getProfile')
  155. ->will($this->returnValue(Profile\Factory::get('2.8')));
  156. $client->expects($this->at(1))
  157. ->method('zscan')
  158. ->with('key:zset', 0, array())
  159. ->will($this->returnValue(array(2, array(
  160. 'member:1st' => 1.0, 'member:2nd' => 2.0,
  161. ))));
  162. $client->expects($this->at(2))
  163. ->method('zscan')
  164. ->with('key:zset', 2, array())
  165. ->will($this->returnValue(array(5, array())));
  166. $client->expects($this->at(3))
  167. ->method('zscan')
  168. ->with('key:zset', 5, array())
  169. ->will($this->returnValue(array(0, array(
  170. 'member:3rd' => 3.0
  171. ))));
  172. $iterator = new SortedSetKey($client, 'key:zset');
  173. $iterator->rewind();
  174. $this->assertTrue($iterator->valid());
  175. $this->assertSame(1.0, $iterator->current());
  176. $this->assertSame('member:1st', $iterator->key());
  177. $iterator->next();
  178. $this->assertTrue($iterator->valid());
  179. $this->assertSame(2.0, $iterator->current());
  180. $this->assertSame('member:2nd', $iterator->key());
  181. $iterator->next();
  182. $this->assertTrue($iterator->valid());
  183. $this->assertSame(3.0, $iterator->current());
  184. $this->assertSame('member:3rd', $iterator->key());
  185. $iterator->next();
  186. $this->assertFalse($iterator->valid());
  187. }
  188. /**
  189. * @group disconnected
  190. */
  191. public function testIterationWithOptionMatch()
  192. {
  193. $client = $this->getMock('Predis\Client', array('getProfile', 'zscan'));
  194. $client->expects($this->any())
  195. ->method('getProfile')
  196. ->will($this->returnValue(Profile\Factory::get('2.8')));
  197. $client->expects($this->at(1))
  198. ->method('zscan')
  199. ->with('key:zset', 0, array('MATCH' => 'member:*'))
  200. ->will($this->returnValue(array(2, array(
  201. 'member:1st' => 1.0, 'member:2nd' => 2.0,
  202. ))));
  203. $iterator = new SortedSetKey($client, 'key:zset', 'member:*');
  204. $iterator->rewind();
  205. $this->assertTrue($iterator->valid());
  206. $this->assertSame(1.0, $iterator->current());
  207. $this->assertSame('member:1st', $iterator->key());
  208. $iterator->next();
  209. $this->assertTrue($iterator->valid());
  210. $this->assertSame(2.0, $iterator->current());
  211. $this->assertSame('member:2nd', $iterator->key());
  212. $iterator->next();
  213. $this->assertFalse($iterator->valid());
  214. }
  215. /**
  216. * @group disconnected
  217. */
  218. public function testIterationWithOptionMatchOnMultipleFetches()
  219. {
  220. $client = $this->getMock('Predis\Client', array('getProfile', 'zscan'));
  221. $client->expects($this->any())
  222. ->method('getProfile')
  223. ->will($this->returnValue(Profile\Factory::get('2.8')));
  224. $client->expects($this->at(1))
  225. ->method('zscan')
  226. ->with('key:zset', 0, array('MATCH' => 'member:*'))
  227. ->will($this->returnValue(array(1, array(
  228. 'member:1st' => 1.0,
  229. ))));
  230. $client->expects($this->at(2))
  231. ->method('zscan')
  232. ->with('key:zset', 1, array('MATCH' => 'member:*'))
  233. ->will($this->returnValue(array(0, array(
  234. 'member:2nd' => 2.0,
  235. ))));
  236. $iterator = new SortedSetKey($client, 'key:zset', 'member:*');
  237. $iterator->rewind();
  238. $this->assertTrue($iterator->valid());
  239. $this->assertSame(1.0, $iterator->current());
  240. $this->assertSame('member:1st', $iterator->key());
  241. $iterator->next();
  242. $this->assertTrue($iterator->valid());
  243. $this->assertSame(2.0, $iterator->current());
  244. $this->assertSame('member:2nd', $iterator->key());
  245. $iterator->next();
  246. $this->assertFalse($iterator->valid());
  247. }
  248. /**
  249. * @group disconnected
  250. */
  251. public function testIterationWithOptionCount()
  252. {
  253. $client = $this->getMock('Predis\Client', array('getProfile', 'zscan'));
  254. $client->expects($this->any())
  255. ->method('getProfile')
  256. ->will($this->returnValue(Profile\Factory::get('2.8')));
  257. $client->expects($this->at(1))
  258. ->method('zscan')
  259. ->with('key:zset', 0, array('COUNT' => 2))
  260. ->will($this->returnValue(array(0, array(
  261. 'member:1st' => 1.0, 'member:2nd' => 2.0,
  262. ))));
  263. $iterator = new SortedSetKey($client, 'key:zset', null, 2);
  264. $iterator->rewind();
  265. $this->assertTrue($iterator->valid());
  266. $this->assertSame(1.0, $iterator->current());
  267. $this->assertSame('member:1st', $iterator->key());
  268. $iterator->next();
  269. $this->assertTrue($iterator->valid());
  270. $this->assertSame(2.0, $iterator->current());
  271. $this->assertSame('member:2nd', $iterator->key());
  272. $iterator->next();
  273. $this->assertFalse($iterator->valid());
  274. }
  275. /**
  276. * @group disconnected
  277. */
  278. public function testIterationWithOptionCountOnMultipleFetches()
  279. {
  280. $client = $this->getMock('Predis\Client', array('getProfile', 'zscan'));
  281. $client->expects($this->any())
  282. ->method('getProfile')
  283. ->will($this->returnValue(Profile\Factory::get('2.8')));
  284. $client->expects($this->at(1))
  285. ->method('zscan')
  286. ->with('key:zset', 0, array('COUNT' => 1))
  287. ->will($this->returnValue(array(1, array(
  288. 'member:1st' => 1.0,
  289. ))));
  290. $client->expects($this->at(2))
  291. ->method('zscan')
  292. ->with('key:zset', 1, array('COUNT' => 1))
  293. ->will($this->returnValue(array(0, array(
  294. 'member:2nd' => 2.0,
  295. ))));
  296. $iterator = new SortedSetKey($client, 'key:zset', null, 1);
  297. $iterator->rewind();
  298. $this->assertTrue($iterator->valid());
  299. $this->assertSame(1.0, $iterator->current());
  300. $this->assertSame('member:1st', $iterator->key());
  301. $iterator->next();
  302. $this->assertTrue($iterator->valid());
  303. $this->assertSame(2.0, $iterator->current());
  304. $this->assertSame('member:2nd', $iterator->key());
  305. $iterator->next();
  306. $this->assertFalse($iterator->valid());
  307. }
  308. /**
  309. * @group disconnected
  310. */
  311. public function testIterationWithOptionsMatchAndCount()
  312. {
  313. $client = $this->getMock('Predis\Client', array('getProfile', 'zscan'));
  314. $client->expects($this->any())
  315. ->method('getProfile')
  316. ->will($this->returnValue(Profile\Factory::get('2.8')));
  317. $client->expects($this->at(1))
  318. ->method('zscan')
  319. ->with('key:zset', 0, array('MATCH' => 'member:*', 'COUNT' => 2))
  320. ->will($this->returnValue(array(0, array(
  321. 'member:1st' => 1.0, 'member:2nd' => 2.0,
  322. ))));
  323. $iterator = new SortedSetKey($client, 'key:zset', 'member:*', 2);
  324. $iterator->rewind();
  325. $this->assertTrue($iterator->valid());
  326. $this->assertSame(1.0, $iterator->current());
  327. $this->assertSame('member:1st', $iterator->key());
  328. $iterator->next();
  329. $this->assertTrue($iterator->valid());
  330. $this->assertSame(2.0, $iterator->current());
  331. $this->assertSame('member:2nd', $iterator->key());
  332. $iterator->next();
  333. $this->assertFalse($iterator->valid());
  334. }
  335. /**
  336. * @group disconnected
  337. */
  338. public function testIterationWithOptionsMatchAndCountOnMultipleFetches()
  339. {
  340. $client = $this->getMock('Predis\Client', array('getProfile', 'zscan'));
  341. $client->expects($this->any())
  342. ->method('getProfile')
  343. ->will($this->returnValue(Profile\Factory::get('2.8')));
  344. $client->expects($this->at(1))
  345. ->method('zscan')
  346. ->with('key:zset', 0, array('MATCH' => 'member:*', 'COUNT' => 1))
  347. ->will($this->returnValue(array(1, array(
  348. 'member:1st' => 1.0,
  349. ))));
  350. $client->expects($this->at(2))
  351. ->method('zscan')
  352. ->with('key:zset', 1, array('MATCH' => 'member:*', 'COUNT' => 1))
  353. ->will($this->returnValue(array(0, array(
  354. 'member:2nd' => 2.0,
  355. ))));
  356. $iterator = new SortedSetKey($client, 'key:zset', 'member:*', 1);
  357. $iterator->rewind();
  358. $this->assertTrue($iterator->valid());
  359. $this->assertSame(1.0, $iterator->current());
  360. $this->assertSame('member:1st', $iterator->key());
  361. $iterator->next();
  362. $this->assertTrue($iterator->valid());
  363. $this->assertSame(2.0, $iterator->current());
  364. $this->assertSame('member:2nd', $iterator->key());
  365. $iterator->next();
  366. $this->assertFalse($iterator->valid());
  367. }
  368. /**
  369. * @group disconnected
  370. */
  371. public function testIterationRewindable()
  372. {
  373. $client = $this->getMock('Predis\Client', array('getProfile', 'zscan'));
  374. $client->expects($this->any())
  375. ->method('getProfile')
  376. ->will($this->returnValue(Profile\Factory::get('2.8')));
  377. $client->expects($this->exactly(2))
  378. ->method('zscan')
  379. ->with('key:zset', 0, array())
  380. ->will($this->returnValue(array(0, array(
  381. 'member:1st' => 1.0, 'member:2nd' => 2.0,
  382. ))));
  383. $iterator = new SortedSetKey($client, 'key:zset');
  384. $iterator->rewind();
  385. $this->assertTrue($iterator->valid());
  386. $this->assertSame(1.0, $iterator->current());
  387. $this->assertSame('member:1st', $iterator->key());
  388. $iterator->rewind();
  389. $this->assertTrue($iterator->valid());
  390. $this->assertSame(1.0, $iterator->current());
  391. $this->assertSame('member:1st', $iterator->key());
  392. $iterator->next();
  393. $this->assertTrue($iterator->valid());
  394. $this->assertSame(2.0, $iterator->current());
  395. $this->assertSame('member:2nd', $iterator->key());
  396. $iterator->next();
  397. $this->assertFalse($iterator->valid());
  398. }
  399. }