ZRANGE_Test.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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\Redis;
  11. /**
  12. * @group commands
  13. * @group realm-zset
  14. */
  15. class ZRANGE_Test extends PredisCommandTestCase
  16. {
  17. /**
  18. * {@inheritdoc}
  19. */
  20. protected function getExpectedCommand()
  21. {
  22. return 'Predis\Command\Redis\ZRANGE';
  23. }
  24. /**
  25. * {@inheritdoc}
  26. */
  27. protected function getExpectedId()
  28. {
  29. return 'ZRANGE';
  30. }
  31. /**
  32. * @group disconnected
  33. */
  34. public function testFilterArguments()
  35. {
  36. $arguments = array('zset', 0, 100, array('withscores' => true));
  37. $expected = array('zset', 0, 100, 'WITHSCORES');
  38. $command = $this->getCommand();
  39. $command->setArguments($arguments);
  40. $this->assertSame($expected, $command->getArguments());
  41. }
  42. /**
  43. * @group disconnected
  44. */
  45. public function testFilterArgumentsWithStringWithscores()
  46. {
  47. $arguments = array('zset', 0, 100, 'withscores');
  48. $expected = array('zset', 0, 100, 'WITHSCORES');
  49. $command = $this->getCommand();
  50. $command->setArguments($arguments);
  51. $this->assertSame($expected, $command->getArguments());
  52. }
  53. /**
  54. * @group disconnected
  55. */
  56. public function testParseResponse()
  57. {
  58. $raw = array('element1', 'element2', 'element3');
  59. $expected = array('element1', 'element2', 'element3');
  60. $command = $this->getCommand();
  61. $this->assertSame($expected, $command->parseResponse($raw));
  62. }
  63. /**
  64. * @group disconnected
  65. */
  66. public function testParseResponseWithScores()
  67. {
  68. $raw = array('element1', '1', 'element2', '2', 'element3', '3');
  69. $expected = array('element1' => '1', 'element2' => '2', 'element3' => '3');
  70. $command = $this->getCommandWithArgumentsArray(array('zset', 0, 1, 'withscores'));
  71. $this->assertSame($expected, $command->parseResponse($raw));
  72. }
  73. /**
  74. * @group disconnected
  75. */
  76. public function testAddsWithscoresModifiersOnlyWhenOptionIsTrue()
  77. {
  78. $command = $this->getCommandWithArguments('zset', 0, 100, array('withscores' => true));
  79. $this->assertSame(array('zset', 0, 100, 'WITHSCORES'), $command->getArguments());
  80. $command = $this->getCommandWithArguments('zset', 0, 100, array('withscores' => 1));
  81. $this->assertSame(array('zset', 0, 100, 'WITHSCORES'), $command->getArguments());
  82. $command = $this->getCommandWithArguments('zset', 0, 100, array('withscores' => false));
  83. $this->assertSame(array('zset', 0, 100), $command->getArguments());
  84. $command = $this->getCommandWithArguments('zset', 0, 100, array('withscores' => 0));
  85. $this->assertSame(array('zset', 0, 100), $command->getArguments());
  86. }
  87. /**
  88. * @group connected
  89. */
  90. public function testReturnsElementsInRange()
  91. {
  92. $redis = $this->getClient();
  93. $redis->zadd('letters', -10, 'a', 0, 'b', 10, 'c', 20, 'd', 20, 'e', 30, 'f');
  94. $this->assertSame(array(), $redis->zrange('letters', 1, 0));
  95. $this->assertSame(array('a'), $redis->zrange('letters', 0, 0));
  96. $this->assertSame(array('a', 'b', 'c', 'd'), $redis->zrange('letters', 0, 3));
  97. $this->assertSame(array('a', 'b', 'c', 'd', 'e', 'f'), $redis->zrange('letters', 0, -1));
  98. $this->assertSame(array('a', 'b', 'c'), $redis->zrange('letters', 0, -4));
  99. $this->assertSame(array('c'), $redis->zrange('letters', 2, -4));
  100. $this->assertSame(array('a', 'b', 'c', 'd', 'e', 'f'), $redis->zrange('letters', -100, 100));
  101. $this->assertSame(array(), $redis->zrange('unknown', 0, 30));
  102. }
  103. /**
  104. * @group connected
  105. */
  106. public function testRangeWithWithscoresModifier()
  107. {
  108. $redis = $this->getClient();
  109. $redis->zadd('letters', -10, 'a', 0, 'b', 10, 'c', 20, 'd', 20, 'e', 30, 'f');
  110. $expected = array('c' => '10', 'd' => '20', 'e' => '20');
  111. $this->assertSame($expected, $redis->zrange('letters', 2, 4, 'withscores'));
  112. $this->assertSame($expected, $redis->zrange('letters', 2, 4, array('withscores' => true)));
  113. }
  114. /**
  115. * @group connected
  116. * @expectedException \Predis\Response\ServerException
  117. * @expectedExceptionMessage Operation against a key holding the wrong kind of value
  118. */
  119. public function testThrowsExceptionOnWrongType()
  120. {
  121. $redis = $this->getClient();
  122. $redis->set('foo', 'bar');
  123. $redis->zrange('foo', 0, 10);
  124. }
  125. }