ListKeyTest.php 8.0 KB

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