KeyScanTest.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 PredisTestCase;
  12. /**
  13. * @group commands
  14. * @group realm-key
  15. */
  16. class KeyScanTest extends PredisCommandTestCase
  17. {
  18. /**
  19. * {@inheritdoc}
  20. */
  21. protected function getExpectedCommand()
  22. {
  23. return 'Predis\Command\KeyScan';
  24. }
  25. /**
  26. * {@inheritdoc}
  27. */
  28. protected function getExpectedId()
  29. {
  30. return 'SCAN';
  31. }
  32. /**
  33. * @group disconnected
  34. */
  35. public function testFilterArguments()
  36. {
  37. $arguments = array(0, 'MATCH', 'key:*', 'COUNT', 5);
  38. $expected = array(0, 'MATCH', 'key:*', 'COUNT', 5);
  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(0);
  49. $expected = array(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(0, array('match' => 'key:*', 'count' => 5));
  60. $expected = array(0, 'MATCH', 'key:*', 'COUNT', 5);
  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('key:1', 'key:2', 'key:3'));
  71. $expected = array(3, array('key:1', 'key:2', 'key:3'));
  72. $command = $this->getCommand();
  73. $this->assertSame($expected, $command->parseResponse($raw));
  74. }
  75. /**
  76. * @group connected
  77. */
  78. public function testScanWithoutMatch()
  79. {
  80. $kvs = array('key:one' => 'one', 'key:two' => 'two', 'key:three' => 'three', 'key:four' => 'four');
  81. $redis = $this->getClient();
  82. $redis->mset($kvs);
  83. $response = $redis->scan(0);
  84. $this->assertSameValues(array_keys($kvs), $response[1]);
  85. }
  86. /**
  87. * @group connected
  88. */
  89. public function testScanWithMatchingKeys()
  90. {
  91. $kvs = array('key:one' => 'one', 'key:two' => 'two', 'key:three' => 'three', 'key:four' => 'four');
  92. $redis = $this->getClient();
  93. $redis->mset($kvs);
  94. $response = $redis->scan(0, 'MATCH', 'key:t*');
  95. $this->assertSameValues(array('key:two', 'key:three'), $response[1]);
  96. }
  97. /**
  98. * @group connected
  99. */
  100. public function testScanWithNoMatchingKeys()
  101. {
  102. $kvs = array('key:one' => 'one', 'key:two' => 'two', 'key:three' => 'three', 'key:four' => 'four');
  103. $redis = $this->getClient();
  104. $redis->mset($kvs);
  105. $response = $redis->scan(0, 'MATCH', 'nokey:*');
  106. $this->assertSame(0, $response[0]);
  107. $this->assertEmpty($response[1]);
  108. }
  109. }