ZSetReverseRangeByScoreTest.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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 testAddsWithscoresModifiersOnlyWhenOptionIsTrue()
  93. {
  94. $command = $this->getCommandWithArguments('zset', 0, 100, array('withscores' => true));
  95. $this->assertSame(array('zset', 0, 100, 'WITHSCORES'), $command->getArguments());
  96. $command = $this->getCommandWithArguments('zset', 0, 100, array('withscores' => 1));
  97. $this->assertSame(array('zset', 0, 100, 'WITHSCORES'), $command->getArguments());
  98. $command = $this->getCommandWithArguments('zset', 0, 100, array('withscores' => false));
  99. $this->assertSame(array('zset', 0, 100), $command->getArguments());
  100. $command = $this->getCommandWithArguments('zset', 0, 100, array('withscores' => 0));
  101. $this->assertSame(array('zset', 0, 100), $command->getArguments());
  102. }
  103. /**
  104. * @group connected
  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. */
  119. public function testInfinityScoreIntervals()
  120. {
  121. $redis = $this->getClient();
  122. $redis->zadd('letters', -10, 'a', 0, 'b', 10, 'c', 20, 'd', 20, 'e', 30, 'f');
  123. $this->assertSame(array('f', 'e', 'd'), $redis->zrevrangebyscore('letters', '+inf', 15));
  124. $this->assertSame(array('c', 'b', 'a'), $redis->zrevrangebyscore('letters', 15, '-inf'));
  125. $this->assertSame(array('f', 'e', 'd', 'c', 'b', 'a'), $redis->zrevrangebyscore('letters', '+inf', '-inf'));
  126. }
  127. /**
  128. * @group connected
  129. */
  130. public function testExclusiveScoreIntervals()
  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('e', 'd', 'c'), $redis->zrevrangebyscore('letters', '(30', 10));
  135. $this->assertSame(array('f', 'e', 'd'), $redis->zrevrangebyscore('letters', 30, '(10'));
  136. $this->assertSame(array('e', 'd'), $redis->zrevrangebyscore('letters', '(30', '(10'));
  137. }
  138. /**
  139. * @group connected
  140. */
  141. public function testRangeWithWithscoresModifier()
  142. {
  143. $redis = $this->getClient();
  144. $redis->zadd('letters', -10, 'a', 0, 'b', 10, 'c', 20, 'd', 20, 'e', 30, 'f');
  145. $expected = array(array('e', '20'), array('d', '20'), array('c', '10'));
  146. $this->assertSame($expected, $redis->zrevrangebyscore('letters', 20, 10, 'withscores'));
  147. $this->assertSame($expected, $redis->zrevrangebyscore('letters', 20, 10, array('withscores' => true)));
  148. }
  149. /**
  150. * @group connected
  151. */
  152. public function testRangeWithLimitModifier()
  153. {
  154. $redis = $this->getClient();
  155. $redis->zadd('letters', -10, 'a', 0, 'b', 10, 'c', 20, 'd', 20, 'e', 30, 'f');
  156. $expected = array('d', 'c');
  157. $this->assertSame($expected, $redis->zrevrangebyscore('letters', 20, 10, array('limit' => array(1, 2))));
  158. $this->assertSame($expected, $redis->zrevrangebyscore('letters', 20, 10, array('limit' => array('offset' => 1, 'count' => 2))));
  159. }
  160. /**
  161. * @group connected
  162. */
  163. public function testRangeWithCombinedModifiers()
  164. {
  165. $redis = $this->getClient();
  166. $redis->zadd('letters', -10, 'a', 0, 'b', 10, 'c', 20, 'd', 20, 'e', 30, 'f');
  167. $options = array('limit' => array(1, 2), 'withscores' => true);
  168. $expected = array(array('d', '20'), array('c', '10'));
  169. $this->assertSame($expected, $redis->zrevrangebyscore('letters', 20, 10, $options));
  170. }
  171. /**
  172. * @group connected
  173. * @expectedException Predis\Response\ServerException
  174. * @expectedExceptionMessage Operation against a key holding the wrong kind of value
  175. */
  176. public function testThrowsExceptionOnWrongType()
  177. {
  178. $redis = $this->getClient();
  179. $redis->set('foo', 'bar');
  180. $redis->zrevrangebyscore('foo', 0, 10);
  181. }
  182. }