Skip to content

Commit 781248d

Browse files
authored
Merge pull request #35 from holmberd/34-fix-datetimeimmutable-not-respecting-timezone-type
2 parents 5884616 + 7ac2579 commit 781248d

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

src/ClockMock.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ public static function mock_date_create_immutable_from_format(): callable
211211
// Create an immutable instance starting from the mutable mock, so we don't have to replicate mocking logic.
212212
$mutableDateTime = date_create_from_format($format, $datetime, $timezone);
213213

214-
return new \DateTimeImmutable($mutableDateTime->format('Y-m-d\TH:i:s.uT'), $timezone);
214+
return new \DateTimeImmutable($mutableDateTime->format('Y-m-d\TH:i:s.u'), $mutableDateTime->getTimezone());
215215
};
216216
}
217217

src/DateTimeMock/DateTimeImmutableMock.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@ public function __construct(?string $datetime = 'now', ?DateTimeZone $timezone =
1717
// Create an immutable instance starting from the mutable mock, so we don't have to replicate mocking logic.
1818
$mutableDateTime = new DateTimeMock($datetime, $timezone);
1919

20-
parent::__construct($mutableDateTime->format('Y-m-d\TH:i:s.uT'), $timezone);
20+
parent::__construct($mutableDateTime->format('Y-m-d\TH:i:s.u'), $mutableDateTime->getTimezone());
2121
}
2222
}

tests/ClockMockTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,38 @@ public function test_DateTimeImmutable_constructor_with_timezone()
5252
$this->assertEquals($dateWithTimezone, new \DateTimeImmutable('1986-06-05 14:41:32+02:00'));
5353
}
5454

55+
public function test_DateTimeImmutable_constructor_with_timezone_respect_zone_type()
56+
{
57+
ClockMock::freeze(new \DateTimeImmutable('now'));
58+
59+
$timezoneType3 = 'Asia/Tokyo';
60+
$date = new \DateTimeImmutable('1986-06-05', new \DateTimeZone($timezoneType3));
61+
62+
$this->assertEquals($date->getTimezone()->getName(), $timezoneType3);
63+
64+
$timezoneType2 = 'CDT';
65+
$date = new \DateTimeImmutable('1986-06-05', new \DateTimeZone($timezoneType2));
66+
67+
$this->assertEquals($date->getTimezone()->getName(), $timezoneType2);
68+
}
69+
70+
public function test_DateTimeImmutable_constructor_without_timezone()
71+
{
72+
$originalTimezone = date_default_timezone_get();
73+
$defaultTimezone = 'Asia/Tokyo';
74+
date_default_timezone_set($defaultTimezone);
75+
76+
try {
77+
ClockMock::freeze(new \DateTimeImmutable('1986-06-05'));
78+
79+
$newDate = new \DateTimeImmutable('1986-06-05');
80+
81+
$this->assertEquals($newDate->getTimezone()->getName(), $defaultTimezone);
82+
} finally {
83+
date_default_timezone_set($originalTimezone); // Revert timezone.
84+
}
85+
}
86+
5587
public function test_DateTimeImmutable_createFromFormat()
5688
{
5789
ClockMock::freeze(new \DateTimeImmutable('1986-06-05 12:13:14'));

0 commit comments

Comments
 (0)