<?php
/**
 * 获取器、设置器
 * User: Siam
 * Date: 2019/10/31
 * Time: 9:35
 */

namespace EasySwoole\ORM\Tests;


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


use EasySwoole\ORM\Tests\models\TestUserListGetterModel;

class SetterGetterTest 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);
    }

    // 插入的时候会调用setter
    public function testAdd()
    {
        $testUserModel = new TestUserListGetterModel();
        $testUserModel->state = 1;
        $testUserModel->name = 'Siam';
        $testUserModel->age = 18;
        $testUserModel->addTime = date('Y-m-d H:i:s');
        $data = $testUserModel->save();
        $this->assertIsInt($data);
    }
    // 查询结果不会调用setter
    // 获取会调用getter
    public function testGetter()
    {
        $test = TestUserListGetterModel::create()->all();
        $this->assertEquals($test[0]->addTime, 123);

        $test = TestUserListGetterModel::create()->get();
        $this->assertEquals($test['addTime'], 123);
    }

    public function testGetterJson()
    {
        $test = TestUserListGetterModel::create()->all();
        $json = json_encode($test);
        $decode = json_decode($json);
        $this->assertEquals($decode[0]->addTime, 123);
    }

    public function testDeleteAll()
    {
        $res = TestUserListGetterModel::create()->destroy(null, true);
        $this->assertIsInt($res);
    }
}