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); } }