diff --git a/app/classes/ReleaseInsights/Duration.php b/app/classes/ReleaseInsights/Duration.php index 713876a..74e1771 100644 --- a/app/classes/ReleaseInsights/Duration.php +++ b/app/classes/ReleaseInsights/Duration.php @@ -16,7 +16,8 @@ class Duration public function __construct( public readonly Datetime $start, - public readonly Datetime $end) + public readonly Datetime $end + ) { $this->wellness_days = include DATA . 'wellness_days.php'; } @@ -50,9 +51,22 @@ public function weeks(): float */ public function isWorkDay(DateTime $day): bool { - // We substract week-ends and wellness days. - return ! in_array($day->format('l'), ['Saturday','Sunday']) - && ! in_array($day->format('Y-m-d'), $this->wellness_days); + // We don't consider the current day as a working day + if ($day->format('Y-m-d') === (new DateTime())->format('Y-m-d')) { + return false; + } + + // We substract week-end days + if (in_array($day->format('l'), ['Saturday','Sunday'])) { + return false; + } + + // We substract wellness days + if (in_array($day->format('Y-m-d'), $this->wellness_days)) { + return false; + } + + return true; } /** @@ -66,13 +80,13 @@ public function workDays(): int $range = new DatePeriod(start: $this->start, end: $this->end, interval: new DateInterval('P1D')); foreach($range as $date){ + if ($this->isWorkDay($date)) { $count++; } } - // We substract 1 because we don't count the first day of the period - return $count - 1; + return $count; } /** diff --git a/tests/Unit/ReleaseInsights/DurationTest.php b/tests/Unit/ReleaseInsights/DurationTest.php index 77a223f..d140615 100644 --- a/tests/Unit/ReleaseInsights/DurationTest.php +++ b/tests/Unit/ReleaseInsights/DurationTest.php @@ -22,18 +22,22 @@ test('Duration->isWorkDay()', function () { $obj = new Duration(new DateTime('2023-11-01'), new DateTime('2023-12-01')); - expect($obj->isWorkDay(new DateTime('2023-11-01'))) + expect($obj->isWorkDay(new DateTime('2023-11-01'))) // Working day ->toBeTrue(); - expect($obj->isWorkDay(new DateTime('2023-11-11'))) + expect($obj->isWorkDay(new DateTime('2023-11-11'))) // Saturday ->toBeFalse(); - expect($obj->isWorkDay(new DateTime('2024-02-16'))) + expect($obj->isWorkDay(new DateTime('2023-11-12'))) // Sunday + ->toBeFalse(); + expect($obj->isWorkDay(new DateTime('2024-02-16'))) // Wellness day + ->toBeFalse(); + expect($obj->isWorkDay(new DateTime())) // Today ->toBeFalse(); }); test('Duration->workDays()', function () { $obj = new Duration(new DateTime('2023-11-01'), new DateTime('2023-12-01')); expect($obj->workDays()) - ->toBe(21); + ->toBe(22); }); test('Duration->report()', function () {