From c3a55320280ab3c55926f06716fba0bf356c3aa8 Mon Sep 17 00:00:00 2001 From: myrmidex Date: Thu, 20 Aug 2026 20:24:09 +0200 Subject: [PATCH] 60 - Make schedule test assertion counts deterministic --- phpstan-baseline.neon | 6 ---- tests/Unit/Schedule/ScheduleGeneratorTest.php | 29 ++++++++++--------- .../Services/ScheduleCalendarServiceTest.php | 17 ++++++----- 3 files changed, 25 insertions(+), 27 deletions(-) diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 6e558a6..9c0b924 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -1662,12 +1662,6 @@ parameters: count: 2 path: tests/Unit/Schedule/ScheduleGeneratorTest.php - - - message: '#^Parameter \#1 \$callback of method Illuminate\\Support\\Collection\\:\:reduce\(\) expects callable\(Illuminate\\Support\\Carbon\|null, Carbon\\Carbon, int\)\: Illuminate\\Support\\Carbon, Closure\(Illuminate\\Support\\Carbon\|null, Illuminate\\Support\\Carbon\)\: Illuminate\\Support\\Carbon given\.$#' - identifier: argument.type - count: 1 - path: tests/Unit/Schedule/ScheduleGeneratorTest.php - - message: '#^Access to an undefined property Illuminate\\Database\\Eloquent\\Model\:\:\$id\.$#' identifier: property.notFound diff --git a/tests/Unit/Schedule/ScheduleGeneratorTest.php b/tests/Unit/Schedule/ScheduleGeneratorTest.php index fa2d65d..771f59a 100644 --- a/tests/Unit/Schedule/ScheduleGeneratorTest.php +++ b/tests/Unit/Schedule/ScheduleGeneratorTest.php @@ -12,7 +12,6 @@ use App\WeekdaysEnum; use DishPlanner\Schedule\Services\ScheduleGenerator; use Illuminate\Foundation\Testing\RefreshDatabase; -use Illuminate\Support\Carbon; use Tests\TestCase; use Tests\Traits\HasPlanner; @@ -128,19 +127,23 @@ public function test_it_takes_minimum_recurrences_into_account(): void $this->assertTrue(Schedule::all()->isNotEmpty()); - Schedule::all() - ->filter(fn (Schedule $schedule) => $schedule->scheduledUserDishes()->first()->userDish->dish->id === $dishRecurring->id) + $recurringDates = Schedule::all() + ->filter(fn (Schedule $schedule) => $schedule->scheduledUserDishes + ->contains(fn ($scheduledUserDish) => $scheduledUserDish->userDish->dish->id === $dishRecurring->id) + ) ->map(fn (Schedule $schedule) => $schedule->date) - ->reduce(function (?Carbon $previousDate, Carbon $currentDate) use ($recurringMinimum) { - if (! is_null($previousDate)) { - $this->assertGreaterThanOrEqual( - $recurringMinimum, - $previousDate->diffInDays($currentDate), - 'Dates are not spaced properly' - ); - } + ->sort() + ->values(); - return $currentDate; - }); + $this->assertGreaterThan(1, $recurringDates->count(), 'Recurring dish was not scheduled often enough to verify spacing'); + + $gaps = $recurringDates + ->sliding(2) + ->map(fn ($pair) => (int) $pair->first()->diffInDays($pair->last())); + + $this->assertEmpty( + $gaps->reject(fn (int $gap) => $gap >= $recurringMinimum)->all(), + 'Dates are not spaced properly' + ); } } diff --git a/tests/Unit/Schedule/Services/ScheduleCalendarServiceTest.php b/tests/Unit/Schedule/Services/ScheduleCalendarServiceTest.php index 6c11d28..044decc 100644 --- a/tests/Unit/Schedule/Services/ScheduleCalendarServiceTest.php +++ b/tests/Unit/Schedule/Services/ScheduleCalendarServiceTest.php @@ -55,19 +55,20 @@ public function test_includes_correct_day_numbers(): void public function test_marks_today_correctly(): void { + $this->travelTo(Carbon::createFromDate(2026, 3, 15)->startOfDay()); + $planner = $this->planner; - $today = now(); - $calendarDays = $this->service->getCalendarDays($planner, $today->month, $today->year); + $calendarDays = $this->service->getCalendarDays($planner, 3, 2026); - $todayIndex = $today->day - 1; + $todayIndex = 14; $this->assertTrue($calendarDays[$todayIndex]['isToday']); - foreach ($calendarDays as $index => $day) { - if ($index !== $todayIndex && $day['day'] !== null) { - $this->assertFalse($day['isToday']); - } - } + $otherDaysMarkedToday = collect($calendarDays) + ->filter(fn (array $day, int $index) => $index !== $todayIndex && $day['day'] !== null) + ->filter(fn (array $day) => $day['isToday']); + + $this->assertCount(0, $otherDaysMarkedToday); } public function test_includes_scheduled_dishes(): void