connection = new Connection($config); DbManager::getInstance()->addConnection($this->connection); $connection = DbManager::getInstance()->getConnection(); $this->assertTrue($connection === $this->connection); $this->createTestTable(); } function createTestTable() { $query = new QueryBuilder(); $tableDDL = new Table($this->tableName); $tableDDL->colInt('id', 11)->setIsPrimaryKey()->setIsAutoIncrement(); $tableDDL->colVarChar('name', 255); $tableDDL->colTinyInt('age', 1); $tableDDL->colDateTime('addTime'); $tableDDL->colTinyInt('state', 1); $tableDDL->setIfNotExists(); $sql = $tableDDL->__createDDL(); $query->raw($sql); $data = $this->connection->defer()->query($query); $this->assertTrue($data->getResult()); } function testGetSchemaInfo() { $testUserModel = new TestUserModel(); $schemaInfo = $testUserModel->schemaInfo(); $this->assertTrue($schemaInfo instanceof Table); } function testAdd() { $testUserModel = new TestUserModel(); $testUserModel->state = 1; $testUserModel->name = '仙士可'; $testUserModel->age = 100; $testUserModel->addTime = date('Y-m-d H:i:s'); $data = $testUserModel->save(); $this->assertIsInt($data); } /** * @depends testAdd * testUpdate * @author Tioncico * Time: 15:41 */ function testUpdate() { $testUserModel = new TestUserModel(); /** * @var $user TestUserModel */ $user = $testUserModel->get(); $user->name = '仙士可2号'; $result = $user->update(); $this->assertTrue($result); $user = $testUserModel->get(['id' => $user->id]); $this->assertEquals('仙士可2号', $user->name); $user = $testUserModel->where(function (QueryBuilder $builder)use ($user){ $builder->where('id', $user->id); })->get(); $this->assertEquals('仙士可2号', $user->name); } /** * @depends testAdd * testUpdateWithLimit * @author XueSi * Time: 18:04 */ function testUpdateWithLimit() { $testUserModel = new TestUserModel(); // 无条件进行 limit 更新 /** * @var $user TestUserModel */ $user = $testUserModel->get(); $user->name = 'XueSi-1号'; $result = $user->limit(1)->update([], null, true); $this->assertEquals("UPDATE `test_user_model` SET `name` = 'XueSi-1号' LIMIT 1", $user->lastQuery()->getLastQuery()); $this->assertTrue($result); // 无条件进行 limit 更新 /** * @var $user TestUserModel */ $user = $testUserModel->get(); $user->name = 'XueSi-2号'; $result = $user->limit(2, 3)->update([], null, true); $this->assertEquals("UPDATE `test_user_model` SET `name` = 'XueSi-2号' LIMIT 2", $user->lastQuery()->getLastQuery()); $this->assertTrue($result); // 无条件进行 limit 更新 $result = $testUserModel->limit(3)->update(['name' => 'XueSi-1号-1'], null, true); $this->assertEquals("UPDATE `test_user_model` SET `name` = 'XueSi-1号-1' LIMIT 3", $testUserModel->lastQuery()->getLastQuery()); $this->assertTrue($result); // 无条件进行 limit 更新 $testUserModel = new TestUserModel(); $result = $testUserModel->limit(4, 5)->update(['name' => 'XueSi-2号-2'], null, true); $this->assertEquals("UPDATE `test_user_model` SET `name` = 'XueSi-2号-2' LIMIT 4", $testUserModel->lastQuery()->getLastQuery()); $this->assertTrue($result); // 根据条件进行 limit 更新 $testUserModel = new TestUserModel(); $result = $testUserModel->limit(1)->update(['name' => 'XueSi-3号'], ['id' => 1]); $this->assertEquals("UPDATE `test_user_model` SET `name` = 'XueSi-3号' WHERE `id` = 1 LIMIT 1", $testUserModel->lastQuery()->getLastQuery()); $this->assertTrue($result); } function testGetAll() { $testUserModel = new TestUserModel(); $data = $testUserModel->all(); $this->assertIsArray($data); } function testDelete() { $testUserModel = new TestUserModel(); /** * @var $user TestUserModel */ $user = $testUserModel->get(); $result = $user->destroy(); $this->assertEquals(1, $result); $user = $user->get(['id' => $user->id]); $this->assertNull($user); } }