<?php

namespace MintyPHP\Tests\Service\Access;

use MintyPHP\Service\Access\PermissionService;

trait AuthorizationPolicyTestSupport
{
    /**
     * @param array<int, array<int, string>> $permissionsByUser
     */
    private function permissionGatewayAllowing(array $permissionsByUser): PermissionService
    {
        $permissionService = $this->createMock(PermissionService::class);
        $permissionService->method('userHas')
            ->willReturnCallback(static function (int $userId, string $permission) use ($permissionsByUser): bool {
                return in_array($permission, $permissionsByUser[$userId] ?? [], true);
            });

        return $permissionService;
    }

    private function assertForbiddenDecision(mixed $decision, string $error = 'forbidden'): void
    {
        $this->assertFalse($decision->isAllowed());
        $this->assertSame(403, $decision->status());
        $this->assertSame($error, $decision->error());
    }

    private function assertDeniedDecision(mixed $decision, int $status, string $error): void
    {
        $this->assertFalse($decision->isAllowed());
        $this->assertSame($status, $decision->status());
        $this->assertSame($error, $decision->error());
    }
}
