<?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\OrmFilterSubscriber;
use Doctrine\ORM\EntityManagerInterface;
use PHPUnit\Framework\TestCase;
use Presta\SitemapBundle\Controller\SitemapController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\HttpKernel\Event\RequestEvent;

final class OrmFilterSubscriberTest extends TestCase
{
    public function testAcceptsAnArrayControllerDefinition(): void
    {
        $request = Request::create('/sitemap.xml');
        $request->attributes->set('_controller', [SitemapController::class, 'indexAction']);
        $event = new RequestEvent(
            $this->createStub(KernelInterface::class),
            $request,
            HttpKernelInterface::MAIN_REQUEST,
        );

        set_error_handler(static function (int $severity, string $message, string $file, int $line): never {
            throw new \ErrorException($message, 0, $severity, $file, $line);
        });

        try {
            new OrmFilterSubscriber($this->createStub(EntityManagerInterface::class))->onKernelRequest($event);
        } finally {
            restore_error_handler();
        }

        self::assertTrue(true);
    }
}
