<?php

declare(strict_types=1);

namespace App\Tests\Unit\Module\Core\Domain\Entity;

use App\Module\Core\Domain\Entity\Permission;
use InvalidArgumentException;
use PHPUnit\Framework\TestCase;

/**
 * @internal
 */
final class PermissionTest extends TestCase
{
    public function testValidConstruction(): void
    {
        $p = new Permission('core.users.view', 'Voir les utilisateurs', 'core');
        self::assertSame('core.users.view', $p->getCode());
        self::assertSame('Voir les utilisateurs', $p->getLabel());
        self::assertSame('core', $p->getModule());
        self::assertFalse($p->isOrphan());
    }

    public function testCodeMustContainADot(): void
    {
        $this->expectException(InvalidArgumentException::class);
        new Permission('coreusersview', 'x', 'core');
    }

    public function testCodeCannotBeEmpty(): void
    {
        $this->expectException(InvalidArgumentException::class);
        new Permission('', 'x', 'core');
    }

    public function testLabelCannotBeEmpty(): void
    {
        $this->expectException(InvalidArgumentException::class);
        new Permission('core.users.view', '', 'core');
    }

    public function testModuleCannotBeEmpty(): void
    {
        $this->expectException(InvalidArgumentException::class);
        new Permission('core.users.view', 'x', '');
    }

    public function testMarkOrphanAndRevive(): void
    {
        $p = new Permission('core.users.view', 'Voir', 'core');
        $p->markOrphan();
        self::assertTrue($p->isOrphan());
        $p->revive('Voir maj', 'core');
        self::assertFalse($p->isOrphan());
        self::assertSame('Voir maj', $p->getLabel());
    }
}
