Skip to content

Commit

Permalink
Add missing Cron/OneOff tz test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
yhabteab committed Aug 9, 2023
1 parent 5669694 commit c8fe107
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
34 changes: 34 additions & 0 deletions tests/CronTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace ipl\Tests\Scheduler;

use DateTime;
use DateTimeZone;
use InvalidArgumentException;
use ipl\Scheduler\Contract\Frequency;
use ipl\Scheduler\Cron;
Expand Down Expand Up @@ -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);
}
}
}
27 changes: 26 additions & 1 deletion tests/OneOffTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
}
}
}

0 comments on commit c8fe107

Please sign in to comment.