From f8529b2459582ec62a6302c1e63fea3e2b3acb36 Mon Sep 17 00:00:00 2001 From: Yoga Pangestu Date: Sat, 2 May 2026 18:00:35 +0700 Subject: [PATCH] test: add validation tests for insufficient stock on purchase and order operations --- tests/Feature/Admin/Manage/OrderTest.php | 59 +++++++++++++++++++++ tests/Feature/Admin/Manage/PurchaseTest.php | 55 +++++++++++++++++++ 2 files changed, 114 insertions(+) diff --git a/tests/Feature/Admin/Manage/OrderTest.php b/tests/Feature/Admin/Manage/OrderTest.php index 51c9e0d..10c27ab 100644 --- a/tests/Feature/Admin/Manage/OrderTest.php +++ b/tests/Feature/Admin/Manage/OrderTest.php @@ -213,6 +213,65 @@ ->assertJsonValidationErrors(['customer_name', 'items']); }); + it('returns flash error when stock is insufficient on order store', function () { + $product = Product::factory()->create(['stock' => 1]); + + $data = [ + 'customer_name' => 'John Doe', + 'subtotal' => 20000, + 'discount' => 0, + 'payment' => 20000, + 'payment_method' => PaymentMethod::CASH->value, + 'order_status' => OrderStatus::PENDING->value, + 'order_channel' => OrderChannel::STORE->value, + 'items' => [ + [ + 'product_id' => $product->id, + 'qty' => 5, // lebih dari stok + 'price' => 10000, + 'total' => 50000, + 'price_type' => PriceType::RETAIL->value, + ], + ], + ]; + + postJson(route('order.store'), $data) + ->assertRedirect() + ->assertSessionHas('error'); + + expect($product->fresh()->stock)->toBe(1); + }); + + it('returns flash error when stock is insufficient on order update', function () { + $product = Product::factory()->create(['stock' => 2]); + $order = Order::factory()->create(); + + $newData = [ + 'customer_name' => 'Updated Customer', + 'subtotal' => 30000, + 'discount' => 0, + 'payment' => 30000, + 'payment_method' => PaymentMethod::CASH->value, + 'order_status' => OrderStatus::PROCESSING->value, + 'order_channel' => OrderChannel::STORE->value, + 'items' => [ + [ + 'product_id' => $product->id, + 'qty' => 10, // lebih dari stok + 'price' => 3000, + 'total' => 30000, + 'price_type' => PriceType::RETAIL->value, + ], + ], + ]; + + patchJson(route('order.update', $order), $newData) + ->assertRedirect() + ->assertSessionHas('error'); + + expect($product->fresh()->stock)->toBe(2); + }); + it('can update an order', function () { $product1 = Product::factory()->create(['stock' => 10]); $product2 = Product::factory()->create(['stock' => 10]); diff --git a/tests/Feature/Admin/Manage/PurchaseTest.php b/tests/Feature/Admin/Manage/PurchaseTest.php index eb73ae1..9e846b6 100644 --- a/tests/Feature/Admin/Manage/PurchaseTest.php +++ b/tests/Feature/Admin/Manage/PurchaseTest.php @@ -139,6 +139,61 @@ ->assertJsonValidationErrors(['purchase_date', 'items']); }); + it('returns flash error when stock is insufficient on purchase update', function () { + $product = Product::factory()->create(['stock' => 0]); + + $purchase = Purchase::create([ + 'purchase_date' => now()->format('Y-m-d'), + 'note' => 'Old Note', + 'total' => 5000, + ]); + + $purchase->items()->create([ + 'user_id' => auth()->id(), + 'product_id' => $product->id, + 'quantity' => 5, + 'unit_price' => 1000, + 'total_price' => 5000, + ]); + + $newData = [ + 'purchase_date' => now()->format('Y-m-d'), + 'note' => 'Updated Note', + 'items' => [ + [ + 'product_id' => $product->id, + 'quantity' => 10, + 'unit_price' => 2000, + ], + ], + ]; + + patchJson(route('purchase.update', $purchase), $newData) + ->assertRedirect() + ->assertSessionHas('error'); + + expect($purchase->fresh()->note)->toBe('Old Note'); + }); + + it('returns flash error when stock is insufficient on purchase delete', function () { + $product = Product::factory()->create(['stock' => 0]); + $purchase = Purchase::factory()->create(); + + $purchase->items()->create([ + 'user_id' => auth()->id(), + 'product_id' => $product->id, + 'quantity' => 5, + 'unit_price' => 1000, + 'total_price' => 5000, + ]); + + deleteJson(route('purchase.destroy', $purchase)) + ->assertRedirect() + ->assertSessionHas('error'); + + expect(Purchase::find($purchase->id))->not->toBeNull(); + }); + it('can access purchase edit page', function () { $purchase = Purchase::factory()->create();