<?php

declare(strict_types=1);

namespace LaminasTest\Validator;

use Laminas\Validator\Digits;
use PHPUnit\Framework\TestCase;

use function array_keys;

/**
 * @group      Laminas_Validator
 */
class DigitsTest extends TestCase
{
    /** @var Digits */
    protected $validator;

    protected function setUp(): void
    {
        $this->validator = new Digits();
    }

    /**
     * Ensures that the validator follows expected behavior for basic input values
     *
     * @dataProvider basicDataProvider
     */
    public function testExpectedResultsWithBasicInputValues(string $input, bool $expected): void
    {
        $this->assertSame($expected, $this->validator->isValid($input));
    }

    /**
     * @psalm-return array<string, array{0: string, 1: bool}>
     */
    public function basicDataProvider(): array
    {
        return [
            // phpcs:disable
            'invalid; starts with alphabetic chars'                 => ['abc123',  false],
            'invalid; contains alphabetic chars and one whitespace' => ['abc 123', false],
            'invalid; contains only alphabetic chars'               => ['abcxyz',  false],
            'invalid; contains alphabetic and special chars'        => ['AZ@#4.3', false],
            'invalid; is a float'                                   => ['1.23',    false],
            'invalid; is a hexa notation'                           => ['0x9f',    false],
            'invalid; is empty'                                     => ['',        false],

            'valid; is a normal integer'                            => ['123',     true],
            'valid; starts with a zero'                             => ['09',      true],
            // phpcs:enable
        ];
    }

    /**
     * Ensures that getMessages() returns expected initial value
     */
    public function testMessagesEmptyInitially(): void
    {
        $this->assertEquals([], $this->validator->getMessages());
    }

    public function testEmptyStringValueResultsInProperValidationFailureMessages(): void
    {
        $this->assertFalse($this->validator->isValid(''));
        $messages      = $this->validator->getMessages();
        $arrayExpected = [
            Digits::STRING_EMPTY => 'The input is an empty string',
        ];
        $this->assertSame($arrayExpected, $messages);
    }

    public function testInvalidValueResultsInProperValidationFailureMessages(): void
    {
        $this->assertFalse($this->validator->isValid('#'));
        $messages      = $this->validator->getMessages();
        $arrayExpected = [
            Digits::NOT_DIGITS => 'The input must contain only digits',
        ];
        $this->assertSame($arrayExpected, $messages);
    }

    /**
     * @Laminas-4352
     */
    public function testNonStringValidation(): void
    {
        $this->assertFalse($this->validator->isValid([1 => 1]));
    }

    public function testEqualsMessageTemplates(): void
    {
        $this->assertSame(
            [
                Digits::NOT_DIGITS,
                Digits::STRING_EMPTY,
                Digits::INVALID,
            ],
            array_keys($this->validator->getMessageTemplates())
        );
        $this->assertEquals($this->validator->getOption('messageTemplates'), $this->validator->getMessageTemplates());
    }
}
