setUp() method when implementing UnitTests in Laravel

The dashboard module is protected by required authentication and by the roles and permissions system; thinking about this, let's implement the following code:

tests/Feature/dashboard/PostTest.php

class PostTest extends TestCase
{
    use DatabaseMigrations;

    protected function setUp(): void
    {     
        parent::setUp();

        User::factory(1)->create();
        $user = User::first();
        // dd($user);
        $role = Role::firstOrCreate(['name' => 'Admin']);
        Permission::firstOrCreate(['name' => 'editor.post.index']);
        Permission::firstOrCreate(['name' => 'editor.post.create']);
        Permission::firstOrCreate(['name' => 'editor.post.edit']);
        Permission::firstOrCreate(['name' => 'editor.post.delete']);
        $role->syncPermissions([1,2,3,4]);

        $user->assignRole($role);

        $this->actingAs($user);//->withSession(['role' => 'Admin']);
    }
}

In the previous code, we use the setUp() method that is executed before each test, in this method, we can place common code to execute in each of the tests, in this example, to create the user and establish a role Admin and permissions.

Additionally, once the user is configured, we configure authentication using actingAs() to which we can establish session data if necessary.

Video transcript

Now we are going to deal with the issue of authentication so well for this you obviously have to uncomment everything that we already did before that is to say go to the navigation layout or whatever it is called document everything so that the authentication appears here again, of course the bar navigation to indicate well that where we had the greeting to the user and everything else, I commented on everything to facilitate. But, obviously you only had to comment on the parts referring to the authenticated user, such as the permissions management part.

Permissions and role management

Verify that the permissions and roles have the same name. If you evaluate the content and so on, it will clearly indicate that the login is there or something like that, but we already know that it is due to authentication according to previous tests, so we can go from one to the other.

So here are several things, I'm going to start here by doing this in this test in the test_index test, but let's make it clear that here we have a method that is like a kind of constructor for the tests, that is, it is executed when we execute the tests. It is executed before, that is, when we execute we will first create the method to place fun here and call setup()

protected function setUp(): void
    {     
        parent::setUp();
    }

Create the role structure and Spatie

Before, we created a role column to check the user type, but, we change it with Spatie; here we get the user:

User::factory(1)->create();
$user = User::first();

Now here would be the problem of permissions and roles, so here we already configure it based on what we defined before:

Permission::firstOrCreate(['name' => 'editor.post.index']);
Permission::firstOrCreate(['name' => 'editor.post.create']);
Permission::firstOrCreate(['name' => 'editor.post.edit']);
Permission::firstOrCreate(['name' => 'editor.post.delete']);
$role->syncPermissions([1,2,3,4]);
$user->assignRole($role);

And we authenticate the user:

Then all tests of this class will have an authenticated user in the session:

$this->actingAs($user);//->withSession(['role' => 'Admin']);

- Andrés Cruz

En español

Andrés Cruz

Develop with Laravel, Django, Flask, CodeIgniter, HTML5, CSS3, MySQL, JavaScript, Vue, Android, iOS, Flutter

Andrés Cruz In Udemy

I agree to receive announcements of interest about this Blog.