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