ZSetReverseRangeByLexTest.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. /*
  3. * This file is part of the Predis package.
  4. *
  5. * For the full copyright and license information, please view the LICENSE
  6. * file that was distributed with this source code.
  7. */
  8. namespace Predis\Command;
  9. /**
  10. * @group commands
  11. * @group realm-zset
  12. */
  13. class ZSetRevRangeByLexTest extends PredisCommandTestCase
  14. {
  15. /**
  16. * {@inheritdoc}
  17. */
  18. protected function getExpectedCommand()
  19. {
  20. return 'Predis\Command\ZSetReverseRangeByLex';
  21. }
  22. /**
  23. * {@inheritdoc}
  24. */
  25. protected function getExpectedId()
  26. {
  27. return 'ZREVRANGEBYLEX';
  28. }
  29. /**
  30. * @group disconnected
  31. */
  32. public function testFilterArguments()
  33. {
  34. $modifiers = array(
  35. 'limit' => array(0, 100),
  36. );
  37. $arguments = array('zset', '[a', '[z', $modifiers);
  38. $expected = array('zset', '[a', '[z', 'LIMIT', 0, 100);
  39. $command = $this->getCommand();
  40. $command->setArguments($arguments);
  41. $this->assertSame($expected, $command->getArguments());
  42. }
  43. /**
  44. * @group disconnected
  45. */
  46. public function testFilterArgumentsWithNamedLimit()
  47. {
  48. $arguments = array('zset', '[a', '[z', array('limit' => array('offset' => 1, 'count' => 2)));
  49. $expected = array('zset', '[a', '[z', 'LIMIT', 1, 2);
  50. $command = $this->getCommand();
  51. $command->setArguments($arguments);
  52. $this->assertSame($expected, $command->getArguments());
  53. }
  54. /**
  55. * @group disconnected
  56. */
  57. public function testParseResponse()
  58. {
  59. $raw = array('a', 'b', 'c');
  60. $expected = array('a', 'b', 'c');
  61. $command = $this->getCommand();
  62. $this->assertSame($expected, $command->parseResponse($raw));
  63. }
  64. /**
  65. * @group connected
  66. * @requiresRedisVersion >= 2.8.9
  67. */
  68. public function testReturnsElementsInWholeRange()
  69. {
  70. $redis = $this->getClient();
  71. $redis->zadd('letters', 0, 'a', 0, 'b', 0, 'c', 0, 'd', 0, 'e', 0, 'f', 0, 'g');
  72. $this->assertSame(array('g', 'f', 'e', 'd', 'c', 'b', 'a'), $redis->zrevrangebylex('letters', '+', '-'));
  73. $this->assertSame(array(), $redis->zrevrangebylex('letters', '-', '+'));
  74. $this->assertSame(array(), $redis->zrevrangebylex('unknown', '-', '+'));
  75. $this->assertSame(array(), $redis->zrevrangebylex('unknown', '+', '-'));
  76. }
  77. /**
  78. * @group connected
  79. * @requiresRedisVersion >= 2.8.9
  80. */
  81. public function testReturnsElementsInInclusiveRange()
  82. {
  83. $redis = $this->getClient();
  84. $redis->zadd('letters', 0, 'a', 0, 'b', 0, 'c', 0, 'd', 0, 'e', 0, 'f', 0, 'g');
  85. $this->assertSame(array('a'), $redis->zrevrangebylex('letters', '[a', '[a'));
  86. $this->assertSame(array('f', 'e', 'd', 'c'), $redis->zrevrangebylex('letters', '[f', '[c'));
  87. $this->assertSame(array('g', 'f', 'e'), $redis->zrevrangebylex('letters', '+', '[e'));
  88. $this->assertSame(array(), $redis->zrevrangebylex('letters', '-', '[c'));
  89. $this->assertSame(array(), $redis->zrevrangebylex('letters', '[z', '[x'));
  90. $this->assertSame(array(), $redis->zrevrangebylex('unknown', '[1', '[0'));
  91. }
  92. /**
  93. * @group connected
  94. * @requiresRedisVersion >= 2.8.9
  95. */
  96. public function testReturnsElementsInExclusiveRange()
  97. {
  98. $redis = $this->getClient();
  99. $redis->zadd('letters', 0, 'a', 0, 'b', 0, 'c', 0, 'd', 0, 'e', 0, 'f', 0, 'g');
  100. $this->assertSame(array(), $redis->zrevrangebylex('letters', '(a', '(a'));
  101. $this->assertSame(array('e', 'd'), $redis->zrevrangebylex('letters', '(f', '(c'));
  102. $this->assertSame(array('g', 'f'), $redis->zrevrangebylex('letters', '+', '(e'));
  103. $this->assertSame(array(), $redis->zrevrangebylex('letters', '-', '(c'));
  104. $this->assertSame(array(), $redis->zrevrangebylex('letters', '(z', '(x'));
  105. $this->assertSame(array(), $redis->zrevrangebylex('unknown', '(1', '(0'));
  106. }
  107. /**
  108. * @group connected
  109. * @requiresRedisVersion >= 2.8.9
  110. */
  111. public function testReturnsElementsInMixedRange()
  112. {
  113. $redis = $this->getClient();
  114. $redis->zadd('letters', 0, 'a', 0, 'b', 0, 'c', 0, 'd', 0, 'e', 0, 'f', 0, 'g');
  115. $this->assertSame(array(), $redis->zrevrangebylex('letters', '[a', '(a'));
  116. $this->assertSame(array(), $redis->zrevrangebylex('letters', '(a', '[a'));
  117. $this->assertSame(array('f', 'e', 'd'), $redis->zrevrangebylex('letters', '[f', '(c'));
  118. $this->assertSame(array('e', 'd', 'c'), $redis->zrevrangebylex('letters', '(f', '[c'));
  119. $this->assertSame(array(), $redis->zrevrangebylex('unknown', '[5', '(0'));
  120. }
  121. /**
  122. * @group connected
  123. * @requiresRedisVersion >= 2.8.9
  124. */
  125. public function testRangeWithLimitModifier()
  126. {
  127. $redis = $this->getClient();
  128. $redis->zadd('letters', 0, 'a', 0, 'b', 0, 'c', 0, 'd', 0, 'e', 0, 'f', 0, 'g');
  129. $this->assertSame(array('e', 'd', 'c'), $redis->zrevrangebylex('letters', '+', '-', 'LIMIT', '2', '3'));
  130. $this->assertSame(array('e', 'd', 'c'), $redis->zrevrangebylex('letters', '+', '-', array('limit' => array(2, 3))));
  131. $this->assertSame(array('e', 'd', 'c'), $redis->zrevrangebylex('letters', '+', '-', array('limit' => array('offset' => 2, 'count' => 3))));
  132. $this->assertSame(array(), $redis->zrevrangebylex('letters', '[f', '[a', 'LIMIT', '2', '0'));
  133. $this->assertSame(array(), $redis->zrevrangebylex('letters', '[f', '[a', 'LIMIT', '-4', '2'));
  134. }
  135. /**
  136. * @group connected
  137. * @requiresRedisVersion >= 2.8.9
  138. * @expectedException \Predis\Response\ServerException
  139. * @expectedExceptionMessage min or max not valid string range item
  140. */
  141. public function testThrowsExceptionOnInvalidRangeFormat()
  142. {
  143. $redis = $this->getClient();
  144. $redis->zadd('letters', 0, 'a', 0, 'b', 0, 'c', 0, 'd', 0, 'e', 0, 'f', 0, 'g');
  145. $redis->zrevrangebylex('letters', 'f', 'b');
  146. }
  147. /**
  148. * @group connected
  149. * @requiresRedisVersion >= 2.8.9
  150. * @expectedException \Predis\Response\ServerException
  151. * @expectedExceptionMessage Operation against a key holding the wrong kind of value
  152. */
  153. public function testThrowsExceptionOnWrongType()
  154. {
  155. $redis = $this->getClient();
  156. $redis->set('foo', 'bar');
  157. $redis->zrevrangebylex('foo', '+', '-');
  158. }
  159. }