Files
portal-api/tests/Feature/Console/PostWhmcsSyncSafetyTest.php
T
2026-09-23 15:17:18 +07:00

183 lines
6.7 KiB
PHP

<?php
declare(strict_types=1);
namespace Tests\Feature\Console;
use App\Enums\Invoice\InvoiceStatus;
use App\Enums\Order\OrderStatus;
use App\Models\Customer;
use App\Models\Invoice;
use App\Models\Order;
use App\Models\OrderItem;
use App\Models\Product;
use App\Models\Service;
use App\Repositories\Api\Order\OrderRepository;
use App\Services\InvoiceReminderService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Queue;
use Tests\TestCase;
/**
* Dữ liệu import từ WHMCS mang theo hàng loạt hoá đơn quá hạn nhiều năm, đơn pending cũ và
* dịch vụ đã hết hạn. Các test này chặn những hậu quả dây chuyền ngay sau lần sync đầu tiên.
*/
class PostWhmcsSyncSafetyTest extends TestCase
{
use RefreshDatabase;
protected function setUp(): void
{
parent::setUp();
Queue::fake();
config(['billing.reminder_offsets' => [-7, -3, 0, 3, 7]]);
}
/** Hoá đơn quá hạn nhiều năm KHÔNG được mở lại chuỗi nhắc nợ. */
public function test_reminder_chain_does_not_reopen_for_long_overdue_legacy_invoices(): void
{
$invoice = $this->makeInvoice(now()->subYears(2), InvoiceStatus::OVERDUE->value);
$this->artisan('invoices:send-reminders')->assertExitCode(0);
$this->assertSame(0, (int) $invoice->fresh()->reminder_step);
$this->assertSame(0, app(InvoiceReminderService::class)->dueStep($invoice->fresh()));
}
/** Hoá đơn vừa quá hạn trong cửa sổ nhắc thì vẫn phải được nhắc như thường. */
public function test_recently_overdue_invoice_is_still_reminded(): void
{
$invoice = $this->makeInvoice(now()->subDays(3), InvoiceStatus::OVERDUE->value);
$this->artisan('invoices:send-reminders')->assertExitCode(0);
$this->assertGreaterThan(0, (int) $invoice->fresh()->reminder_step);
}
/** Admin vẫn nhắc tay được hoá đơn cũ dù chuỗi tự động đã đóng. */
public function test_manual_reminder_still_works_on_an_old_invoice(): void
{
$invoice = $this->makeInvoice(now()->subYears(2), InvoiceStatus::OVERDUE->value);
$this->assertTrue(
app(InvoiceReminderService::class)->remind($invoice->load('customer'), force: true)
);
}
/** Đơn import từ WHMCS ở trạng thái pending không được khoá khách đặt đơn mới. */
public function test_legacy_pending_order_does_not_block_new_orders(): void
{
$customer = Customer::factory()->create();
Order::create([
'legacy_id' => 90001,
'order_num' => 'WHMCS-' . uniqid(),
'customer_id' => $customer->id,
'sub_total' => 500_000,
'total' => 500_000,
'status' => OrderStatus::PENDING->value,
]);
$this->assertFalse(app(OrderRepository::class)->hasPendingOrder($customer->id));
}
/** Đơn pending tạo trên portal thì vẫn phải chặn như cũ. */
public function test_local_pending_order_still_blocks_new_orders(): void
{
$customer = Customer::factory()->create();
Order::create([
'order_num' => 'ORD-' . uniqid(),
'customer_id' => $customer->id,
'sub_total' => 500_000,
'total' => 500_000,
'status' => OrderStatus::PENDING->value,
]);
$this->assertTrue(app(OrderRepository::class)->hasPendingOrder($customer->id));
}
/**
* Dịch vụ import có invoice_id NULL dù hoá đơn gia hạn của nó đã được đồng bộ —
* không được xuất thêm hoá đơn trùng kỳ.
*/
public function test_expiry_does_not_duplicate_an_invoice_already_synced_on_the_order(): void
{
$customer = Customer::factory()->create();
$product = Product::factory()->create(['product_type' => 'hosting', 'is_active' => true]);
$order = Order::create([
'legacy_id' => 90002,
'order_num' => 'WHMCS-' . uniqid(),
'customer_id' => $customer->id,
'sub_total' => 90_000,
'total' => 90_000,
'status' => OrderStatus::ACTIVE->value,
]);
$orderItem = OrderItem::create([
'order_id' => $order->id,
'product_id' => $product->id,
'billing_cycle' => 'monthly',
'months' => 1,
'quantity' => 1,
'price' => 90_000,
]);
// Hoá đơn gia hạn đã đồng bộ từ WHMCS, còn chưa thanh toán.
Invoice::create([
'legacy_id' => 90003,
'invoice_number' => 'WHMCS-INV-' . uniqid(),
'customer_id' => $customer->id,
'order_id' => $order->id,
'invoice_type' => 2,
'sub_total' => 90_000,
'tax' => 0,
'total' => 90_000,
'status' => InvoiceStatus::UNPAID->value,
'due_date' => now()->addDays(3)->toDateString(),
]);
// Service import: invoice_id để trống đúng như ServiceWriter ghi.
$service = Service::create([
'customer_id' => $customer->id,
'product_id' => $product->id,
'order_item_id' => $orderItem->id,
'status' => 'active',
'server_name' => 'legacy-server',
'expires_at' => now()->addDays(3),
]);
$this->assertNull($service->invoice_id);
$this->artisan('service:expiry')->assertExitCode(0);
// Vẫn đúng 1 hoá đơn — và service đã được trỏ về hoá đơn đang mở.
$this->assertSame(1, Invoice::where('order_id', $order->id)->count());
$this->assertNotNull($service->fresh()->invoice_id);
}
private function makeInvoice(\Carbon\Carbon $dueDate, int $status): Invoice
{
$customer = Customer::factory()->create();
$order = Order::create([
'order_num' => 'ORD-' . uniqid(),
'customer_id' => $customer->id,
'sub_total' => 100_000,
'total' => 100_000,
'status' => OrderStatus::PENDING->value,
]);
return Invoice::create([
'invoice_number' => 'INV-' . uniqid(),
'customer_id' => $customer->id,
'order_id' => $order->id,
'invoice_type' => 1,
'sub_total' => 100_000,
'tax' => 0,
'total' => 100_000,
'status' => $status,
'due_date' => $dueDate->toDateString(),
]);
}
}