ListKeyTest.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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 Predis\Profile;
  12. use PredisTestCase;
  13. /**
  14. * @group realm-iterators
  15. */
  16. class ListKeyTest extends PredisTestCase
  17. {
  18. /**
  19. * @group disconnected
  20. */
  21. public function testIterationWithNoResults()
  22. {
  23. $client = $this->getMock('Predis\Client', array('getProfile', 'lrange'));
  24. $client->expects($this->any())
  25. ->method('getProfile')
  26. ->will($this->returnValue(Profile\Factory::getDefault()));
  27. $client->expects($this->once())
  28. ->method('lrange')
  29. ->with('key:list', 0, 9)
  30. ->will($this->returnValue(array()));
  31. $iterator = new ListKey($client, 'key:list');
  32. $iterator->rewind();
  33. $this->assertFalse($iterator->valid());
  34. }
  35. /**
  36. * @group disconnected
  37. */
  38. public function testIterationOnSingleFetch()
  39. {
  40. $client = $this->getMock('Predis\Client', array('getProfile', 'lrange'));
  41. $client->expects($this->any())
  42. ->method('getProfile')
  43. ->will($this->returnValue(Profile\Factory::getDefault()));
  44. $client->expects($this->once())
  45. ->method('lrange')
  46. ->with('key:list', 0, 9)
  47. ->will($this->returnValue(array('item:1', 'item:2', 'item:3')));
  48. $iterator = new ListKey($client, 'key:list');
  49. $iterator->rewind();
  50. $this->assertTrue($iterator->valid());
  51. $this->assertSame('item:1', $iterator->current());
  52. $this->assertSame(0, $iterator->key());
  53. $iterator->next();
  54. $this->assertTrue($iterator->valid());
  55. $this->assertSame('item:2', $iterator->current());
  56. $this->assertSame(1, $iterator->key());
  57. $iterator->next();
  58. $this->assertTrue($iterator->valid());
  59. $this->assertSame('item:3', $iterator->current());
  60. $this->assertSame(2, $iterator->key());
  61. $iterator->next();
  62. $this->assertFalse($iterator->valid());
  63. }
  64. /**
  65. * @group disconnected
  66. */
  67. public function testIterationOnMultipleFetches()
  68. {
  69. $client = $this->getMock('Predis\Client', array('getProfile', 'lrange'));
  70. $client->expects($this->any())
  71. ->method('getProfile')
  72. ->will($this->returnValue(Profile\Factory::getDefault()));
  73. $client->expects($this->at(1))
  74. ->method('lrange')
  75. ->with('key:list', 0, 9)
  76. ->will($this->returnValue(array(
  77. 'item:1', 'item:2', 'item:3', 'item:4', 'item:5', 'item:6', 'item:7', 'item:8', 'item:9', 'item:10',
  78. )));
  79. $client->expects($this->at(2))
  80. ->method('lrange')
  81. ->with('key:list', 10, 19)
  82. ->will($this->returnValue(array('item:11', 'item:12')));
  83. $iterator = new ListKey($client, 'key:list');
  84. for ($i = 1, $iterator->rewind(); $i <= 12; $i++, $iterator->next()) {
  85. $this->assertTrue($iterator->valid());
  86. $this->assertSame("item:$i", $iterator->current());
  87. $this->assertSame($i - 1, $iterator->key());
  88. }
  89. $this->assertFalse($iterator->valid());
  90. }
  91. /**
  92. * @group disconnected
  93. * @expectedException \InvalidArgumentException
  94. * @expectedExceptionMessage The $count argument must be a positive integer.
  95. */
  96. public function testThrowsExceptionOnConstructorWithNonIntegerCountParameter()
  97. {
  98. $client = $this->getMock('Predis\ClientInterface');
  99. $client->expects($this->any())
  100. ->method('getProfile')
  101. ->will($this->returnValue(Profile\Factory::getDefault()));
  102. new ListKey($client, 'key:list', 'wrong');
  103. }
  104. /**
  105. * @group disconnected
  106. * @expectedException \InvalidArgumentException
  107. * @expectedExceptionMessage The $count argument must be a positive integer.
  108. */
  109. public function testThrowsExceptionOnConstructorWithNegativeCountParameter()
  110. {
  111. $client = $this->getMock('Predis\ClientInterface');
  112. $client->expects($this->any())
  113. ->method('getProfile')
  114. ->will($this->returnValue(Profile\Factory::getDefault()));
  115. new ListKey($client, 'key:list', 'wrong');
  116. }
  117. /**
  118. * @group disconnected
  119. */
  120. public function testIterationWithCountParameter()
  121. {
  122. $client = $this->getMock('Predis\Client', array('getProfile', 'lrange'));
  123. $client->expects($this->any())
  124. ->method('getProfile')
  125. ->will($this->returnValue(Profile\Factory::getDefault()));
  126. $client->expects($this->at(1))
  127. ->method('lrange')
  128. ->with('key:list', 0, 4)
  129. ->will($this->returnValue(array('item:1', 'item:2')));
  130. $iterator = new ListKey($client, 'key:list', 5);
  131. $iterator->rewind();
  132. $this->assertTrue($iterator->valid());
  133. $this->assertSame('item:1', $iterator->current());
  134. $this->assertSame(0, $iterator->key());
  135. $iterator->next();
  136. $this->assertTrue($iterator->valid());
  137. $this->assertSame('item:2', $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 testIterationWithCountParameterOnMultipleFetches()
  146. {
  147. $client = $this->getMock('Predis\Client', array('getProfile', 'lrange'));
  148. $client->expects($this->any())
  149. ->method('getProfile')
  150. ->will($this->returnValue(Profile\Factory::getDefault()));
  151. $client->expects($this->at(1))
  152. ->method('lrange')
  153. ->with('key:list', 0, 1)
  154. ->will($this->returnValue(array('item:1', 'item:2')));
  155. $client->expects($this->at(2))
  156. ->method('lrange')
  157. ->with('key:list', 2, 3)
  158. ->will($this->returnValue(array('item:3')));
  159. $iterator = new ListKey($client, 'key:list', 2);
  160. $iterator->rewind();
  161. $this->assertTrue($iterator->valid());
  162. $this->assertSame('item:1', $iterator->current());
  163. $this->assertSame(0, $iterator->key());
  164. $iterator->next();
  165. $this->assertTrue($iterator->valid());
  166. $this->assertSame('item:2', $iterator->current());
  167. $this->assertSame(1, $iterator->key());
  168. $iterator->next();
  169. $this->assertTrue($iterator->valid());
  170. $this->assertSame('item:3', $iterator->current());
  171. $this->assertSame(2, $iterator->key());
  172. $iterator->next();
  173. $this->assertFalse($iterator->valid());
  174. }
  175. /**
  176. * @group disconnected
  177. */
  178. public function testIterationRewindable()
  179. {
  180. $client = $this->getMock('Predis\Client', array('getProfile', 'lrange'));
  181. $client->expects($this->any())
  182. ->method('getProfile')
  183. ->will($this->returnValue(Profile\Factory::getDefault()));
  184. $client->expects($this->exactly(2))
  185. ->method('lrange')
  186. ->with('key:list', 0, 9)
  187. ->will($this->returnValue(array('item:1', 'item:2')));
  188. $iterator = new ListKey($client, 'key:list');
  189. $iterator->rewind();
  190. $this->assertTrue($iterator->valid());
  191. $this->assertSame('item:1', $iterator->current());
  192. $this->assertSame(0, $iterator->key());
  193. $iterator->rewind();
  194. $this->assertTrue($iterator->valid());
  195. $this->assertSame('item:1', $iterator->current());
  196. $this->assertSame(0, $iterator->key());
  197. $iterator->next();
  198. $this->assertTrue($iterator->valid());
  199. $this->assertSame(1, $iterator->key());
  200. $this->assertSame('item:2', $iterator->current());
  201. $iterator->next();
  202. $this->assertFalse($iterator->valid());
  203. }
  204. }