Skip to content

Commit 5884616

Browse files
authored
Merge pull request #32 from slope-it/fix-constructor-with-empty-string
2 parents 3a56322 + 922c773 commit 5884616

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

src/DateTimeMock/DateTimeMock.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,16 @@ class DateTimeMock extends \DateTime
1515
{
1616
public function __construct(?string $datetime = 'now', ?DateTimeZone $timezone = null)
1717
{
18-
$datetime = $datetime ?? 'now';
19-
2018
parent::__construct($datetime, $timezone);
2119

2220
$isDateTimeStringRelative = $this->isRelativeDateString($datetime);
2321

22+
// Empty string is not accepted by strtotime, which we use below, so normalize to 'now'. By the way, this is
23+
// also equivalent to how original \DateTime treats it.
24+
if ($datetime === '') {
25+
$datetime = 'now';
26+
}
27+
2428
if ($timezone !== null && !$isDateTimeStringRelative) {
2529
// When there's a timezone and the provided date is absolute, the timestamp must be calculated with that
2630
// specific timezone in order to mimic behavior of the original \DateTime (which does not modify time).

tests/ClockMockTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,16 @@ public function test_DateTime_constructor_with_absolute_date_and_timezone()
8787
$this->assertSame('1986-06-05 12:13:14', $absoluteDateTimeWithTimezone->format('Y-m-d H:i:s'));
8888
}
8989

90+
/**
91+
* @see https://github.com/slope-it/clock-mock/issues/31
92+
*/
93+
public function test_DateTime_constructor_with_empty_string()
94+
{
95+
ClockMock::freeze($fakeNow = new \DateTime('1986-06-05 14:26:29.123456'));
96+
97+
$this->assertEquals($fakeNow, new \DateTime('')); // Empty string should behave exactly as "now"
98+
}
99+
90100
/**
91101
* @see https://github.com/slope-it/clock-mock/issues/7
92102
*/

0 commit comments

Comments
 (0)