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.
382 lines
13 KiB
PHP
382 lines
13 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('traffic_pool_group_id')->nullable();
|
|
$table->string('traffic_quota_mode')->default('finite');
|
|
$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->integer('state_revision')->default(1);
|
|
$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');
|
|
});
|
|
$schema->create('cloud_service_compensation_items', static function (Blueprint $table): void {
|
|
$table->increments('id');
|
|
$table->string('state');
|
|
$table->integer('service_period_id')->nullable();
|
|
$table->integer('backup_entitlement_id')->nullable();
|
|
$table->integer('before_backup_ends_at')->nullable();
|
|
$table->integer('after_backup_ends_at')->nullable();
|
|
});
|
|
|
|
$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()->state_revision === 2
|
|
&& (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.');
|
|
|
|
$instance->offer_id = 4;
|
|
$instance->save();
|
|
$subscription->offer_id = 4;
|
|
$subscription->price = '3.75';
|
|
$subscription->price_version = 5;
|
|
$subscription->save();
|
|
$upgradedSubscriptionBefore = [
|
|
'offer_id' => (int) $subscription->offer_id,
|
|
'price' => (string) $subscription->price,
|
|
'price_version' => (int) $subscription->price_version,
|
|
'enabled' => (bool) $subscription->enabled,
|
|
'renew_next_period' => (bool) $subscription->renew_next_period,
|
|
];
|
|
$backupCommercialBefore = [
|
|
'subscription_id' => (int) $second->subscription_id,
|
|
'instance_id' => (int) $second->instance_id,
|
|
'user_id' => (int) $second->user_id,
|
|
'offer_id' => (int) $second->offer_id,
|
|
'invoice_id' => (int) $second->invoice_id,
|
|
'amount' => (string) $second->amount,
|
|
'full_period_price' => (string) $second->full_period_price,
|
|
'currency' => (string) $second->currency,
|
|
'price_version' => (int) $second->price_version,
|
|
'source' => (string) $second->source,
|
|
'paid_at' => (int) $second->paid_at,
|
|
];
|
|
$backupOldEnd = (int) $nextPeriod->ends_at;
|
|
$backupNewEnd = $backupOldEnd + 3600;
|
|
$extendedBackup = CloudAutomaticBackupBillingService::
|
|
extendEntitlementWindowForCompensationLocked(
|
|
(int) $nextPeriod->id,
|
|
$backupOldEnd,
|
|
$backupNewEnd,
|
|
9100,
|
|
$now + 20
|
|
);
|
|
$backupCommercialAfter = [
|
|
'subscription_id' => (int) $extendedBackup->subscription_id,
|
|
'instance_id' => (int) $extendedBackup->instance_id,
|
|
'user_id' => (int) $extendedBackup->user_id,
|
|
'offer_id' => (int) $extendedBackup->offer_id,
|
|
'invoice_id' => (int) $extendedBackup->invoice_id,
|
|
'amount' => (string) $extendedBackup->amount,
|
|
'full_period_price' => (string) $extendedBackup->full_period_price,
|
|
'currency' => (string) $extendedBackup->currency,
|
|
'price_version' => (int) $extendedBackup->price_version,
|
|
'source' => (string) $extendedBackup->source,
|
|
'paid_at' => (int) $extendedBackup->paid_at,
|
|
];
|
|
$assert((int) $extendedBackup->ends_at === $backupNewEnd
|
|
&& $backupCommercialAfter === $backupCommercialBefore
|
|
&& [
|
|
'offer_id' => (int) $subscription->fresh()->offer_id,
|
|
'price' => (string) $subscription->fresh()->price,
|
|
'price_version' => (int) $subscription->fresh()->price_version,
|
|
'enabled' => (bool) $subscription->fresh()->enabled,
|
|
'renew_next_period' =>
|
|
(bool) $subscription->fresh()->renew_next_period,
|
|
] === $upgradedSubscriptionBefore,
|
|
'Backup compensation rejected or rewrote a valid historical paid term.');
|
|
$nextPeriod->ends_at = $backupNewEnd;
|
|
$nextPeriod->save();
|
|
$capsule->table('cloud_service_compensation_items')->insert([
|
|
'id' => 9100,
|
|
'state' => 'applied',
|
|
'service_period_id' => (int) $nextPeriod->id,
|
|
'backup_entitlement_id' => (int) $extendedBackup->id,
|
|
'before_backup_ends_at' => $backupOldEnd,
|
|
'after_backup_ends_at' => $backupNewEnd,
|
|
]);
|
|
$replayedExtension = CloudAutomaticBackupBillingService::
|
|
extendEntitlementWindowForCompensationLocked(
|
|
(int) $nextPeriod->id,
|
|
$backupOldEnd,
|
|
$backupNewEnd,
|
|
9100,
|
|
$now + 30
|
|
);
|
|
$assert((int) $replayedExtension->id === (int) $extendedBackup->id
|
|
&& (int) $replayedExtension->ends_at === $backupNewEnd,
|
|
'The audited backup-window extension was not idempotent.');
|
|
|
|
$uncoveredPeriod = new CloudServicePeriod();
|
|
$uncoveredPeriod->instance_id = 10;
|
|
$uncoveredPeriod->user_id = 7;
|
|
$uncoveredPeriod->renewal_id = 81;
|
|
$uncoveredPeriod->starts_at = $backupNewEnd;
|
|
$uncoveredPeriod->ends_at = $backupNewEnd + 30 * 86400;
|
|
$uncoveredPeriod->save();
|
|
$entitlementCount = $capsule->table(
|
|
'cloud_backup_period_entitlements'
|
|
)->count();
|
|
$uncoveredResult = CloudAutomaticBackupBillingService::
|
|
extendEntitlementWindowForCompensationLocked(
|
|
(int) $uncoveredPeriod->id,
|
|
(int) $uncoveredPeriod->ends_at,
|
|
(int) $uncoveredPeriod->ends_at + 3600,
|
|
9101,
|
|
$now + 40
|
|
);
|
|
$assert($uncoveredResult === null
|
|
&& $capsule->table('cloud_backup_period_entitlements')->count()
|
|
=== $entitlementCount,
|
|
'Compensation created free automatic-backup cover for an uncovered period.');
|
|
|
|
echo "Automatic backup entitlement grant tests passed.\n";
|