KeyspaceTest.php 15 KB

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