ZREVRANGEBYSCORE_Test.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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\Redis;
  11. /**
  12. * @group commands
  13. * @group realm-zset
  14. */
  15. class ZREVRANGEBYSCORE_Test extends PredisCommandTestCase
  16. {
  17. /**
  18. * {@inheritdoc}
  19. */
  20. protected function getExpectedCommand()
  21. {
  22. return 'Predis\Command\Redis\ZREVRANGEBYSCORE';
  23. }
  24. /**
  25. * {@inheritdoc}
  26. */
  27. protected function getExpectedId()
  28. {
  29. return 'ZREVRANGEBYSCORE';
  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. * @requiresRedisVersion >= 2.2.0
  105. */
  106. public function testReturnsElementsInScoreRange()
  107. {
  108. $redis = $this->getClient();
  109. $redis->zadd('letters', -10, 'a', 0, 'b', 10, 'c', 20, 'd', 20, 'e', 30, 'f');
  110. $this->assertSame(array('a'), $redis->zrevrangebyscore('letters', -10, -10));
  111. $this->assertSame(array(), $redis->zrevrangebyscore('letters', 10, 30));
  112. $this->assertSame(array('e', 'd'), $redis->zrevrangebyscore('letters', 20, 20));
  113. $this->assertSame(array('f', 'e', 'd', 'c', 'b'), $redis->zrevrangebyscore('letters', 30, 0));
  114. $this->assertSame(array(), $redis->zrevrangebyscore('unknown', 0, 30));
  115. }
  116. /**
  117. * @group connected
  118. * @requiresRedisVersion >= 2.2.0
  119. */
  120. public function testInfinityScoreIntervals()
  121. {
  122. $redis = $this->getClient();
  123. $redis->zadd('letters', -10, 'a', 0, 'b', 10, 'c', 20, 'd', 20, 'e', 30, 'f');
  124. $this->assertSame(array('f', 'e', 'd'), $redis->zrevrangebyscore('letters', '+inf', 15));
  125. $this->assertSame(array('c', 'b', 'a'), $redis->zrevrangebyscore('letters', 15, '-inf'));
  126. $this->assertSame(array('f', 'e', 'd', 'c', 'b', 'a'), $redis->zrevrangebyscore('letters', '+inf', '-inf'));
  127. }
  128. /**
  129. * @group connected
  130. * @requiresRedisVersion >= 2.2.0
  131. */
  132. public function testExclusiveScoreIntervals()
  133. {
  134. $redis = $this->getClient();
  135. $redis->zadd('letters', -10, 'a', 0, 'b', 10, 'c', 20, 'd', 20, 'e', 30, 'f');
  136. $this->assertSame(array('e', 'd', 'c'), $redis->zrevrangebyscore('letters', '(30', 10));
  137. $this->assertSame(array('f', 'e', 'd'), $redis->zrevrangebyscore('letters', 30, '(10'));
  138. $this->assertSame(array('e', 'd'), $redis->zrevrangebyscore('letters', '(30', '(10'));
  139. }
  140. /**
  141. * @group connected
  142. * @requiresRedisVersion >= 2.2.0
  143. */
  144. public function testRangeWithWithscoresModifier()
  145. {
  146. $redis = $this->getClient();
  147. $redis->zadd('letters', -10, 'a', 0, 'b', 10, 'c', 20, 'd', 20, 'e', 30, 'f');
  148. $expected = array('e' => '20', 'd' => '20', 'c' => '10');
  149. $this->assertSame($expected, $redis->zrevrangebyscore('letters', 20, 10, 'withscores'));
  150. $this->assertSame($expected, $redis->zrevrangebyscore('letters', 20, 10, array('withscores' => true)));
  151. }
  152. /**
  153. * @group connected
  154. * @requiresRedisVersion >= 2.2.0
  155. */
  156. public function testRangeWithLimitModifier()
  157. {
  158. $redis = $this->getClient();
  159. $redis->zadd('letters', -10, 'a', 0, 'b', 10, 'c', 20, 'd', 20, 'e', 30, 'f');
  160. $expected = array('d', 'c');
  161. $this->assertSame($expected, $redis->zrevrangebyscore('letters', 20, 10, array('limit' => array(1, 2))));
  162. $this->assertSame($expected, $redis->zrevrangebyscore('letters', 20, 10, array('limit' => array('offset' => 1, 'count' => 2))));
  163. }
  164. /**
  165. * @group connected
  166. * @requiresRedisVersion >= 2.2.0
  167. */
  168. public function testRangeWithCombinedModifiers()
  169. {
  170. $redis = $this->getClient();
  171. $redis->zadd('letters', -10, 'a', 0, 'b', 10, 'c', 20, 'd', 20, 'e', 30, 'f');
  172. $options = array('limit' => array(1, 2), 'withscores' => true);
  173. $expected = array('d' => '20', 'c' => '10');
  174. $this->assertSame($expected, $redis->zrevrangebyscore('letters', 20, 10, $options));
  175. }
  176. /**
  177. * @group connected
  178. * @requiresRedisVersion >= 2.2.0
  179. * @expectedException \Predis\Response\ServerException
  180. * @expectedExceptionMessage Operation against a key holding the wrong kind of value
  181. */
  182. public function testThrowsExceptionOnWrongType()
  183. {
  184. $redis = $this->getClient();
  185. $redis->set('foo', 'bar');
  186. $redis->zrevrangebyscore('foo', 0, 10);
  187. }
  188. }