Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Schema\TypeFormats\StringDate: fix check + add tests #144

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/Schema/TypeFormats/StringDate.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ public function __invoke($value): bool
{
// full-date notation as defined by RFC 3339, section 5.6, for example, 2017-07-21

return DateTime::createFromFormat('Y-m-d', $value) !== false;
$datetime = DateTime::createFromFormat('Y-m-d', $value);
if ($datetime === false) {
return false;
}

return $datetime->format('Y-m-d') == $value;
dmachehin marked this conversation as resolved.
Show resolved Hide resolved
}
}
54 changes: 54 additions & 0 deletions tests/Schema/TypeFormats/StringDateTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

declare(strict_types=1);

namespace League\OpenAPIValidation\Tests\Schema\TypeFormats;

use League\OpenAPIValidation\Schema\TypeFormats\StringDate;
use PHPUnit\Framework\TestCase;

final class StringDateTest extends TestCase
{
/**
* @dataProvider dateGreenDataProvider
*/
public function testGreendateTypeFormat(string $date): void
{
$this->assertTrue((new StringDate())($date));
}

/**
* @return string[][]
*/
public function dateGreenDataProvider(): array
{
return [
['1985-04-12'],
['1937-01-01'],
['1996-12-19'],
['1990-12-31'],
];
}

/**
* @dataProvider dateRedDataProvider
*/
public function testRedDateTypeFormat(string $date): void
{
$this->assertFalse((new StringDate())($date));
}

/**
* @return string[][]
*/
public function dateRedDataProvider(): array
{
return [
['2021-0-32'],
['2021-09-32'],
['0000-00-00'],
[''],
['somestring'],
];
}
}