<?php

/*
 * 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\FilterShopCountrySubscriber;
use CpCreation\VitiCore\Bridge\Symfony\Bundle\EventListener\GroupListener;
use CpCreation\VitiCore\Bridge\Symfony\Bundle\EventListener\LocaleListener;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use Symfony\Component\EventDispatcher\Attribute\AsEventListener;

final class ShopCountryListenerPriorityTest extends TestCase
{
    /**
     * @param class-string $countryListenerClass
     */
    #[DataProvider('countryListenerClasses')]
    public function testCountryListenerRunsAfterLocaleListener(string $countryListenerClass): void
    {
        self::assertLessThan(
            $this->getPriority(LocaleListener::class),
            $this->getPriority($countryListenerClass),
            sprintf('%s must run after the VitiCore locale listener.', $countryListenerClass),
        );
    }

    /**
     * @return iterable<string, array{class-string}>
     */
    public static function countryListenerClasses(): iterable
    {
        yield 'country filter' => [FilterShopCountrySubscriber::class];
        yield 'legacy group listener' => [GroupListener::class];
    }

    /**
     * @param class-string $listenerClass
     */
    private function getPriority(string $listenerClass): int
    {
        $attributes = new \ReflectionClass($listenerClass)->getAttributes(AsEventListener::class);

        self::assertCount(1, $attributes);

        return $attributes[0]->newInstance()->priority;
    }
}
