ZSetRangeByLexTest.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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 ZSetRangeByLexTest extends PredisCommandTestCase
  16. {
  17. /**
  18. * {@inheritdoc}
  19. */
  20. protected function getExpectedCommand()
  21. {
  22. return 'Predis\Command\ZSetRangeByLex';
  23. }
  24. /**
  25. * {@inheritdoc}
  26. */
  27. protected function getExpectedId()
  28. {
  29. return 'ZRANGEBYLEX';
  30. }
  31. /**
  32. * @group disconnected
  33. */
  34. public function testFilterArguments()
  35. {
  36. $modifiers = array(
  37. 'limit' => array(0, 100),
  38. );
  39. $arguments = array('zset', '[a', '[z', $modifiers);
  40. $expected = array('zset', '[a', '[z', 'LIMIT', 0, 100);
  41. $command = $this->getCommand();
  42. $command->setArguments($arguments);
  43. $this->assertSame($expected, $command->getArguments());
  44. }
  45. /**
  46. * @group disconnected
  47. */
  48. public function testFilterArgumentsWithNamedLimit()
  49. {
  50. $arguments = array('zset', '[a', '[z', array('limit' => array('offset' => 1, 'count' => 2)));
  51. $expected = array('zset', '[a', '[z', 'LIMIT', 1, 2);
  52. $command = $this->getCommand();
  53. $command->setArguments($arguments);
  54. $this->assertSame($expected, $command->getArguments());
  55. }
  56. /**
  57. * @group disconnected
  58. */
  59. public function testParseResponse()
  60. {
  61. $raw = array('a', 'b', 'c');
  62. $expected = array('a', 'b', 'c');
  63. $command = $this->getCommand();
  64. $this->assertSame($expected, $command->parseResponse($raw));
  65. }
  66. /**
  67. * @group disconnected
  68. */
  69. public function testPrefixKeys()
  70. {
  71. $modifiers = array(
  72. 'limit' => array(0, 100),
  73. );
  74. $arguments = array('zset', '[a', '[z', $modifiers);
  75. $expected = array('prefix:zset', '[a', '[z', 'LIMIT', 0, 100);
  76. $command = $this->getCommandWithArgumentsArray($arguments);
  77. $command->prefixKeys('prefix:');
  78. $this->assertSame($expected, $command->getArguments());
  79. }
  80. /**
  81. * @group disconnected
  82. */
  83. public function testPrefixKeysIgnoredOnEmptyArguments()
  84. {
  85. $command = $this->getCommand();
  86. $command->prefixKeys('prefix:');
  87. $this->assertSame(array(), $command->getArguments());
  88. }
  89. /**
  90. * @group connected
  91. * @requiresRedisVersion >= 2.8.9
  92. */
  93. public function testReturnsElementsInWholeRange()
  94. {
  95. $redis = $this->getClient();
  96. $redis->zadd('letters', 0, 'a', 0, 'b', 0, 'c', 0, 'd', 0, 'e', 0, 'f', 0, 'g');
  97. $this->assertSame(array('a', 'b', 'c', 'd', 'e', 'f', 'g'), $redis->zrangebylex('letters', '-', '+'));
  98. $this->assertSame(array(), $redis->zrangebylex('letters', '+', '-'));
  99. $this->assertSame(array(), $redis->zrangebylex('unknown', '-', '+'));
  100. $this->assertSame(array(), $redis->zrangebylex('unknown', '+', '-'));
  101. }
  102. /**
  103. * @group connected
  104. * @requiresRedisVersion >= 2.8.9
  105. */
  106. public function testReturnsElementsInInclusiveRange()
  107. {
  108. $redis = $this->getClient();
  109. $redis->zadd('letters', 0, 'a', 0, 'b', 0, 'c', 0, 'd', 0, 'e', 0, 'f', 0, 'g');
  110. $this->assertSame(array('a'), $redis->zrangebylex('letters', '[a', '[a'));
  111. $this->assertSame(array('c', 'd', 'e', 'f'), $redis->zrangebylex('letters', '[c', '[f'));
  112. $this->assertSame(array('a', 'b', 'c'), $redis->zrangebylex('letters', '-', '[c'));
  113. $this->assertSame(array(), $redis->zrangebylex('letters', '+', '[c'));
  114. $this->assertSame(array(), $redis->zrangebylex('letters', '[x', '[z'));
  115. $this->assertSame(array(), $redis->zrangebylex('unknown', '[0', '[1'));
  116. }
  117. /**
  118. * @group connected
  119. * @requiresRedisVersion >= 2.8.9
  120. */
  121. public function testReturnsElementsInExclusiveRange()
  122. {
  123. $redis = $this->getClient();
  124. $redis->zadd('letters', 0, 'a', 0, 'b', 0, 'c', 0, 'd', 0, 'e', 0, 'f', 0, 'g');
  125. $this->assertSame(array(), $redis->zrangebylex('letters', '(a', '(a'));
  126. $this->assertSame(array('d', 'e'), $redis->zrangebylex('letters', '(c', '(f'));
  127. $this->assertSame(array('a', 'b'), $redis->zrangebylex('letters', '-', '(c'));
  128. $this->assertSame(array(), $redis->zrangebylex('letters', '+', '(c'));
  129. $this->assertSame(array(), $redis->zrangebylex('letters', '(x', '(z'));
  130. $this->assertSame(array(), $redis->zrangebylex('unknown', '(0', '(1'));
  131. }
  132. /**
  133. * @group connected
  134. * @requiresRedisVersion >= 2.8.9
  135. */
  136. public function testReturnsElementsInMixedRange()
  137. {
  138. $redis = $this->getClient();
  139. $redis->zadd('letters', 0, 'a', 0, 'b', 0, 'c', 0, 'd', 0, 'e', 0, 'f', 0, 'g');
  140. $this->assertSame(array(), $redis->zrangebylex('letters', '[a', '(a'));
  141. $this->assertSame(array(), $redis->zrangebylex('letters', '(a', '[a'));
  142. $this->assertSame(array('c', 'd', 'e'), $redis->zrangebylex('letters', '[c', '(f'));
  143. $this->assertSame(array('d', 'e', 'f'), $redis->zrangebylex('letters', '(c', '[f'));
  144. $this->assertSame(array(), $redis->zrangebylex('unknown', '[0', '(5'));
  145. }
  146. /**
  147. * @group connected
  148. * @requiresRedisVersion >= 2.8.9
  149. */
  150. public function testRangeWithLimitModifier()
  151. {
  152. $redis = $this->getClient();
  153. $redis->zadd('letters', 0, 'a', 0, 'b', 0, 'c', 0, 'd', 0, 'e', 0, 'f', 0, 'g');
  154. $this->assertSame(array('c', 'd', 'e'), $redis->zrangebylex('letters', '-', '+', 'LIMIT', '2', '3'));
  155. $this->assertSame(array('c', 'd', 'e'), $redis->zrangebylex('letters', '-', '+', array('limit' => array(2, 3))));
  156. $this->assertSame(array('c', 'd', 'e'), $redis->zrangebylex('letters', '-', '+', array('limit' => array('offset' => 2, 'count' => 3))));
  157. $this->assertSame(array(), $redis->zrangebylex('letters', '[a', '[f', 'LIMIT', '2', '0'));
  158. $this->assertSame(array(), $redis->zrangebylex('letters', '[a', '[f', 'LIMIT', '-4', '2'));
  159. }
  160. /**
  161. * @group connected
  162. * @requiresRedisVersion >= 2.8.9
  163. * @expectedException Predis\ServerException
  164. * @expectedExceptionMessage min or max not valid string range item
  165. */
  166. public function testThrowsExceptionOnInvalidRangeFormat()
  167. {
  168. $redis = $this->getClient();
  169. $redis->zadd('letters', 0, 'a', 0, 'b', 0, 'c', 0, 'd', 0, 'e', 0, 'f', 0, 'g');
  170. $redis->zrangebylex('letters', 'b', 'f');
  171. }
  172. /**
  173. * @group connected
  174. * @requiresRedisVersion >= 2.8.9
  175. * @expectedException Predis\ServerException
  176. * @expectedExceptionMessage Operation against a key holding the wrong kind of value
  177. */
  178. public function testThrowsExceptionOnWrongType()
  179. {
  180. $redis = $this->getClient();
  181. $redis->set('foo', 'bar');
  182. $redis->zrangebylex('foo', '-', '+');
  183. }
  184. }