ZSetReverseRangeByScoreTest.php 7.8 KB

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