You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
virt/web/tests/CloudAutomaticBackupEntitle...

265 lines
8.5 KiB
PHP

<?php
require dirname(__DIR__) . '/vendor/autoload.php';
use App\Models\CloudBackupSubscription;
use App\Models\CloudInstance;
use App\Models\CloudInvoice;
use App\Models\CloudInvoiceItem;
use App\Models\CloudServicePeriod;
use App\Services\Cloud\CloudAutomaticBackupBillingService;
use App\Services\Cloud\CloudAutomaticBackupService;
use Illuminate\Database\Capsule\Manager as Capsule;
use Illuminate\Database\Schema\Blueprint;
$assert = static function (bool $condition, string $message): void {
if (!$condition) {
throw new RuntimeException($message);
}
};
$capsule = new Capsule();
$capsule->addConnection([
'driver' => 'sqlite', 'database' => ':memory:',
], 'default');
$capsule->setAsGlobal();
$capsule->bootEloquent();
$schema = $capsule->schema('default');
$schema->create('cloud_instances', static function (Blueprint $table): void {
$table->increments('id');
$table->integer('user_id');
$table->integer('offer_id');
});
$schema->create('cloud_service_periods', static function (Blueprint $table): void {
$table->increments('id');
$table->integer('instance_id');
$table->integer('user_id');
$table->integer('order_id')->nullable();
$table->integer('renewal_id')->nullable();
$table->integer('starts_at');
$table->integer('ends_at');
});
$schema->create('cloud_backup_subscriptions', static function (Blueprint $table): void {
$table->increments('id');
$table->integer('instance_id')->unique();
$table->integer('user_id');
$table->integer('offer_id');
$table->string('price');
$table->string('currency');
$table->integer('price_version');
$table->boolean('enabled');
$table->boolean('renew_next_period');
$table->string('frequency');
$table->integer('weekday');
$table->integer('retention_count');
$table->string('timezone');
$table->integer('next_run_at');
$table->integer('last_scheduled_at')->nullable();
$table->integer('last_backup_id')->nullable();
$table->integer('created_at');
$table->integer('updated_at');
});
$schema->create('cloud_invoices', static function (Blueprint $table): void {
$table->increments('id');
$table->string('purpose');
$table->integer('instance_id')->nullable();
$table->integer('order_id')->nullable();
$table->integer('user_id');
$table->string('status');
$table->string('currency');
$table->integer('paid_at')->nullable();
});
$schema->create('cloud_invoice_items', static function (Blueprint $table): void {
$table->increments('id');
$table->integer('invoice_id');
$table->integer('order_item_id')->nullable();
$table->string('code');
$table->string('label');
$table->string('amount');
$table->string('currency');
$table->text('snapshot_json');
$table->integer('created_at');
});
$schema->create('cloud_backup_period_entitlements', static function (Blueprint $table): void {
$table->increments('id');
$table->integer('subscription_id');
$table->integer('instance_id');
$table->integer('service_period_id')->unique();
$table->integer('user_id');
$table->integer('offer_id');
$table->integer('invoice_id');
$table->integer('starts_at');
$table->integer('ends_at');
$table->string('amount');
$table->string('full_period_price');
$table->string('currency');
$table->integer('price_version');
$table->string('source');
$table->integer('paid_at');
$table->integer('created_at');
$table->integer('updated_at');
});
$now = 2_000_000_000;
$instance = new CloudInstance();
$instance->id = 10;
$instance->user_id = 7;
$instance->offer_id = 3;
$instance->save();
$subscription = new CloudBackupSubscription();
$subscription->instance_id = 10;
$subscription->user_id = 7;
$subscription->offer_id = 3;
$subscription->price = '2.50';
$subscription->currency = 'USD';
$subscription->price_version = 4;
$subscription->enabled = true;
$subscription->renew_next_period = true;
$subscription->frequency = 'daily';
$subscription->weekday = 0;
$subscription->retention_count = 2;
$subscription->timezone = 'UTC';
$subscription->next_run_at = $now + 60;
$subscription->created_at = $subscription->updated_at = $now - 60;
$subscription->save();
$provision = new CloudInvoice();
$provision->purpose = 'provision';
$provision->instance_id = 10;
$provision->order_id = 70;
$provision->user_id = 7;
$provision->status = 'paid';
$provision->currency = 'USD';
$provision->paid_at = $now - 60;
$provision->save();
$provisionItem = new CloudInvoiceItem();
$provisionItem->invoice_id = (int) $provision->id;
$provisionItem->order_item_id = 1;
$provisionItem->code = 'automatic_backup';
$provisionItem->label = '自动备份';
$provisionItem->amount = '2.50';
$provisionItem->currency = 'USD';
$provisionItem->snapshot_json = '{}';
$provisionItem->created_at = $now - 60;
$provisionItem->save();
$firstPeriod = new CloudServicePeriod();
$firstPeriod->instance_id = 10;
$firstPeriod->user_id = 7;
$firstPeriod->order_id = 70;
$firstPeriod->starts_at = $now;
$firstPeriod->ends_at = $now + 30 * 86400;
$firstPeriod->save();
$first = CloudAutomaticBackupBillingService::grantProvisionEntitlementLocked(
$instance,
$firstPeriod,
$provision,
$subscription,
$now
);
$same = CloudAutomaticBackupBillingService::grantProvisionEntitlementLocked(
$instance,
$firstPeriod,
$provision,
$subscription,
$now + 10
);
$assert($first !== null && $same !== null
&& (int) $first->id === (int) $same->id
&& (string) $first->source === 'provision'
&& (string) $first->amount === '2.50'
&& (string) $first->full_period_price === '2.50'
&& $capsule->table('cloud_backup_period_entitlements')->count() === 1,
'Provision finalization did not grant exactly one immutable period entitlement.');
$renewal = new CloudInvoice();
$renewal->purpose = 'renewal';
$renewal->instance_id = 10;
$renewal->order_id = null;
$renewal->user_id = 7;
$renewal->status = 'open';
$renewal->currency = 'USD';
$renewal->paid_at = null;
$renewal->save();
$renewalItem = new CloudInvoiceItem();
$renewalItem->invoice_id = (int) $renewal->id;
$renewalItem->order_item_id = null;
$renewalItem->code = 'automatic_backup';
$renewalItem->label = '自动备份续费';
$renewalItem->amount = '2.50';
$renewalItem->currency = 'USD';
$renewalItem->snapshot_json = json_encode([
'subscription_id' => (int) $subscription->id,
], JSON_THROW_ON_ERROR);
$renewalItem->created_at = $now;
$renewalItem->save();
$nextPeriod = new CloudServicePeriod();
$nextPeriod->instance_id = 10;
$nextPeriod->user_id = 7;
$nextPeriod->renewal_id = 80;
$nextPeriod->starts_at = $firstPeriod->ends_at;
$nextPeriod->ends_at = $firstPeriod->ends_at + 30 * 86400;
$nextPeriod->save();
$subscription->refresh();
$subscription->enabled = false;
$subscription->renew_next_period = true;
$subscription->next_run_at = 0;
$subscription->save();
$second = CloudAutomaticBackupBillingService::grantRenewalEntitlementLocked(
$instance,
$nextPeriod,
$renewal,
$now
);
$assert($second !== null
&& (string) $second->source === 'renewal'
&& (int) $second->service_period_id === (int) $nextPeriod->id
&& $capsule->table('cloud_backup_period_entitlements')->count() === 2
&& (bool) $subscription->fresh()->enabled
&& (int) $subscription->fresh()->next_run_at
=== CloudAutomaticBackupService::nextRunAt(
max($now, (int) $nextPeriod->starts_at), 'daily', 0
),
'Paid renewal did not grant its independent backup entitlement.');
$subscription->refresh();
$subscription->enabled = false;
$subscription->renew_next_period = false;
$subscription->next_run_at = 0;
$subscription->save();
$replayedWhileDisabled =
CloudAutomaticBackupBillingService::grantRenewalEntitlementLocked(
$instance, $nextPeriod, $renewal, $now + 1
);
$subscription->refresh();
$assert($replayedWhileDisabled !== null
&& !(bool) $subscription->enabled
&& !(bool) $subscription->renew_next_period
&& (int) $subscription->next_run_at === 0,
'A user-disabled automatic backup was reopened by renewal replay.');
$baseOnly = new CloudInvoice();
$baseOnly->purpose = 'renewal';
$baseOnly->instance_id = 10;
$baseOnly->user_id = 7;
$baseOnly->status = 'open';
$baseOnly->currency = 'USD';
$baseOnly->save();
$withoutBackup = CloudAutomaticBackupBillingService::grantRenewalEntitlementLocked(
$instance,
$nextPeriod,
$baseOnly,
$now
);
$assert($withoutBackup === null
&& $capsule->table('cloud_backup_period_entitlements')->count() === 2,
'A base-only renewal created automatic-backup cover.');
echo "Automatic backup entitlement grant tests passed.\n";