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/CloudBillingPresentationCat...

147 lines
6.4 KiB
PHP

<?php
require dirname(__DIR__) . '/vendor/autoload.php';
use App\Services\Cloud\CloudAdminBillingPresentation;
use App\Services\Cloud\CloudBillingPresentationCatalog;
use App\Services\Cloud\CloudInvoicePresentation;
$assert = static function (bool $condition, string $message): void {
if (!$condition) {
throw new RuntimeException($message);
}
};
$previousTimezone = date_default_timezone_get();
date_default_timezone_set('Asia/Shanghai');
try {
$assert(CloudBillingPresentationCatalog::purposeLabel('backup_addon')
=== '自动备份开通'
&& CloudBillingPresentationCatalog::providerLabel('paytaro')
=== 'Paytaro · 支付宝'
&& CloudBillingPresentationCatalog::money('12.5', 'USD') === '$12.50'
&& CloudBillingPresentationCatalog::money('12.5', 'EUR') === '12.50 EUR'
&& CloudBillingPresentationCatalog::timestamp(1761984000)
=== date('Y-m-d H:i:s', 1761984000),
'Shared billing vocabulary drifted for backup, Paytaro, currency or time.');
$upgradeStatus = CloudBillingPresentationCatalog::status(
'fulfillment', 'running', ['purpose' => 'upgrade']
);
$ipReviewStatus = CloudBillingPresentationCatalog::status(
'fulfillment', 'review', ['purpose' => 'ip_change']
);
$provisionRefundStatus = CloudBillingPresentationCatalog::status(
'fulfillment', 'refunded', ['purpose' => 'provision']
);
$assert($upgradeStatus['label'] === '升级中'
&& $upgradeStatus['tone'] === 'info'
&& $ipReviewStatus['label'] === '待人工核对'
&& $ipReviewStatus['tone'] === 'warning'
&& $provisionRefundStatus['label'] === '开通失败,已退款'
&& $provisionRefundStatus['tone'] === 'danger'
&& CloudBillingPresentationCatalog::status(
'financial', 'void'
)['label'] === '已作废',
'Purpose-aware status semantics escaped the shared catalog.');
$row = [
'id' => '9', 'purpose' => 'backup_addon', 'amount' => '12.50',
'currency' => 'EUR', 'financial_status' => 'paid',
'fulfillment_status' => null, 'payment_method' => 'paytaro',
'created_at' => 1761984000, 'updated_at' => 1761984000,
'due_at' => 1762070400,
];
$page = CloudAdminBillingPresentation::page('invoices', [
'items' => [$row], 'summary' => [
'total_records' => 1, 'total_amount' => '12.50',
'currency_count' => 1, 'currency' => 'EUR',
'mixed_currency' => false,
],
]);
$display = $page['items'][0]['presentation'];
$assert($display['purpose_label'] === '自动备份开通'
&& $display['provider_label'] === 'Paytaro · 支付宝'
&& $display['amount_display'] === '12.50 EUR'
&& $display['financial_status']['label'] === '已支付'
&& $display['created_display']
=== CloudBillingPresentationCatalog::timestamp(1761984000)
&& ($page['items'][0]['financial_status'] ?? null) === 'paid',
'Administrator adapter changed machine fields or diverged from the catalog.');
$assert($page['summary']['presentation']['total_amount_display']
=== '12.50 EUR'
&& $page['summary']['presentation']['mixed_currency'] === false,
'Single-currency administrator summary ignored its frozen currency.');
$mixed = CloudAdminBillingPresentation::page('payments', [
'items' => [],
'summary' => [
'total_records' => 2, 'total_amount' => null,
'paid_invoice_amount' => null,
'credited_balance_amount' => null,
'currency_count' => 2, 'currency' => null,
'mixed_currency' => true,
],
]);
$assert($mixed['summary']['presentation']['total_amount_display']
=== '多币种'
&& $mixed['summary']['presentation']['paid_invoice_amount_display']
=== '多币种'
&& $mixed['summary']['presentation']['mixed_currency'] === true,
'Mixed-currency administrator summary fabricated a combined amount.');
$user = CloudInvoicePresentation::detail([
'invoice' => [
'id' => '9', 'purpose' => 'backup_addon', 'amount' => '12.50',
'currency' => 'EUR', 'status' => 'paid',
'created_at' => 1761984000, 'paid_via' => 'paytaro',
],
]);
$facts = array_column($user['facts'], 'value', 'label');
$assert($user['title'] === $display['purpose_label']
&& $user['amount_display'] === $display['amount_display']
&& ($facts['支付方式'] ?? null) === $display['provider_label'],
'User and administrator presentation stopped sharing billing vocabulary.');
$voidUser = CloudInvoicePresentation::summaries([[
'id' => '10', 'purpose' => 'recharge', 'amount' => '1.00',
'currency' => 'USD', 'status' => 'void', 'created_at' => 1761984000,
]])[0];
$assert($voidUser['status_label']
=== CloudBillingPresentationCatalog::status(
'financial', 'void'
)['label']
&& $voidUser['status_class'] === 'other',
'User adapter overrode the catalog financial label or tone.');
$options = $page['presentation']['catalog'];
$find = static function (array $options, string $value): ?string {
foreach ($options as $option) {
if (($option['value'] ?? null) === $value) {
return $option['label'] ?? null;
}
}
return null;
};
$assert($find($options['purpose'], 'backup_addon') === '自动备份开通'
&& $find($options['provider'], 'paytaro') === 'Paytaro · 支付宝'
&& $find($options['fulfillment'], 'queued') === null
&& $find($options['fulfillment'], 'review') === null,
'Server filter options diverged from row presentation labels.');
$userSource = file_get_contents(
dirname(__DIR__) . '/app/Services/Cloud/CloudInvoicePresentation.php'
);
$assert(is_string($userSource)
&& str_contains($userSource,
"CloudBillingPresentationCatalog::status(\n 'financial'")
&& str_contains($userSource,
"CloudBillingPresentationCatalog::status(\n 'fulfillment'")
&& !str_contains($userSource, 'match (strtolower($status))'),
'User invoice adapter rebuilt status labels outside the shared catalog.');
} finally {
date_default_timezone_set($previousTimezone);
}
echo "Shared billing presentation catalog and administrator adapter checks passed.\n";