I am using Laravel to create an API for an iOS application. I am facing serious problems testing a web application using PHPUnit and Laravel's built-in testing.
My process: 1. the user has an account 2. the user needs to be authenticated to perform any action.
Suppose I have user groups, and one user (owner) can manage members belonging to one of their groups. I want to test the user's ability to add other members to their group.
I have a testing method called testAddGroupMemberPass () which should
- create user (owner)
- create a group and assign an owner
- create user to add to group
claim that the owner has the ability to add this item.
public function testAddGroupMemberPass()
{
$owner = factory(User::class)->create();
$token = JWTAuth::fromUser($owner);
$group = new Group;
$group->owner_id = $owner->id;
$group->save();
$user = factory(User::class)->create();
$this->edit('/groups/' . $group->id . '/add-member/' . $user->username . '?token=' . $token)
->seeJson(['success' => true]);
}
To claim that an owner can add a user, you must first create an owner, secondly create a group, thirdly create the user you want to add to the group, and then finally you can try to make an API request to actually add this user.
The problem is that in the first three steps, various problems can arise that are not completely related to the process of adding a member to the group, for example, with unique restrictions on the username and email.
So what happened to my current approach? They always told me that tests should be completely independent of each other, so it makes no sense to try to use users and groups created by previous AFAIK tests.