Skip to content

Commit

Permalink
Merge pull request #23 from faille76/fix-create-from-format-with-now
Browse files Browse the repository at this point in the history
  • Loading branch information
asprega committed Aug 8, 2022
2 parents 264c22f + 1ceec93 commit f8950af
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/ClockMock.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,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

0 comments on commit f8950af

Please sign in to comment.