<?php

namespace NoahVet\Reef\Test\A_Unit\Domain\Security\Policy;

use NoahVet\Reef\Domain\Security\Model\Resource;
use NoahVet\Reef\Domain\Security\Model\ResourceType;
use NoahVet\Reef\Domain\Security\Policy\PolicyResultGrant;
use PHPUnit\Framework\TestCase;

class PolicyResultGrantTest extends TestCase
{
    private Resource $resource;

    protected function setUp(): void
    {
        $this->resource = Resource::fromString('reef:phpunit:resource', 'test-id');
    }

    public function testConstructorWithAllowOnly(): void
    {
        $grant = new PolicyResultGrant(
            $this->resource,
            'reef:phpunit:principal:user',
            ['permission1', 'permission2'],
        );

        self::assertSame($this->resource, $grant->getResource());
        self::assertSame('reef:phpunit:principal:user', $grant->getPrincipal());
        self::assertSame(['permission1', 'permission2'], $grant->getAllow());
        self::assertSame([], $grant->getDeny());
    }

    public function testConstructorWithAllowAndDeny(): void
    {
        $grant = new PolicyResultGrant(
            $this->resource,
            'reef:phpunit:principal:user',
            ['permission1'],
            ['permission3'],
        );

        self::assertSame(['permission1'], $grant->getAllow());
        self::assertSame(['permission3'], $grant->getDeny());
    }

    public function testResourceTypeAsResource(): void
    {
        $resourceType = ResourceType::fromString('reef:phpunit:type');
        $resource = Resource::fromString($resourceType, 'slug-id');
        $grant = new PolicyResultGrant($resource, 'principal', ['perm']);

        self::assertSame($resource, $grant->getResource());
        self::assertSame('reef:phpunit:type:slug-id', (string) $grant->getResource());
    }
}
