ClientTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Tioncico
  5. * Date: 2019/9/24 0024
  6. * Time: 16:16
  7. */
  8. namespace Test;
  9. use EasySwoole\Redis\Client;
  10. use EasySwoole\Redis\CommandConst;
  11. use PHPUnit\Framework\TestCase;
  12. class ClientTest extends TestCase
  13. {
  14. /**
  15. * @var $client Client
  16. */
  17. protected $client;
  18. protected function setUp():void
  19. {
  20. parent::setUp(); // TODO: Change the autogenerated stub
  21. $client = new Client(REDIS_HOST, REDIS_PORT);
  22. $client->connect();
  23. if (REDIS_AUTH != '') {
  24. $client->sendCommand(['AUTH', REDIS_AUTH]);
  25. $recv = $client->recv();
  26. }
  27. $this->client = $client;
  28. }
  29. function testRecv()
  30. {
  31. $this->client->sendCommand(['set', 'a', '1']);
  32. $recv = $this->client->recv();
  33. $this->assertEquals('OK', $recv->getData());
  34. $this->client->sendCommand(['st', 'a', '1']);
  35. $recv = $this->client->recv();
  36. $this->assertEquals('-1', $recv->getStatus());
  37. $num = 1;
  38. $this->client->sendCommand(['set', 'b', $num]);
  39. $this->client->recv();
  40. $this->client->sendCommand(['incr', 'b']);
  41. $recv = $this->client->recv();
  42. $this->assertEquals('0', $recv->getStatus());
  43. $this->assertEquals($num + 1, $recv->getData());
  44. $this->client->sendCommand(['set', "a1", "a \r\n b \r\n 123456 \r\na"]);
  45. $this->client->recv();
  46. $this->client->sendCommand(['get', "a1"]);
  47. $recv = $this->client->recv();
  48. $this->assertEquals("a \r\n b \r\n 123456 \r\na", $recv->getData());
  49. $this->client->sendCommand(['del', "listA"]);
  50. $this->client->recv();
  51. $this->client->sendCommand(['sadd', "listA", 1]);
  52. $this->client->recv();
  53. $this->client->sendCommand(['sadd', "listA", 'a']);
  54. $this->client->recv();
  55. $this->client->sendCommand(['sadd', "listA", null]);
  56. $this->client->recv();
  57. $this->client->sendCommand(['sadd', "listA", "1\r\n 2\r\n a\r\nf"]);
  58. $this->client->recv();
  59. $this->client->sendCommand(['sadd', "listA", " "]);
  60. $this->client->recv();
  61. $this->client->sendCommand(['smembers', "listA"]);
  62. $recv = $this->client->recv();
  63. $this->assertEquals(5, count($recv->getData()));
  64. }
  65. function testInt()
  66. {
  67. $num = 1;
  68. $this->client->sendCommand(['set', 'b', $num]);
  69. $this->client->recv();
  70. $this->client->sendCommand(['incr', 'b']);
  71. $recv = $this->client->recv();
  72. $this->assertEquals('0', $recv->getStatus());
  73. $this->assertTrue(is_integer($recv->getData()));
  74. $this->assertEquals($num + 1, $recv->getData());
  75. }
  76. function testHash()
  77. {
  78. $this->client->sendCommand(['del', "ha"]);
  79. $this->client->recv();
  80. $this->client->sendCommand(['hset', "ha", 'a', 1]);
  81. $this->client->recv();
  82. $this->client->sendCommand(['hset', "ha", 'b', '0']);
  83. $this->client->recv();
  84. $this->client->sendCommand(['hset', "ha", 'c', null]);
  85. $this->client->recv();
  86. $this->client->sendCommand(['hset', "ha", 'd', "1\r\n 2\r\n a\r\nf"]);
  87. $this->client->recv();
  88. $this->client->sendCommand(['hset', "ha", 'e', " "]);
  89. $this->client->recv();
  90. $this->client->sendCommand(['hgetall', "ha"]);
  91. $recv = $this->client->recv();
  92. $this->assertEquals(10, count($recv->getData()));
  93. $this->assertEquals('a', $recv->getData()[0]);
  94. }
  95. function testGeohash()
  96. {
  97. $this->client->sendCommand(['del', 'geoa']);
  98. $recv = $this->client->recv();
  99. $this->assertEquals($recv::STATUS_OK, $recv->getStatus());
  100. $this->client->sendCommand(['geoadd', "geoa", '118.6197800000', '24.88849', 'user1', '118.6197800000', '24.88859', 'user2', '114.8197800000', '25.88849', 'user3', '118.8197800000', '22.88849', 'user4']);
  101. $recv = $this->client->recv();
  102. $this->assertEquals(4, $recv->getData());
  103. $this->client->sendCommand(['geohash', 'geoa', 'user1', 'user2', 'user3']);
  104. $recv = $this->client->recv();
  105. $this->assertEquals('wskme6b3cn0', $recv->getData()[0]);
  106. $this->client->sendCommand(['georadiusbymember', 'geoa', 'user1', '100', 'km', 'desc']);
  107. $recv = $this->client->recv();
  108. $this->assertEquals('user2', $recv->getData()[0]);
  109. $this->client->sendCommand(['geopos', 'geoa', 'user1', 'user2']);
  110. $recv = $this->client->recv();
  111. $this->client->sendCommand(['georadius', 'geoa', $recv->getData()[0][0], $recv->getData()[0][1], '100', 'm', 'desc']);
  112. $recv = $this->client->recv();
  113. $this->assertEquals('user2', $recv->getData()[0]);
  114. }
  115. }