<?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\EventListener;

use Ferienpass\CoreBundle\Entity\Edition;
use Ferienpass\CoreBundle\Entity\Host;
use Ferienpass\CoreBundle\EventListener\Doctrine\Edition\AliasListener as EditionAliasListener;
use Ferienpass\CoreBundle\EventListener\Doctrine\Host\AliasListener as HostAliasListener;
use PHPUnit\Framework\TestCase;
use Symfony\Component\String\Slugger\AsciiSlugger;

final class DoctrineAliasListenerTest extends TestCase
{
    public function testEditionAliasListenerSetsAliasOnPrePersist(): void
    {
        $slugger = new AsciiSlugger();
        $listener = new EditionAliasListener($slugger);

        $edition = new Edition();
        $edition->setName('Sommer Ferien 2024');
        $listener->prePersist($edition);

        self::assertSame('sommer-ferien-2024', $edition->getAlias());
    }

    public function testEditionAliasListenerSetsAliasOnPreUpdate(): void
    {
        $slugger = new AsciiSlugger();
        $listener = new EditionAliasListener($slugger);

        $edition = new Edition();
        $edition->setName('Winter 2024');
        $listener->preUpdate($edition);

        self::assertSame('winter-2024', $edition->getAlias());
    }

    public function testHostAliasListenerSetsAliasOnPrePersist(): void
    {
        $slugger = new AsciiSlugger();
        $listener = new HostAliasListener($slugger);

        $host = new Host();
        $host->setName('My Host Name');
        $listener->prePersist($host);

        self::assertSame('my-host-name', $host->getAlias());
    }

    public function testHostAliasListenerSetsAliasOnPreUpdate(): void
    {
        $slugger = new AsciiSlugger();
        $listener = new HostAliasListener($slugger);

        $host = new Host();
        $host->setName('Updated Host');
        $listener->preUpdate($host);

        self::assertSame('updated-host', $host->getAlias());
    }
}
