Hello,
I would like to perform a test with RefreshDatabase; if I do so, I am getting this error:
A `index dashboard` permission already exists for guard `web`.
at vendor/spatie/laravel-permission/src/Exceptions/PermissionAlreadyExists.php:11
7▕ class PermissionAlreadyExists extends InvalidArgumentException
8▕ {
9▕ public static function create(string $permissionName, string $guardName)
10▕ {
➜ 11▕ return new static("A `{$permissionName}` permission already exists for guard `{$guardName}`.");
12▕ }
13▕ }
14▕
+1 vendor frames
2 database/seeders/RolesAndPermissionsSeeder.php:22
Spatie\Permission\Models\Permission::create()
+7 vendor frames
10 database/seeders/DatabaseSeeder.php:16
Illuminate\Database\Seeder::call()
This is the TestCase.php
<?php
namespace Tests;
use App\Models\User;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
use Illuminate\Support\Facades\Artisan;
/**
* Class TestCase
*
* @package Tests
*/
abstract class TestCase extends BaseTestCase
{
use CreatesApplication;
/**
* @var
*/
protected $admin;
/**
* @var
*/
protected $user_one;
/**
* @var
*/
protected $user_two;
/**
* @var
*/
protected $user_admin;
/**
* Set up the tests
*/
public function setUp(): void
{
parent::setUp();
Artisan::call('migrate', ['-vvv' => true]);
Artisan::call('passport:install', ['-vvv' => true, '--uuids' => true, '--no-interaction' => true]);
$this->seed();
$this->user_admin = User::where('email', '[email protected]')->first();
}
}
This is the failing test:
<?php
namespace Tests\Feature;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
/**
*
*/
class PrivacyTest extends TestCase
{
use RefreshDatabase;
/**
* @var bool
*/
public $mockConsoleOutput = false;
/**
* Test if the user can log in via HTTP API.
*
* @return void
*/
public function test_user_can_view_own_user_data()
{
$user = $this->user_one;
$this->json('GET', "/api/user/{$user->id}")->assertStatus(200)
->assertJsonStructure([
'token_type',
'expires_at',
'access_token',
]);
}
}
This is the DatabaseSeeder.php
<?php
namespace Database\Seeders;
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder
{
/**
* Seed the application's database.
*
* @return void
*/
public function run()
{
$this->call(RolesAndPermissionsSeeder::class);
$this->call(UsersSeeder::class);
$this->call(CompanySeeder::class);
}
}
And this is the RolesAndPermissionsSeeder.php
<?php
namespace Database\Seeders;
use Illuminate\Database\Seeder;
use Spatie\Permission\Models\Permission;
use Spatie\Permission\Models\Role;
use Spatie\Permission\PermissionRegistrar;
class RolesAndPermissionsSeeder extends Seeder
{
public function run()
{
/*reset cached roles and permissions*/
app(PermissionRegistrar::class)->forgetCachedPermissions();
/*Create permissions*/
/*Dashboard*/
Permission::create(['name' => 'index dashboard']);
/*Add admin role*/
$admin = Role::create(['name' => 'admin']);
/*Dashboard*/
$admin->givePermissionTo('index dashboard');
}
}