Skip to content

Commit

Permalink
Schema\TypeFormats\StringDate: fix check + add tests (#144)
Browse files Browse the repository at this point in the history
* Schema\TypeFormats\StringDate: fix check + add tests

* Schema\TypeFormats\StringDate: fix compare

Co-authored-by: dmachehin <[email protected]>
  • Loading branch information
dmachehin and dmachehin authored Oct 6, 2021
1 parent d246868 commit a87c402
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
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;
}
}
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'],
];
}
}

0 comments on commit a87c402

Please sign in to comment.