respect-validation/tests/unit/Validators/DateTest.php
Alexandre Gomes Gaigalas d9cdc118b2 Introduce REUSE compliance
This commit introduces REUSE compliance by annotating all files
with SPDX information and placing the reused licences in the
LICENSES folder.

We additionally removed the docheader tool which is made obsolete
by this change.

The main LICENSE and copyright text of the project is now not under
my personal name anymore, and it belongs to "The Respect Project
Contributors" instead.

This change restores author names to several files, giving the
appropriate attribution for contributions.
2026-01-21 06:28:11 +00:00

85 lines
2.5 KiB
PHP

<?php
/*
* SPDX-License-Identifier: MIT
* SPDX-FileCopyrightText: (c) Respect Project Contributors
* SPDX-FileContributor: Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-FileContributor: Eduardo Reveles <me@osiux.ws>
* SPDX-FileContributor: Emmerson <emmersonsiqueira@gmail.com>
* SPDX-FileContributor: Henrique Moody <henriquemoody@gmail.com>
* SPDX-FileContributor: Nick Lombard <github@jigsoft.co.za>
* SPDX-FileContributor: Pascal Borreli <pascal@borreli.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'],
];
}
}