ZSetReverseRangeTest.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 ZSetReverseRangeTest extends CommandTestCase
  17. {
  18. /**
  19. * {@inheritdoc}
  20. */
  21. protected function getExpectedCommand()
  22. {
  23. return 'Predis\Command\ZSetReverseRange';
  24. }
  25. /**
  26. * {@inheritdoc}
  27. */
  28. protected function getExpectedId()
  29. {
  30. return 'ZREVRANGE';
  31. }
  32. /**
  33. * @group disconnected
  34. */
  35. public function testFilterArguments()
  36. {
  37. $arguments = array('zset', 0, 100, array('withscores' => true));
  38. $expected = array('zset', 0, 100, 'WITHSCORES');
  39. $command = $this->getCommand();
  40. $command->setArguments($arguments);
  41. $this->assertSame($expected, $command->getArguments());
  42. }
  43. /**
  44. * @group disconnected
  45. */
  46. public function testFilterArgumentsWithStringWithscores()
  47. {
  48. $arguments = array('zset', 0, 100, 'withscores');
  49. $expected = array('zset', 0, 100, 'WITHSCORES');
  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('element1', 'element2', 'element3');
  60. $expected = array('element1', 'element2', 'element3');
  61. $command = $this->getCommand();
  62. $this->assertSame($expected, $command->parseResponse($raw));
  63. }
  64. /**
  65. * @group disconnected
  66. */
  67. public function testParseResponseWithScores()
  68. {
  69. $raw = array('element1', '1', 'element2', '2', 'element3', '3');
  70. $expected = array(array('element1', '1'), array('element2', '2'), array('element3', '3'));
  71. $command = $this->getCommandWithArgumentsArray(array('zset', 0, 1, 'withscores'));
  72. $this->assertSame($expected, $command->parseResponse($raw));
  73. }
  74. /**
  75. * @group disconnected
  76. */
  77. public function testPrefixKeys()
  78. {
  79. $arguments = array('zset', 0, 100, array('withscores' => true));
  80. $expected = array('prefix:zset', 0, 100, 'WITHSCORES');
  81. $command = $this->getCommandWithArgumentsArray($arguments);
  82. $command->prefixKeys('prefix:');
  83. $this->assertSame($expected, $command->getArguments());
  84. }
  85. /**
  86. * @group disconnected
  87. */
  88. public function testPrefixKeysIgnoredOnEmptyArguments()
  89. {
  90. $command = $this->getCommand();
  91. $command->prefixKeys('prefix:');
  92. $this->assertSame(array(), $command->getArguments());
  93. }
  94. /**
  95. * @group disconnected
  96. */
  97. public function testAddsWithscoresModifiersOnlyWhenOptionIsTrue()
  98. {
  99. $command = $this->getCommandWithArguments('zset', 0, 100, array('withscores' => true));
  100. $this->assertSame(array('zset', 0, 100, 'WITHSCORES'), $command->getArguments());
  101. $command = $this->getCommandWithArguments('zset', 0, 100, array('withscores' => 1));
  102. $this->assertSame(array('zset', 0, 100, 'WITHSCORES'), $command->getArguments());
  103. $command = $this->getCommandWithArguments('zset', 0, 100, array('withscores' => false));
  104. $this->assertSame(array('zset', 0, 100), $command->getArguments());
  105. $command = $this->getCommandWithArguments('zset', 0, 100, array('withscores' => 0));
  106. $this->assertSame(array('zset', 0, 100), $command->getArguments());
  107. }
  108. /**
  109. * @group connected
  110. */
  111. public function testReturnsElementsInRange()
  112. {
  113. $redis = $this->getClient();
  114. $redis->zadd('letters', -10, 'a', 0, 'b', 10, 'c', 20, 'd', 20, 'e', 30, 'f');
  115. $this->assertSame(array(), $redis->zrevrange('letters', 1, 0));
  116. $this->assertSame(array('f'), $redis->zrevrange('letters', 0, 0));
  117. $this->assertSame(array('f', 'e', 'd', 'c'), $redis->zrevrange('letters', 0, 3));
  118. $this->assertSame(array('f', 'e', 'd', 'c', 'b', 'a'), $redis->zrevrange('letters', 0, -1));
  119. $this->assertSame(array('f', 'e', 'd'), $redis->zrevrange('letters', 0, -4));
  120. $this->assertSame(array('d'), $redis->zrevrange('letters', 2, -4));
  121. $this->assertSame(array('f', 'e', 'd', 'c', 'b', 'a'), $redis->zrevrange('letters', -100, 100));
  122. $this->assertSame(array(), $redis->zrevrange('unknown', 0, 30));
  123. }
  124. /**
  125. * @group connected
  126. */
  127. public function testRangeWithWithscoresModifier()
  128. {
  129. $redis = $this->getClient();
  130. $redis->zadd('letters', -10, 'a', 0, 'b', 10, 'c', 20, 'd', 20, 'e', 30, 'f');
  131. $expected = array(array('d', '20'), array('c', '10'), array('b', '0'));
  132. $this->assertSame($expected, $redis->zrevrange('letters', 2, 4, 'withscores'));
  133. $this->assertSame($expected, $redis->zrevrange('letters', 2, 4, array('withscores' => true)));
  134. }
  135. /**
  136. * @group connected
  137. * @expectedException Predis\Response\ServerException
  138. * @expectedExceptionMessage Operation against a key holding the wrong kind of value
  139. */
  140. public function testThrowsExceptionOnWrongType()
  141. {
  142. $redis = $this->getClient();
  143. $redis->set('foo', 'bar');
  144. $redis->zrevrange('foo', 0, 10);
  145. }
  146. }