<?php

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

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

class ResourceTest extends TestCase
{
    public function testFromStringWithStringResourceType(): void
    {
        $resource = Resource::fromString('reef:phpunit:resource', 'test-id');

        self::assertSame('test-id', $resource->getResourceId());
        self::assertSame('reef:phpunit:resource:test-id', $resource->getSlug());
        self::assertSame('reef:phpunit:resource', (string) $resource->getResourceType());
    }

    public function testFromStringWithResourceTypeObject(): void
    {
        $resourceType = ResourceType::fromString('reef:phpunit:resource');
        $resource = Resource::fromString($resourceType, 'test-id');

        self::assertSame('test-id', $resource->getResourceId());
        self::assertSame('reef:phpunit:resource:test-id', $resource->getSlug());
    }

    public function testFromStringWithStringAndObjectProduceSameSlug(): void
    {
        $fromString = Resource::fromString('reef:phpunit:resource', 'test-id');
        $fromObject = Resource::fromString(ResourceType::fromString('reef:phpunit:resource'), 'test-id');

        self::assertSame((string) $fromString, (string) $fromObject);
        self::assertSame($fromString->getSlug(), $fromObject->getSlug());
    }

    public function testToString(): void
    {
        $resource = Resource::fromString('reef:phpunit:resource', 'abc');

        self::assertSame('reef:phpunit:resource:abc', (string) $resource);
    }

    public function testGetResourceType(): void
    {
        $resource = Resource::fromString('reef:mytype', '42');

        self::assertInstanceOf(ResourceType::class, $resource->getResourceType());
        self::assertSame('reef:mytype', (string) $resource->getResourceType());
    }
}
