feat(subscription): implement resource quota management based on subscription plans
This commit is contained in:
+30
-8
@@ -63,21 +63,43 @@ abstract class ApiTestCase extends WebTestCase
|
||||
* and the patient / service / insurance endpoints answer 403 instead of doing
|
||||
* their job. db_test is never reset, so the insert is idempotent.
|
||||
*
|
||||
* Mirrors the row shipped in the dev database.
|
||||
* Mirrors the row shipped in the dev database, except for max_resources: the
|
||||
* fixture grants an unlimited resource quota so that the dozens of suites which
|
||||
* merely need a room or a device are not rewritten into subscription tests. The
|
||||
* real per-plan quota is exercised explicitly by ResourceQuotaTest, which lowers
|
||||
* it with setPlanResourceQuota().
|
||||
*/
|
||||
private function ensureFreePlan(): void
|
||||
{
|
||||
$repo = $this->em->getRepository(SubscriptionPlan::class);
|
||||
if ($repo->findOneBy(['name' => 'free']) !== null) {
|
||||
$plan = $repo->findOneBy(['name' => 'free']);
|
||||
|
||||
if ($plan === null) {
|
||||
$this->em->persist(new SubscriptionPlan('free', 0, 1, [
|
||||
'patient_records' => true,
|
||||
'services' => true,
|
||||
'sms_panel' => true,
|
||||
'insurance' => true,
|
||||
], SubscriptionPlan::UNLIMITED));
|
||||
$this->em->flush();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$this->em->persist(new SubscriptionPlan('free', 0, 1, [
|
||||
'patient_records' => true,
|
||||
'services' => true,
|
||||
'sms_panel' => true,
|
||||
'insurance' => true,
|
||||
]));
|
||||
// db_test is never reset, so a quota lowered by a previous case must be undone.
|
||||
if ($plan->getMaxResources() !== SubscriptionPlan::UNLIMITED) {
|
||||
$plan->setMaxResources(SubscriptionPlan::UNLIMITED);
|
||||
$this->em->flush();
|
||||
}
|
||||
}
|
||||
|
||||
/** سقف منابعِ پلن مؤثرِ تستها؛ `SubscriptionPlan::UNLIMITED` یعنی بینهایت. */
|
||||
protected function setPlanResourceQuota(int $max, string $plan = 'free'): void
|
||||
{
|
||||
$entity = $this->em->getRepository(SubscriptionPlan::class)->findOneBy(['name' => $plan]);
|
||||
self::assertNotNull($entity, sprintf('Plan "%s" is missing from the test database.', $plan));
|
||||
|
||||
$entity->setMaxResources($max);
|
||||
$this->em->flush();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user