<?php

declare(strict_types=1);

/*
 * This file is part of the Ferienpass package.
 *
 * (c) Richard Henkenjohann <richard@ferienpass.online>
 *
 * For more information visit the project website <https://ferienpass.online>
 * or the documentation under <https://docs.ferienpass.online>.
 */

namespace Ferienpass\CoreBundle\Tests\Entity;

use Ferienpass\CoreBundle\Entity\TenantDomain;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;

/**
 * Host normalisation decides whether a request finds its tenant, so what an
 * admin pastes into the form has to end up matching what arrives in a `Host`
 * header.
 */
final class TenantDomainTest extends TestCase
{
    #[DataProvider('hosts')]
    public function testNormalizesToABareHostname(string $input, string $expected): void
    {
        self::assertSame($expected, TenantDomain::normalizeHost($input));
    }

    public static function hosts(): iterable
    {
        yield 'plain' => ['ferienpass-nordhorn.de', 'ferienpass-nordhorn.de'];
        yield 'upper case' => ['Ferienpass-Nordhorn.DE', 'ferienpass-nordhorn.de'];
        yield 'whitespace' => ['  ferienpass-nordhorn.de ', 'ferienpass-nordhorn.de'];
        yield 'scheme' => ['https://ferienpass-nordhorn.de', 'ferienpass-nordhorn.de'];
        yield 'scheme and path' => ['https://ferienpass-nordhorn.de/angebote', 'ferienpass-nordhorn.de'];
        yield 'port' => ['ferienpass-nordhorn.de:8443', 'ferienpass-nordhorn.de'];
        yield 'trailing dot' => ['ferienpass-nordhorn.de.', 'ferienpass-nordhorn.de'];
        yield 'path only' => ['ferienpass-nordhorn.de/angebote', 'ferienpass-nordhorn.de'];
        yield 'ipv6 literal' => ['[::1]', '[::1]'];
        yield 'empty' => ['', ''];
    }

    public function testTheSetterNormalizes(): void
    {
        $domain = new TenantDomain(null, 'HTTPS://Ferienpass-Nordhorn.de:443/x');

        self::assertSame('ferienpass-nordhorn.de', $domain->getHost());
    }
}
