Skip to content

Commit

Permalink
Fix of the check of date's validity (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
michal-tichy-84 authored Nov 24, 2020
1 parent 4a34b97 commit d8a05a0
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/DateTimeFromString.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ private static function assertValidDateTime(
string $format,
string $originalDateTimeString
): DateTimeImmutable {
if ($dateTime === false) {
if ($dateTime === false || $dateTime->format($format) !== $originalDateTimeString) {
$message = sprintf(
'Can\'t convert %s to datetime using format %s.',
$originalDateTimeString,
Expand Down
63 changes: 55 additions & 8 deletions src/DateTimeFromStringTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,21 @@ public function testCreateDateTime(
public function dateTimeToCreateProvider(): array
{
return [
['2017-05-31T13:32:40+00:00', 'U', '1496237560'],
['2017-05-10T12:13:14+02:00', DateTime::ATOM, '2017-05-10T12:13:14+02:00'],
[
'expectedDateTime' => '2017-05-31T13:32:40+00:00',
'format' => 'U',
'dateTimeString' => '1496237560',
],
[
'expectedDateTime' => '1969-12-31T23:59:59+00:00',
'format' => 'U',
'dateTimeString' => '-1',
],
[
'expectedDateTime' => '2017-05-10T12:13:14+05:00',
'format' => DateTime::ATOM,
'dateTimeString' => '2017-05-10T12:13:14+05:00',
],
];
}

Expand Down Expand Up @@ -67,8 +80,18 @@ public function testCreateDateTimeWithTimezone(
public function dateTimeWithTimeZoneToCreateProvider(): array
{
return [
['2017-05-10T12:13:14+02:00', 'Y-m-d H:i:s', '2017-05-10 12:13:14', new DateTimeZone('Europe/Prague')],
['2017-05-10T12:13:14+00:00', 'Y-m-d H:i:s', '2017-05-10 12:13:14', new DateTimeZone('UTC')],
[
'expectedDateTime' => '2017-05-10T12:13:14+02:00',
'format' => 'Y-m-d H:i:s',
'dateTimeString' => '2017-05-10 12:13:14',
'timeZone' => new DateTimeZone('Europe/Prague'),
],
[
'expectedDateTime' => '2017-05-10T12:13:14+00:00',
'format' => 'Y-m-d H:i:s',
'dateTimeString' => '2017-05-10 12:13:14',
'timeZone' => new DateTimeZone('UTC'),
],
];
}

Expand Down Expand Up @@ -117,10 +140,34 @@ public function testThrowExceptionWhenCantCreateDateTimeWithTimeZone(
public function invalidDataProvider(): array
{
return [
['', 'U'],
['gandalf', 'U'],
['-1', 'U'],
['0000-00-00 00:00:00', 'Y-m-d H:i:s'],
'Empty unix timestamp' => [
'format' => 'U',
'dateTimeString' => '',
],
'Non-digit unix timestamp' => [
'format' => 'U',
'dateTimeString' => 'gandalf',
],
'Classic datetime with zeros' => [
'format' => 'Y-m-d H:i:s',
'dateTimeString' => '0000-00-00 00:00:00',
],
'ISO with zeros' => [
'format' => DateTime::ATOM,
'dateTimeString' => '0000-00-00T00:00:00+00:00',
],
'ISO in PHP corrupted format' => [
'format' => DateTime::ATOM,
'dateTimeString' => '2017-05-10T12:13:14+0200',
],
'ISO with invalid day in month' => [
'format' => DateTime::ATOM,
'dateTimeString' => '2020-11-31T12:13:14+02:00',
],
'ISO with invalid month' => [
'format' => DateTime::ATOM,
'dateTimeString' => '2020-13-05T12:13:14+02:00',
],
];
}
}

0 comments on commit d8a05a0

Please sign in to comment.