ListKeyTest.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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 ListKeyTest extends PredisTestCase
  16. {
  17. /**
  18. * @group disconnected
  19. * @expectedException \Predis\NotSupportedException
  20. * @expectedExceptionMessage 'LRANGE' 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 ListKey($client, 'key:list');
  35. }
  36. /**
  37. * @group disconnected
  38. */
  39. public function testIterationWithNoResults()
  40. {
  41. $client = $this->getMock('Predis\Client', array('getCommandFactory', 'lrange'));
  42. $client
  43. ->expects($this->any())
  44. ->method('getCommandFactory')
  45. ->will($this->returnValue($this->getCommandFactory()));
  46. $client
  47. ->expects($this->once())
  48. ->method('lrange')
  49. ->with('key:list', 0, 9)
  50. ->will($this->returnValue(
  51. array()
  52. ));
  53. $iterator = new ListKey($client, 'key:list');
  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', 'lrange'));
  63. $client
  64. ->expects($this->any())
  65. ->method('getCommandFactory')
  66. ->will($this->returnValue($this->getCommandFactory()));
  67. $client
  68. ->expects($this->once())
  69. ->method('lrange')
  70. ->with('key:list', 0, 9)
  71. ->will($this->returnValue(
  72. array('item:1', 'item:2', 'item:3')
  73. ));
  74. $iterator = new ListKey($client, 'key:list');
  75. $iterator->rewind();
  76. $this->assertTrue($iterator->valid());
  77. $this->assertSame('item:1', $iterator->current());
  78. $this->assertSame(0, $iterator->key());
  79. $iterator->next();
  80. $this->assertTrue($iterator->valid());
  81. $this->assertSame('item:2', $iterator->current());
  82. $this->assertSame(1, $iterator->key());
  83. $iterator->next();
  84. $this->assertTrue($iterator->valid());
  85. $this->assertSame('item:3', $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', 'lrange'));
  96. $client
  97. ->expects($this->any())
  98. ->method('getCommandFactory')
  99. ->will($this->returnValue($this->getCommandFactory()));
  100. $client
  101. ->expects($this->at(1))
  102. ->method('lrange')
  103. ->with('key:list', 0, 9)
  104. ->will($this->returnValue(
  105. array(
  106. 'item:1', 'item:2', 'item:3', 'item:4', 'item:5',
  107. 'item:6', 'item:7', 'item:8', 'item:9', 'item:10',
  108. )
  109. ));
  110. $client->expects($this->at(2))
  111. ->method('lrange')
  112. ->with('key:list', 10, 19)
  113. ->will($this->returnValue(array('item:11', 'item:12')));
  114. $iterator = new ListKey($client, 'key:list');
  115. for ($i = 1, $iterator->rewind(); $i <= 12; $i++, $iterator->next()) {
  116. $this->assertTrue($iterator->valid());
  117. $this->assertSame("item:$i", $iterator->current());
  118. $this->assertSame($i - 1, $iterator->key());
  119. }
  120. $this->assertFalse($iterator->valid());
  121. }
  122. /**
  123. * @group disconnected
  124. * @expectedException \InvalidArgumentException
  125. * @expectedExceptionMessage The $count argument must be a positive integer.
  126. */
  127. public function testThrowsExceptionOnConstructorWithNonIntegerCountParameter()
  128. {
  129. $client = $this->getMock('Predis\ClientInterface');
  130. $client
  131. ->expects($this->any())
  132. ->method('getCommandFactory')
  133. ->will($this->returnValue($this->getCommandFactory()));
  134. new ListKey($client, 'key:list', 'wrong');
  135. }
  136. /**
  137. * @group disconnected
  138. * @expectedException \InvalidArgumentException
  139. * @expectedExceptionMessage The $count argument must be a positive integer.
  140. */
  141. public function testThrowsExceptionOnConstructorWithNegativeCountParameter()
  142. {
  143. $client = $this->getMock('Predis\ClientInterface');
  144. $client
  145. ->expects($this->any())
  146. ->method('getCommandFactory')
  147. ->will($this->returnValue($this->getCommandFactory()));
  148. new ListKey($client, 'key:list', 'wrong');
  149. }
  150. /**
  151. * @group disconnected
  152. */
  153. public function testIterationWithCountParameter()
  154. {
  155. $client = $this->getMock('Predis\Client', array('getCommandFactory', 'lrange'));
  156. $client
  157. ->expects($this->any())
  158. ->method('getCommandFactory')
  159. ->will($this->returnValue($this->getCommandFactory()));
  160. $client
  161. ->expects($this->at(1))
  162. ->method('lrange')
  163. ->with('key:list', 0, 4)
  164. ->will($this->returnValue(
  165. array('item:1', 'item:2')
  166. ));
  167. $iterator = new ListKey($client, 'key:list', 5);
  168. $iterator->rewind();
  169. $this->assertTrue($iterator->valid());
  170. $this->assertSame('item:1', $iterator->current());
  171. $this->assertSame(0, $iterator->key());
  172. $iterator->next();
  173. $this->assertTrue($iterator->valid());
  174. $this->assertSame('item:2', $iterator->current());
  175. $this->assertSame(1, $iterator->key());
  176. $iterator->next();
  177. $this->assertFalse($iterator->valid());
  178. }
  179. /**
  180. * @group disconnected
  181. */
  182. public function testIterationWithCountParameterOnMultipleFetches()
  183. {
  184. $client = $this->getMock('Predis\Client', array('getCommandFactory', 'lrange'));
  185. $client
  186. ->expects($this->any())
  187. ->method('getCommandFactory')
  188. ->will($this->returnValue($this->getCommandFactory()));
  189. $client
  190. ->expects($this->at(1))
  191. ->method('lrange')
  192. ->with('key:list', 0, 1)
  193. ->will($this->returnValue(
  194. array('item:1', 'item:2')
  195. ));
  196. $client
  197. ->expects($this->at(2))
  198. ->method('lrange')
  199. ->with('key:list', 2, 3)
  200. ->will($this->returnValue(
  201. array('item:3')
  202. ));
  203. $iterator = new ListKey($client, 'key:list', 2);
  204. $iterator->rewind();
  205. $this->assertTrue($iterator->valid());
  206. $this->assertSame('item:1', $iterator->current());
  207. $this->assertSame(0, $iterator->key());
  208. $iterator->next();
  209. $this->assertTrue($iterator->valid());
  210. $this->assertSame('item:2', $iterator->current());
  211. $this->assertSame(1, $iterator->key());
  212. $iterator->next();
  213. $this->assertTrue($iterator->valid());
  214. $this->assertSame('item:3', $iterator->current());
  215. $this->assertSame(2, $iterator->key());
  216. $iterator->next();
  217. $this->assertFalse($iterator->valid());
  218. }
  219. /**
  220. * @group disconnected
  221. */
  222. public function testIterationRewindable()
  223. {
  224. $client = $this->getMock('Predis\Client', array('getCommandFactory', 'lrange'));
  225. $client
  226. ->expects($this->any())
  227. ->method('getCommandFactory')
  228. ->will($this->returnValue($this->getCommandFactory()));
  229. $client
  230. ->expects($this->exactly(2))
  231. ->method('lrange')
  232. ->with('key:list', 0, 9)
  233. ->will($this->returnValue(
  234. array('item:1', 'item:2')
  235. ));
  236. $iterator = new ListKey($client, 'key:list');
  237. $iterator->rewind();
  238. $this->assertTrue($iterator->valid());
  239. $this->assertSame('item:1', $iterator->current());
  240. $this->assertSame(0, $iterator->key());
  241. $iterator->rewind();
  242. $this->assertTrue($iterator->valid());
  243. $this->assertSame('item:1', $iterator->current());
  244. $this->assertSame(0, $iterator->key());
  245. $iterator->next();
  246. $this->assertTrue($iterator->valid());
  247. $this->assertSame(1, $iterator->key());
  248. $this->assertSame('item:2', $iterator->current());
  249. $iterator->next();
  250. $this->assertFalse($iterator->valid());
  251. }
  252. }