<?php

declare(strict_types=1);

namespace Ferienpass\CoreBundle\Tests\Validator;

use Ferienpass\CoreBundle\Validator\FriendCodeValidator;
use Ferienpass\CoreBundle\Validator\ValidFriendCode;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\RateLimiter\RateLimiterFactory;
use Symfony\Component\RateLimiter\Storage\InMemoryStorage;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;

final class FriendCodeValidatorTest extends TestCase
{
    public function testWrongConstraintThrowsUnexpectedTypeException(): void
    {
        $this->expectException(UnexpectedTypeException::class);

        $context = $this->createMock(ExecutionContextInterface::class);
        $validator = new FriendCodeValidator(
            new RateLimiterFactory(['id' => 'test', 'policy' => 'no_limit'], new InMemoryStorage()),
            $this->createMock(RequestStack::class),
        );
        $validator->initialize($context);
        $validator->validate(null, $this->createMock(Constraint::class));
    }

    public function testNullValueDoesNotAddViolation(): void
    {
        $context = $this->createMock(ExecutionContextInterface::class);
        $context->expects(self::never())->method('buildViolation');

        $validator = new FriendCodeValidator(
            new RateLimiterFactory(['id' => 'test', 'policy' => 'no_limit'], new InMemoryStorage()),
            $this->createMock(RequestStack::class),
        );
        $validator->initialize($context);
        $validator->validate(null, new ValidFriendCode());
    }
}
