<?php

declare(strict_types=1);

/*
 * This file is part of the VITICORE2 package.
 *
 * (c) CP Creation <web@cpcreation.fr>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace CpCreation\VitiCore\Tests\EventListener;

use CpCreation\VitiCore\Bridge\Symfony\Bundle\EventListener\LocaleListener;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\Routing\RequestContext;

final class LocaleListenerTest extends TestCase
{
    public function testAdminLocaleIsFrenchWithoutAnExistingSession(): void
    {
        $request = Request::create('/admincp/contact/new');
        $request->setLocale('en');
        $requestContext = new RequestContext();

        new LocaleListener('fr', $requestContext)->onKernelRequest($this->createRequestEvent($request));

        self::assertSame('fr', $request->getLocale());
        self::assertSame('fr', $requestContext->getParameter('_locale'));
    }

    public function testPublicLocaleIsNotOverriddenWithoutAnExistingSession(): void
    {
        $request = Request::create('/en/shop');
        $request->setLocale('en');

        new LocaleListener()->onKernelRequest($this->createRequestEvent($request));

        self::assertSame('en', $request->getLocale());
    }

    public function testAdminLocaleReplacesTheLocaleStoredInTheSession(): void
    {
        $request = Request::create('/admincp/contact/new');
        $request->setLocale('en');
        $session = new Session(new MockArraySessionStorage());
        $session->set('_locale', 'en');
        $request->setSession($session);
        $request->cookies->set($session->getName(), $session->getId());

        new LocaleListener()->onKernelRequest($this->createRequestEvent($request));

        self::assertSame('fr', $request->getLocale());
        self::assertSame('fr', $session->get('_locale'));
    }

    public function testStatelessRequestIgnoresAnAttachedSession(): void
    {
        $request = Request::create('/api/commerce/v1/products');
        $request->setLocale('en');
        $request->attributes->set('_stateless', true);
        $session = new Session(new MockArraySessionStorage());
        $request->setSession($session);
        $request->cookies->set($session->getName(), $session->getId());
        $requestContext = new RequestContext();

        new LocaleListener('fr', $requestContext)->onKernelRequest($this->createRequestEvent($request));

        self::assertFalse($session->isStarted());
        self::assertSame('en', $request->getLocale());
        self::assertSame('en', $requestContext->getParameter('_locale'));
    }

    private function createRequestEvent(Request $request): RequestEvent
    {
        return new RequestEvent(
            $this->createStub(KernelInterface::class),
            $request,
            HttpKernelInterface::MAIN_REQUEST,
        );
    }
}
