Files
clinicpro/tests/Representation/JalaliDateServiceTest.php
hamedandClaude Opus 5 1fdfdf9e48 feat(resource): resource calendars, exceptions and national holidays
Section 9 of the design document builds free time by subtracting seven layers.
Four existed and all of them hung off the doctor. This adds the missing ones and
puts them on the resource:

  branch hours ∩ resource shifts − national holidays − resource exceptions

Booked appointments and holds are deliberately NOT subtracted here — those are
tasks 06/07, as is intersecting several resources. The method is called
rawAvailability() so nobody mistakes the output for bookable time. Nothing in this
change calls SlotCalculatorService; the existing slot path stays frozen.

Four types of exception (leave, absence, maintenance, ad-hoc closure) share one
table because all four are "an interval subtracted from a resource's calendar";
splitting them would mean four queries per availability lookup instead of one.
Holiday overrides work in both directions: a clinic that opens on a public holiday,
and a clinic that closes on an ordinary day.

Every empty day carries a reason (national_holiday, no_shift, branch_closed,
outside_branch_hours, exception, …). Without it an empty response is
indistinguishable from a bug and the first person debugging has to read four tables
by hand.

Three real defects found on the way:

JalaliDateService.gregorianToJalali() was wrong — it returned [3006, 7, 3] for
2026-07-30 instead of [1405, 5, 8], roughly 1601 years off. jalaliYear(),
jalaliMonth(), jalaliMonthRange() and jalaliYearRange() all inherit that, so the
representation reports built on them have been filtering by nonsense ranges. The
class's own formatDateTime() was already correct because it used IntlDateFormatter,
so both conversions now go through the same mechanism, and JalaliDateServiceTest
pins Nowruz and the 6/31→7/1 boundary. There were no tests before, which is why
nobody noticed.

TimeInterval added a seconds-based midnight to a minutes-based interval, turning an
eight-hour shift into eight seconds. The conversion is now an explicitly named
minutesToAbsolute() so the unit change cannot happen silently again.

HolidayService.upsertNational() persisted but left flushing to the caller. Every
HTTP request reboots the kernel, so the caller often held a different
EntityManager: persist landed on one, flush on the other, and nothing was written
with no error at all. The write is now self-contained.

119 tests across tests/Resource, tests/Branch and tests/Representation. phpstan
clean on both touched domains.

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

78 lines
2.9 KiB
PHP

<?php
namespace App\Tests\Representation;
use App\Representation\Service\JalaliDateService;
use PHPUnit\Framework\TestCase;
/**
* تبدیل شمسی ↔ میلادی.
*
* پیاده‌سازی دستیِ قبلی غلط بود و کسی متوجه نشده بود چون هیچ تستی نداشت:
* `gregorianToJalali(2026, 7, 30)` مقدار `[3006, 7, 3]` می‌داد. این تست همان
* نقطه‌هایی را قفل می‌کند که آن باگ را لو دادند.
*/
class JalaliDateServiceTest extends TestCase
{
private JalaliDateService $service;
protected function setUp(): void
{
$this->service = new JalaliDateService();
}
/** نوروز لنگرِ همه‌چیز است: ۱ فروردین ۱۴۰۵ = ۲۱ مارس ۲۰۲۶. */
public function testNowruzAnchor(): void
{
self::assertSame([2026, 3, 21], $this->service->jalaliToGregorian(1405, 1, 1));
self::assertSame([1405, 1, 1], $this->service->gregorianToJalali(2026, 3, 21));
}
public function testRoundTripAcrossTheYear(): void
{
foreach ([[1405, 1, 1], [1405, 5, 8], [1405, 6, 31], [1405, 7, 1], [1405, 12, 29]] as [$jy, $jm, $jd]) {
[$gy, $gm, $gd] = $this->service->jalaliToGregorian($jy, $jm, $jd);
self::assertSame(
[$jy, $jm, $jd],
$this->service->gregorianToJalali($gy, $gm, $gd),
sprintf('رفت‌وبرگشت %d/%d/%d', $jy, $jm, $jd),
);
}
}
/** مهر اول نیم‌سال دوم است و ماه‌هایش ۳۰ روزه — مرز ۶/۳۱ به ۷/۱. */
public function testSixthMonthHasThirtyOneDaysAndSeventhStartsNextDay(): void
{
[$gy, $gm, $gd] = $this->service->jalaliToGregorian(1405, 6, 31);
$end = (new \DateTimeImmutable(sprintf('%04d-%02d-%02d', $gy, $gm, $gd)))->modify('+1 day');
self::assertSame(
[1405, 7, 1],
$this->service->gregorianToJalali(
(int) $end->format('Y'),
(int) $end->format('n'),
(int) $end->format('j'),
),
);
}
public function testJalaliYearOfAKnownTimestamp(): void
{
$timestamp = (new \DateTimeImmutable('2026-07-30 12:00:00', new \DateTimeZone('Asia/Tehran')))->getTimestamp();
self::assertSame(1405, $this->service->jalaliYear($timestamp));
self::assertSame(5, $this->service->jalaliMonth($timestamp));
}
/** بازهٔ سال باید نوروز تا آخر اسفند باشد، نه چیزی در قرن سی‌ویکم. */
public function testYearRangeStartsAtNowruz(): void
{
[$start, $end] = $this->service->jalaliYearRange(1405);
self::assertSame('2026-03-21', date('Y-m-d', $start));
self::assertGreaterThan($start, $end);
self::assertSame(1405, $this->service->jalaliYear($start));
}
}