<?php
/**
 * Created by PhpStorm.
 * User: Tioncico
 * Date: 2019/9/24 0024
 * Time: 16:16
 */

namespace Test;

use EasySwoole\Redis\Client;
use EasySwoole\Redis\CommandConst;
use PHPUnit\Framework\TestCase;

class ClientTest extends TestCase
{
    /**
     * @var $client Client
     */
    protected $client;


    protected function setUp():void
    {
        parent::setUp(); // TODO: Change the autogenerated stub
        $client = new Client(REDIS_HOST, REDIS_PORT);
        $client->connect();
        if (REDIS_AUTH != '') {
            $client->sendCommand(['AUTH', REDIS_AUTH]);
            $recv = $client->recv();
        }
        $this->client = $client;
    }
    function testRecv()
    {
        $this->client->sendCommand(['set', 'a', '1']);
        $recv = $this->client->recv();
        $this->assertEquals('OK', $recv->getData());

        $this->client->sendCommand(['st', 'a', '1']);
        $recv = $this->client->recv();
        $this->assertEquals('-1', $recv->getStatus());

        $num = 1;
        $this->client->sendCommand(['set', 'b', $num]);
        $this->client->recv();
        $this->client->sendCommand(['incr', 'b']);
        $recv = $this->client->recv();
        $this->assertEquals('0', $recv->getStatus());
        $this->assertEquals($num + 1, $recv->getData());


        $this->client->sendCommand(['set', "a1", "a \r\n b \r\n 123456 \r\na"]);
        $this->client->recv();
        $this->client->sendCommand(['get', "a1"]);
        $recv = $this->client->recv();
        $this->assertEquals("a \r\n b \r\n 123456 \r\na", $recv->getData());


        $this->client->sendCommand(['del', "listA"]);
        $this->client->recv();
        $this->client->sendCommand(['sadd', "listA", 1]);
        $this->client->recv();
        $this->client->sendCommand(['sadd', "listA", 'a']);
        $this->client->recv();
        $this->client->sendCommand(['sadd', "listA", null]);
        $this->client->recv();
        $this->client->sendCommand(['sadd', "listA", "1\r\n 2\r\n a\r\nf"]);
        $this->client->recv();
        $this->client->sendCommand(['sadd', "listA", " "]);
        $this->client->recv();

        $this->client->sendCommand(['smembers', "listA"]);
        $recv = $this->client->recv();
        $this->assertEquals(5, count($recv->getData()));

    }

    function testInt()
    {
        $num = 1;
        $this->client->sendCommand(['set', 'b', $num]);
        $this->client->recv();
        $this->client->sendCommand(['incr', 'b']);
        $recv = $this->client->recv();

        $this->assertEquals('0', $recv->getStatus());
        $this->assertTrue(is_integer($recv->getData()));
        $this->assertEquals($num + 1, $recv->getData());

    }

    function testHash()
    {
        $this->client->sendCommand(['del', "ha"]);
        $this->client->recv();
        $this->client->sendCommand(['hset', "ha", 'a', 1]);
        $this->client->recv();
        $this->client->sendCommand(['hset', "ha", 'b', '0']);
        $this->client->recv();
        $this->client->sendCommand(['hset', "ha", 'c', null]);
        $this->client->recv();
        $this->client->sendCommand(['hset', "ha", 'd', "1\r\n 2\r\n a\r\nf"]);
        $this->client->recv();
        $this->client->sendCommand(['hset', "ha", 'e', " "]);
        $this->client->recv();

        $this->client->sendCommand(['hgetall', "ha"]);

        $recv = $this->client->recv();
        $this->assertEquals(10, count($recv->getData()));
        $this->assertEquals('a', $recv->getData()[0]);
    }

    function testGeohash()
    {

        $this->client->sendCommand(['del', 'geoa']);
        $recv = $this->client->recv();
        $this->assertEquals($recv::STATUS_OK, $recv->getStatus());
        $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']);
        $recv = $this->client->recv();
        $this->assertEquals(4, $recv->getData());

        $this->client->sendCommand(['geohash', 'geoa', 'user1', 'user2', 'user3']);
        $recv = $this->client->recv();
        $this->assertEquals('wskme6b3cn0', $recv->getData()[0]);

        $this->client->sendCommand(['georadiusbymember', 'geoa', 'user1', '100', 'km', 'desc']);
        $recv = $this->client->recv();
        $this->assertEquals('user2', $recv->getData()[0]);


        $this->client->sendCommand(['geopos', 'geoa', 'user1', 'user2']);
        $recv = $this->client->recv();
        $this->client->sendCommand(['georadius', 'geoa', $recv->getData()[0][0], $recv->getData()[0][1], '100', 'm', 'desc']);
        $recv = $this->client->recv();
        $this->assertEquals('user2', $recv->getData()[0]);

    }


}
