60 - Make schedule test assertion counts deterministic

This commit is contained in:
myrmidex 2026-08-20 20:24:09 +02:00
parent 7f7a9450bf
commit c3a5532028
3 changed files with 25 additions and 27 deletions

View file

@ -1662,12 +1662,6 @@ parameters:
count: 2 count: 2
path: tests/Unit/Schedule/ScheduleGeneratorTest.php path: tests/Unit/Schedule/ScheduleGeneratorTest.php
-
message: '#^Parameter \#1 \$callback of method Illuminate\\Support\\Collection\<int,Carbon\\Carbon\>\:\: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\.$#' message: '#^Access to an undefined property Illuminate\\Database\\Eloquent\\Model\:\:\$id\.$#'
identifier: property.notFound identifier: property.notFound

View file

@ -12,7 +12,6 @@
use App\WeekdaysEnum; use App\WeekdaysEnum;
use DishPlanner\Schedule\Services\ScheduleGenerator; use DishPlanner\Schedule\Services\ScheduleGenerator;
use Illuminate\Foundation\Testing\RefreshDatabase; use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Carbon;
use Tests\TestCase; use Tests\TestCase;
use Tests\Traits\HasPlanner; use Tests\Traits\HasPlanner;
@ -128,19 +127,23 @@ public function test_it_takes_minimum_recurrences_into_account(): void
$this->assertTrue(Schedule::all()->isNotEmpty()); $this->assertTrue(Schedule::all()->isNotEmpty());
Schedule::all() $recurringDates = Schedule::all()
->filter(fn (Schedule $schedule) => $schedule->scheduledUserDishes()->first()->userDish->dish->id === $dishRecurring->id) ->filter(fn (Schedule $schedule) => $schedule->scheduledUserDishes
->contains(fn ($scheduledUserDish) => $scheduledUserDish->userDish->dish->id === $dishRecurring->id)
)
->map(fn (Schedule $schedule) => $schedule->date) ->map(fn (Schedule $schedule) => $schedule->date)
->reduce(function (?Carbon $previousDate, Carbon $currentDate) use ($recurringMinimum) { ->sort()
if (! is_null($previousDate)) { ->values();
$this->assertGreaterThanOrEqual(
$recurringMinimum,
$previousDate->diffInDays($currentDate),
'Dates are not spaced properly'
);
}
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'
);
} }
} }

View file

@ -55,19 +55,20 @@ public function test_includes_correct_day_numbers(): void
public function test_marks_today_correctly(): void public function test_marks_today_correctly(): void
{ {
$this->travelTo(Carbon::createFromDate(2026, 3, 15)->startOfDay());
$planner = $this->planner; $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']); $this->assertTrue($calendarDays[$todayIndex]['isToday']);
foreach ($calendarDays as $index => $day) { $otherDaysMarkedToday = collect($calendarDays)
if ($index !== $todayIndex && $day['day'] !== null) { ->filter(fn (array $day, int $index) => $index !== $todayIndex && $day['day'] !== null)
$this->assertFalse($day['isToday']); ->filter(fn (array $day) => $day['isToday']);
}
} $this->assertCount(0, $otherDaysMarkedToday);
} }
public function test_includes_scheduled_dishes(): void public function test_includes_scheduled_dishes(): void