EventTest.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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\CommandConst;
  10. use EasySwoole\Redis\Config\RedisClusterConfig;
  11. use EasySwoole\Redis\Config\RedisConfig;
  12. use EasySwoole\Redis\Redis;
  13. use EasySwoole\Redis\RedisCluster;
  14. use PHPUnit\Framework\TestCase;
  15. class EventTest extends TestCase
  16. {
  17. /**
  18. * @var $redis Redis
  19. */
  20. protected $redis;
  21. /**
  22. * @var $redis RedisCluster
  23. */
  24. protected $redisCluster;
  25. protected function setUp():void
  26. {
  27. parent::setUp();
  28. $redisConfig = new RedisConfig([
  29. 'host' => REDIS_HOST,
  30. 'port' => REDIS_PORT,
  31. 'auth' => REDIS_AUTH,
  32. ]);
  33. $this->redis = new Redis($redisConfig);
  34. $this->redisCluster = new RedisCluster(new RedisClusterConfig(REDIS_CLUSTER_SERVER_LIST, [
  35. 'auth' => REDIS_CLUSTER_AUTH,
  36. ]));
  37. }
  38. function testRedis(){
  39. $redis = $this->redis;
  40. $redis->getConfig()->onBeforeEvent(function ($commandName,$commandData){
  41. $this->assertEquals(CommandConst::SET,$commandName);
  42. $this->assertEquals('a',$commandData[0]);
  43. $this->assertEquals('1',$commandData[1]);
  44. });
  45. $redis->getConfig()->onAfterEvent(function ($commandName,$commandData,$result){
  46. $this->assertEquals(CommandConst::SET,$commandName);
  47. $this->assertEquals('a',$commandData[0]);
  48. $this->assertEquals('1',$commandData[1]);
  49. $this->assertEquals(true,$result);
  50. });
  51. $redis->set('a',1);
  52. }
  53. function testCluster(){
  54. $redis = $this->redisCluster;
  55. $redis->getConfig()->onBeforeEvent(function ($commandName,$commandData){
  56. $this->assertEquals(CommandConst::SET,$commandName);
  57. $this->assertEquals('a',$commandData[0]);
  58. $this->assertEquals('1',$commandData[1]);
  59. });
  60. $redis->getConfig()->onAfterEvent(function ($commandName,$commandData,$result){
  61. $this->assertEquals(CommandConst::SET,$commandName);
  62. $this->assertEquals('a',$commandData[0]);
  63. $this->assertEquals('1',$commandData[1]);
  64. $this->assertEquals(true,$result);
  65. });
  66. $redis->set('a',1);
  67. }
  68. function testPIPE(){
  69. $redis = $this->redis;
  70. $redis->getConfig()->onBeforeEvent(function ($commandName,$commandData){
  71. var_dump ($commandName,$commandData);
  72. $this->assertEquals(1,1);
  73. });
  74. $redis->getConfig()->onAfterEvent(function ($commandName,$commandData,$result){
  75. var_dump ($commandName,$commandData,$result);
  76. $this->assertEquals(1,1);
  77. });
  78. $redis->startPipe();
  79. $redis->set('a',1);
  80. $redis->get('a',1);
  81. $redis->execPipe();
  82. }
  83. }