SortedSetIteratorTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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\Iterator\Scan;
  11. use \PHPUnit_Framework_TestCase as StandardTestCase;
  12. use Predis\Client;
  13. use Predis\Profile\ServerProfile;
  14. /**
  15. * @group realm-iterators
  16. */
  17. class SortedSetIteratorTest extends StandardTestCase
  18. {
  19. /**
  20. * @group disconnected
  21. * @expectedException Predis\NotSupportedException
  22. * @expectedExceptionMessage The specified server profile does not support the `ZSCAN` command.
  23. */
  24. public function testThrowsExceptionOnInvalidServerProfile()
  25. {
  26. $client = $this->getMock('Predis\ClientInterface');
  27. $client->expects($this->any())
  28. ->method('getProfile')
  29. ->will($this->returnValue(ServerProfile::get('2.0')));
  30. $iterator = new SortedSetIterator($client, 'key:zset');
  31. }
  32. /**
  33. * @group disconnected
  34. */
  35. public function testIterationOnSingleFetch()
  36. {
  37. $client = $this->getMock('Predis\Client', array('getProfile', 'zscan'));
  38. $client->expects($this->any())
  39. ->method('getProfile')
  40. ->will($this->returnValue(ServerProfile::get('2.8')));
  41. $client->expects($this->once())
  42. ->method('zscan')
  43. ->with('key:zset', 0, array())
  44. ->will($this->returnValue(array(0, array(
  45. array('member:1st', 1.0), array('member:2nd', 2.0), array('member:3rd', 3.0),
  46. ))));
  47. $iterator = new SortedSetIterator($client, 'key:zset');
  48. $iterator->rewind();
  49. $this->assertTrue($iterator->valid());
  50. $this->assertSame(1.0, $iterator->current());
  51. $this->assertSame('member:1st', $iterator->key());
  52. $iterator->next();
  53. $this->assertTrue($iterator->valid());
  54. $this->assertSame(2.0, $iterator->current());
  55. $this->assertSame('member:2nd', $iterator->key());
  56. $iterator->next();
  57. $this->assertTrue($iterator->valid());
  58. $this->assertSame(3.0, $iterator->current());
  59. $this->assertSame('member:3rd', $iterator->key());
  60. $iterator->next();
  61. $this->assertFalse($iterator->valid());
  62. }
  63. /**
  64. * @group disconnected
  65. */
  66. public function testIterationOnMultipleFetches()
  67. {
  68. $client = $this->getMock('Predis\Client', array('getProfile', 'zscan'));
  69. $client->expects($this->any())
  70. ->method('getProfile')
  71. ->will($this->returnValue(ServerProfile::get('2.8')));
  72. $client->expects($this->at(1))
  73. ->method('zscan')
  74. ->with('key:zset', 0, array())
  75. ->will($this->returnValue(array(2, array(
  76. array('member:1st', 1.0), array('member:2nd', 2.0),
  77. ))));
  78. $client->expects($this->at(2))
  79. ->method('zscan')
  80. ->with('key:zset', 2, array())
  81. ->will($this->returnValue(array(0, array(
  82. array('member:3rd', 3.0),
  83. ))));
  84. $iterator = new SortedSetIterator($client, 'key:zset');
  85. $iterator->rewind();
  86. $this->assertTrue($iterator->valid());
  87. $this->assertSame(1.0, $iterator->current());
  88. $this->assertSame('member:1st', $iterator->key());
  89. $iterator->next();
  90. $this->assertTrue($iterator->valid());
  91. $this->assertSame(2.0, $iterator->current());
  92. $this->assertSame('member:2nd', $iterator->key());
  93. $iterator->next();
  94. $this->assertTrue($iterator->valid());
  95. $this->assertSame(3.0, $iterator->current());
  96. $this->assertSame('member:3rd', $iterator->key());
  97. $iterator->next();
  98. $this->assertFalse($iterator->valid());
  99. }
  100. /**
  101. * @group disconnected
  102. */
  103. public function testIterationWithMultipleFetchesAndHoles()
  104. {
  105. $client = $this->getMock('Predis\Client', array('getProfile', 'zscan'));
  106. $client->expects($this->any())
  107. ->method('getProfile')
  108. ->will($this->returnValue(ServerProfile::get('2.8')));
  109. $client->expects($this->at(1))
  110. ->method('zscan')
  111. ->with('key:zset', 0, array())
  112. ->will($this->returnValue(array(2, array(
  113. array('member:1st', 1.0), array('member:2nd', 2.0),
  114. ))));
  115. $client->expects($this->at(2))
  116. ->method('zscan')
  117. ->with('key:zset', 2, array())
  118. ->will($this->returnValue(array(5, array())));
  119. $client->expects($this->at(3))
  120. ->method('zscan')
  121. ->with('key:zset', 5, array())
  122. ->will($this->returnValue(array(0, array(
  123. array('member:3rd', 3.0)
  124. ))));
  125. $iterator = new SortedSetIterator($client, 'key:zset');
  126. $iterator->rewind();
  127. $this->assertTrue($iterator->valid());
  128. $this->assertSame(1.0, $iterator->current());
  129. $this->assertSame('member:1st', $iterator->key());
  130. $iterator->next();
  131. $this->assertTrue($iterator->valid());
  132. $this->assertSame(2.0, $iterator->current());
  133. $this->assertSame('member:2nd', $iterator->key());
  134. $iterator->next();
  135. $this->assertTrue($iterator->valid());
  136. $this->assertSame(3.0, $iterator->current());
  137. $this->assertSame('member:3rd', $iterator->key());
  138. $iterator->next();
  139. $this->assertFalse($iterator->valid());
  140. }
  141. /**
  142. * @group disconnected
  143. */
  144. public function testIterationWithOptionMatch()
  145. {
  146. $client = $this->getMock('Predis\Client', array('getProfile', 'zscan'));
  147. $client->expects($this->any())
  148. ->method('getProfile')
  149. ->will($this->returnValue(ServerProfile::get('2.8')));
  150. $client->expects($this->at(1))
  151. ->method('zscan')
  152. ->with('key:zset', 0, array('MATCH' => 'member:*'))
  153. ->will($this->returnValue(array(2, array(
  154. array('member:1st', 1.0), array('member:2nd', 2.0),
  155. ))));
  156. $iterator = new SortedSetIterator($client, 'key:zset', 'member:*');
  157. $iterator->rewind();
  158. $this->assertTrue($iterator->valid());
  159. $this->assertSame(1.0, $iterator->current());
  160. $this->assertSame('member:1st', $iterator->key());
  161. $iterator->next();
  162. $this->assertTrue($iterator->valid());
  163. $this->assertSame(2.0, $iterator->current());
  164. $this->assertSame('member:2nd', $iterator->key());
  165. $iterator->next();
  166. $this->assertFalse($iterator->valid());
  167. }
  168. /**
  169. * @group disconnected
  170. */
  171. public function testIterationWithOptionMatchOnMultipleFetches()
  172. {
  173. $client = $this->getMock('Predis\Client', array('getProfile', 'zscan'));
  174. $client->expects($this->any())
  175. ->method('getProfile')
  176. ->will($this->returnValue(ServerProfile::get('2.8')));
  177. $client->expects($this->at(1))
  178. ->method('zscan')
  179. ->with('key:zset', 0, array('MATCH' => 'member:*'))
  180. ->will($this->returnValue(array(1, array(
  181. array('member:1st', 1.0),
  182. ))));
  183. $client->expects($this->at(2))
  184. ->method('zscan')
  185. ->with('key:zset', 1, array('MATCH' => 'member:*'))
  186. ->will($this->returnValue(array(0, array(
  187. array('member:2nd', 2.0),
  188. ))));
  189. $iterator = new SortedSetIterator($client, 'key:zset', 'member:*');
  190. $iterator->rewind();
  191. $this->assertTrue($iterator->valid());
  192. $this->assertSame(1.0, $iterator->current());
  193. $this->assertSame('member:1st', $iterator->key());
  194. $iterator->next();
  195. $this->assertTrue($iterator->valid());
  196. $this->assertSame(2.0, $iterator->current());
  197. $this->assertSame('member:2nd', $iterator->key());
  198. $iterator->next();
  199. $this->assertFalse($iterator->valid());
  200. }
  201. /**
  202. * @group disconnected
  203. */
  204. public function testIterationWithOptionCount()
  205. {
  206. $client = $this->getMock('Predis\Client', array('getProfile', 'zscan'));
  207. $client->expects($this->any())
  208. ->method('getProfile')
  209. ->will($this->returnValue(ServerProfile::get('2.8')));
  210. $client->expects($this->at(1))
  211. ->method('zscan')
  212. ->with('key:zset', 0, array('COUNT' => 2))
  213. ->will($this->returnValue(array(0, array(
  214. array('member:1st', 1.0), array('member:2nd', 2.0),
  215. ))));
  216. $iterator = new SortedSetIterator($client, 'key:zset', null, 2);
  217. $iterator->rewind();
  218. $this->assertTrue($iterator->valid());
  219. $this->assertSame(1.0, $iterator->current());
  220. $this->assertSame('member:1st', $iterator->key());
  221. $iterator->next();
  222. $this->assertTrue($iterator->valid());
  223. $this->assertSame(2.0, $iterator->current());
  224. $this->assertSame('member:2nd', $iterator->key());
  225. $iterator->next();
  226. $this->assertFalse($iterator->valid());
  227. }
  228. /**
  229. * @group disconnected
  230. */
  231. public function testIterationWithOptionCountOnMultipleFetches()
  232. {
  233. $client = $this->getMock('Predis\Client', array('getProfile', 'zscan'));
  234. $client->expects($this->any())
  235. ->method('getProfile')
  236. ->will($this->returnValue(ServerProfile::get('2.8')));
  237. $client->expects($this->at(1))
  238. ->method('zscan')
  239. ->with('key:zset', 0, array('COUNT' => 1))
  240. ->will($this->returnValue(array(1, array(
  241. array('member:1st', 1.0),
  242. ))));
  243. $client->expects($this->at(2))
  244. ->method('zscan')
  245. ->with('key:zset', 1, array('COUNT' => 1))
  246. ->will($this->returnValue(array(0, array(
  247. array('member:2nd', 2.0),
  248. ))));
  249. $iterator = new SortedSetIterator($client, 'key:zset', null, 1);
  250. $iterator->rewind();
  251. $this->assertTrue($iterator->valid());
  252. $this->assertSame(1.0, $iterator->current());
  253. $this->assertSame('member:1st', $iterator->key());
  254. $iterator->next();
  255. $this->assertTrue($iterator->valid());
  256. $this->assertSame(2.0, $iterator->current());
  257. $this->assertSame('member:2nd', $iterator->key());
  258. $iterator->next();
  259. $this->assertFalse($iterator->valid());
  260. }
  261. /**
  262. * @group disconnected
  263. */
  264. public function testIterationWithOptionsMatchAndCount()
  265. {
  266. $client = $this->getMock('Predis\Client', array('getProfile', 'zscan'));
  267. $client->expects($this->any())
  268. ->method('getProfile')
  269. ->will($this->returnValue(ServerProfile::get('2.8')));
  270. $client->expects($this->at(1))
  271. ->method('zscan')
  272. ->with('key:zset', 0, array('MATCH' => 'member:*', 'COUNT' => 2))
  273. ->will($this->returnValue(array(0, array(
  274. array('member:1st', 1.0), array('member:2nd', 2.0),
  275. ))));
  276. $iterator = new SortedSetIterator($client, 'key:zset', 'member:*', 2);
  277. $iterator->rewind();
  278. $this->assertTrue($iterator->valid());
  279. $this->assertSame(1.0, $iterator->current());
  280. $this->assertSame('member:1st', $iterator->key());
  281. $iterator->next();
  282. $this->assertTrue($iterator->valid());
  283. $this->assertSame(2.0, $iterator->current());
  284. $this->assertSame('member:2nd', $iterator->key());
  285. $iterator->next();
  286. $this->assertFalse($iterator->valid());
  287. }
  288. /**
  289. * @group disconnected
  290. */
  291. public function testIterationWithOptionsMatchAndCountOnMultipleFetches()
  292. {
  293. $client = $this->getMock('Predis\Client', array('getProfile', 'zscan'));
  294. $client->expects($this->any())
  295. ->method('getProfile')
  296. ->will($this->returnValue(ServerProfile::get('2.8')));
  297. $client->expects($this->at(1))
  298. ->method('zscan')
  299. ->with('key:zset', 0, array('MATCH' => 'member:*', 'COUNT' => 1))
  300. ->will($this->returnValue(array(1, array(
  301. array('member:1st', 1.0),
  302. ))));
  303. $client->expects($this->at(2))
  304. ->method('zscan')
  305. ->with('key:zset', 1, array('MATCH' => 'member:*', 'COUNT' => 1))
  306. ->will($this->returnValue(array(0, array(
  307. array('member:2nd', 2.0),
  308. ))));
  309. $iterator = new SortedSetIterator($client, 'key:zset', 'member:*', 1);
  310. $iterator->rewind();
  311. $this->assertTrue($iterator->valid());
  312. $this->assertSame(1.0, $iterator->current());
  313. $this->assertSame('member:1st', $iterator->key());
  314. $iterator->next();
  315. $this->assertTrue($iterator->valid());
  316. $this->assertSame(2.0, $iterator->current());
  317. $this->assertSame('member:2nd', $iterator->key());
  318. $iterator->next();
  319. $this->assertFalse($iterator->valid());
  320. }
  321. /**
  322. * @group disconnected
  323. */
  324. public function testIterationRewindable()
  325. {
  326. $client = $this->getMock('Predis\Client', array('getProfile', 'zscan'));
  327. $client->expects($this->any())
  328. ->method('getProfile')
  329. ->will($this->returnValue(ServerProfile::get('2.8')));
  330. $client->expects($this->exactly(2))
  331. ->method('zscan')
  332. ->with('key:zset', 0, array())
  333. ->will($this->returnValue(array(0, array(
  334. array('member:1st', 1.0), array('member:2nd', 2.0),
  335. ))));
  336. $iterator = new SortedSetIterator($client, 'key:zset');
  337. $iterator->rewind();
  338. $this->assertTrue($iterator->valid());
  339. $this->assertSame(1.0, $iterator->current());
  340. $this->assertSame('member:1st', $iterator->key());
  341. $iterator->rewind();
  342. $this->assertTrue($iterator->valid());
  343. $this->assertSame(1.0, $iterator->current());
  344. $this->assertSame('member:1st', $iterator->key());
  345. $iterator->next();
  346. $this->assertTrue($iterator->valid());
  347. $this->assertSame(2.0, $iterator->current());
  348. $this->assertSame('member:2nd', $iterator->key());
  349. $iterator->next();
  350. $this->assertFalse($iterator->valid());
  351. }
  352. }