ListKeyTest.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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->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 ListKey($client, 'key:list');
  33. }
  34. /**
  35. * @group disconnected
  36. */
  37. public function testIterationWithNoResults()
  38. {
  39. $client = $this->getMock('Predis\Client', array('getCommandFactory', 'lrange'));
  40. $client->expects($this->any())
  41. ->method('getCommandFactory')
  42. ->will($this->returnValue($this->getCommandFactory()));
  43. $client->expects($this->once())
  44. ->method('lrange')
  45. ->with('key:list', 0, 9)
  46. ->will($this->returnValue(array()));
  47. $iterator = new ListKey($client, 'key:list');
  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', 'lrange'));
  57. $client->expects($this->any())
  58. ->method('getCommandFactory')
  59. ->will($this->returnValue($this->getCommandFactory()));
  60. $client->expects($this->once())
  61. ->method('lrange')
  62. ->with('key:list', 0, 9)
  63. ->will($this->returnValue(array('item:1', 'item:2', 'item:3')));
  64. $iterator = new ListKey($client, 'key:list');
  65. $iterator->rewind();
  66. $this->assertTrue($iterator->valid());
  67. $this->assertSame('item:1', $iterator->current());
  68. $this->assertSame(0, $iterator->key());
  69. $iterator->next();
  70. $this->assertTrue($iterator->valid());
  71. $this->assertSame('item:2', $iterator->current());
  72. $this->assertSame(1, $iterator->key());
  73. $iterator->next();
  74. $this->assertTrue($iterator->valid());
  75. $this->assertSame('item:3', $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', 'lrange'));
  86. $client->expects($this->any())
  87. ->method('getCommandFactory')
  88. ->will($this->returnValue($this->getCommandFactory()));
  89. $client->expects($this->at(1))
  90. ->method('lrange')
  91. ->with('key:list', 0, 9)
  92. ->will($this->returnValue(array(
  93. 'item:1', 'item:2', 'item:3', 'item:4', 'item:5', 'item:6', 'item:7', 'item:8', 'item:9', 'item:10',
  94. )));
  95. $client->expects($this->at(2))
  96. ->method('lrange')
  97. ->with('key:list', 10, 19)
  98. ->will($this->returnValue(array('item:11', 'item:12')));
  99. $iterator = new ListKey($client, 'key:list');
  100. for ($i = 1, $iterator->rewind(); $i <= 12; $i++, $iterator->next()) {
  101. $this->assertTrue($iterator->valid());
  102. $this->assertSame("item:$i", $iterator->current());
  103. $this->assertSame($i - 1, $iterator->key());
  104. }
  105. $this->assertFalse($iterator->valid());
  106. }
  107. /**
  108. * @group disconnected
  109. * @expectedException \InvalidArgumentException
  110. * @expectedExceptionMessage The $count argument must be a positive integer.
  111. */
  112. public function testThrowsExceptionOnConstructorWithNonIntegerCountParameter()
  113. {
  114. $client = $this->getMock('Predis\ClientInterface');
  115. $client->expects($this->any())
  116. ->method('getCommandFactory')
  117. ->will($this->returnValue($this->getCommandFactory()));
  118. new ListKey($client, 'key:list', 'wrong');
  119. }
  120. /**
  121. * @group disconnected
  122. * @expectedException \InvalidArgumentException
  123. * @expectedExceptionMessage The $count argument must be a positive integer.
  124. */
  125. public function testThrowsExceptionOnConstructorWithNegativeCountParameter()
  126. {
  127. $client = $this->getMock('Predis\ClientInterface');
  128. $client->expects($this->any())
  129. ->method('getCommandFactory')
  130. ->will($this->returnValue($this->getCommandFactory()));
  131. new ListKey($client, 'key:list', 'wrong');
  132. }
  133. /**
  134. * @group disconnected
  135. */
  136. public function testIterationWithCountParameter()
  137. {
  138. $client = $this->getMock('Predis\Client', array('getCommandFactory', 'lrange'));
  139. $client->expects($this->any())
  140. ->method('getCommandFactory')
  141. ->will($this->returnValue($this->getCommandFactory()));
  142. $client->expects($this->at(1))
  143. ->method('lrange')
  144. ->with('key:list', 0, 4)
  145. ->will($this->returnValue(array('item:1', 'item:2')));
  146. $iterator = new ListKey($client, 'key:list', 5);
  147. $iterator->rewind();
  148. $this->assertTrue($iterator->valid());
  149. $this->assertSame('item:1', $iterator->current());
  150. $this->assertSame(0, $iterator->key());
  151. $iterator->next();
  152. $this->assertTrue($iterator->valid());
  153. $this->assertSame('item:2', $iterator->current());
  154. $this->assertSame(1, $iterator->key());
  155. $iterator->next();
  156. $this->assertFalse($iterator->valid());
  157. }
  158. /**
  159. * @group disconnected
  160. */
  161. public function testIterationWithCountParameterOnMultipleFetches()
  162. {
  163. $client = $this->getMock('Predis\Client', array('getCommandFactory', 'lrange'));
  164. $client->expects($this->any())
  165. ->method('getCommandFactory')
  166. ->will($this->returnValue($this->getCommandFactory()));
  167. $client->expects($this->at(1))
  168. ->method('lrange')
  169. ->with('key:list', 0, 1)
  170. ->will($this->returnValue(array('item:1', 'item:2')));
  171. $client->expects($this->at(2))
  172. ->method('lrange')
  173. ->with('key:list', 2, 3)
  174. ->will($this->returnValue(array('item:3')));
  175. $iterator = new ListKey($client, 'key:list', 2);
  176. $iterator->rewind();
  177. $this->assertTrue($iterator->valid());
  178. $this->assertSame('item:1', $iterator->current());
  179. $this->assertSame(0, $iterator->key());
  180. $iterator->next();
  181. $this->assertTrue($iterator->valid());
  182. $this->assertSame('item:2', $iterator->current());
  183. $this->assertSame(1, $iterator->key());
  184. $iterator->next();
  185. $this->assertTrue($iterator->valid());
  186. $this->assertSame('item:3', $iterator->current());
  187. $this->assertSame(2, $iterator->key());
  188. $iterator->next();
  189. $this->assertFalse($iterator->valid());
  190. }
  191. /**
  192. * @group disconnected
  193. */
  194. public function testIterationRewindable()
  195. {
  196. $client = $this->getMock('Predis\Client', array('getCommandFactory', 'lrange'));
  197. $client->expects($this->any())
  198. ->method('getCommandFactory')
  199. ->will($this->returnValue($this->getCommandFactory()));
  200. $client->expects($this->exactly(2))
  201. ->method('lrange')
  202. ->with('key:list', 0, 9)
  203. ->will($this->returnValue(array('item:1', 'item:2')));
  204. $iterator = new ListKey($client, 'key:list');
  205. $iterator->rewind();
  206. $this->assertTrue($iterator->valid());
  207. $this->assertSame('item:1', $iterator->current());
  208. $this->assertSame(0, $iterator->key());
  209. $iterator->rewind();
  210. $this->assertTrue($iterator->valid());
  211. $this->assertSame('item:1', $iterator->current());
  212. $this->assertSame(0, $iterator->key());
  213. $iterator->next();
  214. $this->assertTrue($iterator->valid());
  215. $this->assertSame(1, $iterator->key());
  216. $this->assertSame('item:2', $iterator->current());
  217. $iterator->next();
  218. $this->assertFalse($iterator->valid());
  219. }
  220. }