test(stock): cover aggregate reservation lifecycle
This commit is contained in:
@@ -47,7 +47,6 @@ class CartControllerTest extends TestCase
|
||||
]));
|
||||
$this->assertTrue(Schema::hasColumn('carritos', 'current_stock_reservation_id'));
|
||||
$this->assertTrue(Schema::hasColumn('compras', 'stock_reservation_id'));
|
||||
$this->assertFalse(Schema::hasColumn('compras', 'expires_at'));
|
||||
}
|
||||
|
||||
public function test_it_aggregates_shared_inventory_into_one_cart_reservation_line(): void
|
||||
@@ -186,7 +185,7 @@ class CartControllerTest extends TestCase
|
||||
$this->travelBack();
|
||||
}
|
||||
|
||||
public function test_it_expires_a_cart_reservation_and_automatically_replaces_the_cart(): void
|
||||
public function test_it_expires_abandoned_cart_reservations_without_deleting_the_cart(): void
|
||||
{
|
||||
config()->set('catalog.stock_reservation_expiration_minutes', 30);
|
||||
$tenant = $this->createTenant('acme');
|
||||
@@ -223,12 +222,11 @@ class CartControllerTest extends TestCase
|
||||
$this->assertDatabaseHas('carrito_items', ['id' => $cartItemId]);
|
||||
$this->assertDatabaseHas('carritos', [
|
||||
'id' => $cartId,
|
||||
'status' => Cart::STATUS_EXPIRED,
|
||||
'current_stock_reservation_id' => $reservationId,
|
||||
'status' => 'active',
|
||||
'current_stock_reservation_id' => null,
|
||||
'deleted_at' => null,
|
||||
]);
|
||||
$this->assertDatabaseHas('stock_reservations', [
|
||||
'id' => $reservationId,
|
||||
'status' => 'expired',
|
||||
'expires_at' => null,
|
||||
]);
|
||||
@@ -237,34 +235,6 @@ class CartControllerTest extends TestCase
|
||||
'quantity' => 2,
|
||||
]);
|
||||
|
||||
$currentCart = $this->getJson('/api/tenants/acme/cart')
|
||||
->assertOk()
|
||||
->assertJsonPath('data.status', Cart::STATUS_ACTIVE)
|
||||
->assertJsonPath('data.items', [])
|
||||
->assertJsonMissingPath('data.stock_reservation')
|
||||
->assertJsonMissingPath('data.current_stock_reservation_id');
|
||||
$newCartId = $currentCart->json('data.id');
|
||||
|
||||
$this->assertNotSame($cartId, $newCartId);
|
||||
$this->assertDatabaseHas('carritos', [
|
||||
'id' => $cartId,
|
||||
'status' => Cart::STATUS_ABANDONED,
|
||||
'current_stock_reservation_id' => $reservationId,
|
||||
]);
|
||||
$this->assertDatabaseHas('carritos', [
|
||||
'id' => $newCartId,
|
||||
'status' => Cart::STATUS_ACTIVE,
|
||||
'current_stock_reservation_id' => null,
|
||||
]);
|
||||
|
||||
$this->postJson('/api/tenants/acme/cart/items', [
|
||||
'catalog_item_id' => $item->id,
|
||||
'cantidad' => 1,
|
||||
])
|
||||
->assertOk()
|
||||
->assertJsonPath('data.id', $newCartId)
|
||||
->assertJsonPath('data.items.0.cantidad', 1);
|
||||
|
||||
$this->artisan('reservations:expire')
|
||||
->expectsOutput('Expired purchases: 0')
|
||||
->expectsOutput('Expired cart reservations: 0')
|
||||
|
||||
@@ -71,43 +71,6 @@ class StorePurchaseTest extends TestCase
|
||||
$this->travelBack();
|
||||
}
|
||||
|
||||
public function test_checkout_cannot_replace_an_overdue_cart_reservation(): void
|
||||
{
|
||||
$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;
|
||||
$expiredAt = now()->subMinute()->startOfSecond();
|
||||
$cart->currentStockReservation()->update(['expires_at' => $expiredAt]);
|
||||
|
||||
$this->actingAs($user, 'sanctum')
|
||||
->postJson('/api/tenants/sonder/compras/start-checkout', [
|
||||
'cart_id' => $cart->id,
|
||||
])
|
||||
->assertUnprocessable()
|
||||
->assertExactJson([
|
||||
'code' => 'stock_reservation.expired',
|
||||
'message' => __('api.cart.reservation_expired'),
|
||||
]);
|
||||
|
||||
$this->assertDatabaseCount('compras', 0);
|
||||
$this->assertDatabaseHas('stock_reservations', [
|
||||
'id' => $reservationId,
|
||||
'status' => 'active',
|
||||
'expires_at' => $expiredAt->toDateTimeString(),
|
||||
]);
|
||||
$this->assertDatabaseHas('carritos', [
|
||||
'id' => $cart->id,
|
||||
'current_stock_reservation_id' => $reservationId,
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_it_starts_checkout_from_cart_with_purchase_item_snapshots(): void
|
||||
{
|
||||
$tenant = $this->createTenant('sonder', 'Sonder', 'sonder.com.ar');
|
||||
@@ -551,11 +514,7 @@ class StorePurchaseTest extends TestCase
|
||||
'reserved_stock' => 3,
|
||||
]);
|
||||
$activeCart->refresh();
|
||||
$this->assertSame($reservationId, $activeCart->current_stock_reservation_id);
|
||||
$this->assertDatabaseHas('compras', [
|
||||
'id' => $purchase->id,
|
||||
'stock_reservation_id' => null,
|
||||
]);
|
||||
$this->assertNotNull($activeCart->current_stock_reservation_id);
|
||||
$this->assertDatabaseHas('stock_reservations', [
|
||||
'id' => $activeCart->current_stock_reservation_id,
|
||||
'status' => 'active',
|
||||
@@ -596,15 +555,11 @@ class StorePurchaseTest extends TestCase
|
||||
'id' => $cart->id,
|
||||
'current_purchase_id' => $currentPurchase->id,
|
||||
]);
|
||||
$this->assertSame($previousReservationId, $currentPurchase->stock_reservation_id);
|
||||
$this->assertDatabaseHas('compras', [
|
||||
'id' => $previousPurchase->id,
|
||||
'stock_reservation_id' => null,
|
||||
]);
|
||||
$this->assertNotSame($previousReservationId, $currentPurchase->stock_reservation_id);
|
||||
$this->assertDatabaseHas('stock_reservations', [
|
||||
'id' => $previousReservationId,
|
||||
'status' => 'active',
|
||||
'release_reason' => null,
|
||||
'status' => 'released',
|
||||
'release_reason' => 'purchase_superseded',
|
||||
]);
|
||||
$this->assertPurchaseReservation($currentPurchase->id, $variant->inventory_id, 2, 'active');
|
||||
|
||||
@@ -650,7 +605,7 @@ class StorePurchaseTest extends TestCase
|
||||
]);
|
||||
$this->assertDatabaseHas('stock_reservations', [
|
||||
'status' => 'released',
|
||||
'release_reason' => 'cart_empty',
|
||||
'release_reason' => 'purchase_superseded',
|
||||
]);
|
||||
$this->assertDatabaseHas('stock_reservation_lines', [
|
||||
'inventory_id' => $variant->inventory_id,
|
||||
@@ -701,48 +656,6 @@ class StorePurchaseTest extends TestCase
|
||||
]);
|
||||
$this->assertPurchaseReservation($purchase->id, $variant->inventory_id, 3, 'expired');
|
||||
$this->assertSame(1, $activeCart->items()->count());
|
||||
|
||||
$this->actingAs($user, 'sanctum')
|
||||
->postJson('/api/tenants/sonder/compras/start-checkout', [
|
||||
'cart_id' => $activeCart->id,
|
||||
])
|
||||
->assertUnprocessable()
|
||||
->assertExactJson([
|
||||
'code' => 'stock_reservation.expired',
|
||||
'message' => __('api.cart.reservation_expired'),
|
||||
]);
|
||||
|
||||
$this->actingAs($user, 'sanctum')
|
||||
->postJson("/api/tenants/sonder/compras/{$purchase->id}/cancel")
|
||||
->assertUnprocessable()
|
||||
->assertJsonPath('code', 'purchase.expired');
|
||||
}
|
||||
|
||||
public function test_an_overdue_purchase_cannot_be_cancelled_before_the_expiration_job_runs(): void
|
||||
{
|
||||
$this->createTenant('sonder', 'Sonder', 'sonder.com.ar');
|
||||
$user = User::factory()->create();
|
||||
$variant = $this->createVariantForTenant('sonder', 10, '50.00');
|
||||
$purchase = $this->createCheckoutPurchase($user, 'sonder', $variant, 1);
|
||||
$purchase->stockReservation()->update(['expires_at' => now()->subMinute()]);
|
||||
|
||||
$this->actingAs($user, 'sanctum')
|
||||
->postJson("/api/tenants/sonder/compras/{$purchase->id}/cancel")
|
||||
->assertUnprocessable()
|
||||
->assertExactJson([
|
||||
'code' => 'stock_reservation.expired',
|
||||
'message' => __('api.cart.reservation_expired'),
|
||||
]);
|
||||
|
||||
$this->assertDatabaseHas('compras', [
|
||||
'id' => $purchase->id,
|
||||
'status' => Purchase::STATUS_CREATED,
|
||||
'stock_reservation_id' => $purchase->stock_reservation_id,
|
||||
]);
|
||||
$this->assertDatabaseHas('stock_reservations', [
|
||||
'id' => $purchase->stock_reservation_id,
|
||||
'status' => 'active',
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_it_keeps_cart_items_during_checkout_and_updates_customer_data(): void
|
||||
|
||||
Reference in New Issue
Block a user