From 118af92ebcb831e33f76c4a550b45f241f898204 Mon Sep 17 00:00:00 2001 From: dmachehin Date: Wed, 6 Oct 2021 14:03:58 +0300 Subject: [PATCH 1/2] Schema\TypeFormats\StringDate: fix check + add tests --- src/Schema/TypeFormats/StringDate.php | 7 ++- tests/Schema/TypeFormats/StringDateTest.php | 54 +++++++++++++++++++++ 2 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 tests/Schema/TypeFormats/StringDateTest.php diff --git a/src/Schema/TypeFormats/StringDate.php b/src/Schema/TypeFormats/StringDate.php index 6bf8a65a..719000d7 100644 --- a/src/Schema/TypeFormats/StringDate.php +++ b/src/Schema/TypeFormats/StringDate.php @@ -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; } } diff --git a/tests/Schema/TypeFormats/StringDateTest.php b/tests/Schema/TypeFormats/StringDateTest.php new file mode 100644 index 00000000..17b47784 --- /dev/null +++ b/tests/Schema/TypeFormats/StringDateTest.php @@ -0,0 +1,54 @@ +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'], + ]; + } +} From 4445d5af527aa03a6fb165a7b96b000c19dacd31 Mon Sep 17 00:00:00 2001 From: dmachehin Date: Wed, 6 Oct 2021 16:19:25 +0300 Subject: [PATCH 2/2] Schema\TypeFormats\StringDate: fix compare --- src/Schema/TypeFormats/StringDate.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Schema/TypeFormats/StringDate.php b/src/Schema/TypeFormats/StringDate.php index 719000d7..f36774b7 100644 --- a/src/Schema/TypeFormats/StringDate.php +++ b/src/Schema/TypeFormats/StringDate.php @@ -20,6 +20,6 @@ public function __invoke($value): bool return false; } - return $datetime->format('Y-m-d') == $value; + return $datetime->format('Y-m-d') === $value; } }