createUser() drew a random mobile and recovered from a collision by catching the unique-constraint violation and calling resetManager(). That hands back a brand-new EntityManager, which detaches every entity the running test had built so far; its next flush died with "Multiple non-persisted new entities were found", always in a different test and never reproducible in isolation. The number is now checked before the insert, so the collision never reaches the database and the manager stays open. testParentIdAddsNoQueryPerSpecialty counted queries on the first request of each size, so one-shot per-process caches — site config, subscription plan, Doctrine metadata — landed inside the count or not depending on which tests had run before it. Both requests are now warmed first; the assertion measures steady-state growth, which is what it was always about. The 23 PHPUnit notices were all one complaint: doubles created with createMock() that never had an expectation. The ones that only stub return values became createStub(); in SmsServiceLookupOnlyTest the provider and the bus got the expectations they were missing, since "dispatch does not touch the provider" and "sendNow does not enqueue" are exactly what that suite is there to prove. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
110 lines
4.2 KiB
PHP
110 lines
4.2 KiB
PHP
<?php
|
|
|
|
namespace App\Tests\Subscription;
|
|
|
|
use App\Auth\Entity\User;
|
|
use App\Config\Repository\SiteConfigRepository;
|
|
use App\Shared\Constant\ErrorCodes;
|
|
use App\Shared\Exception\AppException;
|
|
use App\Subscription\Entity\ClinicSubscription;
|
|
use App\Subscription\Entity\SubscriptionPeriod;
|
|
use App\Subscription\Entity\SubscriptionPlan;
|
|
use App\Subscription\Repository\ClinicSubscriptionRepository;
|
|
use App\Subscription\Repository\SubscriptionPeriodRepository;
|
|
use App\Subscription\Repository\SubscriptionPlanRepository;
|
|
use App\Subscription\Service\SubscriptionService;
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
/**
|
|
* An admin granting a subscription must produce a paid-tier subscription with no
|
|
* payment attached, an audit trail, and no side effect on the target's unused
|
|
* trial.
|
|
*/
|
|
class GrantSubscriptionTest extends TestCase
|
|
{
|
|
private ?ClinicSubscription $saved = null;
|
|
|
|
private function service(?SubscriptionPeriod $period, ?ClinicSubscription $active): SubscriptionService
|
|
{
|
|
$subscriptionRepo = $this->createStub(ClinicSubscriptionRepository::class);
|
|
$subscriptionRepo->method('findActive')->willReturn($active);
|
|
$subscriptionRepo->method('save')->willReturnCallback(function (ClinicSubscription $s): void {
|
|
$this->saved = $s;
|
|
});
|
|
|
|
$periodRepo = $this->createStub(SubscriptionPeriodRepository::class);
|
|
$periodRepo->method('findByUuid')->willReturn($period);
|
|
|
|
return new SubscriptionService(
|
|
$subscriptionRepo,
|
|
$this->createStub(SubscriptionPlanRepository::class),
|
|
$periodRepo,
|
|
$this->createStub(SiteConfigRepository::class),
|
|
);
|
|
}
|
|
|
|
private function period(int $durationMonths): SubscriptionPeriod
|
|
{
|
|
$period = $this->createStub(SubscriptionPeriod::class);
|
|
$period->method('getPlan')->willReturn($this->createStub(SubscriptionPlan::class));
|
|
$period->method('getDurationMonths')->willReturn($durationMonths);
|
|
|
|
return $period;
|
|
}
|
|
|
|
public function testGrantCreatesSubscriptionWithoutPaymentAndRecordsTheAdmin(): void
|
|
{
|
|
$admin = $this->createStub(User::class);
|
|
$service = $this->service($this->period(1), null);
|
|
|
|
$subscription = $service->grant('doctor', 7, 'period-uuid', $admin);
|
|
|
|
$this->assertSame($subscription, $this->saved);
|
|
$this->assertSame('doctor', $subscription->getEntityType());
|
|
$this->assertSame(7, $subscription->getEntityId());
|
|
$this->assertNull($subscription->getPayment());
|
|
$this->assertSame($admin, $subscription->getGrantedBy());
|
|
$this->assertTrue($subscription->isGranted());
|
|
$this->assertEqualsWithDelta(time() + 30 * 86400, $subscription->getExpiresAt(), 5);
|
|
}
|
|
|
|
/** Granting must never burn an unused trial — `hasUsedTrial` reads this flag. */
|
|
public function testGrantIsNeverMarkedAsTrial(): void
|
|
{
|
|
$service = $this->service($this->period(1), null);
|
|
|
|
$subscription = $service->grant('clinic', 3, 'period-uuid', $this->createStub(User::class));
|
|
|
|
$this->assertFalse($subscription->isTrial());
|
|
$this->assertTrue($subscription->toArray()['is_granted']);
|
|
}
|
|
|
|
public function testUnknownPeriodIsRejected(): void
|
|
{
|
|
$service = $this->service(null, null);
|
|
|
|
try {
|
|
$service->grant('doctor', 1, 'missing-uuid', $this->createStub(User::class));
|
|
$this->fail('expected AppException');
|
|
} catch (AppException $e) {
|
|
$this->assertSame(ErrorCodes::ERR_NOT_FOUND_001, $e->getErrorCode());
|
|
$this->assertSame(404, $e->getHttpStatus());
|
|
}
|
|
}
|
|
|
|
/** Boundary: an active subscription is extended from its own expiry, not from today. */
|
|
public function testGrantExtendsAnActiveSubscriptionInsteadOfRestartingIt(): void
|
|
{
|
|
$currentExpiry = time() + 20 * 86400;
|
|
|
|
$active = $this->createStub(ClinicSubscription::class);
|
|
$active->method('getExpiresAt')->willReturn($currentExpiry);
|
|
|
|
$service = $this->service($this->period(1), $active);
|
|
|
|
$subscription = $service->grant('doctor', 7, 'period-uuid', $this->createStub(User::class));
|
|
|
|
$this->assertEqualsWithDelta($currentExpiry + 30 * 86400, $subscription->getExpiresAt(), 5);
|
|
}
|
|
}
|