SetKeyTest.php 15 KB

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