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

use Ferienpass\CmsBundle\EventListener\IcuLocaleListener;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\HttpKernelInterface;

class IcuLocaleListenerTest extends TestCase
{
    private string $previousLocale;

    protected function setUp(): void
    {
        $this->previousLocale = \Locale::getDefault();
    }

    protected function tearDown(): void
    {
        \Locale::setDefault($this->previousLocale);
    }

    public function testOnKernelRequestWithPrsLocale(): void
    {
        $request = new Request();
        $request->setLocale('prs');

        $event = new RequestEvent(
            $this->createStub(HttpKernelInterface::class),
            $request,
            HttpKernelInterface::MAIN_REQUEST
        );

        $listener = new IcuLocaleListener();
        $listener->onKernelRequest($event);

        $this->assertSame('fa_AF', \Locale::getDefault());
        $this->assertSame('prs', $request->getLocale());

        // Ensure NumberFormatter works now
        $formatter = new \NumberFormatter(\Locale::getDefault(), \NumberFormatter::CURRENCY);
        $this->assertInstanceOf(\NumberFormatter::class, $formatter);
    }

    public function testOnKernelRequestWithDeLocale(): void
    {
        $request = new Request();
        $request->setLocale('de');

        \Locale::setDefault('de');

        $event = new RequestEvent(
            $this->createStub(HttpKernelInterface::class),
            $request,
            HttpKernelInterface::MAIN_REQUEST
        );

        $listener = new IcuLocaleListener();
        $listener->onKernelRequest($event);

        $this->assertSame('de', \Locale::getDefault());
        $this->assertSame('de', $request->getLocale());
    }
}
