<?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\MarketingAttributionListener;
use CpCreation\VitiCore\Bridge\Symfony\Bundle\Tracking\MarketingAttributionStorage;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\Event\ResponseEvent;

final class MarketingAttributionListenerTest extends TestCase
{
    public function testStatelessRequestDoesNotCaptureAttributionOrSetACookie(): void
    {
        $request = Request::create('/api/commerce/v1/instance');
        $request->attributes->set('_stateless', true);
        $kernel = $this->createStub(KernelInterface::class);
        $listener = new MarketingAttributionListener(new MarketingAttributionStorage());

        $listener->onKernelRequest(new RequestEvent(
            $kernel,
            $request,
            HttpKernelInterface::MAIN_REQUEST,
        ));
        self::assertNull($request->attributes->get(MarketingAttributionStorage::REQUEST_ATTRIBUTE));

        $request->attributes->set(MarketingAttributionStorage::REQUEST_ATTRIBUTE, ['visitor_id' => 'ignored']);
        $response = new Response();
        $listener->onKernelResponse(new ResponseEvent(
            $kernel,
            $request,
            HttpKernelInterface::MAIN_REQUEST,
            $response,
        ));

        self::assertSame([], $response->headers->getCookies());
    }
}
