test(stock): cover aggregate reservation lifecycle

This commit is contained in:
2026-08-25 15:05:29 -03:00
parent 24bfef431b
commit e5f7ba3615
6 changed files with 222 additions and 104 deletions

View File

@@ -5,10 +5,12 @@ namespace Tests\Feature\Cart;
use App\Domains\Attachable\Enums\AttachmentType; use App\Domains\Attachable\Enums\AttachmentType;
use App\Domains\Attachable\Models\Attachment; use App\Domains\Attachable\Models\Attachment;
use App\Domains\Auth\Models\User; use App\Domains\Auth\Models\User;
use App\Domains\Cart\Models\Cart;
use App\Domains\Catalog\Enums\InventoryPolicy; use App\Domains\Catalog\Enums\InventoryPolicy;
use App\Domains\Catalog\Models\CatalogItem; use App\Domains\Catalog\Models\CatalogItem;
use App\Domains\Catalog\Models\Inventory; use App\Domains\Catalog\Models\Inventory;
use App\Domains\Catalog\Models\Variant; use App\Domains\Catalog\Models\Variant;
use App\Domains\Catalog\Services\CatalogService;
use App\Domains\Purchase\Models\Purchase; use App\Domains\Purchase\Models\Purchase;
use App\Domains\Purchase\Models\PurchaseItem; use App\Domains\Purchase\Models\PurchaseItem;
use App\Domains\Tenant\Models\Tenant; use App\Domains\Tenant\Models\Tenant;
@@ -28,6 +30,80 @@ class CartControllerTest extends TestCase
])); ]));
$this->assertFalse(Schema::hasColumn('carrito_items', 'buyable_type')); $this->assertFalse(Schema::hasColumn('carrito_items', 'buyable_type'));
$this->assertFalse(Schema::hasColumn('carrito_items', 'buyable_id')); $this->assertFalse(Schema::hasColumn('carrito_items', 'buyable_id'));
$this->assertTrue(Schema::hasColumns('stock_reservations', [
'status',
'expires_at',
'committed_at',
'released_at',
'expired_at',
'release_reason',
]));
$this->assertFalse(Schema::hasColumn('stock_reservations', 'quantity'));
$this->assertTrue(Schema::hasColumns('stock_reservation_lines', [
'stock_reservation_id',
'inventory_id',
'quantity',
'tracks_inventory',
]));
$this->assertTrue(Schema::hasColumn('carritos', 'current_stock_reservation_id'));
$this->assertTrue(Schema::hasColumn('compras', 'stock_reservation_id'));
}
public function test_it_aggregates_shared_inventory_into_one_cart_reservation_line(): void
{
$tenant = $this->createTenant('acme');
$firstItem = $this->createDirectItem($tenant, 10, '10.00');
$secondItem = app(CatalogService::class)->create([
'tenant_code' => $tenant->codigo,
'type' => 'bundle',
'slug' => 'second-shared-item',
'nombre' => 'Second shared item',
'precio' => '20.00',
'components' => [[
'catalog_item_id' => $firstItem->id,
'quantity' => 1,
]],
]);
$firstResponse = $this->postJson('/api/tenants/acme/cart/items', [
'catalog_item_id' => $firstItem->id,
'cantidad' => 2,
])->assertOk();
$guestToken = $firstResponse->getCookie('guest_token', false)?->getValue();
$this->call(
'POST',
'/api/tenants/acme/cart/items',
[],
['guest_token' => $guestToken],
[],
['HTTP_Accept' => 'application/json', 'CONTENT_TYPE' => 'application/json'],
json_encode([
'catalog_item_id' => $secondItem->id,
'cantidad' => 3,
]),
)->assertOk();
$reservationId = (int) $firstResponse->json('data.id');
$reservationId = (int) Cart::query()
->findOrFail($reservationId)
->current_stock_reservation_id;
$this->assertDatabaseCount('stock_reservations', 1);
$this->assertDatabaseHas('stock_reservations', [
'id' => $reservationId,
'status' => 'active',
]);
$this->assertDatabaseHas('stock_reservation_lines', [
'stock_reservation_id' => $reservationId,
'inventory_id' => $firstItem->inventory_id,
'quantity' => 5,
]);
$this->assertDatabaseCount('stock_reservation_lines', 1);
$this->assertDatabaseHas('inventories', [
'id' => $firstItem->inventory_id,
'reserved_stock' => 5,
]);
} }
public function test_it_adds_a_catalog_item_without_a_variant(): void public function test_it_adds_a_catalog_item_without_a_variant(): void
@@ -74,9 +150,11 @@ class CartControllerTest extends TestCase
'id' => $item->inventory_id, 'id' => $item->inventory_id,
'reserved_stock' => 2, 'reserved_stock' => 2,
]); ]);
$this->assertDatabaseHas('stock_reservations', [ $this->assertDatabaseHas('stock_reservation_lines', [
'inventory_id' => $item->inventory_id, 'inventory_id' => $item->inventory_id,
'quantity' => 2, 'quantity' => 2,
]);
$this->assertDatabaseHas('stock_reservations', [
'status' => 'active', 'status' => 'active',
]); ]);
} }
@@ -96,16 +174,18 @@ class CartControllerTest extends TestCase
])->assertOk(); ])->assertOk();
$this->assertDatabaseHas('stock_reservations', [ $this->assertDatabaseHas('stock_reservations', [
'inventory_id' => $item->inventory_id,
'quantity' => 2,
'status' => 'active', 'status' => 'active',
'expires_at' => $now->copy()->addMinutes(45)->toDateTimeString(), 'expires_at' => $now->copy()->addMinutes(45)->toDateTimeString(),
]); ]);
$this->assertDatabaseHas('stock_reservation_lines', [
'inventory_id' => $item->inventory_id,
'quantity' => 2,
]);
$this->travelBack(); $this->travelBack();
} }
public function test_it_expires_abandoned_cart_reservations_and_removes_empty_carts(): void public function test_it_expires_abandoned_cart_reservations_without_deleting_the_cart(): void
{ {
config()->set('catalog.stock_reservation_expiration_minutes', 30); config()->set('catalog.stock_reservation_expiration_minutes', 30);
$tenant = $this->createTenant('acme'); $tenant = $this->createTenant('acme');
@@ -120,14 +200,14 @@ class CartControllerTest extends TestCase
$this->artisan('reservations:expire') $this->artisan('reservations:expire')
->expectsOutput('Expired purchases: 0') ->expectsOutput('Expired purchases: 0')
->expectsOutput('Expired cart items: 0') ->expectsOutput('Expired cart reservations: 0')
->assertSuccessful(); ->assertSuccessful();
$this->travel(31)->minutes(); $this->travel(31)->minutes();
$this->artisan('reservations:expire') $this->artisan('reservations:expire')
->expectsOutput('Expired purchases: 0') ->expectsOutput('Expired purchases: 0')
->expectsOutput('Expired cart items: 1') ->expectsOutput('Expired cart reservations: 1')
->assertSuccessful(); ->assertSuccessful();
$this->assertDatabaseHas('inventories', [ $this->assertDatabaseHas('inventories', [
@@ -135,23 +215,25 @@ class CartControllerTest extends TestCase
'real_stock' => 10, 'real_stock' => 10,
'reserved_stock' => 0, 'reserved_stock' => 0,
]); ]);
$this->assertDatabaseMissing('carrito_items', ['id' => $cartItemId]); $this->assertDatabaseHas('carrito_items', ['id' => $cartItemId]);
$this->assertSoftDeleted('carritos', [ $this->assertDatabaseHas('carritos', [
'id' => $cartId, 'id' => $cartId,
'status' => 'expired', 'status' => 'active',
'current_stock_reservation_id' => null,
'deleted_at' => null,
]); ]);
$this->assertDatabaseHas('stock_reservations', [ $this->assertDatabaseHas('stock_reservations', [
'inventory_id' => $item->inventory_id,
'cart_item_id' => null,
'purchase_id' => null,
'quantity' => 0,
'status' => 'expired', 'status' => 'expired',
'expires_at' => null, 'expires_at' => null,
]); ]);
$this->assertDatabaseHas('stock_reservation_lines', [
'inventory_id' => $item->inventory_id,
'quantity' => 2,
]);
$this->artisan('reservations:expire') $this->artisan('reservations:expire')
->expectsOutput('Expired purchases: 0') ->expectsOutput('Expired purchases: 0')
->expectsOutput('Expired cart items: 0') ->expectsOutput('Expired cart reservations: 0')
->assertSuccessful(); ->assertSuccessful();
$this->travelBack(); $this->travelBack();
@@ -212,10 +294,11 @@ class CartControllerTest extends TestCase
'id' => $variant->inventory_id, 'id' => $variant->inventory_id,
'reserved_stock' => 5, 'reserved_stock' => 5,
]); ]);
$this->assertDatabaseHas('stock_reservations', [ $this->assertDatabaseHas('stock_reservation_lines', [
'cart_item_id' => $response->json('data.items.0.id'),
'inventory_id' => $variant->inventory_id, 'inventory_id' => $variant->inventory_id,
'quantity' => 5, 'quantity' => 5,
]);
$this->assertDatabaseHas('stock_reservations', [
'status' => 'active', 'status' => 'active',
]); ]);
} }
@@ -229,6 +312,7 @@ class CartControllerTest extends TestCase
$this->createPurchaseItem($tenant, $user, $item, 1); $this->createPurchaseItem($tenant, $user, $item, 1);
$this->actingAs($user, 'sanctum') $this->actingAs($user, 'sanctum')
->withHeader('Accept-Language', 'es')
->postJson('/api/tenants/acme/cart/items', [ ->postJson('/api/tenants/acme/cart/items', [
'catalog_item_id' => $item->id, 'catalog_item_id' => $item->id,
'cantidad' => 2, 'cantidad' => 2,
@@ -237,6 +321,7 @@ class CartControllerTest extends TestCase
->assertJsonPath('data.items.0.cantidad', 2); ->assertJsonPath('data.items.0.cantidad', 2);
$this->actingAs($user, 'sanctum') $this->actingAs($user, 'sanctum')
->withHeader('Accept-Language', 'es')
->postJson('/api/tenants/acme/cart/items', [ ->postJson('/api/tenants/acme/cart/items', [
'catalog_item_id' => $item->id, 'catalog_item_id' => $item->id,
'cantidad' => 2, 'cantidad' => 2,
@@ -381,10 +466,12 @@ class CartControllerTest extends TestCase
'reserved_stock' => 0, 'reserved_stock' => 0,
]); ]);
$this->assertDatabaseHas('stock_reservations', [ $this->assertDatabaseHas('stock_reservations', [
'cart_item_id' => null,
'inventory_id' => $variant->inventory_id,
'quantity' => 0,
'status' => 'released', 'status' => 'released',
'release_reason' => 'cart_empty',
]);
$this->assertDatabaseHas('stock_reservation_lines', [
'inventory_id' => $variant->inventory_id,
'quantity' => 5,
]); ]);
} }

View File

@@ -198,11 +198,16 @@ class TelepagosWebhookTest extends TestCase
'compra_id' => $newerPurchase->id, 'compra_id' => $newerPurchase->id,
'cantidad' => 2, 'cantidad' => 2,
]); ]);
$newerReservationId = $newerPurchase->fresh()->stock_reservation_id;
$this->assertNotNull($newerReservationId);
$this->assertDatabaseHas('stock_reservations', [ $this->assertDatabaseHas('stock_reservations', [
'purchase_id' => $newerPurchase->id, 'id' => $newerReservationId,
'status' => 'active',
]);
$this->assertDatabaseHas('stock_reservation_lines', [
'stock_reservation_id' => $newerReservationId,
'inventory_id' => $variant->inventory_id, 'inventory_id' => $variant->inventory_id,
'quantity' => 2, 'quantity' => 2,
'status' => 'active',
]); ]);
$this->assertSoftDeleted('carritos', [ $this->assertSoftDeleted('carritos', [

View File

@@ -32,6 +32,45 @@ class StorePurchaseTest extends TestCase
Queue::fake(); 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 public function test_it_starts_checkout_from_cart_with_purchase_item_snapshots(): void
{ {
$tenant = $this->createTenant('sonder', 'Sonder', 'sonder.com.ar'); $tenant = $this->createTenant('sonder', 'Sonder', 'sonder.com.ar');
@@ -119,12 +158,7 @@ class StorePurchaseTest extends TestCase
'precio_unitario' => '50.00', 'precio_unitario' => '50.00',
'total' => '100.00', 'total' => '100.00',
]); ]);
$this->assertDatabaseHas('stock_reservations', [ $this->assertPurchaseReservation($purchaseId, $inventory->id, 2, 'active');
'inventory_id' => $inventory->id,
'purchase_id' => $purchaseId,
'quantity' => 2,
'status' => 'active',
]);
$this->assertDatabaseHas('carritos', [ $this->assertDatabaseHas('carritos', [
'id' => $cartId, 'id' => $cartId,
'user_id' => $user->id, 'user_id' => $user->id,
@@ -168,12 +202,7 @@ class StorePurchaseTest extends TestCase
'id' => $cartId, 'id' => $cartId,
'current_purchase_id' => $replacementPurchaseId, 'current_purchase_id' => $replacementPurchaseId,
]); ]);
$this->assertDatabaseHas('stock_reservations', [ $this->assertPurchaseReservation($replacementPurchaseId, $inventory->id, 2, 'active');
'inventory_id' => $inventory->id,
'purchase_id' => $replacementPurchaseId,
'quantity' => 2,
'status' => 'active',
]);
$this->assertDatabaseHas('inventories', [ $this->assertDatabaseHas('inventories', [
'id' => $inventory->id, 'id' => $inventory->id,
'reserved_stock' => 2, 'reserved_stock' => 2,
@@ -227,12 +256,7 @@ class StorePurchaseTest extends TestCase
'precio_unitario' => '50.00', 'precio_unitario' => '50.00',
'total' => '150.00', 'total' => '150.00',
]); ]);
$this->assertDatabaseHas('stock_reservations', [ $this->assertPurchaseReservation($response->json('data.id'), $variant->inventory_id, 3, 'active');
'inventory_id' => $variant->inventory_id,
'purchase_id' => $response->json('data.id'),
'quantity' => 3,
'status' => 'active',
]);
$this->assertDatabaseHas('inventories', [ $this->assertDatabaseHas('inventories', [
'id' => $variant->inventory_id, 'id' => $variant->inventory_id,
'real_stock' => 10, 'real_stock' => 10,
@@ -244,10 +268,7 @@ class StorePurchaseTest extends TestCase
->assertOk() ->assertOk()
->assertJsonPath('data.status', Purchase::STATUS_CANCELLED); ->assertJsonPath('data.status', Purchase::STATUS_CANCELLED);
$this->assertDatabaseHas('stock_reservations', [ $this->assertPurchaseReservation($response->json('data.id'), $variant->inventory_id, 3, 'released');
'purchase_id' => $response->json('data.id'),
'status' => 'released',
]);
$this->assertDatabaseHas('inventories', [ $this->assertDatabaseHas('inventories', [
'id' => $variant->inventory_id, 'id' => $variant->inventory_id,
'real_stock' => 10, 'real_stock' => 10,
@@ -296,18 +317,8 @@ class StorePurchaseTest extends TestCase
'origin' => Cart::ORIGIN_DIRECT_CHECKOUT, 'origin' => Cart::ORIGIN_DIRECT_CHECKOUT,
]); ]);
$this->assertDatabaseCount('compra_items', 2); $this->assertDatabaseCount('compra_items', 2);
$this->assertDatabaseHas('stock_reservations', [ $this->assertPurchaseReservation($purchaseId, $firstVariant->inventory_id, 1, 'active');
'inventory_id' => $firstVariant->inventory_id, $this->assertPurchaseReservation($purchaseId, $secondVariant->inventory_id, 1, 'active');
'purchase_id' => $purchaseId,
'quantity' => 1,
'status' => 'active',
]);
$this->assertDatabaseHas('stock_reservations', [
'inventory_id' => $secondVariant->inventory_id,
'purchase_id' => $purchaseId,
'quantity' => 1,
'status' => 'active',
]);
$this->assertDatabaseHas('inventories', [ $this->assertDatabaseHas('inventories', [
'id' => $firstVariant->inventory_id, 'id' => $firstVariant->inventory_id,
'reserved_stock' => 1, 'reserved_stock' => 1,
@@ -430,7 +441,7 @@ class StorePurchaseTest extends TestCase
$tenant = $this->createTenant('sonder', 'Sonder', 'sonder.com.ar'); $tenant = $this->createTenant('sonder', 'Sonder', 'sonder.com.ar');
$user = User::factory()->create(); $user = User::factory()->create();
$firstVariant = $this->createVariantForTenant('sonder', 20, '50.00'); $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]); $secondInventory = Inventory::query()->create(['real_stock' => 20]);
$secondVariant = Variant::query()->create([ $secondVariant = Variant::query()->create([
'catalog_item_id' => $firstVariant->catalog_item_id, '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($firstVariant->catalog_item_id, $firstVariant->id, 2);
$cart->addItem($secondVariant->catalog_item_id, $secondVariant->id, 2); $cart->addItem($secondVariant->catalog_item_id, $secondVariant->id, 2);
$firstVariant->catalogItem->update(['max_units_per_user' => 3]);
$this->actingAs($user, 'sanctum') $this->actingAs($user, 'sanctum')
->postJson('/api/tenants/sonder/compras/start-checkout', [ ->postJson('/api/tenants/sonder/compras/start-checkout', [
@@ -500,12 +512,17 @@ class StorePurchaseTest extends TestCase
'id' => $variant->inventory_id, 'id' => $variant->inventory_id,
'reserved_stock' => 3, 'reserved_stock' => 3,
]); ]);
$activeCart->refresh();
$this->assertNotNull($activeCart->current_stock_reservation_id);
$this->assertDatabaseHas('stock_reservations', [ $this->assertDatabaseHas('stock_reservations', [
'cart_item_id' => $cartItemId, 'id' => $activeCart->current_stock_reservation_id,
'purchase_id' => null,
'quantity' => 3,
'status' => 'active', '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()); $this->assertSame(1, $activeCart->items()->count());
} }
@@ -516,6 +533,7 @@ class StorePurchaseTest extends TestCase
$variant = $this->createVariantForTenant('sonder', 10, '50.00'); $variant = $this->createVariantForTenant('sonder', 10, '50.00');
$previousPurchase = $this->createCheckoutPurchase($user, 'sonder', $variant, 2); $previousPurchase = $this->createCheckoutPurchase($user, 'sonder', $variant, 2);
$cart = $previousPurchase->cart; $cart = $previousPurchase->cart;
$previousReservationId = $previousPurchase->stock_reservation_id;
$previousPurchase->update([ $previousPurchase->update([
'status' => Purchase::STATUS_PENDING_PAYMENT, 'status' => Purchase::STATUS_PENDING_PAYMENT,
@@ -538,11 +556,13 @@ class StorePurchaseTest extends TestCase
'id' => $cart->id, 'id' => $cart->id,
'current_purchase_id' => $currentPurchase->id, 'current_purchase_id' => $currentPurchase->id,
]); ]);
$this->assertNotSame($previousReservationId, $currentPurchase->stock_reservation_id);
$this->assertDatabaseHas('stock_reservations', [ $this->assertDatabaseHas('stock_reservations', [
'purchase_id' => $currentPurchase->id, 'id' => $previousReservationId,
'quantity' => 2, 'status' => 'released',
'status' => 'active', 'release_reason' => 'purchase_superseded',
]); ]);
$this->assertPurchaseReservation($currentPurchase->id, $variant->inventory_id, 2, 'active');
try { try {
app(CheckoutService::class)->confirmPaidPurchase($previousPurchase->fresh()); app(CheckoutService::class)->confirmPaidPurchase($previousPurchase->fresh());
@@ -556,11 +576,7 @@ class StorePurchaseTest extends TestCase
'reserved_stock' => 2, 'reserved_stock' => 2,
'sold_units' => 0, 'sold_units' => 0,
]); ]);
$this->assertDatabaseHas('stock_reservations', [ $this->assertPurchaseReservation($currentPurchase->id, $variant->inventory_id, 2, 'active');
'purchase_id' => $currentPurchase->id,
'quantity' => 2,
'status' => 'active',
]);
} }
public function test_removing_a_checkout_item_supersedes_the_purchase_and_restores_the_user_quota(): void 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, 'current_purchase_id' => null,
]); ]);
$this->assertDatabaseHas('stock_reservations', [ $this->assertDatabaseHas('stock_reservations', [
'inventory_id' => $variant->inventory_id,
'cart_item_id' => null,
'purchase_id' => null,
'quantity' => 0,
'status' => 'released', 'status' => 'released',
'release_reason' => 'purchase_superseded',
]);
$this->assertDatabaseHas('stock_reservation_lines', [
'inventory_id' => $variant->inventory_id,
'quantity' => 2,
]); ]);
$this->assertDatabaseHas('inventories', [ $this->assertDatabaseHas('inventories', [
'id' => $variant->inventory_id, 'id' => $variant->inventory_id,
@@ -628,7 +645,7 @@ class StorePurchaseTest extends TestCase
]); ]);
$this->assertDatabaseHas('inventories', [ $this->assertDatabaseHas('inventories', [
'id' => $variant->inventory_id, 'id' => $variant->inventory_id,
'reserved_stock' => 3, 'reserved_stock' => 0,
]); ]);
$this->assertDatabaseHas('carritos', [ $this->assertDatabaseHas('carritos', [
'id' => $activeCart->id, 'id' => $activeCart->id,
@@ -637,12 +654,7 @@ class StorePurchaseTest extends TestCase
'current_purchase_id' => null, 'current_purchase_id' => null,
'deleted_at' => null, 'deleted_at' => null,
]); ]);
$this->assertDatabaseHas('stock_reservations', [ $this->assertPurchaseReservation($purchase->id, $variant->inventory_id, 3, 'expired');
'cart_item_id' => $cartItemId,
'purchase_id' => null,
'quantity' => 3,
'status' => 'active',
]);
$this->assertSame(1, $activeCart->items()->count()); $this->assertSame(1, $activeCart->items()->count());
} }
@@ -912,10 +924,7 @@ class StorePurchaseTest extends TestCase
'current_purchase_id' => null, 'current_purchase_id' => null,
'deleted_at' => null, 'deleted_at' => null,
]); ]);
$this->assertDatabaseHas('stock_reservations', [ $this->assertPurchaseReservation($purchase->id, $variant->inventory_id, 2, 'active');
'purchase_id' => $purchase->id,
'status' => 'active',
]);
} }
public function test_it_rejects_review_for_a_purchase_that_is_not_awaiting_payment(): void 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') $this->artisan('reservations:expire')
->expectsOutput('Expired purchases: 1') ->expectsOutput('Expired purchases: 1')
->expectsOutput('Expired cart items: 0') ->expectsOutput('Expired cart reservations: 0')
->assertSuccessful(); ->assertSuccessful();
$this->assertDatabaseHas('compras', [ $this->assertDatabaseHas('compras', [
@@ -968,15 +977,11 @@ class StorePurchaseTest extends TestCase
'compra_id' => $purchase->id, 'compra_id' => $purchase->id,
'cantidad' => 3, 'cantidad' => 3,
]); ]);
$this->assertDatabaseHas('stock_reservations', [ $this->assertPurchaseReservation($purchase->id, $variant->inventory_id, 3, 'expired');
'purchase_id' => null,
'quantity' => 3,
'status' => 'active',
]);
$this->assertDatabaseHas('inventories', [ $this->assertDatabaseHas('inventories', [
'id' => $variant->inventory_id, 'id' => $variant->inventory_id,
'real_stock' => 10, 'real_stock' => 10,
'reserved_stock' => 3, 'reserved_stock' => 0,
'sold_units' => 0, 'sold_units' => 0,
]); ]);
$this->assertDatabaseHas('carritos', [ $this->assertDatabaseHas('carritos', [
@@ -989,7 +994,7 @@ class StorePurchaseTest extends TestCase
$this->artisan('reservations:expire') $this->artisan('reservations:expire')
->expectsOutput('Expired purchases: 0') ->expectsOutput('Expired purchases: 0')
->expectsOutput('Expired cart items: 0') ->expectsOutput('Expired cart reservations: 0')
->assertSuccessful(); ->assertSuccessful();
} }
@@ -1006,6 +1011,7 @@ class StorePurchaseTest extends TestCase
'source_catalog_item_id' => $variant->catalog_item_id, 'source_catalog_item_id' => $variant->catalog_item_id,
'source_variant_id' => $variant->id, 'source_variant_id' => $variant->id,
'nombre' => 'Inconsistent item', 'nombre' => 'Inconsistent item',
'item_nombre' => 'Inconsistent item',
'slug' => 'inconsistent-item', 'slug' => 'inconsistent-item',
'cantidad' => 1, 'cantidad' => 1,
'precio_unitario' => '50.00', 'precio_unitario' => '50.00',
@@ -1018,7 +1024,7 @@ class StorePurchaseTest extends TestCase
$this->artisan('reservations:expire') $this->artisan('reservations:expire')
->expectsOutput('Expired purchases: 2') ->expectsOutput('Expired purchases: 2')
->expectsOutput('Expired cart items: 0') ->expectsOutput('Expired cart reservations: 0')
->assertSuccessful(); ->assertSuccessful();
$this->assertDatabaseHas('compras', [ $this->assertDatabaseHas('compras', [
@@ -1160,12 +1166,7 @@ class StorePurchaseTest extends TestCase
'reserved_stock' => 0, 'reserved_stock' => 0,
'sold_units' => 2, 'sold_units' => 2,
]); ]);
$this->assertDatabaseHas('stock_reservations', [ $this->assertPurchaseReservation($purchase->id, $variant->inventory_id, 2, 'committed');
'purchase_id' => $purchase->id,
'inventory_id' => $variant->inventory_id,
'quantity' => 2,
'status' => 'committed',
]);
$this->assertSoftDeleted('carritos', [ $this->assertSoftDeleted('carritos', [
'id' => $purchase->cart_id, '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 protected function createTenant(string $codigo, string $nombre, string $dominio): Tenant
{ {
$hdrKey = (string) Str::uuid(); $hdrKey = (string) Str::uuid();

View File

@@ -520,8 +520,10 @@ class AdminAppSaleControllerTest extends TestCase
->assertJsonPath('data.status', Purchase::STATUS_CANCELLED); ->assertJsonPath('data.status', Purchase::STATUS_CANCELLED);
$this->assertSoftDeleted('carritos', ['id' => $sourceCartId]); $this->assertSoftDeleted('carritos', ['id' => $sourceCartId]);
$reservationId = $purchase->fresh()->stock_reservation_id;
$this->assertNotNull($reservationId);
$this->assertDatabaseHas('stock_reservations', [ $this->assertDatabaseHas('stock_reservations', [
'purchase_id' => $purchase->id, 'id' => $reservationId,
'status' => 'released', 'status' => 'released',
]); ]);
} }

View File

@@ -300,10 +300,13 @@ class DesfilePuraTendenciaSeederTest extends TestCase
->where('user_id', $user->id) ->where('user_id', $user->id)
->where('source_catalog_item_id', $catalogItemId) ->where('source_catalog_item_id', $catalogItemId)
->count()); ->count());
$this->assertSame(48, DB::table('stock_reservations') $this->assertSame(1, DB::table('stock_reservations')
->where('purchase_id', $purchase->id) ->where('id', $purchase->stock_reservation_id)
->where('status', 'committed') ->where('status', 'committed')
->count()); ->count());
$this->assertSame(48, DB::table('stock_reservation_lines')
->where('stock_reservation_id', $purchase->stock_reservation_id)
->count());
foreach ([ foreach ([
['sector' => 'A', 'fila' => '1', 'tipo' => 'NORMAL', 'count' => 16], ['sector' => 'A', 'fila' => '1', 'tipo' => 'NORMAL', 'count' => 16],

View File

@@ -12,7 +12,7 @@ use Tests\TestCase;
class ExpireStockReservationsServiceTest extends TestCase class ExpireStockReservationsServiceTest extends TestCase
{ {
public function test_it_expires_purchases_before_abandoned_cart_items(): void public function test_it_expires_purchases_before_abandoned_cart_reservations(): void
{ {
$checkout = \Mockery::mock(CheckoutService::class); $checkout = \Mockery::mock(CheckoutService::class);
$checkout->shouldReceive('expireOverduePurchases') $checkout->shouldReceive('expireOverduePurchases')
@@ -36,7 +36,7 @@ class ExpireStockReservationsServiceTest extends TestCase
->with('Stock reservation cleanup completed.', [ ->with('Stock reservation cleanup completed.', [
'command' => 'reservations:expire', 'command' => 'reservations:expire',
'expired_purchases' => 2, 'expired_purchases' => 2,
'expired_cart_items' => 3, 'expired_cart_reservations' => 3,
'total_expired' => 5, 'total_expired' => 5,
]); ]);
@@ -44,7 +44,7 @@ class ExpireStockReservationsServiceTest extends TestCase
$this->assertSame([ $this->assertSame([
'purchases' => 2, 'purchases' => 2,
'cart_items' => 3, 'cart_reservations' => 3,
], $result); ], $result);
} }
@@ -67,7 +67,7 @@ class ExpireStockReservationsServiceTest extends TestCase
->with('Stock reservation cleanup failed.', [ ->with('Stock reservation cleanup failed.', [
'command' => 'reservations:expire', 'command' => 'reservations:expire',
'expired_purchases' => 2, 'expired_purchases' => 2,
'expired_cart_items' => null, 'expired_cart_reservations' => null,
'exception' => $exception, 'exception' => $exception,
]); ]);