ZSetRangeByScoreTest.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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\Command;
  11. /**
  12. * @group commands
  13. * @group realm-zset
  14. */
  15. class ZSetRangeByScoreTest extends PredisCommandTestCase
  16. {
  17. /**
  18. * {@inheritdoc}
  19. */
  20. protected function getExpectedCommand()
  21. {
  22. return 'Predis\Command\ZSetRangeByScore';
  23. }
  24. /**
  25. * {@inheritdoc}
  26. */
  27. protected function getExpectedId()
  28. {
  29. return 'ZRANGEBYSCORE';
  30. }
  31. /**
  32. * @group disconnected
  33. */
  34. public function testFilterArguments()
  35. {
  36. $modifiers = array(
  37. 'withscores' => true,
  38. 'limit' => array(0, 100),
  39. );
  40. $arguments = array('zset', 0, 100, $modifiers);
  41. $expected = array('zset', 0, 100, 'LIMIT', 0, 100, 'WITHSCORES');
  42. $command = $this->getCommand();
  43. $command->setArguments($arguments);
  44. $this->assertSame($expected, $command->getArguments());
  45. }
  46. /**
  47. * @group disconnected
  48. */
  49. public function testFilterArgumentsWithStringWithscores()
  50. {
  51. $arguments = array('zset', 0, 100, 'withscores');
  52. $expected = array('zset', 0, 100, 'WITHSCORES');
  53. $command = $this->getCommand();
  54. $command->setArguments($arguments);
  55. $this->assertSame($expected, $command->getArguments());
  56. }
  57. /**
  58. * @group disconnected
  59. */
  60. public function testFilterArgumentsWithNamedLimit()
  61. {
  62. $arguments = array('zset', 0, 100, array('limit' => array('offset' => 1, 'count' => 2)));
  63. $expected = array('zset', 0, 100, 'LIMIT', 1, 2);
  64. $command = $this->getCommand();
  65. $command->setArguments($arguments);
  66. $this->assertSame($expected, $command->getArguments());
  67. }
  68. /**
  69. * @group disconnected
  70. */
  71. public function testParseResponse()
  72. {
  73. $raw = array('element1', 'element2', 'element3');
  74. $expected = array('element1', 'element2', 'element3');
  75. $command = $this->getCommand();
  76. $this->assertSame($expected, $command->parseResponse($raw));
  77. }
  78. /**
  79. * @group disconnected
  80. */
  81. public function testParseResponseWithScores()
  82. {
  83. $raw = array('element1', '1', 'element2', '2', 'element3', '3');
  84. $expected = array('element1' => '1', 'element2' => '2', 'element3' => '3');
  85. $command = $this->getCommandWithArgumentsArray(array('zset', 0, 1, 'withscores'));
  86. $this->assertSame($expected, $command->parseResponse($raw));
  87. }
  88. /**
  89. * @group disconnected
  90. */
  91. public function testAddsWithscoresModifiersOnlyWhenOptionIsTrue()
  92. {
  93. $command = $this->getCommandWithArguments('zset', 0, 100, array('withscores' => true));
  94. $this->assertSame(array('zset', 0, 100, 'WITHSCORES'), $command->getArguments());
  95. $command = $this->getCommandWithArguments('zset', 0, 100, array('withscores' => 1));
  96. $this->assertSame(array('zset', 0, 100, 'WITHSCORES'), $command->getArguments());
  97. $command = $this->getCommandWithArguments('zset', 0, 100, array('withscores' => false));
  98. $this->assertSame(array('zset', 0, 100), $command->getArguments());
  99. $command = $this->getCommandWithArguments('zset', 0, 100, array('withscores' => 0));
  100. $this->assertSame(array('zset', 0, 100), $command->getArguments());
  101. }
  102. /**
  103. * @group connected
  104. */
  105. public function testReturnsElementsInScoreRange()
  106. {
  107. $redis = $this->getClient();
  108. $redis->zadd('letters', -10, 'a', 0, 'b', 10, 'c', 20, 'd', 20, 'e', 30, 'f');
  109. $this->assertSame(array('a'), $redis->zrangebyscore('letters', -10, -10));
  110. $this->assertSame(array('c', 'd', 'e', 'f'), $redis->zrangebyscore('letters', 10, 30));
  111. $this->assertSame(array('d', 'e'), $redis->zrangebyscore('letters', 20, 20));
  112. $this->assertSame(array(), $redis->zrangebyscore('letters', 30, 0));
  113. $this->assertSame(array(), $redis->zrangebyscore('unknown', 0, 30));
  114. }
  115. /**
  116. * @group connected
  117. */
  118. public function testInfinityScoreIntervals()
  119. {
  120. $redis = $this->getClient();
  121. $redis->zadd('letters', -10, 'a', 0, 'b', 10, 'c', 20, 'd', 20, 'e', 30, 'f');
  122. $this->assertSame(array('a', 'b', 'c'), $redis->zrangebyscore('letters', '-inf', 15));
  123. $this->assertSame(array('d', 'e', 'f'), $redis->zrangebyscore('letters', 15, '+inf'));
  124. $this->assertSame(array('a', 'b', 'c', 'd', 'e', 'f'), $redis->zrangebyscore('letters', '-inf', '+inf'));
  125. }
  126. /**
  127. * @group connected
  128. */
  129. public function testExclusiveScoreIntervals()
  130. {
  131. $redis = $this->getClient();
  132. $redis->zadd('letters', -10, 'a', 0, 'b', 10, 'c', 20, 'd', 20, 'e', 30, 'f');
  133. $this->assertSame(array('c', 'd', 'e'), $redis->zrangebyscore('letters', 10, '(30'));
  134. $this->assertSame(array('d', 'e', 'f'), $redis->zrangebyscore('letters', '(10', 30));
  135. $this->assertSame(array('d', 'e'), $redis->zrangebyscore('letters', '(10', '(30'));
  136. }
  137. /**
  138. * @group connected
  139. */
  140. public function testRangeWithWithscoresModifier()
  141. {
  142. $redis = $this->getClient();
  143. $redis->zadd('letters', -10, 'a', 0, 'b', 10, 'c', 20, 'd', 20, 'e', 30, 'f');
  144. $expected = array('c' => '10', 'd' => '20', 'e' => '20');
  145. $this->assertSame($expected, $redis->zrangebyscore('letters', 10, 20, 'withscores'));
  146. $this->assertSame($expected, $redis->zrangebyscore('letters', 10, 20, array('withscores' => true)));
  147. }
  148. /**
  149. * @group connected
  150. */
  151. public function testRangeWithLimitModifier()
  152. {
  153. $redis = $this->getClient();
  154. $redis->zadd('letters', -10, 'a', 0, 'b', 10, 'c', 20, 'd', 20, 'e', 30, 'f');
  155. $expected = array('d', 'e');
  156. $this->assertSame($expected, $redis->zrangebyscore('letters', 10, 20, array('limit' => array(1, 2))));
  157. $this->assertSame($expected, $redis->zrangebyscore('letters', 10, 20, array('limit' => array('offset' => 1, 'count' => 2))));
  158. }
  159. /**
  160. * @group connected
  161. */
  162. public function testRangeWithCombinedModifiers()
  163. {
  164. $redis = $this->getClient();
  165. $redis->zadd('letters', -10, 'a', 0, 'b', 10, 'c', 20, 'd', 20, 'e', 30, 'f');
  166. $options = array('limit' => array(1, 2), 'withscores' => true);
  167. $expected = array('d' => '20', 'e' => '20');
  168. $this->assertSame($expected, $redis->zrangebyscore('letters', 10, 20, $options));
  169. }
  170. /**
  171. * @group connected
  172. * @expectedException \Predis\Response\ServerException
  173. * @expectedExceptionMessage Operation against a key holding the wrong kind of value
  174. */
  175. public function testThrowsExceptionOnWrongType()
  176. {
  177. $redis = $this->getClient();
  178. $redis->set('foo', 'bar');
  179. $redis->zrangebyscore('foo', 0, 10);
  180. }
  181. }