ZSetScanTest.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 ZSetScanTest extends CommandTestCase
  17. {
  18. /**
  19. * {@inheritdoc}
  20. */
  21. protected function getExpectedCommand()
  22. {
  23. return 'Predis\Command\ZSetScan';
  24. }
  25. /**
  26. * {@inheritdoc}
  27. */
  28. protected function getExpectedId()
  29. {
  30. return 'ZSCAN';
  31. }
  32. /**
  33. * @group disconnected
  34. */
  35. public function testFilterArguments()
  36. {
  37. $arguments = array('key', 0, 'MATCH', 'member:*', 'COUNT', 10);
  38. $expected = array('key', 0, 'MATCH', 'member:*', 'COUNT', 10);
  39. $command = $this->getCommand();
  40. $command->setArguments($arguments);
  41. $this->assertSame($expected, $command->getArguments());
  42. }
  43. /**
  44. * @group disconnected
  45. */
  46. public function testFilterArgumentsBasicUsage()
  47. {
  48. $arguments = array('key', 0);
  49. $expected = array('key', 0);
  50. $command = $this->getCommand();
  51. $command->setArguments($arguments);
  52. $this->assertSame($expected, $command->getArguments());
  53. }
  54. /**
  55. * @group disconnected
  56. */
  57. public function testFilterArgumentsWithOptionsArray()
  58. {
  59. $arguments = array('key', 0, array('match' => 'member:*', 'count' => 10));
  60. $expected = array('key', 0, 'MATCH', 'member:*', 'COUNT', 10);
  61. $command = $this->getCommand();
  62. $command->setArguments($arguments);
  63. $this->assertSame($expected, $command->getArguments());
  64. }
  65. /**
  66. * @group disconnected
  67. */
  68. public function testParseResponse()
  69. {
  70. $raw = array('3', array('member:1', '1', 'member:2', '2', 'member:3', '3'));
  71. $expected = array(3, array(array('member:1' , 1.0), array('member:2', 2.0), array('member:3' , 3.0)));
  72. $command = $this->getCommand();
  73. $this->assertSame($expected, $command->parseResponse($raw));
  74. }
  75. /**
  76. * @group disconnected
  77. */
  78. public function testPrefixKeys()
  79. {
  80. $arguments = array('key', '0', 'MATCH', 'member:*', 'COUNT', 10);
  81. $expected = array('prefix:key', '0', 'MATCH', 'member:*', 'COUNT', 10);
  82. $command = $this->getCommandWithArgumentsArray($arguments);
  83. $command->prefixKeys('prefix:');
  84. $this->assertSame($expected, $command->getArguments());
  85. }
  86. /**
  87. * @group disconnected
  88. */
  89. public function testPrefixKeysIgnoredOnEmptyArguments()
  90. {
  91. $command = $this->getCommand();
  92. $command->prefixKeys('prefix:');
  93. $this->assertSame(array(), $command->getArguments());
  94. }
  95. /**
  96. * @group connected
  97. */
  98. public function testScanWithoutMatch()
  99. {
  100. $expectedMembers = array('member:one', 'member:two', 'member:three', 'member:four');
  101. $expectedScores = array(1.0, 2.0, 3.0, 4.0);
  102. $redis = $this->getClient();
  103. $redis->zadd('key', array_combine($expectedMembers, $expectedScores));
  104. $response = $redis->zscan('key', 0);
  105. $this->assertSame(0, $response[0]);
  106. $this->assertSame($expectedMembers, array_map(function($e) { return $e[0]; }, $response[1]));
  107. $this->assertSame($expectedScores, array_map(function($e) { return $e[1]; }, $response[1]));
  108. }
  109. /**
  110. * @group connected
  111. */
  112. public function testScanWithMatchingMembers()
  113. {
  114. $redis = $this->getClient();
  115. $redis->zadd('key', array('member:one' => 1.0, 'member:two' => 2.0, 'member:three' => 3.0, 'member:four' => 4.0));
  116. $response = $redis->zscan('key', 0, 'MATCH', 'member:t*');
  117. $this->assertSame(array('member:two', 'member:three'), array_map(function($e) { return $e[0]; }, $response[1]));
  118. $this->assertSame(array(2.0, 3.0), array_map(function($e) { return $e[1]; }, $response[1]));
  119. }
  120. /**
  121. * @group connected
  122. */
  123. public function testScanWithNoMatchingMembers()
  124. {
  125. $redis = $this->getClient();
  126. $redis->zadd('key', $members = array('member:one' => 1.0, 'member:two' => 2.0, 'member:three' => 3.0, 'member:four' => 4.0));
  127. $response = $redis->zscan('key', 0, 'MATCH', 'nomember:*');
  128. $this->assertSame(0, $response[0]);
  129. $this->assertEmpty($response[1]);
  130. }
  131. }