Skip to content

Commit

Permalink
Merge pull request #35 from holmberd/34-fix-datetimeimmutable-not-res…
Browse files Browse the repository at this point in the history
…pecting-timezone-type
  • Loading branch information
asprega committed Apr 5, 2023
2 parents 5884616 + 7ac2579 commit 781248d
Show file tree
Hide file tree
Showing 3 changed files with 34 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());
}
}
32 changes: 32 additions & 0 deletions tests/ClockMockTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,38 @@ 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);

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

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

$this->assertEquals($newDate->getTimezone()->getName(), $defaultTimezone);
} finally {
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 781248d

Please sign in to comment.