From c8fe1073eb2a0c3807a45e299e69875a95a584e7 Mon Sep 17 00:00:00 2001 From: Yonas Habteab Date: Wed, 9 Aug 2023 14:53:09 +0200 Subject: [PATCH] Add missing `Cron/OneOff` tz test cases --- tests/CronTest.php | 34 ++++++++++++++++++++++++++++++++++ tests/OneOffTest.php | 27 ++++++++++++++++++++++++++- 2 files changed, 60 insertions(+), 1 deletion(-) diff --git a/tests/CronTest.php b/tests/CronTest.php index 59d2ba0..686221a 100644 --- a/tests/CronTest.php +++ b/tests/CronTest.php @@ -3,6 +3,7 @@ namespace ipl\Tests\Scheduler; use DateTime; +use DateTimeZone; use InvalidArgumentException; use ipl\Scheduler\Contract\Frequency; use ipl\Scheduler\Cron; @@ -132,4 +133,37 @@ public function testJsonSerializeAndDeserialize() $this->assertEquals($start, $fromJson->getStart()); $this->assertEquals($end, $fromJson->getEnd()); } + + public function testSerializeAndDeserializeHandleTimezonesCorrectly() + { + $oldTz = date_default_timezone_get(); + date_default_timezone_set('Europe/Berlin'); + + try { + $start = new DateTime('01-06-2023T12:00:00', new DateTimeZone('America/New_York')); + $end = new DateTime('01-06-2024T07:00:00', new DateTimeZone('America/New_York')); + + $cron = Cron::fromJson( + json_encode( + (new Cron('@minutely')) + ->startAt($start) + ->endAt($end) + ) + ); + + $this->assertEquals( + new DateTime('2023-06-01T18:00:00'), + $cron->getStart(), + 'Cron::jsonSerialize() does not restore the start date with a time zone correctly' + ); + + $this->assertEquals( + new DateTime('2024-06-01T13:00:00'), + $cron->getEnd(), + 'Cron::jsonSerialize() does not restore the end date with a time zone correctly' + ); + } finally { + date_default_timezone_set($oldTz); + } + } } diff --git a/tests/OneOffTest.php b/tests/OneOffTest.php index 1b3cb46..d3219ec 100644 --- a/tests/OneOffTest.php +++ b/tests/OneOffTest.php @@ -3,7 +3,7 @@ namespace ipl\Tests\Scheduler; use DateTime; -use ipl\Scheduler\Contract\Frequency; +use DateTimeZone; use ipl\Scheduler\OneOff; use PHPUnit\Framework\TestCase; @@ -45,4 +45,29 @@ public function testJsonSerialize() $this->assertEquals($oneOff, $fromJson); $this->assertEquals($now, $fromJson->getStart()); } + + public function testSerializeAndDeserializeHandleTimezonesCorrectly() + { + $oldTz = date_default_timezone_get(); + date_default_timezone_set('Europe/Berlin'); + + try { + $dateTime = new DateTime('01-06-2023T12:00:00', new DateTimeZone('America/New_York')); + $oneOff = OneOff::fromJson(json_encode(new OneOff($dateTime))); + + $this->assertEquals( + new DateTime('2023-06-01T18:00:00'), + $oneOff->getStart(), + 'OneOff::jsonSerialize() does not restore the start date with a time zone correctly' + ); + + $this->assertEquals( + new DateTime('2023-06-01T18:00:00'), + $oneOff->getEnd(), + 'OneOff::jsonSerialize() does not restore the start/end date with a time zone correctly' + ); + } finally { + date_default_timezone_set($oldTz); + } + } }