Files
clinicpro/tests/Insurance/TenantInsuranceCleanupTest.php
T
hamedandClaude Opus 4.8 4cf6873900 fix(db): purge insurance config when a doctor/clinic is deleted (H5)
tenant_insurances, entity_insurance_pricing and tenant_service_coverages
reference their owner through a polymorphic (entity_type, entity_id) pair, so no
database FK can cascade their cleanup. Hard-deleting a doctor (DoctorController)
or clinic (AdminApiController) left these rows orphaned.

Add TenantInsuranceCleanupService::purgeForEntity() and call it from both delete
paths — removes coverage (via owning tenant_insurance ids), then tenant
insurances, then pricing.

Residual (separate, lower-freq paths): deleting an insurance category or a
service_item still orphans rows that reference them by id — tracked under the
medium-tier soft-ref findings.

Regression: tests/Insurance/TenantInsuranceCleanupTest.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-28 19:16:10 +03:30

44 lines
1.7 KiB
PHP

<?php
namespace App\Tests\Insurance;
use App\Insurance\Entity\EntityInsurancePricing;
use App\Insurance\Entity\TenantInsurance;
use App\Insurance\Entity\TenantServiceCoverage;
use App\Insurance\Service\TenantInsuranceCleanupService;
use App\Tests\ApiTestCase;
/**
* Deleting a doctor/clinic must purge the insurance config it owns. entity_id is
* polymorphic, so this cleanup is enforced in the application, not by a DB FK.
*/
class TenantInsuranceCleanupTest extends ApiTestCase
{
public function testPurgeRemovesTenantInsurancePricingAndCoverage(): void
{
$entityType = TenantInsurance::TYPE_DOCTOR;
$entityId = random_int(1_000_000, 9_999_999); // a doctor id not shared with other tests
$tenant = new TenantInsurance($entityType, $entityId, 1);
$this->em->persist($tenant);
$this->em->flush();
$coverage = new TenantServiceCoverage($tenant->getId(), 1);
$this->em->persist($coverage);
$this->em->persist(new EntityInsurancePricing($entityType, $entityId, 1));
$this->em->flush();
static::getContainer()->get(TenantInsuranceCleanupService::class)
->purgeForEntity($entityType, $entityId);
$this->em->clear();
$this->assertCount(0, $this->em->getRepository(TenantInsurance::class)
->findBy(['entityType' => $entityType, 'entityId' => $entityId]));
$this->assertCount(0, $this->em->getRepository(EntityInsurancePricing::class)
->findBy(['entityType' => $entityType, 'entityId' => $entityId]));
$this->assertNull($this->em->getRepository(TenantServiceCoverage::class)
->findOneBy(['tenantInsuranceId' => $tenant->getId()]));
}
}