<?php

declare(strict_types=1);

namespace CpCreation\VitiCore\Tests\Form\Admin;

use CpCreation\VitiCore\Bridge\Symfony\Bundle\Form\Admin\PromoType;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Form\Extension\Core\Type\CountryType;
use Symfony\Component\Form\FormBuilderInterface;

final class PromoTypeTest extends TestCase
{
    public function testDeliveryCountriesCanBeSelectedWithoutBeingRequired(): void
    {
        $countryType = null;
        $countryOptions = null;
        $builder = $this->createMock(FormBuilderInterface::class);
        $builder->method('add')
            ->willReturnCallback(
                static function (mixed $child, ?string $type = null, array $options = []) use (&$countryType, &$countryOptions, $builder): FormBuilderInterface {
                    if ('restrictedCountryCodes' === $child) {
                        $countryType = $type;
                        $countryOptions = $options;
                    }

                    return $builder;
                }
            );

        new PromoType()->buildForm($builder, []);

        self::assertSame(CountryType::class, $countryType);
        self::assertIsArray($countryOptions);
        self::assertFalse($countryOptions['required']);
        self::assertTrue($countryOptions['multiple']);
        self::assertSame('select2', $countryOptions['attr']['class']);
        self::assertSame('admin.promo.delivery_countries', $countryOptions['label']);
        self::assertSame('admin.promo.delivery_countries_help', $countryOptions['help']);
    }
}
