<?php
/**
 *  ResultColumn  ResultIndexBy ResultScalar
 */

namespace EasySwoole\ORM\Tests;

use EasySwoole\ORM\Db\Config;
use EasySwoole\ORM\Db\Connection;
use EasySwoole\ORM\DbManager;
use EasySwoole\ORM\Tests\models\TestRelationModel;
use PHPUnit\Framework\TestCase;
use EasySwoole\Mysqli\QueryBuilder;

class ResultTest extends TestCase
{
    /**
     * @var $connection Connection
     */
    protected $connection;
    protected $tableName = 'user_test_list';

    protected function setUp(): void
    {
        parent::setUp(); // TODO: Change the autogenerated stub
        $config = new Config(MYSQL_CONFIG);
        $this->connection = new Connection($config);
        DbManager::getInstance()->addConnection($this->connection);
        $connection = DbManager::getInstance()->getConnection();
        $this->assertTrue($connection === $this->connection);
        $this->connection->defer()->query((new QueryBuilder())->raw('TRUNCATE TABLE ' . $this->tableName));
        $this->connection->defer()->query(
            (new QueryBuilder())
                ->raw("INSERT INTO {$this->tableName} (name,age,addTime,state) VALUES
                    ('一',10,'2019-12-13 01:02:03.0',1),('二',11,'2019-12-13 01:02:03.0',2);"));
    }

    public function testGetResult()
    {
        $result = DbManager::getInstance()->query(
            (new QueryBuilder())->get($this->tableName)
        )->getResult();
        $this->assertIsArray($result);
    }

    public function testGetResultOne()
    {
        $result = DbManager::getInstance()->query(
            (new QueryBuilder())->get($this->tableName)
        )->getResultOne();
        $this->assertIsArray($result);
        $this->assertTrue(isset($result['name']));
        $this->assertTrue($result['name'] === '一');
    }

    public function testGetResultColumn()
    {
        $result = DbManager::getInstance()->query(
            (new QueryBuilder())->fields('age')->orderBy('age')->get($this->tableName)
        )->getResultColumn();
        $this->assertIsArray($result);
        $this->assertTrue($result[0] === 11);
        $this->assertTrue($result[1] === 10);

        $result = DbManager::getInstance()->query(
            (new QueryBuilder())->fields('name, age')->orderBy('age')->get($this->tableName)
        )->getResultColumn('age');
        $this->assertIsArray($result);
        $this->assertTrue($result[0] === 11);
        $this->assertTrue($result[1] === 10);
    }

    public function testGetResultScalar()
    {
        $result = DbManager::getInstance()->query(
            (new QueryBuilder())->fields('age')->orderBy('age')->get($this->tableName)
        )->getResultScalar();
        $this->assertTrue($result === 11);

        $result = DbManager::getInstance()->query(
            (new QueryBuilder())->fields('name, age')->orderBy('age')->get($this->tableName)
        )->getResultScalar('age');
        $this->assertTrue($result === 11);
    }

    public function testGetResultIndexBy()
    {
        $result = DbManager::getInstance()->query(
            (new QueryBuilder())->fields('name, age')->orderBy('age')->get($this->tableName)
        )->getResultIndexBy('name');
        $this->assertTrue(isset($result['一']));
        $this->assertTrue(isset($result['二']));
        $this->assertTrue($result['二']['name'] === '二');
        $this->assertTrue($result['二']['age'] === 11);
    }

    public function testTotalCount()
    {
        $model = TestRelationModel::create();
        $model->withTotalCount()->all();
        $count = $model->lastQueryResult()->getTotalCount();
        $sql = DbManager::getInstance()->getLastQuery()->getLastQuery();

        $this->assertEquals(0, $count);
        $this->assertEquals("SELECT SQL_CALC_FOUND_ROWS * FROM `test_user_model`", $sql);
    }
}
