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.
255 lines
9.8 KiB
PHP
255 lines
9.8 KiB
PHP
<?php
|
|
|
|
require dirname(__DIR__) . '/vendor/autoload.php';
|
|
|
|
use App\Services\Cloud\CloudInvoicePageService;
|
|
use App\Services\Cloud\CloudInvoiceService;
|
|
use App\Models\CloudInvoice;
|
|
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->string('name');
|
|
$table->string('status');
|
|
$table->string('service_status')->default('active');
|
|
$table->text('spec_json')->default('{}');
|
|
});
|
|
$schema->create('cloud_jobs', static function (Blueprint $table): void {
|
|
$table->increments('id');
|
|
$table->integer('order_id')->nullable();
|
|
$table->integer('invoice_id')->nullable();
|
|
$table->integer('instance_id')->nullable();
|
|
$table->integer('user_id');
|
|
$table->string('kind');
|
|
$table->string('phase');
|
|
$table->string('outcome')->nullable();
|
|
$table->integer('attempts')->default(0);
|
|
$table->integer('updated_at')->default(0);
|
|
$table->string('error_code')->nullable();
|
|
$table->text('result_json')->nullable();
|
|
$table->string('effect_resolution')->default('not_applicable');
|
|
$table->text('sealed_payload')->nullable();
|
|
});
|
|
$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->integer('renews_expires_at')->nullable();
|
|
$table->integer('due_at');
|
|
$table->string('amount');
|
|
$table->string('status');
|
|
$table->integer('validity_days')->default(30);
|
|
$table->integer('traffic_quota_bytes')->default(0);
|
|
$table->string('paid_via')->nullable();
|
|
$table->integer('paid_at')->nullable();
|
|
$table->integer('created_at');
|
|
});
|
|
$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')->default('USD');
|
|
$table->text('snapshot_json');
|
|
$table->integer('created_at');
|
|
});
|
|
|
|
$now = time();
|
|
$expiry = $now + 3600;
|
|
Capsule::table('cloud_instances')->insert([
|
|
['id' => 1, 'user_id' => 1, 'name' => 'customer-api', 'status' => 'active'],
|
|
['id' => 2, 'user_id' => 2, 'name' => 'foreign-vm', 'status' => 'active'],
|
|
]);
|
|
Capsule::table('cloud_invoices')->insert([
|
|
'id' => 1,
|
|
'purpose' => 'renewal',
|
|
'instance_id' => 1,
|
|
'order_id' => null,
|
|
'user_id' => 1,
|
|
'renews_expires_at' => $expiry,
|
|
'due_at' => $expiry,
|
|
'amount' => '10.00',
|
|
'status' => 'paid',
|
|
'created_at' => $now - 100,
|
|
]);
|
|
Capsule::table('cloud_invoices')->insert([
|
|
'id' => 2,
|
|
'purpose' => 'renewal',
|
|
'instance_id' => 2,
|
|
'order_id' => null,
|
|
'user_id' => 2,
|
|
'renews_expires_at' => $expiry,
|
|
'due_at' => $expiry,
|
|
'amount' => '20.00',
|
|
'status' => 'open',
|
|
'created_at' => $now - 99,
|
|
]);
|
|
Capsule::table('cloud_invoices')->insert([
|
|
'id' => 3,
|
|
'purpose' => 'upgrade',
|
|
'instance_id' => 1,
|
|
'order_id' => null,
|
|
'user_id' => 1,
|
|
'renews_expires_at' => null,
|
|
'due_at' => $now,
|
|
'amount' => '5.00',
|
|
'status' => 'paid',
|
|
'created_at' => $now + 100,
|
|
]);
|
|
Capsule::table('cloud_jobs')->insert([
|
|
'id' => 3,
|
|
'order_id' => null,
|
|
'invoice_id' => 3,
|
|
'instance_id' => 1,
|
|
'user_id' => 1,
|
|
'kind' => 'instance_upgrade',
|
|
'phase' => 'accepted',
|
|
'outcome' => null,
|
|
'effect_resolution' => 'not_applicable',
|
|
]);
|
|
Capsule::table('cloud_invoices')->insert([
|
|
'id' => 50,
|
|
'purpose' => 'backup_addon',
|
|
'instance_id' => 1,
|
|
'order_id' => null,
|
|
'user_id' => 1,
|
|
'renews_expires_at' => null,
|
|
'due_at' => $now,
|
|
'amount' => '2.50',
|
|
'status' => 'paid',
|
|
'paid_via' => 'balance',
|
|
'paid_at' => $now,
|
|
'created_at' => $now + 200,
|
|
]);
|
|
for ($number = 0; $number < 12; $number++) {
|
|
Capsule::table('cloud_invoices')->insert([
|
|
'purpose' => 'ip_change',
|
|
'instance_id' => null,
|
|
'order_id' => null,
|
|
'user_id' => 1,
|
|
'renews_expires_at' => null,
|
|
'due_at' => $now + $number,
|
|
'amount' => '1.00',
|
|
'status' => 'paid',
|
|
'created_at' => $now + $number,
|
|
]);
|
|
}
|
|
|
|
$all = CloudInvoicePageService::forUser(1);
|
|
$assert($all['purpose'] === 'all'
|
|
&& $all['pagination']['total'] === 15
|
|
&& $all['pagination']['per_page'] === 100
|
|
&& count($all['invoices']) === 15
|
|
&& in_array('1', array_column($all['invoices'], 'id'), true),
|
|
'The default all-invoice view did not include every owner bill within the bounded page.');
|
|
|
|
$renewals = CloudInvoicePageService::forUser(1, 1, 10, 'renewal');
|
|
$invoice = $renewals['invoices'][0] ?? [];
|
|
$assert($renewals['purpose'] === 'renewal'
|
|
&& $renewals['pagination']['total'] === 1
|
|
&& count($renewals['invoices']) === 1
|
|
&& ($invoice['id'] ?? null) === '1'
|
|
&& ($invoice['purpose'] ?? null) === 'renewal'
|
|
&& ($invoice['renews_expires_at'] ?? null) === $expiry
|
|
&& ($invoice['payment_deadline_at'] ?? null)
|
|
=== $expiry + CloudInvoiceService::PAYMENT_GRACE_SECONDS,
|
|
'The user renewal filter hid the owner invoice or returned the wrong fixed deadline.');
|
|
$upgrades = CloudInvoicePageService::forUser(1, 1, 10, 'upgrade');
|
|
$upgrade = $upgrades['invoices'][0] ?? [];
|
|
$assert($upgrades['purpose'] === 'upgrade'
|
|
&& $upgrades['pagination']['total'] === 1
|
|
&& ($upgrade['title'] ?? null) === '实例升级'
|
|
&& ($upgrade['instance_id'] ?? null) === '1'
|
|
&& ($upgrade['instance_name'] ?? null) === 'customer-api'
|
|
&& ($upgrade['task_id'] ?? null) === '3'
|
|
&& ($upgrade['job_status'] ?? null) === 'active'
|
|
&& ($upgrade['fulfillment_status'] ?? null) === 'active',
|
|
'Upgrade invoices did not expose their target instance and invoice-linked task state.');
|
|
$upgradeInvoice = CloudInvoice::find(3);
|
|
$upgradeStatus = CloudInvoiceService::status($upgradeInvoice);
|
|
$upgradeDetail = CloudInvoiceService::display($upgradeInvoice);
|
|
$assert(($upgradeStatus['payable'] ?? null) === false
|
|
&& ($upgradeStatus['instance_id'] ?? null) === '1'
|
|
&& ($upgradeStatus['task_id'] ?? null) === '3'
|
|
&& ($upgradeStatus['job_status'] ?? null) === 'active'
|
|
&& ($upgradeDetail['title'] ?? null) === '实例升级'
|
|
&& ($upgradeDetail['instance_name'] ?? null) === 'customer-api'
|
|
&& ($upgradeDetail['task_id'] ?? null) === '3'
|
|
&& ($upgradeDetail['payable'] ?? null) === false
|
|
&& array_key_exists('payment_deadline_at', $upgradeDetail)
|
|
&& $upgradeDetail['payment_deadline_at'] === null,
|
|
'Upgrade invoice detail or polling lost its target, task link, or non-payable state.');
|
|
|
|
$backupAddons = CloudInvoicePageService::forUser(
|
|
1,
|
|
1,
|
|
10,
|
|
'backup_addon'
|
|
);
|
|
$backupAddon = $backupAddons['invoices'][0] ?? [];
|
|
$backupInvoice = CloudInvoice::find(50);
|
|
$backupStatus = CloudInvoiceService::status($backupInvoice);
|
|
$backupDetail = CloudInvoiceService::display($backupInvoice);
|
|
$assert($backupAddons['purpose'] === 'backup_addon'
|
|
&& $backupAddons['pagination']['total'] === 1
|
|
&& ($backupAddon['title'] ?? null) === '自动备份开通'
|
|
&& ($backupAddon['instance_id'] ?? null) === '1'
|
|
&& ($backupAddon['instance_name'] ?? null) === 'customer-api'
|
|
&& ($backupStatus['payable'] ?? null) === false
|
|
&& ($backupStatus['instance_id'] ?? null) === '1'
|
|
&& ($backupDetail['title'] ?? null) === '自动备份开通'
|
|
&& ($backupDetail['instance_id'] ?? null) === '1'
|
|
&& ($backupDetail['instance_name'] ?? null) === 'customer-api'
|
|
&& ($backupDetail['payable'] ?? null) === false,
|
|
'Automatic-backup add-on bills were hidden, payable, or detached from their instance.');
|
|
|
|
Capsule::table('cloud_jobs')->where('id', 3)->update([
|
|
'phase' => 'terminal',
|
|
'outcome' => 'failed',
|
|
'effect_resolution' => 'unknown',
|
|
]);
|
|
$unknownStatus = CloudInvoiceService::status($upgradeInvoice);
|
|
$assert($unknownStatus['job_status'] === 'active'
|
|
&& $unknownStatus['job_completed'] === false,
|
|
'Unknown upgrade effect was presented as a completed failure.');
|
|
Capsule::table('cloud_jobs')->where('id', 3)->update([
|
|
'effect_resolution' => 'applied',
|
|
]);
|
|
$appliedStatus = CloudInvoiceService::status($upgradeInvoice);
|
|
$assert($appliedStatus['job_status'] === 'succeeded'
|
|
&& $appliedStatus['job_completed'] === true,
|
|
'Recovered applied upgrade was not presented as fulfilled.');
|
|
Capsule::table('cloud_instances')->where('id', 1)->delete();
|
|
$deletedList = CloudInvoicePageService::forUser(1, 1, 10, 'upgrade');
|
|
$deletedStatus = CloudInvoiceService::status($upgradeInvoice);
|
|
$deletedDetail = CloudInvoiceService::display($upgradeInvoice);
|
|
$assert(($deletedList['invoices'][0]['instance_id'] ?? null) === '1'
|
|
&& ($deletedStatus['instance_id'] ?? null) === '1'
|
|
&& ($deletedDetail['instance_id'] ?? null) === '1'
|
|
&& ($deletedDetail['instance_name'] ?? null) === '已删除实例',
|
|
'Upgrade audit lost its referenced instance ID after the instance row disappeared.');
|
|
$assert(CloudInvoicePageService::normalizePurpose('upgrade') === 'upgrade'
|
|
&& CloudInvoicePageService::normalizePurpose('backup_addon') === 'backup_addon'
|
|
&& CloudInvoicePageService::normalizePurpose('invalid') === 'all'
|
|
&& CloudInvoicePageService::normalizePurpose(['renewal']) === 'all',
|
|
'Invoice purpose filtering accepted an unsupported or structured value.');
|
|
|
|
echo "User invoice filtering, ownership, deadlines and upgrade task checks passed.\n";
|