Files
clinicpro/tests/Subscription/AdminPlanResourceLimitTest.php
T

102 lines
3.6 KiB
PHP

<?php
namespace App\Tests\Subscription;
use App\Subscription\Entity\SubscriptionPlan;
use App\Tests\ApiTestCase;
/**
* سقف منابعِ پلن از پنل ادمین نوشته و خوانده می‌شود.
*
* `-1` یعنی نامحدود؛ `0` و هر منفیِ دیگری رد می‌شود — پلنی که هیچ منبعی نمی‌دهد یا
* منفیِ بی‌معنا دارد، خاموش شکست می‌خورد و در پنل چیزی نشان نمی‌دهد.
*/
class AdminPlanResourceLimitTest extends ApiTestCase
{
private function planName(): string
{
return 'quota_plan_' . bin2hex(random_bytes(4));
}
public function testPlanIsCreatedWithItsResourceQuota(): void
{
$admin = $this->createUser(['ROLE_USER', 'ROLE_ADMIN']);
$body = $this->authJson('POST', '/api/v1/admin/subscription/plan', $admin, [
'name' => $this->planName(),
'level' => 7,
'max_secretaries' => 2,
'max_resources' => 5,
'features' => ['services' => true],
]);
self::assertSame(201, $this->responseCode(), json_encode($body, JSON_UNESCAPED_UNICODE));
self::assertSame(5, $body['data']['max_resources']);
}
/** نیامدنِ فیلد یعنی محافظه‌کارانه‌ترین سقف، نه نامحدود. */
public function testOmittedQuotaDefaultsToOne(): void
{
$admin = $this->createUser(['ROLE_USER', 'ROLE_ADMIN']);
$body = $this->authJson('POST', '/api/v1/admin/subscription/plan', $admin, [
'name' => $this->planName(),
'level' => 7,
]);
self::assertSame(201, $this->responseCode());
self::assertSame(1, $body['data']['max_resources']);
}
public function testQuotaIsUpdatedAndUnlimitedIsAccepted(): void
{
$admin = $this->createUser(['ROLE_USER', 'ROLE_ADMIN']);
$created = $this->authJson('POST', '/api/v1/admin/subscription/plan', $admin, [
'name' => $this->planName(),
'level' => 7,
'max_resources' => 3,
]);
$body = $this->authJson('PATCH', '/api/v1/admin/subscription/plan/' . $created['data']['uuid'], $admin, [
'max_resources' => SubscriptionPlan::UNLIMITED,
]);
self::assertSame(200, $this->responseCode());
self::assertSame(SubscriptionPlan::UNLIMITED, $body['data']['max_resources']);
}
#[\PHPUnit\Framework\Attributes\DataProvider('invalidQuotas')]
public function testInvalidQuotaIsRejected(int $quota): void
{
$admin = $this->createUser(['ROLE_USER', 'ROLE_ADMIN']);
$this->authJson('POST', '/api/v1/admin/subscription/plan', $admin, [
'name' => $this->planName(),
'level' => 7,
'max_resources' => $quota,
]);
self::assertSame(422, $this->responseCode());
}
/** @return array<string, array{0: int}> */
public static function invalidQuotas(): array
{
return [
'zero' => [0],
'below unlimited' => [-2],
];
}
/** فهرست پلن‌ها سقف را می‌دهد؛ وگرنه کارت‌های صفحهٔ اشتراک چیزی برای نشان‌دادن ندارند. */
public function testPlanListExposesTheQuota(): void
{
$user = $this->createUser(['ROLE_USER']);
$body = $this->authJson('GET', '/api/v1/subscription/plans', $user);
self::assertSame(200, $this->responseCode());
self::assertArrayHasKey('max_resources', $body['data'][0]);
}
}