Files
clinicpro/tests/Shared/TimeIntervalTest.php
T
hamedandClaude Opus 5 4d722830e3 feat(resource): calendar UI, holiday admin, backfill and interval algebra
Completes task 03. The resource calendar page edits weekly shifts, records leave and
maintenance, and previews two weeks of availability with a Persian reason for every
empty day — showing the raw server key ("outside_branch_hours") to a user would have
been a meaningless message. The preview is labelled raw on the page itself, because
booked appointments are not subtracted yet and mistaking it for bookable time leads
to overbooking.

The interval algebra moved to src/Shared/Time/TimeInterval.php with twelve unit
tests: tasks 05 and 06 need the same union/intersect/subtract, and a second
implementation is how two subtly different definitions of "overlap" get born. The
half-open [start, end) contract is what makes a shift ending at 13:00 and one
starting at 13:00 not overlap.

AvailabilityQueryCountTest locks the query count flat: one day and ninety days cost
exactly the same number of queries. Without it the first refactor can put a query
inside the day loop and a 90-day response quietly becomes hundreds of queries —
something only production would reveal.

app:resource:calendar:backfill derives shifts from existing WeeklySchedule sessions,
so the resources created in task 02 are not left with empty calendars. It skips any
resource a user has already configured, which is also what makes it idempotent. The
weekly schedule itself is untouched: this is a copy, not a migration.

Also added --replace to the holiday import. upsert keys on the date, so a row written
with a *wrong* date can never correct itself — re-running just creates the right row
beside the wrong one. That is exactly what happened after fixing the Jalali
conversion bug, and it was caught while capturing real responses for the docs.

Deferred with reasons recorded in the checklist: seasonal shift validity (two
nullable columns can be added later without backfill, so "needed from day one" does
not hold), and a Jalali date picker in the exception form.

1154 tests / 3229 assertions. phpstan at its 14-error baseline, none in touched
files. tsc clean, vitest 622 tests.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-07-30 18:28:48 +03:30

128 lines
4.4 KiB
PHP

<?php
namespace App\Tests\Shared;
use App\Shared\Time\TimeInterval;
use PHPUnit\Framework\TestCase;
/**
* جبرِ بازه — واحد و بدون دیتابیس.
*
* قرارداد همه‌جا نیم‌باز `[start, end)` است؛ همین باعث می‌شود «۱۳:۰۰ پایان» و
* «۱۳:۰۰ شروع» تداخل نداشته باشند.
*/
class TimeIntervalTest extends TestCase
{
/** @param list<array{int, int}> $pairs */
private function set(array $pairs): array
{
return array_map(static fn (array $p): TimeInterval => new TimeInterval($p[0], $p[1]), $pairs);
}
/** @return list<array{int, int}> */
private function flatten(array $intervals): array
{
return array_map(static fn (TimeInterval $i): array => [$i->start, $i->end], $intervals);
}
public function testEndMustBeAfterStart(): void
{
$this->expectException(\InvalidArgumentException::class);
new TimeInterval(600, 600);
}
public function testMergeJoinsOverlappingAndTouchingIntervals(): void
{
$merged = TimeInterval::mergeAll($this->set([[540, 780], [700, 900], [900, 960], [1200, 1260]]));
self::assertSame([[540, 960], [1200, 1260]], $this->flatten($merged));
}
public function testMergeSortsUnorderedInput(): void
{
$merged = TimeInterval::mergeAll($this->set([[1200, 1260], [540, 600]]));
self::assertSame([[540, 600], [1200, 1260]], $this->flatten($merged));
}
public function testIntersectKeepsOnlyTheCommonPart(): void
{
$result = TimeInterval::intersectAll(
$this->set([[540, 1020]]),
$this->set([[600, 720]]),
);
self::assertSame([[600, 720]], $this->flatten($result));
}
/** بازه‌های صرفاً چسبیده تقاطع ندارند — نیم‌باز بودن دقیقاً همین را می‌گوید. */
public function testTouchingIntervalsDoNotIntersect(): void
{
$result = TimeInterval::intersectAll($this->set([[540, 780]]), $this->set([[780, 900]]));
self::assertSame([], $result);
}
public function testIntersectOfDisjointSetsIsEmpty(): void
{
$result = TimeInterval::intersectAll($this->set([[540, 600]]), $this->set([[1080, 1200]]));
self::assertSame([], $result);
}
/** بریدن از وسط، دو تکه می‌سازد و بقیهٔ روز سرِ جایش می‌ماند. */
public function testSubtractSplitsAnIntervalInTwo(): void
{
$result = TimeInterval::subtractAll($this->set([[540, 1020]]), $this->set([[600, 720]]));
self::assertSame([[540, 600], [720, 1020]], $this->flatten($result));
}
public function testSubtractTrimsTheEdges(): void
{
self::assertSame(
[[600, 1020]],
$this->flatten(TimeInterval::subtractAll($this->set([[540, 1020]]), $this->set([[500, 600]]))),
);
self::assertSame(
[[540, 900]],
$this->flatten(TimeInterval::subtractAll($this->set([[540, 1020]]), $this->set([[900, 1100]]))),
);
}
public function testSubtractCanEmptyTheSet(): void
{
self::assertSame([], TimeInterval::subtractAll($this->set([[540, 1020]]), $this->set([[400, 1200]])));
}
/** دو مسدودکنندهٔ هم‌پوشان اول اتحاد می‌شوند، بعد کسر — نه کسر پشت‌سرهم. */
public function testOverlappingBlocksAreUnionedBeforeSubtracting(): void
{
$result = TimeInterval::subtractAll(
$this->set([[540, 1020]]),
$this->set([[600, 780], [720, 900]]),
);
self::assertSame([[540, 600], [900, 1020]], $this->flatten($result));
}
public function testSubtractingNothingReturnsTheInputMerged(): void
{
self::assertSame(
[[540, 1020]],
$this->flatten(TimeInterval::subtractAll($this->set([[540, 780], [780, 1020]]), [])),
);
}
/** تبدیل «دقیقه از نیمه‌شب» به timestamp — ضرب در ۶۰ باید صریح بماند. */
public function testMinutesToAbsoluteMultipliesBySixty(): void
{
$absolute = (new TimeInterval(540, 1020))->minutesToAbsolute(1_000_000);
self::assertSame(1_000_000 + 540 * 60, $absolute->start);
self::assertSame(1_000_000 + 1020 * 60, $absolute->end);
self::assertSame(480 * 60, $absolute->end - $absolute->start);
}
}