KeyspaceTest.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  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
  26. ->expects($this->any())
  27. ->method('supportsCommand')
  28. ->will($this->returnValue(false));
  29. $client = $this->getMock('Predis\ClientInterface');
  30. $client
  31. ->expects($this->any())
  32. ->method('getCommandFactory')
  33. ->will($this->returnValue($commands));
  34. new Keyspace($client);
  35. }
  36. /**
  37. * @group disconnected
  38. */
  39. public function testIterationWithNoResults()
  40. {
  41. $client = $this->getMock('Predis\Client', array('getCommandFactory', 'scan'));
  42. $client
  43. ->expects($this->any())
  44. ->method('getCommandFactory')
  45. ->will($this->returnValue($this->getCommandFactory()));
  46. $client
  47. ->expects($this->once())
  48. ->method('scan')
  49. ->with(0, array())
  50. ->will($this->returnValue(
  51. array(0, array())
  52. ));
  53. $iterator = new Keyspace($client);
  54. $iterator->rewind();
  55. $this->assertFalse($iterator->valid());
  56. }
  57. /**
  58. * @group disconnected
  59. */
  60. public function testIterationOnSingleFetch()
  61. {
  62. $client = $this->getMock('Predis\Client', array('getCommandFactory', 'scan'));
  63. $client
  64. ->expects($this->any())
  65. ->method('getCommandFactory')
  66. ->will($this->returnValue($this->getCommandFactory()));
  67. $client
  68. ->expects($this->once())
  69. ->method('scan')
  70. ->with(0, array())
  71. ->will($this->returnValue(
  72. array(0, array('key:1st', 'key:2nd', 'key:3rd'))
  73. ));
  74. $iterator = new Keyspace($client);
  75. $iterator->rewind();
  76. $this->assertTrue($iterator->valid());
  77. $this->assertSame('key:1st', $iterator->current());
  78. $this->assertSame(0, $iterator->key());
  79. $iterator->next();
  80. $this->assertTrue($iterator->valid());
  81. $this->assertSame('key:2nd', $iterator->current());
  82. $this->assertSame(1, $iterator->key());
  83. $iterator->next();
  84. $this->assertTrue($iterator->valid());
  85. $this->assertSame('key:3rd', $iterator->current());
  86. $this->assertSame(2, $iterator->key());
  87. $iterator->next();
  88. $this->assertFalse($iterator->valid());
  89. }
  90. /**
  91. * @group disconnected
  92. */
  93. public function testIterationOnMultipleFetches()
  94. {
  95. $client = $this->getMock('Predis\Client', array('getCommandFactory', 'scan'));
  96. $client
  97. ->expects($this->any())
  98. ->method('getCommandFactory')
  99. ->will($this->returnValue($this->getCommandFactory()));
  100. $client
  101. ->expects($this->at(1))
  102. ->method('scan')
  103. ->with(0, array())
  104. ->will($this->returnValue(
  105. array(2, array('key:1st', 'key:2nd'))
  106. ));
  107. $client
  108. ->expects($this->at(2))
  109. ->method('scan')
  110. ->with(2, array())
  111. ->will($this->returnValue(
  112. array(0, array('key:3rd'))
  113. ));
  114. $iterator = new Keyspace($client);
  115. $iterator->rewind();
  116. $this->assertTrue($iterator->valid());
  117. $this->assertSame('key:1st', $iterator->current());
  118. $this->assertSame(0, $iterator->key());
  119. $iterator->next();
  120. $this->assertTrue($iterator->valid());
  121. $this->assertSame('key:2nd', $iterator->current());
  122. $this->assertSame(1, $iterator->key());
  123. $iterator->next();
  124. $this->assertTrue($iterator->valid());
  125. $this->assertSame('key:3rd', $iterator->current());
  126. $this->assertSame(2, $iterator->key());
  127. $iterator->next();
  128. $this->assertFalse($iterator->valid());
  129. }
  130. /**
  131. * @group disconnected
  132. */
  133. public function testIterationOnMultipleFetchesAndHoleInFirstFetch()
  134. {
  135. $client = $this->getMock('Predis\Client', array('getCommandFactory', 'scan'));
  136. $client
  137. ->expects($this->any())
  138. ->method('getCommandFactory')
  139. ->will($this->returnValue($this->getCommandFactory()));
  140. $client
  141. ->expects($this->at(1))
  142. ->method('scan')
  143. ->with(0, array())
  144. ->will($this->returnValue(
  145. array(4, array())
  146. ));
  147. $client
  148. ->expects($this->at(2))
  149. ->method('scan')
  150. ->with(4, array())
  151. ->will($this->returnValue(
  152. array(0, array('key:1st', 'key:2nd'))
  153. ));
  154. $iterator = new Keyspace($client);
  155. $iterator->rewind();
  156. $this->assertTrue($iterator->valid());
  157. $this->assertSame('key:1st', $iterator->current());
  158. $this->assertSame(0, $iterator->key());
  159. $iterator->next();
  160. $this->assertTrue($iterator->valid());
  161. $this->assertSame('key:2nd', $iterator->current());
  162. $this->assertSame(1, $iterator->key());
  163. $iterator->next();
  164. $this->assertFalse($iterator->valid());
  165. }
  166. /**
  167. * @group disconnected
  168. */
  169. public function testIterationOnMultipleFetchesAndHoleInMidFetch()
  170. {
  171. $client = $this->getMock('Predis\Client', array('getCommandFactory', 'scan'));
  172. $client
  173. ->expects($this->any())
  174. ->method('getCommandFactory')
  175. ->will($this->returnValue($this->getCommandFactory()));
  176. $client
  177. ->expects($this->at(1))
  178. ->method('scan')
  179. ->with(0, array())
  180. ->will($this->returnValue(
  181. array(2, array('key:1st', 'key:2nd'))
  182. ));
  183. $client
  184. ->expects($this->at(2))
  185. ->method('scan')
  186. ->with(2, array())
  187. ->will($this->returnValue(
  188. array(5, array())
  189. ));
  190. $client
  191. ->expects($this->at(3))
  192. ->method('scan')
  193. ->with(5, array())
  194. ->will($this->returnValue(
  195. array(0, array('key:3rd'))
  196. ));
  197. $iterator = new Keyspace($client);
  198. $iterator->rewind();
  199. $this->assertTrue($iterator->valid());
  200. $this->assertSame('key:1st', $iterator->current());
  201. $this->assertSame(0, $iterator->key());
  202. $iterator->next();
  203. $this->assertTrue($iterator->valid());
  204. $this->assertSame('key:2nd', $iterator->current());
  205. $this->assertSame(1, $iterator->key());
  206. $iterator->next();
  207. $this->assertTrue($iterator->valid());
  208. $this->assertSame('key:3rd', $iterator->current());
  209. $this->assertSame(2, $iterator->key());
  210. $iterator->next();
  211. $this->assertFalse($iterator->valid());
  212. }
  213. /**
  214. * @group disconnected
  215. */
  216. public function testIterationWithOptionMatch()
  217. {
  218. $client = $this->getMock('Predis\Client', array('getCommandFactory', 'scan'));
  219. $client
  220. ->expects($this->any())
  221. ->method('getCommandFactory')
  222. ->will($this->returnValue($this->getCommandFactory()));
  223. $client
  224. ->expects($this->at(1))
  225. ->method('scan')
  226. ->with(0, array('MATCH' => 'key:*'))
  227. ->will($this->returnValue(
  228. array(0, array('key:1st', 'key:2nd'))
  229. ));
  230. $iterator = new Keyspace($client, 'key:*');
  231. $iterator->rewind();
  232. $this->assertTrue($iterator->valid());
  233. $this->assertSame('key:1st', $iterator->current());
  234. $this->assertSame(0, $iterator->key());
  235. $iterator->next();
  236. $this->assertTrue($iterator->valid());
  237. $this->assertSame('key:2nd', $iterator->current());
  238. $this->assertSame(1, $iterator->key());
  239. $iterator->next();
  240. $this->assertFalse($iterator->valid());
  241. }
  242. /**
  243. * @group disconnected
  244. */
  245. public function testIterationWithOptionMatchOnMultipleFetches()
  246. {
  247. $client = $this->getMock('Predis\Client', array('getCommandFactory', 'scan'));
  248. $client
  249. ->expects($this->any())
  250. ->method('getCommandFactory')
  251. ->will($this->returnValue($this->getCommandFactory()));
  252. $client
  253. ->expects($this->at(1))
  254. ->method('scan')
  255. ->with(0, array('MATCH' => 'key:*'))
  256. ->will($this->returnValue(
  257. array(1, array('key:1st'))
  258. ));
  259. $client
  260. ->expects($this->at(2))
  261. ->method('scan')
  262. ->with(1, array('MATCH' => 'key:*'))
  263. ->will($this->returnValue(
  264. array(0, array('key:2nd'))
  265. ));
  266. $iterator = new Keyspace($client, 'key:*');
  267. $iterator->rewind();
  268. $this->assertTrue($iterator->valid());
  269. $this->assertSame('key:1st', $iterator->current());
  270. $this->assertSame(0, $iterator->key());
  271. $iterator->next();
  272. $this->assertTrue($iterator->valid());
  273. $this->assertSame('key:2nd', $iterator->current());
  274. $this->assertSame(1, $iterator->key());
  275. $iterator->next();
  276. $this->assertFalse($iterator->valid());
  277. }
  278. /**
  279. * @group disconnected
  280. */
  281. public function testIterationWithOptionCount()
  282. {
  283. $client = $this->getMock('Predis\Client', array('getCommandFactory', 'scan'));
  284. $client
  285. ->expects($this->any())
  286. ->method('getCommandFactory')
  287. ->will($this->returnValue($this->getCommandFactory()));
  288. $client
  289. ->expects($this->at(1))
  290. ->method('scan')
  291. ->with(0, array('COUNT' => 2))
  292. ->will($this->returnValue(
  293. array(0, array('key:1st', 'key:2nd'))
  294. ));
  295. $iterator = new Keyspace($client, null, 2);
  296. $iterator->rewind();
  297. $this->assertTrue($iterator->valid());
  298. $this->assertSame('key:1st', $iterator->current());
  299. $this->assertSame(0, $iterator->key());
  300. $iterator->next();
  301. $this->assertTrue($iterator->valid());
  302. $this->assertSame('key:2nd', $iterator->current());
  303. $this->assertSame(1, $iterator->key());
  304. $iterator->next();
  305. $this->assertFalse($iterator->valid());
  306. }
  307. /**
  308. * @group disconnected
  309. */
  310. public function testIterationWithOptionCountOnMultipleFetches()
  311. {
  312. $client = $this->getMock('Predis\Client', array('getCommandFactory', 'scan'));
  313. $client
  314. ->expects($this->any())
  315. ->method('getCommandFactory')
  316. ->will($this->returnValue($this->getCommandFactory()));
  317. $client
  318. ->expects($this->at(1))
  319. ->method('scan')
  320. ->with(0, array('COUNT' => 1))
  321. ->will($this->returnValue(array(1, array('key:1st'))));
  322. $client
  323. ->expects($this->at(2))
  324. ->method('scan')
  325. ->with(1, array('COUNT' => 1))
  326. ->will($this->returnValue(
  327. array(0, array('key:2nd'))
  328. ));
  329. $iterator = new Keyspace($client, null, 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 testIterationWithOptionsMatchAndCount()
  345. {
  346. $client = $this->getMock('Predis\Client', array('getCommandFactory', 'scan'));
  347. $client
  348. ->expects($this->any())
  349. ->method('getCommandFactory')
  350. ->will($this->returnValue($this->getCommandFactory()));
  351. $client
  352. ->expects($this->at(1))
  353. ->method('scan')
  354. ->with(0, array('MATCH' => 'key:*', 'COUNT' => 2))
  355. ->will($this->returnValue(
  356. array(0, array('key:1st', 'key:2nd'))
  357. ));
  358. $iterator = new Keyspace($client, 'key:*', 2);
  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('key:2nd', $iterator->current());
  366. $this->assertSame(1, $iterator->key());
  367. $iterator->next();
  368. $this->assertFalse($iterator->valid());
  369. }
  370. /**
  371. * @group disconnected
  372. */
  373. public function testIterationWithOptionsMatchAndCountOnMultipleFetches()
  374. {
  375. $client = $this->getMock('Predis\Client', array('getCommandFactory', 'scan'));
  376. $client
  377. ->expects($this->any())
  378. ->method('getCommandFactory')
  379. ->will($this->returnValue($this->getCommandFactory()));
  380. $client
  381. ->expects($this->at(1))
  382. ->method('scan')
  383. ->with(0, array('MATCH' => 'key:*', 'COUNT' => 1))
  384. ->will($this->returnValue(
  385. array(1, array('key:1st'))
  386. ));
  387. $client
  388. ->expects($this->at(2))
  389. ->method('scan')
  390. ->with(1, array('MATCH' => 'key:*', 'COUNT' => 1))
  391. ->will($this->returnValue(
  392. array(0, array('key:2nd'))
  393. ));
  394. $iterator = new Keyspace($client, 'key:*', 1);
  395. $iterator->rewind();
  396. $this->assertTrue($iterator->valid());
  397. $this->assertSame('key:1st', $iterator->current());
  398. $this->assertSame(0, $iterator->key());
  399. $iterator->next();
  400. $this->assertTrue($iterator->valid());
  401. $this->assertSame('key:2nd', $iterator->current());
  402. $this->assertSame(1, $iterator->key());
  403. $iterator->next();
  404. $this->assertFalse($iterator->valid());
  405. }
  406. /**
  407. * @group disconnected
  408. */
  409. public function testIterationRewindable()
  410. {
  411. $client = $this->getMock('Predis\Client', array('getCommandFactory', 'scan'));
  412. $client
  413. ->expects($this->any())
  414. ->method('getCommandFactory')
  415. ->will($this->returnValue($this->getCommandFactory()));
  416. $client
  417. ->expects($this->exactly(2))
  418. ->method('scan')
  419. ->with(0, array())
  420. ->will($this->returnValue(
  421. array(0, array('key:1st', 'key:2nd'))
  422. ));
  423. $iterator = new Keyspace($client);
  424. $iterator->rewind();
  425. $this->assertTrue($iterator->valid());
  426. $this->assertSame('key:1st', $iterator->current());
  427. $this->assertSame(0, $iterator->key());
  428. $iterator->rewind();
  429. $this->assertTrue($iterator->valid());
  430. $this->assertSame('key:1st', $iterator->current());
  431. $this->assertSame(0, $iterator->key());
  432. $iterator->next();
  433. $this->assertTrue($iterator->valid());
  434. $this->assertSame(1, $iterator->key());
  435. $this->assertSame('key:2nd', $iterator->current());
  436. $iterator->next();
  437. $this->assertFalse($iterator->valid());
  438. }
  439. }