setReturnCollection(true); $this->connection = new Connection($config); DbManager::getInstance()->addConnection($this->connection); $connection = DbManager::getInstance()->getConnection(); $this->assertTrue($connection === $this->connection); } public function testException() { $model = TestUserModel::create(); $addTime = date('Y-m-d'); $data = [ 'name' => '史迪仔', 'age' => 21, 'addTime' => $addTime, 'state' => 1 ]; $id = $model->data($data)->save(); $this->assertIsInt($id); $this->expectException(Exception::class); $this->expectExceptionMessage("SQLSTATE[23000] [1062] Duplicate entry '{$id}' for key 'PRIMARY' [INSERT INTO `test_user_model` (`name`, `age`, `addTime`, `state`, `id`) VALUES ('史迪仔', 21, '{$addTime}', 1, {$id})]"); $model->data($data)->save(); $this->fail('replace test exception error'); } public function testReplaceInto() { TestUserModel::create()->destroy(null, true); $model = TestUserModel::create(); $addTime = date('Y-m-d'); $data = [ 'name' => '史迪仔', 'age' => 21, 'addTime' => $addTime, 'state' => 1 ]; $id = $model->data($data)->save(); $this->assertIsInt($id); $data['name'] = 'replace into'; $model->data($data)->replace()->save(); $sql = DbManager::getInstance()->getLastQuery()->getLastQuery(); $this->assertEquals("REPLACE INTO `test_user_model` (`name`, `age`, `addTime`, `state`, `id`) VALUES ('replace into', 21, '{$addTime}', 1, {$id})", $sql); $ret = TestUserModel::create()->get($id); $this->assertEquals('replace into', $ret->name); } public function tearDown(): void { TestUserModel::create()->destroy(null, true); } }