OtherTest.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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\Config\RedisConfig;
  10. use EasySwoole\Redis\Redis;
  11. use PHPUnit\Framework\TestCase;
  12. class OtherTest extends TestCase
  13. {
  14. /**
  15. * @var $redis Redis
  16. */
  17. protected $redis;
  18. /**
  19. * @var $redisPHPSerialize Redis
  20. */
  21. protected $redisPHPSerialize;
  22. /**
  23. * @var $redisJsonSerialize Redis
  24. */
  25. protected $redisJsonSerialize;
  26. protected function setUp() :void
  27. {
  28. parent::setUp(); // TODO: Change the autogenerated stub
  29. $this->redis = new Redis(new RedisConfig([
  30. 'host' => REDIS_HOST,
  31. 'port' => REDIS_PORT,
  32. 'auth' => REDIS_AUTH
  33. ]));
  34. $this->redisPHPSerialize = new Redis(new RedisConfig([
  35. 'host' => REDIS_HOST,
  36. 'port' => REDIS_PORT,
  37. 'auth' => REDIS_AUTH,
  38. 'serialize' => RedisConfig::SERIALIZE_PHP
  39. ]));
  40. $this->redisJsonSerialize = new Redis(new RedisConfig([
  41. 'host' => REDIS_HOST,
  42. 'port' => REDIS_PORT,
  43. 'auth' => REDIS_AUTH,
  44. 'serialize' => RedisConfig::SERIALIZE_JSON
  45. ]));
  46. $this->redis->connect();
  47. $this->redisPHPSerialize->connect();
  48. $this->redisJsonSerialize->connect();
  49. }
  50. /**
  51. * testPHPSerialize
  52. * @dataProvider phpSerializeData
  53. * @param $data
  54. * @author Tioncico
  55. * Time: 15:45
  56. */
  57. function testPHPSerialize($data)
  58. {
  59. $key = 'test';
  60. //进行序列化测试,数字,字符串,empty,0,null,对象,空对象,数组,空数组
  61. //使用未序列化的redis进行set
  62. $this->redis->set($key, serialize($data));
  63. //使用序列化的redis进行get
  64. $redisData = $this->redisPHPSerialize->get($key);
  65. $this->assertEquals($redisData, $data);
  66. }
  67. /**
  68. * testJSONSerialize
  69. * @dataProvider jsonSerializeData
  70. * @param $data
  71. * @author Tioncico
  72. * Time: 15:45
  73. */
  74. function testJSONSerialize($data)
  75. {
  76. $key = 'test';
  77. //进行序列化测试,数字,字符串,empty,0,null,对象,空对象,数组,空数组
  78. //使用未序列化的redis进行set
  79. $this->redis->set($key, json_encode($data));
  80. //使用序列化的redis进行get
  81. $redisData = $this->redisJsonSerialize->get($key);
  82. $this->assertEquals($redisData, $data);
  83. }
  84. public function phpSerializeData()
  85. {
  86. $class1 = new \stdClass();
  87. $class1->a = 1;
  88. $class1->b = 2;
  89. $class2 = new \stdClass();
  90. $class2->a = '';
  91. $class3 = new \stdClass();
  92. $class3->a = [];
  93. return [
  94. [1],
  95. ['1'],
  96. [' '],
  97. [0],
  98. [null],
  99. [$class1],
  100. [$class2],
  101. [$class3],
  102. [''],
  103. [['name' => 'xsk']],
  104. [[]]
  105. ];
  106. }
  107. public function jsonSerializeData()
  108. {
  109. return [
  110. [1],
  111. ['1'],
  112. [' '],
  113. [0],
  114. [''],
  115. [['name' => 'xsk']],
  116. [[]]
  117. ];
  118. }
  119. }