KeyspaceTest.php 15 KB

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