ZSetReverseRangeByLexTest.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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 ZSetReverseRangeByLexTest extends PredisCommandTestCase
  16. {
  17. /**
  18. * {@inheritdoc}
  19. */
  20. protected function getExpectedCommand()
  21. {
  22. return 'Predis\Command\ZSetReverseRangeByLex';
  23. }
  24. /**
  25. * {@inheritdoc}
  26. */
  27. protected function getExpectedId()
  28. {
  29. return 'ZREVRANGEBYLEX';
  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 connected
  68. * @requiresRedisVersion >= 2.8.9
  69. */
  70. public function testReturnsElementsInWholeRange()
  71. {
  72. $redis = $this->getClient();
  73. $redis->zadd('letters', 0, 'a', 0, 'b', 0, 'c', 0, 'd', 0, 'e', 0, 'f', 0, 'g');
  74. $this->assertSame(array('g', 'f', 'e', 'd', 'c', 'b', 'a'), $redis->zrevrangebylex('letters', '+', '-'));
  75. $this->assertSame(array(), $redis->zrevrangebylex('letters', '-', '+'));
  76. $this->assertSame(array(), $redis->zrevrangebylex('unknown', '-', '+'));
  77. $this->assertSame(array(), $redis->zrevrangebylex('unknown', '+', '-'));
  78. }
  79. /**
  80. * @group connected
  81. * @requiresRedisVersion >= 2.8.9
  82. */
  83. public function testReturnsElementsInInclusiveRange()
  84. {
  85. $redis = $this->getClient();
  86. $redis->zadd('letters', 0, 'a', 0, 'b', 0, 'c', 0, 'd', 0, 'e', 0, 'f', 0, 'g');
  87. $this->assertSame(array('a'), $redis->zrevrangebylex('letters', '[a', '[a'));
  88. $this->assertSame(array('f', 'e', 'd', 'c'), $redis->zrevrangebylex('letters', '[f', '[c'));
  89. $this->assertSame(array('g', 'f', 'e'), $redis->zrevrangebylex('letters', '+', '[e'));
  90. $this->assertSame(array(), $redis->zrevrangebylex('letters', '-', '[c'));
  91. $this->assertSame(array(), $redis->zrevrangebylex('letters', '[z', '[x'));
  92. $this->assertSame(array(), $redis->zrevrangebylex('unknown', '[1', '[0'));
  93. }
  94. /**
  95. * @group connected
  96. * @requiresRedisVersion >= 2.8.9
  97. */
  98. public function testReturnsElementsInExclusiveRange()
  99. {
  100. $redis = $this->getClient();
  101. $redis->zadd('letters', 0, 'a', 0, 'b', 0, 'c', 0, 'd', 0, 'e', 0, 'f', 0, 'g');
  102. $this->assertSame(array(), $redis->zrevrangebylex('letters', '(a', '(a'));
  103. $this->assertSame(array('e', 'd'), $redis->zrevrangebylex('letters', '(f', '(c'));
  104. $this->assertSame(array('g', 'f'), $redis->zrevrangebylex('letters', '+', '(e'));
  105. $this->assertSame(array(), $redis->zrevrangebylex('letters', '-', '(c'));
  106. $this->assertSame(array(), $redis->zrevrangebylex('letters', '(z', '(x'));
  107. $this->assertSame(array(), $redis->zrevrangebylex('unknown', '(1', '(0'));
  108. }
  109. /**
  110. * @group connected
  111. * @requiresRedisVersion >= 2.8.9
  112. */
  113. public function testReturnsElementsInMixedRange()
  114. {
  115. $redis = $this->getClient();
  116. $redis->zadd('letters', 0, 'a', 0, 'b', 0, 'c', 0, 'd', 0, 'e', 0, 'f', 0, 'g');
  117. $this->assertSame(array(), $redis->zrevrangebylex('letters', '[a', '(a'));
  118. $this->assertSame(array(), $redis->zrevrangebylex('letters', '(a', '[a'));
  119. $this->assertSame(array('f', 'e', 'd'), $redis->zrevrangebylex('letters', '[f', '(c'));
  120. $this->assertSame(array('e', 'd', 'c'), $redis->zrevrangebylex('letters', '(f', '[c'));
  121. $this->assertSame(array(), $redis->zrevrangebylex('unknown', '[5', '(0'));
  122. }
  123. /**
  124. * @group connected
  125. * @requiresRedisVersion >= 2.8.9
  126. */
  127. public function testRangeWithLimitModifier()
  128. {
  129. $redis = $this->getClient();
  130. $redis->zadd('letters', 0, 'a', 0, 'b', 0, 'c', 0, 'd', 0, 'e', 0, 'f', 0, 'g');
  131. $this->assertSame(array('e', 'd', 'c'), $redis->zrevrangebylex('letters', '+', '-', 'LIMIT', '2', '3'));
  132. $this->assertSame(array('e', 'd', 'c'), $redis->zrevrangebylex('letters', '+', '-', array('limit' => array(2, 3))));
  133. $this->assertSame(array('e', 'd', 'c'), $redis->zrevrangebylex('letters', '+', '-', array('limit' => array('offset' => 2, 'count' => 3))));
  134. $this->assertSame(array(), $redis->zrevrangebylex('letters', '[f', '[a', 'LIMIT', '2', '0'));
  135. $this->assertSame(array(), $redis->zrevrangebylex('letters', '[f', '[a', 'LIMIT', '-4', '2'));
  136. }
  137. /**
  138. * @group connected
  139. * @requiresRedisVersion >= 2.8.9
  140. * @expectedException \Predis\Response\ServerException
  141. * @expectedExceptionMessage min or max not valid string range item
  142. */
  143. public function testThrowsExceptionOnInvalidRangeFormat()
  144. {
  145. $redis = $this->getClient();
  146. $redis->zadd('letters', 0, 'a', 0, 'b', 0, 'c', 0, 'd', 0, 'e', 0, 'f', 0, 'g');
  147. $redis->zrevrangebylex('letters', 'f', 'b');
  148. }
  149. /**
  150. * @group connected
  151. * @requiresRedisVersion >= 2.8.9
  152. * @expectedException \Predis\Response\ServerException
  153. * @expectedExceptionMessage Operation against a key holding the wrong kind of value
  154. */
  155. public function testThrowsExceptionOnWrongType()
  156. {
  157. $redis = $this->getClient();
  158. $redis->set('foo', 'bar');
  159. $redis->zrevrangebylex('foo', '+', '-');
  160. }
  161. }