Files
clinicpro/tests/Appointment/AppointmentExpiryServiceTest.php
T
hamedandClaude Opus 5 c9d4348c46 feat(tenant): mark the financial tables with their owning environment
Phase 6 of the tenant series. GlobalTables::DEFERRED is now empty and the
coverage test asserts it stays that way.

payments carries the (entity_type, entity_id) pair and belongs to the
receiving side, never the payer: an appointment payment takes the
appointment's environment, a subscription takes the environment its buyer
owns, and an SMS wallet top-up takes the wallet's. The patient never chose
an environment, so TenantFilter stays off for them and they still see their
own payment.

Three corrections to the analysis the phase was planned on, each backed by
the code or the data rather than the plan:

- A third payment type exists. Payment::TYPE_SMS_WALLET is created in
  SmsWalletController and already carries its environment in the metadata;
  without assigning it the write would fail at flush.
- clinic_subscriptions has no user_id, and its trial rows carry no payment,
  so it cannot drive the subscription backfill. The environment is derived
  the way handleSubscriptionActivation derives it — and that method now
  reads the pair off the payment instead of re-deriving it, so a payment and
  the subscription it buys can no longer land on different environments.
- WalletTransaction is not a child of Payment. payment_id is nullable and
  none of the four creation sites set it; the wallet is a person's, with a
  running balance per user. It and Settlement, which withdraws from that same
  wallet, are global with a recorded reason instead.

bank_accounts and pos_devices move from the registering user to the
environment. Their pair is deliberately nullable: nothing in the existing
data says which of a multi-environment owner's cards belongs where, and
guessing would point real money at the wrong account. Ambiguous rows stay
unassigned and the migration reports how many. The cost is that such a row
is invisible in every environment, so the owner reaches it through a
user-scoped lookup that runs outside the filter, and assigns it with
PATCH .../{uuid}/environment. The admin panel marks those rows and offers
the assignment.

Tests: 896 backend (+11), 570 frontend (+4). PHPStan unchanged at its 17
pre-existing errors.

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

83 lines
3.3 KiB
PHP

<?php
namespace App\Tests\Appointment;
use App\Appointment\Entity\Appointment;
use App\Appointment\Service\AppointmentExpiryService;
use App\Doctor\Entity\Doctor;
use App\Payment\Entity\Payment;
use App\Tests\ApiTestCase;
/**
* Covers AppointmentExpiryService: stale pending bookings are expired and their
* pending payments cancelled. Also guards the N+1 fix (batch payment fetch) by
* exercising several appointments at once.
*/
class AppointmentExpiryServiceTest extends ApiTestCase
{
public function testExpiresStaleAndCancelsPendingPayments(): void
{
$owner = $this->createUser(['ROLE_DOCTOR']);
$doctor = new Doctor($owner, 'دکتر تست');
$this->em->persist($doctor);
$past = time() - 3600;
$appointments = [];
for ($i = 0; $i < 5; $i++) {
$patient = $this->createUser(['ROLE_USER']);
// distinct past slots — one live booking per (doctor, slot)
$slotStart = $past - $i * 1000;
$appt = $this->newAppointment($doctor, $patient, $slotStart, $slotStart + 900);
// مثل مسیر واقعیِ رزرو آنلاین: نگه‌داشتِ موقت تا پرداخت درگاه.
$appt->markPendingWithTtl(-1);
$this->em->persist($appt);
$payment = new Payment($patient, 100_000, 'mellat', 'appointment');
$payment->setAppointment($appt);
$this->stampTenant($payment);
$this->em->persist($payment);
$appointments[] = [$appt, $payment];
}
$this->em->flush();
$service = static::getContainer()->get(AppointmentExpiryService::class);
$count = $service->expireStale();
// At least our 5 — db_test is shared and may hold other stale pendings
// from earlier tests/runs; the per-row checks below verify our own 5.
$this->assertGreaterThanOrEqual(5, $count);
$this->em->clear();
foreach ($appointments as [$appt, $payment]) {
$freshAppt = $this->em->getRepository(Appointment::class)->find($appt->getId());
$freshPay = $this->em->getRepository(Payment::class)->find($payment->getId());
$this->assertSame(Appointment::STATUS_EXPIRED, $freshAppt->getStatus());
$this->assertSame(Payment::STATUS_CANCELED, $freshPay->getStatus());
}
}
/**
* نوبت «ثبت‌شده»ی پنل TTL ندارد؛ گذشتنِ ساعتِ نوبت نباید خودبه‌خود منقضی‌اش کند —
* قطعی/لغو کردنش تصمیم اپراتور است.
*/
public function testPanelRegisteredPendingSurvivesExpiry(): void
{
$owner = $this->createUser(['ROLE_DOCTOR']);
$doctor = new Doctor($owner, 'دکتر پنل');
$this->em->persist($doctor);
$slotStart = time() - 7200;
$appt = $this->newAppointment($doctor, $this->createUser(['ROLE_USER']), $slotStart, $slotStart + 900);
$this->em->persist($appt);
$this->em->flush();
static::getContainer()->get(AppointmentExpiryService::class)->expireStale();
$this->em->clear();
$fresh = $this->em->getRepository(Appointment::class)->find($appt->getId());
$this->assertSame(Appointment::STATUS_PENDING, $fresh->getStatus());
}
}