ZSetRangeByScoreTest.php 6.7 KB

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