<?php
/**
 * 自动时间戳
 * User: Administrator
 * Date: 2019/11/3 0003
 * Time: 0:05
 */

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\TestTimeStampModel;

/**
 * Class TestUserModel
 * @package EasySwoole\ORM\Tests
 * @property $id
 * @property $name
 * @property $age
 * @property $addTime
 * @property $state
 */
class TimeStampTest 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);
    }

    /**
     * @throws \EasySwoole\ORM\Exception\Exception
     * @throws \Throwable
     */
    public function testInsertInt()
    {
        $user = TestTimeStampModel::create();
        $user->name = 'siam';
        $user->age = 21;
        $res = $user->save();

        $this->assertIsInt($user->id);
        $this->assertIsInt($user->create_at);
        $this->assertIsInt($user->update_at);
    }

    /**
     * @throws \EasySwoole\Mysqli\Exception\Exception
     * @throws \EasySwoole\ORM\Exception\Exception
     * @throws \Throwable
     */
    public function testUpdateInt()
    {
        $user = TestTimeStampModel::create()->get([
            'name' => 'siam'
        ]);
        $user->age = 22;
        $time = $user->update_at;
        sleep(1);
        $user->update();
        $this->assertTrue(($user->update_at - $time) > 0);
    }

    /**
     * @throws \EasySwoole\ORM\Exception\Exception
     * @throws \Throwable
     */
    public function testInsertDate()
    {
        $user = TestTimeStampModel::create();
        $user->setAutoTime('datetime');
        $user->setCreateTime('create_time');
        $user->setUpdateTime('update_time');
        $user->name = 'siam_date';
        $user->age = 21;
        $res = $user->save();

        $this->assertIsInt($user->id);
        $this->assertIsInt(strtotime($user->create_time));
        $this->assertIsInt(strtotime($user->update_time));
    }

    /**
     * @throws \EasySwoole\Mysqli\Exception\Exception
     * @throws \EasySwoole\ORM\Exception\Exception
     * @throws \Throwable
     */
    public function testUpdateDate()
    {
        $user = TestTimeStampModel::create();

        $user = $user->get([
            'name' => 'siam_date'
        ]);
        // get返回的是一个新模型 需要在这里设置
        $user->setAutoTime('datetime');
        $user->setCreateTime('create_time');
        $user->setUpdateTime('update_time');

        $user->age = 22;
        sleep(1);
        $time = date('Y-m-d H:i:s');
        $user->update();

        $this->assertEquals($time, $user->update_time);
    }

    /**
     * @throws \EasySwoole\ORM\Exception\Exception
     * @throws \Throwable
     */
    public function testDeleteAll()
    {
        $res = TestTimeStampModel::create()->destroy(null, true);
        $this->assertIsInt($res);
    }
}