<?php

use App\Models\User;
use Modules\Routing\Policies\RoutePolicy;
use Spatie\Permission\Models\Permission;

beforeEach(function () {
    Permission::findOrCreate('manage_routes', 'web');
});

test('every gate requires the manage_routes permission', function (string $method) {
    $policy = new RoutePolicy;

    $withPermission = User::factory()->create()->givePermissionTo('manage_routes');
    $withoutPermission = User::factory()->create();

    $args = $method === 'viewAny' || $method === 'create'
        ? [$withPermission]
        : [$withPermission, null];

    expect($policy->{$method}(...$args))->toBeTrue();

    $args = $method === 'viewAny' || $method === 'create'
        ? [$withoutPermission]
        : [$withoutPermission, null];

    expect($policy->{$method}(...$args))->toBeFalse();
})->with(['viewAny', 'create', 'update', 'delete']);
