ZSetReverseRangeByScoreTest.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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 ZSetReverseRangeByScoreTest extends PredisCommandTestCase
  16. {
  17. /**
  18. * {@inheritdoc}
  19. */
  20. protected function getExpectedCommand()
  21. {
  22. return 'Predis\Command\ZSetReverseRangeByScore';
  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(array('element1', '1'), array('element2', '2'), array('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 testPrefixKeys()
  92. {
  93. $modifiers = array(
  94. 'withscores' => true,
  95. 'limit' => array(0, 100),
  96. );
  97. $arguments = array('zset', 0, 100, $modifiers);
  98. $expected = array('prefix:zset', 0, 100, 'LIMIT', 0, 100, 'WITHSCORES');
  99. $command = $this->getCommandWithArgumentsArray($arguments);
  100. $command->prefixKeys('prefix:');
  101. $this->assertSame($expected, $command->getArguments());
  102. }
  103. /**
  104. * @group disconnected
  105. */
  106. public function testPrefixKeysIgnoredOnEmptyArguments()
  107. {
  108. $command = $this->getCommand();
  109. $command->prefixKeys('prefix:');
  110. $this->assertSame(array(), $command->getArguments());
  111. }
  112. /**
  113. * @group disconnected
  114. */
  115. public function testAddsWithscoresModifiersOnlyWhenOptionIsTrue()
  116. {
  117. $command = $this->getCommandWithArguments('zset', 0, 100, array('withscores' => true));
  118. $this->assertSame(array('zset', 0, 100, 'WITHSCORES'), $command->getArguments());
  119. $command = $this->getCommandWithArguments('zset', 0, 100, array('withscores' => 1));
  120. $this->assertSame(array('zset', 0, 100, 'WITHSCORES'), $command->getArguments());
  121. $command = $this->getCommandWithArguments('zset', 0, 100, array('withscores' => false));
  122. $this->assertSame(array('zset', 0, 100), $command->getArguments());
  123. $command = $this->getCommandWithArguments('zset', 0, 100, array('withscores' => 0));
  124. $this->assertSame(array('zset', 0, 100), $command->getArguments());
  125. }
  126. /**
  127. * @group connected
  128. */
  129. public function testReturnsElementsInScoreRange()
  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('a'), $redis->zrevrangebyscore('letters', -10, -10));
  134. $this->assertSame(array(), $redis->zrevrangebyscore('letters', 10, 30));
  135. $this->assertSame(array('e', 'd'), $redis->zrevrangebyscore('letters', 20, 20));
  136. $this->assertSame(array('f', 'e', 'd', 'c', 'b'), $redis->zrevrangebyscore('letters', 30, 0));
  137. $this->assertSame(array(), $redis->zrevrangebyscore('unknown', 0, 30));
  138. }
  139. /**
  140. * @group connected
  141. */
  142. public function testInfinityScoreIntervals()
  143. {
  144. $redis = $this->getClient();
  145. $redis->zadd('letters', -10, 'a', 0, 'b', 10, 'c', 20, 'd', 20, 'e', 30, 'f');
  146. $this->assertSame(array('f', 'e', 'd'), $redis->zrevrangebyscore('letters', '+inf', 15));
  147. $this->assertSame(array('c', 'b', 'a'), $redis->zrevrangebyscore('letters', 15, '-inf'));
  148. $this->assertSame(array('f', 'e', 'd', 'c', 'b', 'a'), $redis->zrevrangebyscore('letters', '+inf', '-inf'));
  149. }
  150. /**
  151. * @group connected
  152. */
  153. public function testExclusiveScoreIntervals()
  154. {
  155. $redis = $this->getClient();
  156. $redis->zadd('letters', -10, 'a', 0, 'b', 10, 'c', 20, 'd', 20, 'e', 30, 'f');
  157. $this->assertSame(array('e', 'd', 'c'), $redis->zrevrangebyscore('letters', '(30', 10));
  158. $this->assertSame(array('f', 'e', 'd'), $redis->zrevrangebyscore('letters', 30, '(10'));
  159. $this->assertSame(array('e', 'd'), $redis->zrevrangebyscore('letters', '(30', '(10'));
  160. }
  161. /**
  162. * @group connected
  163. */
  164. public function testRangeWithWithscoresModifier()
  165. {
  166. $redis = $this->getClient();
  167. $redis->zadd('letters', -10, 'a', 0, 'b', 10, 'c', 20, 'd', 20, 'e', 30, 'f');
  168. $expected = array(array('e', '20'), array('d', '20'), array('c', '10'));
  169. $this->assertSame($expected, $redis->zrevrangebyscore('letters', 20, 10, 'withscores'));
  170. $this->assertSame($expected, $redis->zrevrangebyscore('letters', 20, 10, array('withscores' => true)));
  171. }
  172. /**
  173. * @group connected
  174. */
  175. public function testRangeWithLimitModifier()
  176. {
  177. $redis = $this->getClient();
  178. $redis->zadd('letters', -10, 'a', 0, 'b', 10, 'c', 20, 'd', 20, 'e', 30, 'f');
  179. $expected = array('d', 'c');
  180. $this->assertSame($expected, $redis->zrevrangebyscore('letters', 20, 10, array('limit' => array(1, 2))));
  181. $this->assertSame($expected, $redis->zrevrangebyscore('letters', 20, 10, array('limit' => array('offset' => 1, 'count' => 2))));
  182. }
  183. /**
  184. * @group connected
  185. */
  186. public function testRangeWithCombinedModifiers()
  187. {
  188. $redis = $this->getClient();
  189. $redis->zadd('letters', -10, 'a', 0, 'b', 10, 'c', 20, 'd', 20, 'e', 30, 'f');
  190. $options = array('limit' => array(1, 2), 'withscores' => true);
  191. $expected = array(array('d', '20'), array('c', '10'));
  192. $this->assertSame($expected, $redis->zrevrangebyscore('letters', 20, 10, $options));
  193. }
  194. /**
  195. * @group connected
  196. * @expectedException Predis\ServerException
  197. * @expectedExceptionMessage Operation against a key holding the wrong kind of value
  198. */
  199. public function testThrowsExceptionOnWrongType()
  200. {
  201. $redis = $this->getClient();
  202. $redis->set('foo', 'bar');
  203. $redis->zrevrangebyscore('foo', 0, 10);
  204. }
  205. }