<?php

namespace Tests\Feature;

use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Spatie\Permission\Models\Permission;
use Spatie\Permission\Models\Role;
use Spatie\Permission\PermissionRegistrar;
use Tests\TestCase;

class RoleAssignmentImmediateTest extends TestCase
{
    use RefreshDatabase;

    public function test_role_assignment_updates_permissions_immediately(): void
    {
        $this->app[PermissionRegistrar::class]->forgetCachedPermissions();

        $permission = Permission::create(['name' => 'users.view']);
        $role = Role::create(['name' => 'administrador']);
        $role->givePermissionTo($permission);

        $user = User::factory()->create();
        $user->assignRole($role);

        $this->assertTrue($user->fresh()->can('users.view'));
    }
}
