SetIteratorTest.php 13 KB

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