<?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']],
            [[]]
        ];
    }

}
