Testing | laravel-permission | Spatie

   [Docs](https://spatie.be/docs)  [Laravel-permission](https://spatie.be/docs/laravel-permission/v6)  Advanced-usage  Testing

 Version   v7   v6   v5   v4   v3

 Other versions for crawler [v7](https://spatie.be/docs/laravel-permission/v7) [v6](https://spatie.be/docs/laravel-permission/v6) [v5](https://spatie.be/docs/laravel-permission/v5) [v4](https://spatie.be/docs/laravel-permission/v4) [v3](https://spatie.be/docs/laravel-permission/v3)

- [ Introduction ](https://spatie.be/docs/laravel-permission/v6/introduction)
- [ Support us ](https://spatie.be/docs/laravel-permission/v6/support-us)
- [ Prerequisites ](https://spatie.be/docs/laravel-permission/v6/prerequisites)
- [ Installation in Laravel ](https://spatie.be/docs/laravel-permission/v6/installation-laravel)
- [ Installation in Lumen ](https://spatie.be/docs/laravel-permission/v6/installation-lumen)
- [ Upgrading ](https://spatie.be/docs/laravel-permission/v6/upgrading)
- [ Questions and issues ](https://spatie.be/docs/laravel-permission/v6/questions-issues)
- [ Changelog ](https://spatie.be/docs/laravel-permission/v6/changelog)
- [ About us ](https://spatie.be/docs/laravel-permission/v6/about-us)

Basic Usage
-----------

- [ Basic Usage ](https://spatie.be/docs/laravel-permission/v6/basic-usage/basic-usage)
- [ Direct Permissions ](https://spatie.be/docs/laravel-permission/v6/basic-usage/direct-permissions)
- [ Using Permissions via Roles ](https://spatie.be/docs/laravel-permission/v6/basic-usage/role-permissions)
- [ Enums ](https://spatie.be/docs/laravel-permission/v6/basic-usage/enums)
- [ Teams permissions ](https://spatie.be/docs/laravel-permission/v6/basic-usage/teams-permissions)
- [ Wildcard permissions ](https://spatie.be/docs/laravel-permission/v6/basic-usage/wildcard-permissions)
- [ Blade directives ](https://spatie.be/docs/laravel-permission/v6/basic-usage/blade-directives)
- [ Defining a Super-Admin ](https://spatie.be/docs/laravel-permission/v6/basic-usage/super-admin)
- [ Using multiple guards ](https://spatie.be/docs/laravel-permission/v6/basic-usage/multiple-guards)
- [ Artisan Commands ](https://spatie.be/docs/laravel-permission/v6/basic-usage/artisan)
- [ Middleware ](https://spatie.be/docs/laravel-permission/v6/basic-usage/middleware)
- [ Passport Client Credentials Grant usage ](https://spatie.be/docs/laravel-permission/v6/basic-usage/passport)
- [ Example App ](https://spatie.be/docs/laravel-permission/v6/basic-usage/new-app)

Best Practices
--------------

- [ Roles vs Permissions ](https://spatie.be/docs/laravel-permission/v6/best-practices/roles-vs-permissions)
- [ Model Policies ](https://spatie.be/docs/laravel-permission/v6/best-practices/using-policies)
- [ Performance Tips ](https://spatie.be/docs/laravel-permission/v6/best-practices/performance)

Advanced usage
--------------

- [ Testing ](https://spatie.be/docs/laravel-permission/v6/advanced-usage/testing)
- [ Database Seeding ](https://spatie.be/docs/laravel-permission/v6/advanced-usage/seeding)
- [ Exceptions ](https://spatie.be/docs/laravel-permission/v6/advanced-usage/exceptions)
- [ Extending ](https://spatie.be/docs/laravel-permission/v6/advanced-usage/extending)
- [ Cache ](https://spatie.be/docs/laravel-permission/v6/advanced-usage/cache)
- [ Events ](https://spatie.be/docs/laravel-permission/v6/advanced-usage/events)
- [ Custom Permission Check ](https://spatie.be/docs/laravel-permission/v6/advanced-usage/custom-permission-check)
- [ UUID/ULID ](https://spatie.be/docs/laravel-permission/v6/advanced-usage/uuid)
- [ PhpStorm Interaction ](https://spatie.be/docs/laravel-permission/v6/advanced-usage/phpstorm)
- [ Other ](https://spatie.be/docs/laravel-permission/v6/advanced-usage/other)
- [ Timestamps ](https://spatie.be/docs/laravel-permission/v6/advanced-usage/timestamps)
- [ UI Options ](https://spatie.be/docs/laravel-permission/v6/advanced-usage/ui-options)

      You are viewing the documentation for **an older version** of this package. You can check the version you are using with the following command:

 `                                    composer show spatie/laravel-permission                                                                                                                                                                                                                                    `

Testing
=======

###  On this page

1. [ Clear Cache During Tests ](#content-clear-cache-during-tests)
2. [ Clear Cache When Using Seeders ](#content-clear-cache-when-using-seeders)
3. [ Bypassing Cache When Testing ](#content-bypassing-cache-when-testing)
4. [ Testing Using Factories ](#content-testing-using-factories)

Clear Cache During Tests
--------------------------------------------------------------------------------------------------------------------------------

In your application's tests, if you are not seeding roles and permissions as part of your test `setUp()` then you may run into a chicken/egg situation where roles and permissions aren't registered with the gate (because your tests create them after that gate registration is done). Working around this is simple:

In your tests simply add a `setUp()` instruction to re-register the permissions, like this:

```
    protected function setUp(): void
    {
        // first include all the normal setUp operations
        parent::setUp();

        // now de-register all the roles and permissions by clearing the permission cache
        $this->app->make(\Spatie\Permission\PermissionRegistrar::class)->forgetCachedPermissions();
    }
```

Clear Cache When Using Seeders
--------------------------------------------------------------------------------------------------------------------------------------------------

If you are using Laravel's `LazilyRefreshDatabase` trait, you most likely want to avoid seeding permissions before every test, because that would negate the use of the `LazilyRefreshDatabase` trait. To overcome this, you should wrap your seeder in an event listener for the `DatabaseRefreshed` event:

```
Event::listen(DatabaseRefreshed::class, function () {
    $this->artisan('db:seed', ['--class' => RoleAndPermissionSeeder::class]);
    $this->app->make(\Spatie\Permission\PermissionRegistrar::class)->forgetCachedPermissions();
});
```

Note that `PermissionRegistrar::forgetCachedPermissions()` is called AFTER seeding. This is to prevent a caching issue that can occur when the database is set up after permissions have already been registered and cached.

Bypassing Cache When Testing
--------------------------------------------------------------------------------------------------------------------------------------------

The caching infrastructure for this package is "always on", but when running your test suite you may wish to reduce its impact.

Two things you might wish to explore include:

- Change the cache driver to `array`. **Very often you will have already done this in your `phpunit.xml` configuration.**
- Shorten cache lifetime to 1 second, by setting the config (not necessary if cache driver is set to `array`) in your test suite TestCase:

    `'permission.cache.expiration_time' = \DateInterval::createFromDateString('1 seconds')`

Testing Using Factories
-----------------------------------------------------------------------------------------------------------------------------

Many applications do not require using factories to create fake roles/permissions for testing, because they use a Seeder to create specific roles and permissions that the application uses; thus tests are performed using the declared roles and permissions.

However, if your application allows users to define their own roles and permissions you may wish to use Model Factories to generate roles and permissions as part of your test suite.

When using Laravel's class-based Model Factory features you will need to `extend` this package's `Role` and/or `Permission` model into your app's namespace, add the `HasFactory` trait to it, and define a model factory for it. Then you can use that factory in your seeders like any other factory related to your application's models.
