<?php


namespace EasySwoole\ORM\Tests;


use EasySwoole\ORM\Db\Config;
use EasySwoole\ORM\Db\Connection;
use EasySwoole\ORM\DbManager;
use EasySwoole\ORM\Tests\models\CastsModel;
use PHPUnit\Framework\TestCase;

class CastsTest extends TestCase
{
    /**
     * @var $connection Connection
     */
    protected $connection;

    protected $data = [
        'int_value' => '1',
        'float_value' => '2',
        'json_value' => ['a' => 1, 'b' => 2],
        'array_value' => ['a' => 1, 'b' => 2],
        'date_value' => '2020-11-27 15:08:25',
        'datetime_value' => '2020-11-27 15:08:25',
        'timestamp_value' => '1606460910'
    ];

    protected function setUp(): void
    {
        parent::setUp(); // TODO: Change the autogenerated stub
        $config = new Config(MYSQL_CONFIG);
        $config->setReturnCollection(true);
        $this->connection = new Connection($config);
        DbManager::getInstance()->addConnection($this->connection);
        $connection = DbManager::getInstance()->getConnection();
        $this->assertTrue($connection === $this->connection);
    }

    public function testInt()
    {
        $id = CastsModel::create()->data($this->data)->save();
        $this->assertIsInt(CastsModel::create()->get($id)->int_value);
        $this->assertIsInt(CastsModel::create()->get($id)->getAttr('int_value'));
    }

    public function testFloat()
    {
        $id = CastsModel::create()->data($this->data)->save();
        $this->assertIsFloat(CastsModel::create()->get($id)->float_value);
        $this->assertIsFloat(CastsModel::create()->get($id)->getAttr('float_value'));
    }

    public function testJson()
    {
        $id = CastsModel::create()->data($this->data)->save();
        $this->assertIsObject(CastsModel::create()->get($id)->json_value);
        $this->assertIsObject(CastsModel::create()->get($id)->getAttr('json_value'));
    }

    public function testArray()
    {
        $id = CastsModel::create()->data($this->data)->save();
        $this->assertIsArray(CastsModel::create()->get($id)->array_value);
        $this->assertIsArray(CastsModel::create()->get($id)->getAttr('array_value'));
    }
}