Correct test thread with Laravel?

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()
    {
        // 1. create owner
        $owner = factory(User::class)->create();
        $token = JWTAuth::fromUser($owner);
    
        // 2. create group and attach user
        $group = new Group;
        $group->owner_id = $owner->id;
        $group->save();
    
        // 3. create the member to be
        $user = factory(User::class)->create();
    
        // 4. attempt attach the member to the group
        $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.

+4
1

1, 2 3 , .

:

// TestCase.php
protected function createOwner()
{
    return factory(User::class)->create();
}

protected function createGroup($owner)
{
    // You didn't show this as a factory, but I figured you might want that
    $group = factory(Group::class)->create();
    $group->owner()->associate($owner);
    $group->save();

    return $group;
}

protected function createUser()
{
    return factory(User::class)->create();
}

// In the file you mention above (should be extending TestCase.php)
public function testAddGroupMemberPass()
{
    // 1. create owner
    $owner = $this->createOwner();
    $token = JWTAuth::fromUser($owner);

    // 2. create group and attach user
    $group = $this->createGroup($owner);

    // 3. create the member to be
    $user = $this->createUser();

    // 4. attempt attach the member to the group
    $this->edit('/groups/' . $group->id . '/add-member/' . $user->username . '?token=' . $token)
         ->seeJson(['success' => true]);
}

, :

// In UserTest.php (also extends TestCase.php)
public function testItCreatesAUser()
{
    $user = $this->createUser();

    $this->seeInDatabase('users', $user);
}

, , , - , -. .

0

Source: https://habr.com/ru/post/1622834/


All Articles