mirror of
https://github.com/Respect/Validation.git
synced 2026-03-15 23:05:45 +01:00
I ran the `bin/console spdx --fix` with different strategies for different files. For most of the core classes, since they've been drastically rebuilt, I've run it with the `git-blame` strategy, for for the `src/Validators`, in which the API changed completely but the logic remains the same, I use the `git-log` strategy.
81 lines
2.3 KiB
PHP
81 lines
2.3 KiB
PHP
<?php
|
|
|
|
/*
|
|
* SPDX-License-Identifier: MIT
|
|
* SPDX-FileCopyrightText: (c) Respect Project Contributors
|
|
* SPDX-FileContributor: Alexandre Gomes Gaigalas <alganet@gmail.com>
|
|
* SPDX-FileContributor: Henrique Moody <henriquemoody@gmail.com>
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Respect\Validation\Validators;
|
|
|
|
use DateTime;
|
|
use DateTimeImmutable;
|
|
use PHPUnit\Framework\Attributes\CoversClass;
|
|
use PHPUnit\Framework\Attributes\DataProvider;
|
|
use PHPUnit\Framework\Attributes\Group;
|
|
use PHPUnit\Framework\Attributes\Test;
|
|
use Respect\Validation\Exceptions\InvalidValidatorException;
|
|
use Respect\Validation\Test\RuleTestCase;
|
|
|
|
#[Group('validator')]
|
|
#[CoversClass(Date::class)]
|
|
final class DateTest extends RuleTestCase
|
|
{
|
|
#[Test]
|
|
#[DataProvider('validFormatsProvider')]
|
|
public function shouldThrowAnExceptionWhenFormatIsNotValid(string $format): void
|
|
{
|
|
$this->expectException(InvalidValidatorException::class);
|
|
|
|
new Date($format);
|
|
}
|
|
|
|
/** @return string[][] */
|
|
public static function validFormatsProvider(): array
|
|
{
|
|
return [
|
|
['Y-m-d H:i:s'],
|
|
['c'],
|
|
];
|
|
}
|
|
|
|
/** @return iterable<array{Date, mixed}> */
|
|
public static function providerForValidInput(): iterable
|
|
{
|
|
return [
|
|
[new Date(), '2017-12-31'],
|
|
[new Date('m/d/y'), '12/31/17'],
|
|
[new Date('F jS, Y'), 'May 1st, 2017'],
|
|
[new Date('Ydm'), 20173112],
|
|
[new Date(), '2020-02-29'],
|
|
];
|
|
}
|
|
|
|
/** @return iterable<array{Date, mixed}> */
|
|
public static function providerForInvalidInput(): iterable
|
|
{
|
|
return [
|
|
[new Date(), 'not-a-date'],
|
|
[new Date(), []],
|
|
[new Date(), true],
|
|
[new Date(), false],
|
|
[new Date(), null],
|
|
[new Date(), ''],
|
|
[new Date(), '1988-02-30'],
|
|
[new Date('d/m/y'), '12/31/17'],
|
|
[new Date(), '2019-02-29'],
|
|
[new Date(), new DateTime()],
|
|
[new Date(), new DateTimeImmutable()],
|
|
[new Date(), ''],
|
|
[new Date('Y-m-d'), '2009-12-00'],
|
|
[new Date('Y-m-d'), '2018-02-29'],
|
|
[new Date(), '2014-99'],
|
|
[new Date('d'), 1],
|
|
[new Date('Y-m'), '2014-99'],
|
|
[new Date('m'), '99'],
|
|
];
|
|
}
|
|
}
|