test: add validation tests for insufficient stock on purchase and order operations

This commit is contained in:
Yoga Pangestu 2026-05-02 18:00:35 +07:00
parent 23cc07596c
commit f8529b2459
2 changed files with 114 additions and 0 deletions

View File

@ -213,6 +213,65 @@
->assertJsonValidationErrors(['customer_name', 'items']); ->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 () { it('can update an order', function () {
$product1 = Product::factory()->create(['stock' => 10]); $product1 = Product::factory()->create(['stock' => 10]);
$product2 = Product::factory()->create(['stock' => 10]); $product2 = Product::factory()->create(['stock' => 10]);

View File

@ -139,6 +139,61 @@
->assertJsonValidationErrors(['purchase_date', 'items']); ->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 () { it('can access purchase edit page', function () {
$purchase = Purchase::factory()->create(); $purchase = Purchase::factory()->create();