test: add comprehensive tests for retail stock transfer functionality, including permission checks, quantity validation, and stock updates
Some checks are pending
linter / quality (push) Waiting to run
tests / ci (8.3) (push) Waiting to run
tests / ci (8.4) (push) Waiting to run
tests / ci (8.5) (push) Waiting to run

This commit is contained in:
Yoga Pangestu 2026-07-05 17:46:45 +07:00
parent 0b3bf891cc
commit 2cb283018c

View File

@ -195,3 +195,122 @@ function createCompletedCuttingSetup(): array
]);
});
});
// ─── Retail Stock Transfer ──────────────────────────────────
describe('Retail Stock Transfer', function () {
test('user with permission can transfer stock from good to retail', function () {
$user = createStockUserWithPermission(PermissionEnum::STOCKS_VIEW);
$product = Product::factory()->create();
$variant = ProductVariant::factory()->create([
'product_id' => $product->id,
'stock' => 10,
'retail_stock' => 5,
]);
$this->actingAs($user)
->postJson(route('admin.manage.retail-stock.transfer'), [
'product_variant_id' => $variant->id,
'quantity' => 3,
'notes' => 'Transfer test',
])
->assertOk();
$variant->refresh();
$this->assertEquals(7, $variant->stock);
$this->assertEquals(8, $variant->retail_stock);
$this->assertDatabaseHas('retail_stock_histories', [
'product_variant_id' => $variant->id,
'user_id' => $user->id,
'quantity' => 3,
'stock_before' => 10,
'retail_stock_before' => 5,
'stock_after' => 7,
'retail_stock_after' => 8,
'notes' => 'Transfer test',
]);
});
test('retail stock transfer does not require owner verification', function () {
$user = createStockUserWithPermission(PermissionEnum::STOCKS_VIEW);
$product = Product::factory()->create();
$variant = ProductVariant::factory()->create([
'product_id' => $product->id,
'stock' => 10,
'retail_stock' => 5,
]);
$this->actingAs($user)
->postJson(route('admin.manage.retail-stock.transfer'), [
'product_variant_id' => $variant->id,
'quantity' => 3,
])
->assertOk();
$this->assertDatabaseCount('owner_verification_requests', 0);
$variant->refresh();
$this->assertEquals(7, $variant->stock);
$this->assertEquals(8, $variant->retail_stock);
});
test('retail stock transfer fails when quantity exceeds stock', function () {
$user = createStockUserWithPermission(PermissionEnum::STOCKS_VIEW);
$product = Product::factory()->create();
$variant = ProductVariant::factory()->create([
'product_id' => $product->id,
'stock' => 5,
'retail_stock' => 0,
]);
$this->actingAs($user)
->postJson(route('admin.manage.retail-stock.transfer'), [
'product_variant_id' => $variant->id,
'quantity' => 10,
])
->assertUnprocessable()
->assertJsonValidationErrors('quantity');
});
test('retail stock transfer fails when quantity is zero', function () {
$user = createStockUserWithPermission(PermissionEnum::STOCKS_VIEW);
$product = Product::factory()->create();
$variant = ProductVariant::factory()->create([
'product_id' => $product->id,
'stock' => 10,
'retail_stock' => 5,
]);
$this->actingAs($user)
->postJson(route('admin.manage.retail-stock.transfer'), [
'product_variant_id' => $variant->id,
'quantity' => 0,
])
->assertUnprocessable()
->assertJsonValidationErrors('quantity');
});
test('user without permission cannot transfer stock', function () {
$user = User::factory()->create();
$product = Product::factory()->create();
$variant = ProductVariant::factory()->create([
'product_id' => $product->id,
'stock' => 10,
'retail_stock' => 5,
]);
$this->actingAs($user)
->postJson(route('admin.manage.retail-stock.transfer'), [
'product_variant_id' => $variant->id,
'quantity' => 3,
])
->assertForbidden();
});
});