The following doesn't assign an existing user to a group. I've confirmed that both the user and the group exist. Any ideas?
$user = User::find()->username($username)->one();
$user->setGroups([Craft::$app->userGroups->getGroupById(1)]);
Craft::$app->elements->saveElement($user);
I found this answer, which will probably solve it for me, but it leaves me wondering what the purpose of setGroups is.