Files
thatguygriffandClaude Fable 5 14f43232c9
CI / Tests (PHP 8.2) (pull_request) Successful in 1m15s
CI / Tests (PHP 8.1) (pull_request) Successful in 1m16s
CI / No Debug Code (pull_request) Successful in 2s
CI / Coding Standards (pull_request) Successful in 3m15s
CI / PHPStan (pull_request) Successful in 3m14s
CI / Tests (PHP 8.3) (pull_request) Successful in 2m37s
CI / Build Plugin Zip (pull_request) Skipped
Default lessons to a week view on the booking page and in wp-admin
The front-end booking calendar now opens in the Week view (anchored to the
week of the earliest open slot) with List still available. The Scheduler and
My Lessons admin pages gain a week calendar (usc_view/usc_week, bucketed via
a new generic WeekCalendar::bucket()) and open in it by default; the original
table remains as the List view since it carries the HST / e-transfer forms.

Closes #76

Co-Authored-By: Claude Fable 5 <[email protected]>
2026-07-22 10:13:04 -03:00

93 lines
3.9 KiB
PHP

<?php
declare(strict_types=1);
namespace Unsupervised\Schedular\Tests\Unit\Availability;
use Unsupervised\Schedular\Availability\AvailabilitySlot;
use Unsupervised\Schedular\Availability\WeekCalendar;
use Unsupervised\Schedular\Tests\Unit\TestCase;
class WeekCalendarTest extends TestCase
{
public function testWeekStartShiftsBackToConfiguredStartOfWeek(): void
{
// 2026-07-08 is a Wednesday; with Monday (1) starts, the week begins Jul 6.
self::assertSame('2026-07-06', WeekCalendar::weekStart('2026-07-08', 1, '2026-01-01'));
// With Sunday (0) starts, the same Wednesday's week begins Jul 5.
self::assertSame('2026-07-05', WeekCalendar::weekStart('2026-07-08', 0, '2026-01-01'));
}
public function testWeekStartIsIdempotentWhenAnchorIsAlreadyTheStart(): void
{
self::assertSame('2026-07-06', WeekCalendar::weekStart('2026-07-06', 1, '2026-01-01'));
}
public function testWeekStartFallsBackToTodayForInvalidInput(): void
{
// 2026-07-05 is a Sunday; with Monday starts, its week began Jun 29.
self::assertSame('2026-06-29', WeekCalendar::weekStart('', 1, '2026-07-05'));
self::assertSame('2026-06-29', WeekCalendar::weekStart('not-a-date', 1, '2026-07-05'));
self::assertSame('2026-06-29', WeekCalendar::weekStart('2026-13-40', 1, '2026-07-05'));
}
public function testDaysReturnsSevenBucketsWithSlotsOnTheirDates(): void
{
$monday = new AvailabilitySlot(5, '2026-07-06 09:00:00', '2026-07-06 10:00:00', 60, null, false, null, 1);
$mondayTwo = new AvailabilitySlot(5, '2026-07-06 10:00:00', '2026-07-06 11:00:00', 60, null, false, null, 2);
$thursday = new AvailabilitySlot(5, '2026-07-09 14:00:00', '2026-07-09 15:00:00', 60, null, false, null, 3);
$nextWeek = new AvailabilitySlot(5, '2026-07-13 09:00:00', '2026-07-13 10:00:00', 60, null, false, null, 4);
$days = WeekCalendar::days('2026-07-06', [$monday, $mondayTwo, $thursday, $nextWeek]);
self::assertCount(7, $days);
self::assertSame('2026-07-06', $days[0]['date']);
self::assertSame('2026-07-12', $days[6]['date']);
self::assertCount(2, $days[0]['slots']);
self::assertSame(3, $days[3]['slots'][0]->id);
self::assertSame([], $days[1]['slots']);
// A slot outside the week is not bucketed anywhere.
$ids = array_merge(...array_map(
static fn (array $day): array => array_map(static fn (AvailabilitySlot $s): ?int => $s->id, $day['slots']),
$days
));
self::assertNotContains(4, $ids);
}
public function testDaysCrossesMonthBoundary(): void
{
$days = WeekCalendar::days('2026-06-29', []);
self::assertSame('2026-06-29', $days[0]['date']);
self::assertSame('2026-07-05', $days[6]['date']);
}
public function testBucketGroupsItemsByExtractedDay(): void
{
$items = [
['day' => '2026-07-06', 'label' => 'a'],
['day' => '2026-07-06', 'label' => 'b'],
['day' => '2026-07-09', 'label' => 'c'],
['day' => '2026-07-13', 'label' => 'outside'],
['day' => '', 'label' => 'dayless'],
];
$days = WeekCalendar::bucket('2026-07-06', $items, static fn (array $i): string => $i['day']);
self::assertCount(7, $days);
self::assertSame('2026-07-06', $days[0]['date']);
self::assertSame('2026-07-12', $days[6]['date']);
self::assertSame(['a', 'b'], array_column($days[0]['items'], 'label'));
self::assertSame(['c'], array_column($days[3]['items'], 'label'));
self::assertSame([], $days[1]['items']);
// Items outside the week (or with no day) are not bucketed anywhere.
$labels = array_merge(...array_column($days, 'items'));
self::assertNotContains('outside', array_column($labels, 'label'));
self::assertNotContains('dayless', array_column($labels, 'label'));
}
}