123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- <?php
- /**
- * Created by PhpStorm.
- * User: Tioncico
- * Date: 2019/9/24 0024
- * Time: 16:16
- */
- namespace Test;
- use EasySwoole\Redis\Config\RedisConfig;
- use EasySwoole\Redis\Redis;
- use PHPUnit\Framework\TestCase;
- class OtherTest extends TestCase
- {
- /**
- * @var $redis Redis
- */
- protected $redis;
- /**
- * @var $redisPHPSerialize Redis
- */
- protected $redisPHPSerialize;
- /**
- * @var $redisJsonSerialize Redis
- */
- protected $redisJsonSerialize;
- protected function setUp() :void
- {
- parent::setUp(); // TODO: Change the autogenerated stub
- $this->redis = new Redis(new RedisConfig([
- 'host' => REDIS_HOST,
- 'port' => REDIS_PORT,
- 'auth' => REDIS_AUTH
- ]));
- $this->redisPHPSerialize = new Redis(new RedisConfig([
- 'host' => REDIS_HOST,
- 'port' => REDIS_PORT,
- 'auth' => REDIS_AUTH,
- 'serialize' => RedisConfig::SERIALIZE_PHP
- ]));
- $this->redisJsonSerialize = new Redis(new RedisConfig([
- 'host' => REDIS_HOST,
- 'port' => REDIS_PORT,
- 'auth' => REDIS_AUTH,
- 'serialize' => RedisConfig::SERIALIZE_JSON
- ]));
- $this->redis->connect();
- $this->redisPHPSerialize->connect();
- $this->redisJsonSerialize->connect();
- }
- /**
- * testPHPSerialize
- * @dataProvider phpSerializeData
- * @param $data
- * @author Tioncico
- * Time: 15:45
- */
- function testPHPSerialize($data)
- {
- $key = 'test';
- //进行序列化测试,数字,字符串,empty,0,null,对象,空对象,数组,空数组
- //使用未序列化的redis进行set
- $this->redis->set($key, serialize($data));
- //使用序列化的redis进行get
- $redisData = $this->redisPHPSerialize->get($key);
- $this->assertEquals($redisData, $data);
- }
- /**
- * testJSONSerialize
- * @dataProvider jsonSerializeData
- * @param $data
- * @author Tioncico
- * Time: 15:45
- */
- function testJSONSerialize($data)
- {
- $key = 'test';
- //进行序列化测试,数字,字符串,empty,0,null,对象,空对象,数组,空数组
- //使用未序列化的redis进行set
- $this->redis->set($key, json_encode($data));
- //使用序列化的redis进行get
- $redisData = $this->redisJsonSerialize->get($key);
- $this->assertEquals($redisData, $data);
- }
- public function phpSerializeData()
- {
- $class1 = new \stdClass();
- $class1->a = 1;
- $class1->b = 2;
- $class2 = new \stdClass();
- $class2->a = '';
- $class3 = new \stdClass();
- $class3->a = [];
- return [
- [1],
- ['1'],
- [' '],
- [0],
- [null],
- [$class1],
- [$class2],
- [$class3],
- [''],
- [['name' => 'xsk']],
- [[]]
- ];
- }
- public function jsonSerializeData()
- {
- return [
- [1],
- ['1'],
- [' '],
- [0],
- [''],
- [['name' => 'xsk']],
- [[]]
- ];
- }
- }
|