refactor(branch): remove the branch domain, keep the address

Branches and rooms are not part of the resource-first product: a room is a
resource like any other, and the only thing the branch pages still managed —
opening hours — duplicated the resource's own shift.

What could not go is the address. Every appointment carries address_id (75 of
75 rows), the public booking site reads /clinic-pro/doctor-address/{id}, and a
resource derives its tenant pair from the address it belongs to. So
DoctorAddress stays as an invisible anchor with no page and no menu entry, and
GET /api/v1/addresses replaces GET /api/v1/branches for the forms that still
need to say "where".

BranchResolver was likewise not a branch feature. doctor_addresses is a global
table, so TenantFilter does not cover it and eight callers across booking,
availability, pricing and the catalog went through this resolver to avoid
leaking another clinic's address. It moved to Doctor\Service\AddressResolver
rather than dying with the domain.

The availability engine loses one layer: a resource's real hours were the
branch hours intersected with its shift, and are now the shift alone. That is
the single behavioural change, and the three tests that asserted the old
contract are replaced by one that states the new one.

Rooms already had a resource row each; the migration drops only the bridge
back to `rooms`, and drops it before the table — that foreign key is ON DELETE
CASCADE and the other order would take the resources, and their appointments,
with it.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
hamed
2026-08-02 15:25:32 +03:30
co-authored by Claude Opus 5
parent 1c4f2a2451
commit dd284ec622
59 changed files with 566 additions and 3128 deletions
-144
View File
@@ -1,144 +0,0 @@
<?php
namespace App\Tests\Branch;
use App\Doctor\Entity\DoctorAddress;
/**
* دو ویژگی تازهٔ شعبه (`active` / `timezone`) و فهرست شعبه‌های محیط جاری.
*/
class BranchFieldsTest extends BranchTestCase
{
/** ردیف‌های موجود بدون backfill درست می‌شوند؛ هیچ رفتار فعلی عوض نمی‌شود. */
public function testExistingBranchGetsSafeDefaults(): void
{
[$user, , $address] = $this->doctorWithAddress();
self::assertTrue($address->isActive());
self::assertSame(DoctorAddress::DEFAULT_TIMEZONE, $address->getTimezone());
$body = $this->authJson('GET', '/api/v1/branches', $user);
self::assertSame(200, $this->responseCode());
self::assertTrue($body['data'][0]['active']);
self::assertSame('Asia/Tehran', $body['data'][0]['timezone']);
}
public function testListReportsWorkingHoursAndRoomCounts(): void
{
[$user, , $address] = $this->doctorWithAddress();
$before = $this->authJson('GET', '/api/v1/branches', $user);
self::assertFalse($before['data'][0]['working_hours_defined']);
self::assertSame(0, $before['data'][0]['rooms_count']);
$this->authJson('PUT', "/api/v1/branch/{$address->getUuid()}/working-hours", $user, [
'days' => [1 => [['start_minute' => 540, 'end_minute' => 780]]],
]);
$this->authJson('POST', '/api/v1/room', $user, [
'address_uuid' => $address->getUuid(),
'name' => 'اتاق ۱',
]);
$after = $this->authJson('GET', '/api/v1/branches', $user);
self::assertTrue($after['data'][0]['working_hours_defined']);
self::assertSame(1, $after['data'][0]['rooms_count']);
}
/** فقط اتاق فعال شمرده می‌شود — اتاق غیرفعال ظرفیت واقعی شعبه نیست. */
public function testInactiveRoomIsNotCounted(): void
{
[$user, , $address] = $this->doctorWithAddress();
$room = $this->authJson('POST', '/api/v1/room', $user, [
'address_uuid' => $address->getUuid(),
'name' => 'اتاق بسته',
]);
$this->authJson('PATCH', "/api/v1/room/{$room['data']['uuid']}", $user, ['active' => false]);
$body = $this->authJson('GET', '/api/v1/branches', $user);
self::assertSame(0, $body['data'][0]['rooms_count']);
}
public function testListShowsOnlyTheCurrentContextBranches(): void
{
[$doctorUser, , $doctorAddress] = $this->doctorWithAddress('مطب شخصی');
$this->clinicWithAddress('شعبهٔ کلینیک بیگانه');
$body = $this->authJson('GET', '/api/v1/branches', $doctorUser);
self::assertCount(1, $body['data']);
self::assertSame($doctorAddress->getUuid(), $body['data'][0]['uuid']);
}
public function testBranchIsDeactivated(): void
{
[$user, , $address] = $this->doctorWithAddress();
$body = $this->authJson('PATCH', "/api/v1/branch/{$address->getUuid()}", $user, ['active' => false]);
self::assertSame(200, $this->responseCode());
self::assertFalse($body['data']['active']);
}
public function testTimezoneIsUpdated(): void
{
[$user, , $address] = $this->doctorWithAddress();
$body = $this->authJson('PATCH', "/api/v1/branch/{$address->getUuid()}", $user, [
'timezone' => 'Asia/Dubai',
]);
self::assertSame(200, $this->responseCode());
self::assertSame('Asia/Dubai', $body['data']['timezone']);
}
/** با DateTimeZone::listIdentifiers سنجیده می‌شود، نه با regex. */
public function testUnknownTimezoneIsRejected(): void
{
[$user, , $address] = $this->doctorWithAddress();
$body = $this->authJson('PATCH', "/api/v1/branch/{$address->getUuid()}", $user, ['timezone' => 'Tehran']);
self::assertSame(422, $this->responseCode());
self::assertSame('timezone', $body['errors'][0]['field']);
}
public function testForeignBranchCannotBePatched(): void
{
[$doctorUser] = $this->doctorWithAddress();
[, , $foreignAddress] = $this->clinicWithAddress();
$this->authJson('PATCH', "/api/v1/branch/{$foreignAddress->getUuid()}", $doctorUser, ['active' => false]);
self::assertSame(404, $this->responseCode());
}
/** شمارش‌ها گروهی‌اند: تعداد کوئری‌ها با تعداد شعبه‌ها رشد نمی‌کند. */
public function testListQueryCountDoesNotGrowWithBranches(): void
{
// یک کرنل برای هر دو اندازه‌گیری، وگرنه reboot دادهٔ کوئری‌ها را می‌ریزد.
$this->client->disableReboot();
[$user, $doctor] = $this->doctorWithAddress();
$queriesForOne = $this->countQueries(
fn () => $this->authJson('GET', '/api/v1/branches', $user)
);
for ($i = 0; $i < 4; $i++) {
$extra = DoctorAddress::forDoctor($doctor);
$extra->setName("شعبهٔ $i");
$this->em->persist($extra);
}
$this->em->flush();
$queriesForFive = $this->countQueries(
fn () => $this->authJson('GET', '/api/v1/branches', $user)
);
self::assertCount(5, json_decode($this->client->getResponse()->getContent(), true)['data']);
self::assertSame($queriesForOne, $queriesForFive);
}
}
-50
View File
@@ -1,50 +0,0 @@
<?php
namespace App\Tests\Branch;
use App\Auth\Entity\User;
use App\Clinic\Entity\Clinic;
use App\Doctor\Entity\Doctor;
use App\Doctor\Entity\DoctorAddress;
use App\Tests\ApiTestCase;
/**
* فیکسچرهای مشترک دامنهٔ شعبه. «شعبه» همان DoctorAddress است، پس هر تست به یک آدرس
* از محیط جاری و یک آدرس از محیط بیگانه نیاز دارد تا مرز ۴۰۴ را واقعاً بسنجد.
*/
abstract class BranchTestCase extends ApiTestCase
{
/** @return array{0: User, 1: Doctor, 2: DoctorAddress} */
protected function doctorWithAddress(string $name = 'مطب مرکزی'): array
{
$user = $this->createUser(['ROLE_USER', 'ROLE_DOCTOR']);
$doctor = new Doctor($user, 'دکتر شعبه');
$doctor->setMobileNumber($user->getMobileNumber());
$this->em->persist($doctor);
$this->em->flush();
$address = DoctorAddress::forDoctor($doctor);
$address->setName($name);
$this->em->persist($address);
$this->em->flush();
return [$user, $doctor, $address];
}
/** @return array{0: User, 1: Clinic, 2: DoctorAddress} */
protected function clinicWithAddress(string $name = 'شعبهٔ کلینیک'): array
{
$user = $this->createUser(['ROLE_USER', 'ROLE_CLINIC']);
$clinic = new Clinic($user);
$clinic->setName('کلینیک تست شعبه');
$this->em->persist($clinic);
$this->em->flush();
$address = DoctorAddress::forClinic($clinic->getId());
$address->setName($name);
$this->em->persist($address);
$this->em->flush();
return [$user, $clinic, $address];
}
}
-172
View File
@@ -1,172 +0,0 @@
<?php
namespace App\Tests\Branch;
use App\Branch\Entity\Room;
class RoomCrudTest extends BranchTestCase
{
/** @param array<string, mixed> $body */
private function createRoom(\App\Auth\Entity\User $user, string $addressUuid, array $body = []): array
{
return $this->authJson('POST', '/api/v1/room', $user, $body + [
'address_uuid' => $addressUuid,
'name' => 'اتاق تزریق',
]);
}
public function testRoomIsCreatedWithTenantPairDerivedFromTheBranch(): void
{
[$clinicUser, $clinic, $address] = $this->clinicWithAddress();
$body = $this->createRoom($clinicUser, $address->getUuid(), ['capacity' => 3, 'floor' => '2']);
self::assertSame(201, $this->responseCode(), json_encode($body, JSON_UNESCAPED_UNICODE));
self::assertSame(3, $body['data']['capacity']);
self::assertSame('2', $body['data']['floor']);
self::assertSame($address->getUuid(), $body['data']['address_uuid']);
$room = $this->em->getRepository(Room::class)->findOneBy(['uuid' => $body['data']['uuid']]);
self::assertSame('clinic', $room->getEntityType());
self::assertSame($clinic->getId(), $room->getEntityId());
}
public function testPersonalBranchRoomBelongsToTheDoctor(): void
{
[$doctorUser, $doctor, $address] = $this->doctorWithAddress();
$body = $this->createRoom($doctorUser, $address->getUuid());
self::assertSame(201, $this->responseCode());
$room = $this->em->getRepository(Room::class)->findOneBy(['uuid' => $body['data']['uuid']]);
self::assertSame('doctor', $room->getEntityType());
self::assertSame($doctor->getId(), $room->getEntityId());
}
public function testCapacityDefaultsToOne(): void
{
[$user, , $address] = $this->doctorWithAddress();
$body = $this->createRoom($user, $address->getUuid());
self::assertSame(1, $body['data']['capacity']);
self::assertTrue($body['data']['active']);
}
public function testZeroCapacityIsRejected(): void
{
[$user, , $address] = $this->doctorWithAddress();
$body = $this->createRoom($user, $address->getUuid(), ['capacity' => 0]);
self::assertSame(422, $this->responseCode());
self::assertSame('capacity', $body['errors'][0]['field']);
}
public function testBlankNameIsRejected(): void
{
[$user, , $address] = $this->doctorWithAddress();
$body = $this->authJson('POST', '/api/v1/room', $user, [
'address_uuid' => $address->getUuid(),
'name' => ' ',
]);
self::assertSame(422, $this->responseCode());
self::assertSame('name', $body['errors'][0]['field']);
}
public function testMissingAddressUuidIsRejected(): void
{
[$user] = $this->doctorWithAddress();
$body = $this->authJson('POST', '/api/v1/room', $user, ['name' => 'اتاق']);
self::assertSame(422, $this->responseCode());
self::assertSame('address_uuid', $body['errors'][0]['field']);
}
/** جفت محیط از آدرس می‌آید، پس نمی‌شود اتاق را روی شعبهٔ محیط دیگر نشاند. */
public function testRoomCannotBeCreatedOnAForeignBranch(): void
{
[$doctorUser] = $this->doctorWithAddress();
[, , $foreignAddress] = $this->clinicWithAddress();
$this->createRoom($doctorUser, $foreignAddress->getUuid());
self::assertSame(404, $this->responseCode());
}
public function testRoomIsUpdated(): void
{
[$user, , $address] = $this->doctorWithAddress();
$created = $this->createRoom($user, $address->getUuid());
$body = $this->authJson('PATCH', "/api/v1/room/{$created['data']['uuid']}", $user, [
'name' => 'اتاق پانسمان',
'capacity' => 2,
'room_type' => 'پانسمان',
'active' => false,
]);
self::assertSame(200, $this->responseCode());
self::assertSame('اتاق پانسمان', $body['data']['name']);
self::assertSame(2, $body['data']['capacity']);
self::assertSame('پانسمان', $body['data']['room_type']);
self::assertFalse($body['data']['active']);
}
/** رشتهٔ خالی روی فیلد اختیاری یعنی «پاک کن»، نه ذخیرهٔ رشتهٔ خالی. */
public function testBlankOptionalFieldBecomesNull(): void
{
[$user, , $address] = $this->doctorWithAddress();
$created = $this->createRoom($user, $address->getUuid(), ['room_type' => 'تزریق']);
$body = $this->authJson('PATCH', "/api/v1/room/{$created['data']['uuid']}", $user, ['room_type' => '']);
self::assertNull($body['data']['room_type']);
}
public function testRoomIsDeleted(): void
{
[$user, , $address] = $this->doctorWithAddress();
$created = $this->createRoom($user, $address->getUuid());
$this->authJson('DELETE', "/api/v1/room/{$created['data']['uuid']}", $user);
self::assertSame(200, $this->responseCode());
$this->authJson('PATCH', "/api/v1/room/{$created['data']['uuid']}", $user, ['name' => 'x']);
self::assertSame(404, $this->responseCode());
}
public function testForeignRoomIsNotFound(): void
{
[$clinicUser, , $clinicAddress] = $this->clinicWithAddress();
$created = $this->createRoom($clinicUser, $clinicAddress->getUuid());
[$doctorUser] = $this->doctorWithAddress();
$this->authJson('PATCH', "/api/v1/room/{$created['data']['uuid']}", $doctorUser, ['name' => 'دزدیده‌شده']);
self::assertSame(404, $this->responseCode());
$this->authJson('DELETE', "/api/v1/room/{$created['data']['uuid']}", $doctorUser);
self::assertSame(404, $this->responseCode());
}
public function testBranchRoomsAreListedForItsOwnerOnly(): void
{
[$user, , $address] = $this->doctorWithAddress();
$this->createRoom($user, $address->getUuid(), ['name' => 'اتاق ۱']);
$this->createRoom($user, $address->getUuid(), ['name' => 'اتاق ۲']);
$body = $this->authJson('GET', "/api/v1/branch/{$address->getUuid()}/rooms", $user);
self::assertSame(200, $this->responseCode());
self::assertCount(2, $body['data']);
[, , $foreignAddress] = $this->clinicWithAddress();
$this->authJson('GET', "/api/v1/branch/{$foreignAddress->getUuid()}/rooms", $user);
self::assertSame(404, $this->responseCode());
}
}
-229
View File
@@ -1,229 +0,0 @@
<?php
namespace App\Tests\Branch;
use App\Branch\Entity\BranchWorkingHours;
/**
* ساعت کاری هفتگی شعبه — GET/PUT روی /api/v1/branch/{addressUuid}/working-hours
*/
class WorkingHoursTest extends BranchTestCase
{
/** @param array<int, list<array{start_minute: int, end_minute: int}>> $days */
private function put(\App\Auth\Entity\User $user, string $addressUuid, array $days): array
{
return $this->authJson('PUT', "/api/v1/branch/$addressUuid/working-hours", $user, ['days' => $days]);
}
public function testEmptyBranchReportsSevenEmptyDaysAndUndefined(): void
{
[$user, , $address] = $this->doctorWithAddress();
$body = $this->authJson('GET', "/api/v1/branch/{$address->getUuid()}/working-hours", $user);
self::assertSame(200, $this->responseCode());
self::assertFalse($body['data']['defined'], 'شعبهٔ بدون ساعت باید «تعریف‌نشده» باشد، نه همیشه‌باز');
self::assertSame(range(0, 6), array_map('intval', array_keys($body['data']['days'])));
foreach ($body['data']['days'] as $ranges) {
self::assertSame([], $ranges);
}
}
public function testFullWeekIsStoredAndReadBackIdentically(): void
{
[$user, , $address] = $this->doctorWithAddress();
$days = [];
foreach (range(0, 6) as $day) {
$days[$day] = [
['start_minute' => 540, 'end_minute' => 780], // 09:00-13:00
['start_minute' => 960, 'end_minute' => 1200], // 16:00-20:00
];
}
$written = $this->put($user, $address->getUuid(), $days);
self::assertSame(200, $this->responseCode(), json_encode($written, JSON_UNESCAPED_UNICODE));
self::assertTrue($written['data']['defined']);
$read = $this->authJson('GET', "/api/v1/branch/{$address->getUuid()}/working-hours", $user);
self::assertSame($written['data']['days'], $read['data']['days']);
self::assertSame('09:00', $read['data']['days'][0][0]['start_time']);
self::assertSame('20:00', $read['data']['days'][0][1]['end_time']);
self::assertSame([0, 1], array_column($read['data']['days'][0], 'sequence'));
}
/** PUT قرارداد جایگزینی کامل دارد: آرایهٔ خالی یعنی شعبه بسته، نه «تغییری نده». */
public function testEmptyPayloadClosesTheBranch(): void
{
[$user, , $address] = $this->doctorWithAddress();
$this->put($user, $address->getUuid(), [3 => [['start_minute' => 600, 'end_minute' => 700]]]);
$body = $this->put($user, $address->getUuid(), []);
self::assertSame(200, $this->responseCode());
self::assertFalse($body['data']['defined']);
self::assertSame([], $body['data']['days'][3]);
}
public function testEndBeforeStartIsRejected(): void
{
[$user, , $address] = $this->doctorWithAddress();
$body = $this->put($user, $address->getUuid(), [0 => [['start_minute' => 800, 'end_minute' => 800]]]);
self::assertSame(422, $this->responseCode());
self::assertSame('end_minute', $body['errors'][0]['field']);
}
public function testOverlappingRangesInOneDayAreRejected(): void
{
[$user, , $address] = $this->doctorWithAddress();
$body = $this->put($user, $address->getUuid(), [2 => [
['start_minute' => 540, 'end_minute' => 780],
['start_minute' => 700, 'end_minute' => 900],
]]);
self::assertSame(422, $this->responseCode());
self::assertStringContainsString('هم‌پوشانی', $body['errors'][0]['message']);
}
/** بازهٔ چسبیده مجاز است: پایان یکی = شروع بعدی، هم‌پوشانی نیست. */
public function testTouchingRangesAreAccepted(): void
{
[$user, , $address] = $this->doctorWithAddress();
$this->put($user, $address->getUuid(), [2 => [
['start_minute' => 540, 'end_minute' => 780],
['start_minute' => 780, 'end_minute' => 900],
]]);
self::assertSame(200, $this->responseCode());
}
public function testAllDayRangeIsOneRow(): void
{
[$user, , $address] = $this->doctorWithAddress();
$body = $this->put($user, $address->getUuid(), [
5 => [['start_minute' => 0, 'end_minute' => BranchWorkingHours::MINUTES_IN_DAY]],
]);
self::assertSame(200, $this->responseCode());
self::assertCount(1, $body['data']['days'][5]);
self::assertSame('24:00', $body['data']['days'][5][0]['end_time']);
}
public function testMinuteBeyondOneDayIsRejected(): void
{
[$user, , $address] = $this->doctorWithAddress();
$this->put($user, $address->getUuid(), [1 => [['start_minute' => 0, 'end_minute' => 1441]]]);
self::assertSame(422, $this->responseCode());
}
public function testInvalidDayKeyIsRejected(): void
{
[$user, , $address] = $this->doctorWithAddress();
$body = $this->put($user, $address->getUuid(), [7 => [['start_minute' => 0, 'end_minute' => 60]]]);
self::assertSame(422, $this->responseCode());
self::assertSame('day_of_week', $body['errors'][0]['field']);
}
/**
* اتمی بودن: بازهٔ نامعتبر در روز ششم نباید روزهای درستِ قبل را پاک کند.
* بدون اعتبارسنجیِ کاملِ پیش از DELETE، این تست هفتهٔ ذخیره‌شده را خالی می‌بیند.
*/
public function testInvalidLaterDayLeavesTheStoredWeekUntouched(): void
{
[$user, , $address] = $this->doctorWithAddress();
$valid = [];
foreach (range(0, 6) as $day) {
$valid[$day] = [['start_minute' => 540, 'end_minute' => 780]];
}
$this->put($user, $address->getUuid(), $valid);
$broken = $valid;
$broken[5] = [['start_minute' => 900, 'end_minute' => 100]];
$this->put($user, $address->getUuid(), $broken);
self::assertSame(422, $this->responseCode());
$read = $this->authJson('GET', "/api/v1/branch/{$address->getUuid()}/working-hours", $user);
self::assertTrue($read['data']['defined']);
foreach (range(0, 6) as $day) {
self::assertCount(1, $read['data']['days'][$day], "روز $day نباید پاک شده باشد");
}
}
public function testMissingDaysFieldIsRejected(): void
{
[$user, , $address] = $this->doctorWithAddress();
$body = $this->authJson('PUT', "/api/v1/branch/{$address->getUuid()}/working-hours", $user, ['x' => 1]);
self::assertSame(422, $this->responseCode());
self::assertSame('days', $body['errors'][0]['field']);
}
/** آدرس محیط دیگر: ۴۰۴ نه ۴۰۳ — وجود دادهٔ محیط بیگانه لو نمی‌رود. */
public function testForeignBranchIsNotFound(): void
{
[$doctorUser] = $this->doctorWithAddress();
[, , $foreignAddress] = $this->clinicWithAddress();
$this->authJson('GET', "/api/v1/branch/{$foreignAddress->getUuid()}/working-hours", $doctorUser);
self::assertSame(404, $this->responseCode());
}
public function testForeignBranchCannotBeWritten(): void
{
[$doctorUser] = $this->doctorWithAddress();
[, , $foreignAddress] = $this->clinicWithAddress();
$this->put($doctorUser, $foreignAddress->getUuid(), [0 => [['start_minute' => 0, 'end_minute' => 60]]]);
self::assertSame(404, $this->responseCode());
}
/**
* `days` باید **شیء** JSON با کلیدهای "0".."6" باشد، نه آرایه.
* کلیدهای ۰..۶ پشت‌سرهم‌اند و json_encode بی‌مراقبت آرایه می‌ساخت؛ کلاینت
* `days["0"]` هر دو را می‌خواند، ولی شکل پاسخ با جا افتادن یک روز عوض می‌شد.
*/
public function testDaysIsAJsonObjectNotAnArray(): void
{
[$user, , $address] = $this->doctorWithAddress();
$this->put($user, $address->getUuid(), [0 => [['start_minute' => 540, 'end_minute' => 780]]]);
foreach (['PUT', 'GET'] as $method) {
if ($method === 'GET') {
$this->authJson('GET', "/api/v1/branch/{$address->getUuid()}/working-hours", $user);
}
$raw = json_decode($this->client->getResponse()->getContent(), false);
self::assertInstanceOf(\stdClass::class, $raw->data->days, "$method: days باید شیء باشد");
// get_object_vars نامِ عددیِ ویژگی‌ها را به int برمی‌گرداند؛ آنچه مهم است
// stdClass بودن بالا سنجیده شد. اینجا فقط کامل بودن هفت روز.
self::assertSame(range(0, 6), array_keys(get_object_vars($raw->data->days)));
}
}
public function testClinicOwnerManagesItsOwnBranch(): void
{
[$clinicUser, , $address] = $this->clinicWithAddress();
$body = $this->put($clinicUser, $address->getUuid(), [
0 => [['start_minute' => 480, 'end_minute' => 1020]],
]);
self::assertSame(200, $this->responseCode(), json_encode($body, JSON_UNESCAPED_UNICODE));
self::assertSame('08:00', $body['data']['days'][0][0]['start_time']);
}
}
+5 -25
View File
@@ -57,30 +57,12 @@ class BackfillResourceTest extends ResourceTestCase
public function testDryRunWritesNothing(): void
{
[, , $address] = $this->clinicWithAddress();
$room = $this->room($address, 'اتاق دراِی‌ران');
$staff = $this->staff($address, 'اپراتور دراِی‌ران');
$output = $this->runBackfill(false, $this->pairOf($address));
self::assertStringContainsString('Dry run', $output);
self::assertNull($this->resources()->findForSubject($room));
}
public function testRoomBecomesAResourceCarryingItsCapacity(): void
{
[, , $address] = $this->clinicWithAddress();
$room = $this->room($address, 'اتاق تزریق سه‌تخته', 3);
$this->runBackfill(true, $this->pairOf($address));
$this->em->clear();
$resource = $this->resources()->findForSubject(
$this->em->getRepository(\App\Branch\Entity\Room::class)->find($room->getId())
);
self::assertNotNull($resource);
self::assertSame(3, $resource->getCapacity(), 'اتاق سه‌تخته یک منبع با ظرفیت ۳ است، نه سه منبع');
self::assertSame(ResourceType::CODE_ROOM, $resource->getType()->getCode());
self::assertTrue($resource->getType()->isSystem());
self::assertNull($this->resources()->findForSubject($staff));
}
public function testStaffOfASingleBranchEnvironmentIsBridged(): void
@@ -182,7 +164,6 @@ class BackfillResourceTest extends ResourceTestCase
public function testRunningTwiceCreatesNothingNew(): void
{
[, , $address] = $this->clinicWithAddress();
$this->room($address, 'اتاق تکراری');
$this->staff($address, 'اپراتور تکراری');
$this->runBackfill(true, $this->pairOf($address));
@@ -190,7 +171,6 @@ class BackfillResourceTest extends ResourceTestCase
$secondOutput = $this->runBackfill(true, $this->pairOf($address));
self::assertStringContainsString('اتاق: 0', $secondOutput);
self::assertStringContainsString('پرسنل: 0', $secondOutput);
}
@@ -291,15 +271,15 @@ class BackfillResourceTest extends ResourceTestCase
public function testASecondBridgeIsRefused(): void
{
[, , $address] = $this->clinicWithAddress();
$room = $this->room($address, 'اتاق دوپل');
$staff = $this->staff($address, 'پرسنل دوپل');
$other = $this->staff($address, 'پرسنل دوپل دوم');
$type = $this->resourceType($address, 'mixed', 'ترکیبی');
$resource = new \App\Resource\Entity\ClinicResource($address, $type, 'منبع دوپل');
$resource->linkTo($room);
$resource->linkTo($staff);
$this->expectException(\InvalidArgumentException::class);
$resource->linkTo($staff);
$resource->linkTo($other);
}
/**
+8 -53
View File
@@ -244,42 +244,14 @@ class ResourceAvailabilityTest extends ResourceTestCase
self::assertContains('tenant_holiday', $days[0]['reasons']);
}
/** شیفت بیرون از ساعت شعبه رد نمی‌شود — تقاطع گرفته می‌شود. */
public function testShiftIsIntersectedWithBranchHours(): void
{
[$user, $address, $uuid] = $this->resourceWithShifts([0]);
// شعبه فقط ۱۰ تا ۱۲ باز است؛ شیفت منبع ۹ تا ۱۷.
$this->authJson('PUT', "/api/v1/branch/{$address->getUuid()}/working-hours", $user, [
'days' => [0 => [['start_minute' => 600, 'end_minute' => 720]]],
]);
self::assertSame(200, $this->responseCode());
$saturday = $this->nextSaturday();
$days = $this->availability($user, $uuid, $saturday, $saturday);
self::assertCount(1, $days[0]['intervals']);
self::assertSame(120, $days[0]['total_minutes'], 'تقاطع ۱۰ تا ۱۲');
}
/** تقاطع خالی → روز خالی، با دلیل صریح تا از یک باگ قابل تشخیص باشد. */
public function testEmptyIntersectionReportsOutsideBranchHours(): void
{
[$user, $address, $uuid] = $this->resourceWithShifts([0]);
$this->authJson('PUT', "/api/v1/branch/{$address->getUuid()}/working-hours", $user, [
'days' => [0 => [['start_minute' => 1080, 'end_minute' => 1200]]], // 18:00-20:00
]);
$saturday = $this->nextSaturday();
$days = $this->availability($user, $uuid, $saturday, $saturday);
self::assertSame([], $days[0]['intervals']);
self::assertContains('outside_branch_hours', $days[0]['reasons']);
}
/** شعبهٔ بدون ساعت کاری = «تعریف‌نشده»، پس شیفت منبع بی‌قید اعمال می‌شود. */
public function testBranchWithoutHoursDoesNotConstrainTheShift(): void
/**
* شیفت خودِ منبع تنها مرجع ساعت کاری است.
*
* تا پیش از حذف دامنهٔ شعبه، این شیفت با ساعت کاری شعبه تقاطع می‌گرفت و سه تست
* جداگانه آن لایه را می‌سنجیدند. با رفتن شعبه، لایه هم رفت و «۴۸۰ دقیقه» یعنی
* دقیقاً همان چیزی که در تقویم منبع نوشته شده.
*/
public function testTheResourceShiftAloneDecidesTheDay(): void
{
[$user, , $uuid] = $this->resourceWithShifts([0]);
@@ -289,23 +261,6 @@ class ResourceAvailabilityTest extends ResourceTestCase
self::assertSame(480, $days[0]['total_minutes']);
}
/** روزی که شعبه بسته است با «تعریف‌نشده» یکی نیست. */
public function testBranchClosedDayIsDistinctFromUndefined(): void
{
[$user, $address, $uuid] = $this->resourceWithShifts([0, 1]);
$this->authJson('PUT', "/api/v1/branch/{$address->getUuid()}/working-hours", $user, [
'days' => [0 => [['start_minute' => 540, 'end_minute' => 1020]]],
]);
$saturday = $this->nextSaturday();
$days = $this->availability($user, $uuid, $saturday, $this->dayAfter($saturday, 1));
self::assertNotSame([], $days[0]['intervals']);
self::assertSame([], $days[1]['intervals']);
self::assertContains('branch_closed', $days[1]['reasons']);
}
public function testInactiveResourceHasNoAvailability(): void
{
[$user, , $uuid] = $this->resourceWithShifts();
-11
View File
@@ -3,7 +3,6 @@
namespace App\Tests\Resource;
use App\Auth\Entity\User;
use App\Branch\Entity\Room;
use App\Clinic\Entity\Clinic;
use App\Doctor\Entity\Doctor;
use App\Doctor\Entity\DoctorAddress;
@@ -80,16 +79,6 @@ abstract class ResourceTestCase extends ApiTestCase
return $staff;
}
protected function room(DoctorAddress $address, string $name = 'اتاق تزریق', int $capacity = 1): Room
{
$room = new Room($address, $name);
$room->setCapacity($capacity);
$this->em->persist($room);
$this->em->flush();
return $room;
}
/** @param array<string, mixed> $body */
protected function createResource(User $user, DoctorAddress $address, ResourceType $type, array $body = []): array
{