Skip to content

Commit

Permalink
Fix datetimeimmutable not respecting timezone type
Browse files Browse the repository at this point in the history
  • Loading branch information
checkfrontChase committed Mar 20, 2023
1 parent 5884616 commit 5bbf59f
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/ClockMock.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ public static function mock_date_create_immutable_from_format(): callable
// Create an immutable instance starting from the mutable mock, so we don't have to replicate mocking logic.
$mutableDateTime = date_create_from_format($format, $datetime, $timezone);

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

Expand Down
2 changes: 1 addition & 1 deletion src/DateTimeMock/DateTimeImmutableMock.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ public function __construct(?string $datetime = 'now', ?DateTimeZone $timezone =
// Create an immutable instance starting from the mutable mock, so we don't have to replicate mocking logic.
$mutableDateTime = new DateTimeMock($datetime, $timezone);

parent::__construct($mutableDateTime->format('Y-m-d\TH:i:s.uT'), $timezone);
parent::__construct($mutableDateTime->format('Y-m-d\TH:i:s.u'), $mutableDateTime->getTimezone());
}
}
31 changes: 31 additions & 0 deletions tests/ClockMockTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,37 @@ public function test_DateTimeImmutable_constructor_with_timezone()
$this->assertEquals($dateWithTimezone, new \DateTimeImmutable('1986-06-05 14:41:32+02:00'));
}

public function test_DateTimeImmutable_constructor_with_timezone_respect_zone_type()
{
ClockMock::freeze(new \DateTimeImmutable('now'));

$timezoneType3 = 'Asia/Tokyo';
$date = new \DateTimeImmutable('1986-06-05', new \DateTimeZone($timezoneType3));

$this->assertEquals($date->getTimezone()->getName(), $timezoneType3);

$timezoneType2 = 'CDT';
$date = new \DateTimeImmutable('1986-06-05', new \DateTimeZone($timezoneType2));

$this->assertEquals($date->getTimezone()->getName(), $timezoneType2);
}

public function test_DateTimeImmutable_constructor_without_timezone()
{
$originalTimezone = date_default_timezone_get();

$defaultTimezone = 'Asia/Tokyo';
date_default_timezone_set($defaultTimezone);

ClockMock::freeze(new \DateTimeImmutable('1986-06-05'));

$newDate = new \DateTimeImmutable('1986-06-05');

$this->assertEquals($newDate->getTimezone()->getName(), $defaultTimezone);

date_default_timezone_set($originalTimezone); // Revert timezone.
}

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

0 comments on commit 5bbf59f

Please sign in to comment.