mirror of
https://github.com/Respect/Validation.git
synced 2024-06-08 08:42:15 +02:00
d1f108dc87
This commit will ensure that all rules that cannot be created because of invalid arguments in the constructor will throw the InvalidRuleConstructorException. It will also make ComponentException extend LogicException, which makes it easier to determine that the client has improperly used the library. I also introduced some tests for two exceptions with logic in their constructor. Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
82 lines
2.2 KiB
PHP
82 lines
2.2 KiB
PHP
<?php
|
|
|
|
/*
|
|
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
|
|
* SPDX-License-Identifier: MIT
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Respect\Validation\Rules;
|
|
|
|
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\InvalidRuleConstructorException;
|
|
use Respect\Validation\Test\RuleTestCase;
|
|
|
|
#[Group('rule')]
|
|
#[CoversClass(Date::class)]
|
|
final class DateTest extends RuleTestCase
|
|
{
|
|
#[Test]
|
|
#[DataProvider('validFormatsProvider')]
|
|
public function shouldThrowAnExceptionWhenFormatIsNotValid(string $format): void
|
|
{
|
|
$this->expectException(InvalidRuleConstructorException::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'],
|
|
];
|
|
}
|
|
}
|