Skip to content

Commit

Permalink
fix: update PHP tests for scheduling
Browse files Browse the repository at this point in the history
  • Loading branch information
cmgriffing committed Feb 24, 2024
1 parent 3b7e782 commit dd96d19
Show file tree
Hide file tree
Showing 3 changed files with 250 additions and 81 deletions.
7 changes: 4 additions & 3 deletions clients/php/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,10 @@
},
"scripts": {
"test": [
"composer install",
"ls -a",
"composer exec phpunit -- --coverage-text --coverage-filter src/*.php --verbose --bootstrap vendor/autoload.php tests/*.php"
"./vendor/bin/phpunit --verbose --bootstrap vendor/autoload.php tests/*.php"
],
"coverage": [
"composer exec phpunit -- --coverage-text --coverage-filter src/**/*.php --verbose --bootstrap vendor/autoload.php tests/*.php"
]
}
}
105 changes: 46 additions & 59 deletions clients/php/src/Scheduler.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,89 +47,76 @@ static function isScheduleActiveWithNow(Schedule $schedule, string $scheduleType
return false;
}

$startTime = Carbon::createFromTimestampMsUTC($schedule->startTime);
$endTime = Carbon::createFromTimestampMsUTC($schedule->endTime);

return Scheduler::isScheduleTimeActive($schedule->timeType, $now, $startDate, $startTime, $endDate, $endTime);
return Scheduler::isScheduleTimeActive($schedule->timeType, $now, $startDate, $schedule->startTime, $endDate, $schedule->endTime);

default:
return false;
}
}

private static function isScheduleTimeActive(string $timeType, Carbon $now, Carbon $startDate, Carbon $startTime, Carbon $endDate, Carbon $endTime): bool
private static function isScheduleTimeActive(string $timeType, Carbon $now, Carbon $startDate, int $startTime, Carbon $endDate, int $endTime): bool
{
$nowMillis = $now->timestamp * 1000;

switch ($timeType) {

case ScheduleTimeType::NONE:
return true;

case ScheduleTimeType::START_END:

$startDateTime = $startDate->clone();
$startDateTime
->setHours($startTime->hour)
->setMinutes($startTime->minute)
->setSeconds($startTime->second)
->setMicroseconds($startTime->microsecond);
$startOfStartDate = $startDate->clone();
$startOfStartDate->setHours(0);
$startOfStartDate->setMinutes(0);
$startOfStartDate->setSeconds(0);
$startOfStartDate->setMicroseconds(0);

$startOfStartDateMillis = $startOfStartDate->timestamp * 1000;

$startOfEndDate = $endDate->clone();
$startOfEndDate->setHours(0);
$startOfEndDate->setMinutes(0);
$startOfEndDate->setSeconds(0);
$startOfEndDate->setMicroseconds(0);

$startOfEndDateMillis = $startOfEndDate->timestamp * 1000;

$endDateTime = $endDate->clone();
$endDateTime
->setHours($endTime->hour)
->setMinutes($endTime->minute)
->setSeconds($endTime->second)
->setMicroseconds($endTime->microsecond);
$startDateTimestampWithStartTime = $startOfStartDateMillis + $startTime;
$endDateTimestampWithEndTime = $startOfEndDateMillis + $endTime;

return $now->betweenIncluded($startDateTime, $endDateTime);
return $startDateTimestampWithStartTime <= $nowMillis && $nowMillis <= $endDateTimestampWithEndTime;

case ScheduleTimeType::DAILY:

$zeroDay = Carbon::createFromTimestampMsUTC(0);
$nowTimestamp = $now->timestamp * 1000;

$todayZeroTimestamp = Carbon::create(
$now->year,
$now->month,
$now->day,
0,
0,
0,
"UTC"
)->timestamp * 1000;

$zeroedStartTimestamp = Carbon::create(
$zeroDay->year,
$zeroDay->month,
$zeroDay->day,
$startTime->hour,
$startTime->minute,
$startTime->second,
"UTC"
)->timestamp * 1000;


$zeroedEndDateTime = Carbon::create(
$zeroDay->year,
$zeroDay->month,
$zeroDay->day,
$endTime->hour,
$endTime->minute,
$endTime->second,
"UTC"
);

$zeroedEndTimestamp = $zeroedEndDateTime->timestamp * 1000;

$startTimestamp = $todayZeroTimestamp + $zeroedStartTimestamp;
$endTimestamp = $todayZeroTimestamp + $zeroedEndTimestamp;

if ($zeroedStartTimestamp > $zeroedEndTimestamp) {
return $startTimestamp <= $nowTimestamp || $nowTimestamp <= $endTimestamp;

$zeroDayWithNow = $zeroDay->clone()
->setHours($now->hour)
->setMinutes($now->minute)
->setSeconds($now->second)
->setMilliseconds($now->millisecond);

$zeroDayWithNowTimestamp = $zeroDayWithNow->timestamp * 1000;

if ($startTime > $endTime) {
return $startTime <= $zeroDayWithNowTimestamp || $zeroDayWithNowTimestamp <= $endTime;
} else {
return $startTimestamp <= $nowTimestamp && $nowTimestamp <= $endTimestamp;
return $startTime <= $zeroDayWithNowTimestamp && $zeroDayWithNowTimestamp <= $endTime;
}


// if schedule.start_time > schedule.end_time {
// Some(
// schedule.start_time <= zero_day_with_now_time
// || schedule.end_time >= zero_day_with_now_time,
// )
// } else {
// Some(
// schedule.start_time <= zero_day_with_now_time
// && schedule.end_time >= zero_day_with_now_time,
// )
// }

default:
return false;
}
Expand Down
Loading

0 comments on commit dd96d19

Please sign in to comment.