ZSetReverseRangeByScoreTest.php 7.5 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\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 testAddsWithscoresModifiersOnlyWhenOptionIsTrue()
  108. {
  109. $command = $this->getCommandWithArguments('zset', 0, 100, array('withscores' => true));
  110. $this->assertSame(array('zset', 0, 100, 'WITHSCORES'), $command->getArguments());
  111. $command = $this->getCommandWithArguments('zset', 0, 100, array('withscores' => 1));
  112. $this->assertSame(array('zset', 0, 100, 'WITHSCORES'), $command->getArguments());
  113. $command = $this->getCommandWithArguments('zset', 0, 100, array('withscores' => false));
  114. $this->assertSame(array('zset', 0, 100), $command->getArguments());
  115. $command = $this->getCommandWithArguments('zset', 0, 100, array('withscores' => 0));
  116. $this->assertSame(array('zset', 0, 100), $command->getArguments());
  117. }
  118. /**
  119. * @group connected
  120. */
  121. public function testReturnsElementsInScoreRange()
  122. {
  123. $redis = $this->getClient();
  124. $redis->zadd('letters', -10, 'a', 0, 'b', 10, 'c', 20, 'd', 20, 'e', 30, 'f');
  125. $this->assertSame(array('a'), $redis->zrevrangebyscore('letters', -10, -10));
  126. $this->assertSame(array(), $redis->zrevrangebyscore('letters', 10, 30));
  127. $this->assertSame(array('e', 'd'), $redis->zrevrangebyscore('letters', 20, 20));
  128. $this->assertSame(array('f', 'e', 'd', 'c', 'b'), $redis->zrevrangebyscore('letters', 30, 0));
  129. $this->assertSame(array(), $redis->zrevrangebyscore('unknown', 0, 30));
  130. }
  131. /**
  132. * @group connected
  133. */
  134. public function testInfinityScoreIntervals()
  135. {
  136. $redis = $this->getClient();
  137. $redis->zadd('letters', -10, 'a', 0, 'b', 10, 'c', 20, 'd', 20, 'e', 30, 'f');
  138. $this->assertSame(array('f', 'e', 'd'), $redis->zrevrangebyscore('letters', '+inf', 15));
  139. $this->assertSame(array('c', 'b', 'a'), $redis->zrevrangebyscore('letters', 15, '-inf'));
  140. $this->assertSame(array('f', 'e', 'd', 'c', 'b', 'a'), $redis->zrevrangebyscore('letters', '+inf', '-inf'));
  141. }
  142. /**
  143. * @group connected
  144. */
  145. public function testExclusiveScoreIntervals()
  146. {
  147. $redis = $this->getClient();
  148. $redis->zadd('letters', -10, 'a', 0, 'b', 10, 'c', 20, 'd', 20, 'e', 30, 'f');
  149. $this->assertSame(array('e', 'd', 'c'), $redis->zrevrangebyscore('letters', '(30', 10));
  150. $this->assertSame(array('f', 'e', 'd'), $redis->zrevrangebyscore('letters', 30, '(10'));
  151. $this->assertSame(array('e', 'd'), $redis->zrevrangebyscore('letters', '(30', '(10'));
  152. }
  153. /**
  154. * @group connected
  155. */
  156. public function testRangeWithWithscoresModifier()
  157. {
  158. $redis = $this->getClient();
  159. $redis->zadd('letters', -10, 'a', 0, 'b', 10, 'c', 20, 'd', 20, 'e', 30, 'f');
  160. $expected = array(array('e', '20'), array('d', '20'), array('c', '10'));
  161. $this->assertSame($expected, $redis->zrevrangebyscore('letters', 20, 10, 'withscores'));
  162. $this->assertSame($expected, $redis->zrevrangebyscore('letters', 20, 10, array('withscores' => true)));
  163. }
  164. /**
  165. * @group connected
  166. */
  167. public function testRangeWithLimitModifier()
  168. {
  169. $redis = $this->getClient();
  170. $redis->zadd('letters', -10, 'a', 0, 'b', 10, 'c', 20, 'd', 20, 'e', 30, 'f');
  171. $expected = array('d', 'c');
  172. $this->assertSame($expected, $redis->zrevrangebyscore('letters', 20, 10, array('limit' => array(1, 2))));
  173. $this->assertSame($expected, $redis->zrevrangebyscore('letters', 20, 10, array('limit' => array('offset' => 1, 'count' => 2))));
  174. }
  175. /**
  176. * @group connected
  177. */
  178. public function testRangeWithCombinedModifiers()
  179. {
  180. $redis = $this->getClient();
  181. $redis->zadd('letters', -10, 'a', 0, 'b', 10, 'c', 20, 'd', 20, 'e', 30, 'f');
  182. $options = array('limit' => array(1, 2), 'withscores' => true);
  183. $expected = array(array('d', '20'), array('c', '10'));
  184. $this->assertSame($expected, $redis->zrevrangebyscore('letters', 20, 10, $options));
  185. }
  186. /**
  187. * @group connected
  188. * @expectedException Predis\ServerException
  189. * @expectedExceptionMessage Operation against a key holding the wrong kind of value
  190. */
  191. public function testThrowsExceptionOnWrongType()
  192. {
  193. $redis = $this->getClient();
  194. $redis->set('foo', 'bar');
  195. $redis->zrevrangebyscore('foo', 0, 10);
  196. }
  197. }