createUser(['ROLE_USER', 'ROLE_DOCTOR']); $doctor = new Doctor($doctorUser, 'دکتر عضو کلینیک بیمه'); $doctor->setMobileNumber($doctorUser->getMobileNumber()); $this->em->persist($doctor); $owner = $this->createUser(['ROLE_USER', 'ROLE_CLINIC']); $clinic = new Clinic($owner); $clinic->setName('کلینیک محدودهٔ بیمه'); $clinic->getDoctors()->add($doctor); $this->em->persist($clinic); $this->em->flush(); $this->em->persist(new UserActiveContext( $this->em->find(User::class, $owner->getId()), $clinic->getUuid(), 'clinic', )); $this->em->flush(); return [$owner, $doctor, $clinic]; } private function insurance(): Insurance { $insurance = new Insurance('بیمه محدوده ' . random_int(1000, 9999), InsuranceType::Basic); $this->em->persist($insurance); $this->em->flush(); return $insurance; } private function appointmentAtClinic(Doctor $doctor, Clinic $clinic, User $patient): Appointment { $appointment = $this->newAppointment($doctor, $patient, time() + 3600, time() + 5400, $clinic); $this->em->persist($appointment); $this->em->flush(); return $appointment; } public function testAContractStoredOnTheDoctorAppliesToTheirClinicAppointment(): void { [$owner, $doctor, $clinic] = $this->clinicWithMemberDoctor(); $insurance = $this->insurance(); $this->authJson('POST', '/api/v1/billing/tenant-insurances', $owner, [ 'doctor_uuid' => $doctor->getUuid(), 'insurance_id' => $insurance->getId(), 'coverage_percent' => 80, ]); self::assertSame(201, $this->responseCode()); $appointment = $this->appointmentAtClinic($doctor, $clinic, $this->createUser(['ROLE_USER'])); $this->authJson('PATCH', '/api/v1/appointment/' . $appointment->getUuid(), $owner, [ 'insurance_base_id' => $insurance->getId(), ]); self::assertSame(200, $this->responseCode(), 'قرارداد پزشک باید روی نوبت کلینیک اعمال شود'); } public function testTheClinicContractIsInheritedWhenTheDoctorHasNone(): void { [$owner, $doctor, $clinic] = $this->clinicWithMemberDoctor(); $insurance = $this->insurance(); // قرارداد روی خودِ کلینیک، بدون هیچ قراردادی برای پزشک. $this->authJson('POST', '/api/v1/billing/tenant-insurances', $owner, [ 'insurance_id' => $insurance->getId(), 'coverage_percent' => 60, ]); self::assertSame(201, $this->responseCode()); $appointment = $this->appointmentAtClinic($doctor, $clinic, $this->createUser(['ROLE_USER'])); $this->authJson('PATCH', '/api/v1/appointment/' . $appointment->getUuid(), $owner, [ 'insurance_base_id' => $insurance->getId(), ]); self::assertSame(200, $this->responseCode(), 'در نبود قرارداد پزشک، قرارداد کلینیک اعمال می‌شود'); } /** * همان قاعده در خواندن: مودالِ قطعی‌کردن با `inherit=1` می‌پرسد «برای این پزشک * چه چیزی اعمال می‌شود» و باید قرارداد پزشک را ببیند، نه فهرست خالیِ کلینیک. */ public function testInheritReadReturnsTheDoctorContractToTheClinicOwner(): void { [$owner, $doctor, ] = $this->clinicWithMemberDoctor(); $insurance = $this->insurance(); $this->authJson('POST', '/api/v1/billing/tenant-insurances', $owner, [ 'doctor_uuid' => $doctor->getUuid(), 'insurance_id' => $insurance->getId(), 'coverage_percent' => 80, ]); $body = $this->authJson( 'GET', '/api/v1/billing/tenant-insurances?doctor_uuid=' . $doctor->getUuid() . '&inherit=1', $owner, ); $ids = array_map(static fn (array $row): int => (int) $row['insurance_id'], $body['data']['data']); self::assertContains($insurance->getId(), $ids); } public function testTheVisitPriceOfTheDoctorWinsOverTheClinicOne(): void { [$owner, $doctor, ] = $this->clinicWithMemberDoctor(); $this->authJson('PUT', '/api/v1/insurance-pricing', $owner, ['free_visit_price_rials' => 1_000_000]); $this->authJson('PUT', '/api/v1/insurance-pricing', $owner, [ 'doctor_uuid' => $doctor->getUuid(), 'free_visit_price_rials' => 2_500_000, ]); $inherited = $this->authJson( 'GET', '/api/v1/insurance-pricing?doctor_uuid=' . $doctor->getUuid() . '&inherit=1', $owner, ); self::assertSame(2_500_000, $inherited['data']['free_visit_price_rials']); } public function testTheClinicVisitPriceIsInheritedWhenTheDoctorHasNone(): void { [$owner, $doctor, ] = $this->clinicWithMemberDoctor(); $this->authJson('PUT', '/api/v1/insurance-pricing', $owner, ['free_visit_price_rials' => 1_000_000]); $inherited = $this->authJson( 'GET', '/api/v1/insurance-pricing?doctor_uuid=' . $doctor->getUuid() . '&inherit=1', $owner, ); self::assertSame(1_000_000, $inherited['data']['free_visit_price_rials']); } /** بدون `inherit`، صفحهٔ تنظیمات باید ردیفِ خودِ پزشک را ببیند — حتی وقتی خالی است. */ public function testTheSettingsReadStaysOnTheDoctorWithoutTheInheritFlag(): void { [$owner, $doctor, ] = $this->clinicWithMemberDoctor(); $this->authJson('PUT', '/api/v1/insurance-pricing', $owner, ['free_visit_price_rials' => 1_000_000]); $own = $this->authJson('GET', '/api/v1/insurance-pricing?doctor_uuid=' . $doctor->getUuid(), $owner); self::assertSame(0, $own['data']['free_visit_price_rials']); } }