<?php
/**
 * 游标模式
 * User: haoxu
 * Date: 2020-01-15
 * Time: 14:42
 */

namespace EasySwoole\ORM\Tests;

use EasySwoole\ORM\Db\Config;
use EasySwoole\ORM\Db\Cursor;
use EasySwoole\ORM\DbManager;
use PHPUnit\Framework\TestCase;
use EasySwoole\ORM\Db\Connection;
use Swoole\Coroutine\MySQL\Statement;


use EasySwoole\ORM\Tests\models\TestUserModel;

class CursorTest 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);
        $config->setFetchMode(true);
        $this->connection = new Connection($config);
        DbManager::getInstance()->addConnection($this->connection);
        $connection = DbManager::getInstance()->getConnection();
        $this->assertTrue($connection === $this->connection);
    }

    public function testAdd()
    {
        $testUserModel = new TestUserModel();
        $testUserModel->state = 1;
        $testUserModel->name = 'Siam';
        $testUserModel->age = 18;
        $testUserModel->addTime = date('Y-m-d H:i:s');
        $data = $testUserModel->save(false, false);
        $this->assertIsInt($data);
    }

    public function testQuery()
    {
        $cursor = TestUserModel::create()->all();
        $this->assertInstanceOf(Cursor::class, $cursor);
    }

    public function testCursorNotArray()
    {
        $cursor = TestUserModel::create()->all();
        $item = $cursor->fetch();
        $this->assertInstanceOf(TestUserModel::class, $item);
    }


}