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

Fix createFromFormat with now format #23

Merged
merged 1 commit into from
Aug 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion src/ClockMock.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ private static function mock_date_create_from_format(): callable
// `date_parse_from_format` returns false for all time parts if no time parts are included in the format,
// but if the format includes at least one time part all time parts not included become zero instead.
$parsedDate = date_parse_from_format($format, (string) $datetime);
if ($parsedDate['hour'] === false) {
if ($dateTimeObject !== false && $parsedDate['hour'] === false) {
$frozen = ClockMock::getFrozenDateTime();
return $dateTimeObject->setTime(
(int) $frozen->format('H'),
Expand Down
8 changes: 8 additions & 0 deletions tests/ClockMockTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,14 @@ public function test_date_create_from_format()
$this->assertSame('2022-05-28 12:13:14', $dateTimeFromFormat->format('Y-m-d H:i:s'));
}

public function test_date_create_from_format_must_return_false_on_error()
{
ClockMock::freeze(new \DateTimeImmutable('1986-06-05 12:13:14'));

$dateTime = \DateTime::createFromFormat('\n\o\w', '2022-01-01');
$this->assertFalse($dateTime);
}

public function test_date_create_immutable()
{
ClockMock::freeze($fakeNow = new \DateTimeImmutable('1986-06-05'));
Expand Down