test(stock): cover aggregate reservation lifecycle
This commit is contained in:
@@ -32,6 +32,45 @@ class StorePurchaseTest extends TestCase
|
||||
Queue::fake();
|
||||
}
|
||||
|
||||
public function test_checkout_keeps_the_cart_reservation_and_refreshes_its_expiration(): void
|
||||
{
|
||||
config()->set('catalog.stock_reservation_expiration_minutes', 5);
|
||||
config()->set('purchase.checkout_expiration_minutes', 30);
|
||||
$now = now()->startOfSecond();
|
||||
$this->travelTo($now);
|
||||
|
||||
$tenant = $this->createTenant('sonder', 'Sonder', 'sonder.com.ar');
|
||||
$user = User::factory()->create();
|
||||
$variant = $this->createVariantForTenant('sonder', 10, '50.00');
|
||||
$cart = Cart::query()->create([
|
||||
'tenant_codigo' => $tenant->codigo,
|
||||
'user_id' => $user->id,
|
||||
'status' => 'active',
|
||||
]);
|
||||
$cart->addItem($variant->catalog_item_id, $variant->id, 2);
|
||||
$reservationId = $cart->fresh()->current_stock_reservation_id;
|
||||
|
||||
$this->assertDatabaseHas('stock_reservations', [
|
||||
'id' => $reservationId,
|
||||
'status' => 'active',
|
||||
'expires_at' => $now->copy()->addMinutes(5)->toDateTimeString(),
|
||||
]);
|
||||
|
||||
$this->travel(2)->minutes();
|
||||
$purchase = app(CheckoutService::class)->startCheckout($tenant, $user->id, [
|
||||
'cart_id' => $cart->id,
|
||||
]);
|
||||
|
||||
$this->assertSame($reservationId, $purchase->stock_reservation_id);
|
||||
$this->assertDatabaseHas('stock_reservations', [
|
||||
'id' => $reservationId,
|
||||
'status' => 'active',
|
||||
'expires_at' => $now->copy()->addMinutes(32)->toDateTimeString(),
|
||||
]);
|
||||
|
||||
$this->travelBack();
|
||||
}
|
||||
|
||||
public function test_it_starts_checkout_from_cart_with_purchase_item_snapshots(): void
|
||||
{
|
||||
$tenant = $this->createTenant('sonder', 'Sonder', 'sonder.com.ar');
|
||||
@@ -119,12 +158,7 @@ class StorePurchaseTest extends TestCase
|
||||
'precio_unitario' => '50.00',
|
||||
'total' => '100.00',
|
||||
]);
|
||||
$this->assertDatabaseHas('stock_reservations', [
|
||||
'inventory_id' => $inventory->id,
|
||||
'purchase_id' => $purchaseId,
|
||||
'quantity' => 2,
|
||||
'status' => 'active',
|
||||
]);
|
||||
$this->assertPurchaseReservation($purchaseId, $inventory->id, 2, 'active');
|
||||
$this->assertDatabaseHas('carritos', [
|
||||
'id' => $cartId,
|
||||
'user_id' => $user->id,
|
||||
@@ -168,12 +202,7 @@ class StorePurchaseTest extends TestCase
|
||||
'id' => $cartId,
|
||||
'current_purchase_id' => $replacementPurchaseId,
|
||||
]);
|
||||
$this->assertDatabaseHas('stock_reservations', [
|
||||
'inventory_id' => $inventory->id,
|
||||
'purchase_id' => $replacementPurchaseId,
|
||||
'quantity' => 2,
|
||||
'status' => 'active',
|
||||
]);
|
||||
$this->assertPurchaseReservation($replacementPurchaseId, $inventory->id, 2, 'active');
|
||||
$this->assertDatabaseHas('inventories', [
|
||||
'id' => $inventory->id,
|
||||
'reserved_stock' => 2,
|
||||
@@ -227,12 +256,7 @@ class StorePurchaseTest extends TestCase
|
||||
'precio_unitario' => '50.00',
|
||||
'total' => '150.00',
|
||||
]);
|
||||
$this->assertDatabaseHas('stock_reservations', [
|
||||
'inventory_id' => $variant->inventory_id,
|
||||
'purchase_id' => $response->json('data.id'),
|
||||
'quantity' => 3,
|
||||
'status' => 'active',
|
||||
]);
|
||||
$this->assertPurchaseReservation($response->json('data.id'), $variant->inventory_id, 3, 'active');
|
||||
$this->assertDatabaseHas('inventories', [
|
||||
'id' => $variant->inventory_id,
|
||||
'real_stock' => 10,
|
||||
@@ -244,10 +268,7 @@ class StorePurchaseTest extends TestCase
|
||||
->assertOk()
|
||||
->assertJsonPath('data.status', Purchase::STATUS_CANCELLED);
|
||||
|
||||
$this->assertDatabaseHas('stock_reservations', [
|
||||
'purchase_id' => $response->json('data.id'),
|
||||
'status' => 'released',
|
||||
]);
|
||||
$this->assertPurchaseReservation($response->json('data.id'), $variant->inventory_id, 3, 'released');
|
||||
$this->assertDatabaseHas('inventories', [
|
||||
'id' => $variant->inventory_id,
|
||||
'real_stock' => 10,
|
||||
@@ -296,18 +317,8 @@ class StorePurchaseTest extends TestCase
|
||||
'origin' => Cart::ORIGIN_DIRECT_CHECKOUT,
|
||||
]);
|
||||
$this->assertDatabaseCount('compra_items', 2);
|
||||
$this->assertDatabaseHas('stock_reservations', [
|
||||
'inventory_id' => $firstVariant->inventory_id,
|
||||
'purchase_id' => $purchaseId,
|
||||
'quantity' => 1,
|
||||
'status' => 'active',
|
||||
]);
|
||||
$this->assertDatabaseHas('stock_reservations', [
|
||||
'inventory_id' => $secondVariant->inventory_id,
|
||||
'purchase_id' => $purchaseId,
|
||||
'quantity' => 1,
|
||||
'status' => 'active',
|
||||
]);
|
||||
$this->assertPurchaseReservation($purchaseId, $firstVariant->inventory_id, 1, 'active');
|
||||
$this->assertPurchaseReservation($purchaseId, $secondVariant->inventory_id, 1, 'active');
|
||||
$this->assertDatabaseHas('inventories', [
|
||||
'id' => $firstVariant->inventory_id,
|
||||
'reserved_stock' => 1,
|
||||
@@ -430,7 +441,7 @@ class StorePurchaseTest extends TestCase
|
||||
$tenant = $this->createTenant('sonder', 'Sonder', 'sonder.com.ar');
|
||||
$user = User::factory()->create();
|
||||
$firstVariant = $this->createVariantForTenant('sonder', 20, '50.00');
|
||||
$firstVariant->catalogItem->update(['max_units_per_user' => 3]);
|
||||
$firstVariant->catalogItem->update(['max_units_per_user' => 4]);
|
||||
$secondInventory = Inventory::query()->create(['real_stock' => 20]);
|
||||
$secondVariant = Variant::query()->create([
|
||||
'catalog_item_id' => $firstVariant->catalog_item_id,
|
||||
@@ -443,6 +454,7 @@ class StorePurchaseTest extends TestCase
|
||||
]);
|
||||
$cart->addItem($firstVariant->catalog_item_id, $firstVariant->id, 2);
|
||||
$cart->addItem($secondVariant->catalog_item_id, $secondVariant->id, 2);
|
||||
$firstVariant->catalogItem->update(['max_units_per_user' => 3]);
|
||||
|
||||
$this->actingAs($user, 'sanctum')
|
||||
->postJson('/api/tenants/sonder/compras/start-checkout', [
|
||||
@@ -500,12 +512,17 @@ class StorePurchaseTest extends TestCase
|
||||
'id' => $variant->inventory_id,
|
||||
'reserved_stock' => 3,
|
||||
]);
|
||||
$activeCart->refresh();
|
||||
$this->assertNotNull($activeCart->current_stock_reservation_id);
|
||||
$this->assertDatabaseHas('stock_reservations', [
|
||||
'cart_item_id' => $cartItemId,
|
||||
'purchase_id' => null,
|
||||
'quantity' => 3,
|
||||
'id' => $activeCart->current_stock_reservation_id,
|
||||
'status' => 'active',
|
||||
]);
|
||||
$this->assertDatabaseHas('stock_reservation_lines', [
|
||||
'stock_reservation_id' => $activeCart->current_stock_reservation_id,
|
||||
'inventory_id' => $variant->inventory_id,
|
||||
'quantity' => 3,
|
||||
]);
|
||||
$this->assertSame(1, $activeCart->items()->count());
|
||||
}
|
||||
|
||||
@@ -516,6 +533,7 @@ class StorePurchaseTest extends TestCase
|
||||
$variant = $this->createVariantForTenant('sonder', 10, '50.00');
|
||||
$previousPurchase = $this->createCheckoutPurchase($user, 'sonder', $variant, 2);
|
||||
$cart = $previousPurchase->cart;
|
||||
$previousReservationId = $previousPurchase->stock_reservation_id;
|
||||
|
||||
$previousPurchase->update([
|
||||
'status' => Purchase::STATUS_PENDING_PAYMENT,
|
||||
@@ -538,11 +556,13 @@ class StorePurchaseTest extends TestCase
|
||||
'id' => $cart->id,
|
||||
'current_purchase_id' => $currentPurchase->id,
|
||||
]);
|
||||
$this->assertNotSame($previousReservationId, $currentPurchase->stock_reservation_id);
|
||||
$this->assertDatabaseHas('stock_reservations', [
|
||||
'purchase_id' => $currentPurchase->id,
|
||||
'quantity' => 2,
|
||||
'status' => 'active',
|
||||
'id' => $previousReservationId,
|
||||
'status' => 'released',
|
||||
'release_reason' => 'purchase_superseded',
|
||||
]);
|
||||
$this->assertPurchaseReservation($currentPurchase->id, $variant->inventory_id, 2, 'active');
|
||||
|
||||
try {
|
||||
app(CheckoutService::class)->confirmPaidPurchase($previousPurchase->fresh());
|
||||
@@ -556,11 +576,7 @@ class StorePurchaseTest extends TestCase
|
||||
'reserved_stock' => 2,
|
||||
'sold_units' => 0,
|
||||
]);
|
||||
$this->assertDatabaseHas('stock_reservations', [
|
||||
'purchase_id' => $currentPurchase->id,
|
||||
'quantity' => 2,
|
||||
'status' => 'active',
|
||||
]);
|
||||
$this->assertPurchaseReservation($currentPurchase->id, $variant->inventory_id, 2, 'active');
|
||||
}
|
||||
|
||||
public function test_removing_a_checkout_item_supersedes_the_purchase_and_restores_the_user_quota(): void
|
||||
@@ -589,11 +605,12 @@ class StorePurchaseTest extends TestCase
|
||||
'current_purchase_id' => null,
|
||||
]);
|
||||
$this->assertDatabaseHas('stock_reservations', [
|
||||
'inventory_id' => $variant->inventory_id,
|
||||
'cart_item_id' => null,
|
||||
'purchase_id' => null,
|
||||
'quantity' => 0,
|
||||
'status' => 'released',
|
||||
'release_reason' => 'purchase_superseded',
|
||||
]);
|
||||
$this->assertDatabaseHas('stock_reservation_lines', [
|
||||
'inventory_id' => $variant->inventory_id,
|
||||
'quantity' => 2,
|
||||
]);
|
||||
$this->assertDatabaseHas('inventories', [
|
||||
'id' => $variant->inventory_id,
|
||||
@@ -628,7 +645,7 @@ class StorePurchaseTest extends TestCase
|
||||
]);
|
||||
$this->assertDatabaseHas('inventories', [
|
||||
'id' => $variant->inventory_id,
|
||||
'reserved_stock' => 3,
|
||||
'reserved_stock' => 0,
|
||||
]);
|
||||
$this->assertDatabaseHas('carritos', [
|
||||
'id' => $activeCart->id,
|
||||
@@ -637,12 +654,7 @@ class StorePurchaseTest extends TestCase
|
||||
'current_purchase_id' => null,
|
||||
'deleted_at' => null,
|
||||
]);
|
||||
$this->assertDatabaseHas('stock_reservations', [
|
||||
'cart_item_id' => $cartItemId,
|
||||
'purchase_id' => null,
|
||||
'quantity' => 3,
|
||||
'status' => 'active',
|
||||
]);
|
||||
$this->assertPurchaseReservation($purchase->id, $variant->inventory_id, 3, 'expired');
|
||||
$this->assertSame(1, $activeCart->items()->count());
|
||||
}
|
||||
|
||||
@@ -912,10 +924,7 @@ class StorePurchaseTest extends TestCase
|
||||
'current_purchase_id' => null,
|
||||
'deleted_at' => null,
|
||||
]);
|
||||
$this->assertDatabaseHas('stock_reservations', [
|
||||
'purchase_id' => $purchase->id,
|
||||
'status' => 'active',
|
||||
]);
|
||||
$this->assertPurchaseReservation($purchase->id, $variant->inventory_id, 2, 'active');
|
||||
}
|
||||
|
||||
public function test_it_rejects_review_for_a_purchase_that_is_not_awaiting_payment(): void
|
||||
@@ -957,7 +966,7 @@ class StorePurchaseTest extends TestCase
|
||||
|
||||
$this->artisan('reservations:expire')
|
||||
->expectsOutput('Expired purchases: 1')
|
||||
->expectsOutput('Expired cart items: 0')
|
||||
->expectsOutput('Expired cart reservations: 0')
|
||||
->assertSuccessful();
|
||||
|
||||
$this->assertDatabaseHas('compras', [
|
||||
@@ -968,15 +977,11 @@ class StorePurchaseTest extends TestCase
|
||||
'compra_id' => $purchase->id,
|
||||
'cantidad' => 3,
|
||||
]);
|
||||
$this->assertDatabaseHas('stock_reservations', [
|
||||
'purchase_id' => null,
|
||||
'quantity' => 3,
|
||||
'status' => 'active',
|
||||
]);
|
||||
$this->assertPurchaseReservation($purchase->id, $variant->inventory_id, 3, 'expired');
|
||||
$this->assertDatabaseHas('inventories', [
|
||||
'id' => $variant->inventory_id,
|
||||
'real_stock' => 10,
|
||||
'reserved_stock' => 3,
|
||||
'reserved_stock' => 0,
|
||||
'sold_units' => 0,
|
||||
]);
|
||||
$this->assertDatabaseHas('carritos', [
|
||||
@@ -989,7 +994,7 @@ class StorePurchaseTest extends TestCase
|
||||
|
||||
$this->artisan('reservations:expire')
|
||||
->expectsOutput('Expired purchases: 0')
|
||||
->expectsOutput('Expired cart items: 0')
|
||||
->expectsOutput('Expired cart reservations: 0')
|
||||
->assertSuccessful();
|
||||
}
|
||||
|
||||
@@ -1006,6 +1011,7 @@ class StorePurchaseTest extends TestCase
|
||||
'source_catalog_item_id' => $variant->catalog_item_id,
|
||||
'source_variant_id' => $variant->id,
|
||||
'nombre' => 'Inconsistent item',
|
||||
'item_nombre' => 'Inconsistent item',
|
||||
'slug' => 'inconsistent-item',
|
||||
'cantidad' => 1,
|
||||
'precio_unitario' => '50.00',
|
||||
@@ -1018,7 +1024,7 @@ class StorePurchaseTest extends TestCase
|
||||
|
||||
$this->artisan('reservations:expire')
|
||||
->expectsOutput('Expired purchases: 2')
|
||||
->expectsOutput('Expired cart items: 0')
|
||||
->expectsOutput('Expired cart reservations: 0')
|
||||
->assertSuccessful();
|
||||
|
||||
$this->assertDatabaseHas('compras', [
|
||||
@@ -1160,12 +1166,7 @@ class StorePurchaseTest extends TestCase
|
||||
'reserved_stock' => 0,
|
||||
'sold_units' => 2,
|
||||
]);
|
||||
$this->assertDatabaseHas('stock_reservations', [
|
||||
'purchase_id' => $purchase->id,
|
||||
'inventory_id' => $variant->inventory_id,
|
||||
'quantity' => 2,
|
||||
'status' => 'committed',
|
||||
]);
|
||||
$this->assertPurchaseReservation($purchase->id, $variant->inventory_id, 2, 'committed');
|
||||
|
||||
$this->assertSoftDeleted('carritos', [
|
||||
'id' => $purchase->cart_id,
|
||||
@@ -1440,6 +1441,26 @@ class StorePurchaseTest extends TestCase
|
||||
]);
|
||||
}
|
||||
|
||||
protected function assertPurchaseReservation(
|
||||
int $purchaseId,
|
||||
int $inventoryId,
|
||||
int $quantity,
|
||||
string $status,
|
||||
): void {
|
||||
$reservationId = Purchase::query()->findOrFail($purchaseId)->stock_reservation_id;
|
||||
|
||||
$this->assertNotNull($reservationId);
|
||||
$this->assertDatabaseHas('stock_reservations', [
|
||||
'id' => $reservationId,
|
||||
'status' => $status,
|
||||
]);
|
||||
$this->assertDatabaseHas('stock_reservation_lines', [
|
||||
'stock_reservation_id' => $reservationId,
|
||||
'inventory_id' => $inventoryId,
|
||||
'quantity' => $quantity,
|
||||
]);
|
||||
}
|
||||
|
||||
protected function createTenant(string $codigo, string $nombre, string $dominio): Tenant
|
||||
{
|
||||
$hdrKey = (string) Str::uuid();
|
||||
|
||||
Reference in New Issue
Block a user