Files
portal-api/tests/Unit/ProcessOrderPaymentJobTest.php
T
2025-07-24 16:18:27 +07:00

137 lines
4.4 KiB
PHP

<?php
declare(strict_types=1);
namespace Tests\Unit;
use App\Enums\Order\OrderStatus;
use App\Jobs\VirtualMachine\ProcessOrderPaymentJob;
use App\Models\Customer;
use App\Models\Invoice;
use App\Models\Order;
use App\Models\OrderItem;
use App\Models\Product;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class ProcessOrderPaymentJobTest extends TestCase
{
use RefreshDatabase;
/** @test */
public function it_can_process_order_payment_with_vm_creation()
{
// Arrange
$customer = Customer::factory()->create(['credit' => 1000000]);
$order = Order::factory()->create([
'customer_id' => $customer->id,
'amount' => 500000,
'status' => OrderStatus::PENDING->value
]);
$invoice = Invoice::factory()->create([
'order_id' => $order->id,
'amount' => 500000
]);
$product = Product::factory()->create(['product_type' => 4]); // VPS product
$orderItem = OrderItem::factory()->create([
'order_id' => $order->id,
'product_id' => $product->id,
'product_type' => 4
]);
// Act
$job = new ProcessOrderPaymentJob($order);
$job->handle(
app(\App\Http\Services\Invoice\InvoiceService::class),
app(\App\Http\Services\Discord::class)
);
// Assert
$this->assertEquals(500000, $customer->fresh()->credit); // Credit should be deducted
$this->assertEquals(OrderStatus::ACTIVE->value, $order->fresh()->status);
}
/** @test */
public function it_handles_insufficient_credit()
{
// Arrange
$customer = Customer::factory()->create(['credit' => 100000]); // Insufficient credit
$order = Order::factory()->create([
'customer_id' => $customer->id,
'amount' => 500000,
'status' => OrderStatus::PENDING->value
]);
$invoice = Invoice::factory()->create([
'order_id' => $order->id,
'amount' => 500000
]);
// Act
$job = new ProcessOrderPaymentJob($order);
$job->handle(
app(\App\Http\Services\Invoice\InvoiceService::class),
app(\App\Http\Services\Discord::class)
);
// Assert
$this->assertEquals(100000, $customer->fresh()->credit); // Credit should not be deducted
$this->assertEquals(OrderStatus::PENDING->value, $order->fresh()->status);
}
/** @test */
public function it_handles_missing_invoice()
{
// Arrange
$customer = Customer::factory()->create(['credit' => 1000000]);
$order = Order::factory()->create([
'customer_id' => $customer->id,
'amount' => 500000,
'status' => OrderStatus::PENDING->value
]);
// No invoice created
// Act
$job = new ProcessOrderPaymentJob($order);
$job->handle(
app(\App\Http\Services\Invoice\InvoiceService::class),
app(\App\Http\Services\Discord::class)
);
// Assert
$this->assertEquals(1000000, $customer->fresh()->credit); // Credit should not be deducted
$this->assertEquals(OrderStatus::PENDING->value, $order->fresh()->status);
}
/** @test */
public function it_handles_non_vps_products()
{
// Arrange
$customer = Customer::factory()->create(['credit' => 1000000]);
$order = Order::factory()->create([
'customer_id' => $customer->id,
'amount' => 500000,
'status' => OrderStatus::PENDING->value
]);
$invoice = Invoice::factory()->create([
'order_id' => $order->id,
'amount' => 500000
]);
$product = Product::factory()->create(['product_type' => 1]); // Non-VPS product
$orderItem = OrderItem::factory()->create([
'order_id' => $order->id,
'product_id' => $product->id,
'product_type' => 1
]);
// Act
$job = new ProcessOrderPaymentJob($order);
$job->handle(
app(\App\Http\Services\Invoice\InvoiceService::class),
app(\App\Http\Services\Discord::class)
);
// Assert
$this->assertEquals(1000000, $customer->fresh()->credit); // Credit should not be deducted for non-VPS
$this->assertEquals(OrderStatus::PENDING->value, $order->fresh()->status);
}
}