test(stock): cover aggregate reservation lifecycle
This commit is contained in:
@@ -5,10 +5,12 @@ namespace Tests\Feature\Cart;
|
||||
use App\Domains\Attachable\Enums\AttachmentType;
|
||||
use App\Domains\Attachable\Models\Attachment;
|
||||
use App\Domains\Auth\Models\User;
|
||||
use App\Domains\Cart\Models\Cart;
|
||||
use App\Domains\Catalog\Enums\InventoryPolicy;
|
||||
use App\Domains\Catalog\Models\CatalogItem;
|
||||
use App\Domains\Catalog\Models\Inventory;
|
||||
use App\Domains\Catalog\Models\Variant;
|
||||
use App\Domains\Catalog\Services\CatalogService;
|
||||
use App\Domains\Purchase\Models\Purchase;
|
||||
use App\Domains\Purchase\Models\PurchaseItem;
|
||||
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_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
|
||||
@@ -74,9 +150,11 @@ class CartControllerTest extends TestCase
|
||||
'id' => $item->inventory_id,
|
||||
'reserved_stock' => 2,
|
||||
]);
|
||||
$this->assertDatabaseHas('stock_reservations', [
|
||||
$this->assertDatabaseHas('stock_reservation_lines', [
|
||||
'inventory_id' => $item->inventory_id,
|
||||
'quantity' => 2,
|
||||
]);
|
||||
$this->assertDatabaseHas('stock_reservations', [
|
||||
'status' => 'active',
|
||||
]);
|
||||
}
|
||||
@@ -96,16 +174,18 @@ class CartControllerTest extends TestCase
|
||||
])->assertOk();
|
||||
|
||||
$this->assertDatabaseHas('stock_reservations', [
|
||||
'inventory_id' => $item->inventory_id,
|
||||
'quantity' => 2,
|
||||
'status' => 'active',
|
||||
'expires_at' => $now->copy()->addMinutes(45)->toDateTimeString(),
|
||||
]);
|
||||
$this->assertDatabaseHas('stock_reservation_lines', [
|
||||
'inventory_id' => $item->inventory_id,
|
||||
'quantity' => 2,
|
||||
]);
|
||||
|
||||
$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);
|
||||
$tenant = $this->createTenant('acme');
|
||||
@@ -120,14 +200,14 @@ class CartControllerTest extends TestCase
|
||||
|
||||
$this->artisan('reservations:expire')
|
||||
->expectsOutput('Expired purchases: 0')
|
||||
->expectsOutput('Expired cart items: 0')
|
||||
->expectsOutput('Expired cart reservations: 0')
|
||||
->assertSuccessful();
|
||||
|
||||
$this->travel(31)->minutes();
|
||||
|
||||
$this->artisan('reservations:expire')
|
||||
->expectsOutput('Expired purchases: 0')
|
||||
->expectsOutput('Expired cart items: 1')
|
||||
->expectsOutput('Expired cart reservations: 1')
|
||||
->assertSuccessful();
|
||||
|
||||
$this->assertDatabaseHas('inventories', [
|
||||
@@ -135,23 +215,25 @@ class CartControllerTest extends TestCase
|
||||
'real_stock' => 10,
|
||||
'reserved_stock' => 0,
|
||||
]);
|
||||
$this->assertDatabaseMissing('carrito_items', ['id' => $cartItemId]);
|
||||
$this->assertSoftDeleted('carritos', [
|
||||
$this->assertDatabaseHas('carrito_items', ['id' => $cartItemId]);
|
||||
$this->assertDatabaseHas('carritos', [
|
||||
'id' => $cartId,
|
||||
'status' => 'expired',
|
||||
'status' => 'active',
|
||||
'current_stock_reservation_id' => null,
|
||||
'deleted_at' => null,
|
||||
]);
|
||||
$this->assertDatabaseHas('stock_reservations', [
|
||||
'inventory_id' => $item->inventory_id,
|
||||
'cart_item_id' => null,
|
||||
'purchase_id' => null,
|
||||
'quantity' => 0,
|
||||
'status' => 'expired',
|
||||
'expires_at' => null,
|
||||
]);
|
||||
$this->assertDatabaseHas('stock_reservation_lines', [
|
||||
'inventory_id' => $item->inventory_id,
|
||||
'quantity' => 2,
|
||||
]);
|
||||
|
||||
$this->artisan('reservations:expire')
|
||||
->expectsOutput('Expired purchases: 0')
|
||||
->expectsOutput('Expired cart items: 0')
|
||||
->expectsOutput('Expired cart reservations: 0')
|
||||
->assertSuccessful();
|
||||
|
||||
$this->travelBack();
|
||||
@@ -212,10 +294,11 @@ class CartControllerTest extends TestCase
|
||||
'id' => $variant->inventory_id,
|
||||
'reserved_stock' => 5,
|
||||
]);
|
||||
$this->assertDatabaseHas('stock_reservations', [
|
||||
'cart_item_id' => $response->json('data.items.0.id'),
|
||||
$this->assertDatabaseHas('stock_reservation_lines', [
|
||||
'inventory_id' => $variant->inventory_id,
|
||||
'quantity' => 5,
|
||||
]);
|
||||
$this->assertDatabaseHas('stock_reservations', [
|
||||
'status' => 'active',
|
||||
]);
|
||||
}
|
||||
@@ -229,6 +312,7 @@ class CartControllerTest extends TestCase
|
||||
$this->createPurchaseItem($tenant, $user, $item, 1);
|
||||
|
||||
$this->actingAs($user, 'sanctum')
|
||||
->withHeader('Accept-Language', 'es')
|
||||
->postJson('/api/tenants/acme/cart/items', [
|
||||
'catalog_item_id' => $item->id,
|
||||
'cantidad' => 2,
|
||||
@@ -237,6 +321,7 @@ class CartControllerTest extends TestCase
|
||||
->assertJsonPath('data.items.0.cantidad', 2);
|
||||
|
||||
$this->actingAs($user, 'sanctum')
|
||||
->withHeader('Accept-Language', 'es')
|
||||
->postJson('/api/tenants/acme/cart/items', [
|
||||
'catalog_item_id' => $item->id,
|
||||
'cantidad' => 2,
|
||||
@@ -381,10 +466,12 @@ class CartControllerTest extends TestCase
|
||||
'reserved_stock' => 0,
|
||||
]);
|
||||
$this->assertDatabaseHas('stock_reservations', [
|
||||
'cart_item_id' => null,
|
||||
'inventory_id' => $variant->inventory_id,
|
||||
'quantity' => 0,
|
||||
'status' => 'released',
|
||||
'release_reason' => 'cart_empty',
|
||||
]);
|
||||
$this->assertDatabaseHas('stock_reservation_lines', [
|
||||
'inventory_id' => $variant->inventory_id,
|
||||
'quantity' => 5,
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
@@ -198,11 +198,16 @@ class TelepagosWebhookTest extends TestCase
|
||||
'compra_id' => $newerPurchase->id,
|
||||
'cantidad' => 2,
|
||||
]);
|
||||
$newerReservationId = $newerPurchase->fresh()->stock_reservation_id;
|
||||
$this->assertNotNull($newerReservationId);
|
||||
$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,
|
||||
'quantity' => 2,
|
||||
'status' => 'active',
|
||||
]);
|
||||
|
||||
$this->assertSoftDeleted('carritos', [
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -520,8 +520,10 @@ class AdminAppSaleControllerTest extends TestCase
|
||||
->assertJsonPath('data.status', Purchase::STATUS_CANCELLED);
|
||||
|
||||
$this->assertSoftDeleted('carritos', ['id' => $sourceCartId]);
|
||||
$reservationId = $purchase->fresh()->stock_reservation_id;
|
||||
$this->assertNotNull($reservationId);
|
||||
$this->assertDatabaseHas('stock_reservations', [
|
||||
'purchase_id' => $purchase->id,
|
||||
'id' => $reservationId,
|
||||
'status' => 'released',
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -300,10 +300,13 @@ class DesfilePuraTendenciaSeederTest extends TestCase
|
||||
->where('user_id', $user->id)
|
||||
->where('source_catalog_item_id', $catalogItemId)
|
||||
->count());
|
||||
$this->assertSame(48, DB::table('stock_reservations')
|
||||
->where('purchase_id', $purchase->id)
|
||||
$this->assertSame(1, DB::table('stock_reservations')
|
||||
->where('id', $purchase->stock_reservation_id)
|
||||
->where('status', 'committed')
|
||||
->count());
|
||||
$this->assertSame(48, DB::table('stock_reservation_lines')
|
||||
->where('stock_reservation_id', $purchase->stock_reservation_id)
|
||||
->count());
|
||||
|
||||
foreach ([
|
||||
['sector' => 'A', 'fila' => '1', 'tipo' => 'NORMAL', 'count' => 16],
|
||||
|
||||
@@ -12,7 +12,7 @@ use Tests\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->shouldReceive('expireOverduePurchases')
|
||||
@@ -36,7 +36,7 @@ class ExpireStockReservationsServiceTest extends TestCase
|
||||
->with('Stock reservation cleanup completed.', [
|
||||
'command' => 'reservations:expire',
|
||||
'expired_purchases' => 2,
|
||||
'expired_cart_items' => 3,
|
||||
'expired_cart_reservations' => 3,
|
||||
'total_expired' => 5,
|
||||
]);
|
||||
|
||||
@@ -44,7 +44,7 @@ class ExpireStockReservationsServiceTest extends TestCase
|
||||
|
||||
$this->assertSame([
|
||||
'purchases' => 2,
|
||||
'cart_items' => 3,
|
||||
'cart_reservations' => 3,
|
||||
], $result);
|
||||
}
|
||||
|
||||
@@ -67,7 +67,7 @@ class ExpireStockReservationsServiceTest extends TestCase
|
||||
->with('Stock reservation cleanup failed.', [
|
||||
'command' => 'reservations:expire',
|
||||
'expired_purchases' => 2,
|
||||
'expired_cart_items' => null,
|
||||
'expired_cart_reservations' => null,
|
||||
'exception' => $exception,
|
||||
]);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user