From 49f42d450747e51d08cfd1cb3ba65953e72a2d21 Mon Sep 17 00:00:00 2001 From: ncoronel Date: Tue, 25 Aug 2026 15:28:23 -0300 Subject: [PATCH] test(stock): cover authoritative reservation expiration --- tests/Feature/Cart/CartControllerTest.php | 1 + .../ExpireStockReservationsServiceTest.php | 51 +++++-------------- 2 files changed, 15 insertions(+), 37 deletions(-) diff --git a/tests/Feature/Cart/CartControllerTest.php b/tests/Feature/Cart/CartControllerTest.php index ff6aa3f..6fb4062 100644 --- a/tests/Feature/Cart/CartControllerTest.php +++ b/tests/Feature/Cart/CartControllerTest.php @@ -47,6 +47,7 @@ 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 diff --git a/tests/Unit/Catalog/ExpireStockReservationsServiceTest.php b/tests/Unit/Catalog/ExpireStockReservationsServiceTest.php index 8bd8d23..1abef5c 100644 --- a/tests/Unit/Catalog/ExpireStockReservationsServiceTest.php +++ b/tests/Unit/Catalog/ExpireStockReservationsServiceTest.php @@ -14,7 +14,9 @@ use Tests\TestCase; class ExpireStockReservationsServiceTest extends TestCase { - public function test_it_expires_purchases_before_abandoned_cart_reservations(): void + use RefreshDatabase; + + public function test_it_expires_an_orphan_reservation_and_releases_its_inventory(): void { $inventory = Inventory::query()->create([ 'real_stock' => 10, @@ -31,31 +33,13 @@ class ExpireStockReservationsServiceTest extends TestCase 'tracks_inventory' => true, ]); - $carts = \Mockery::mock(ExpireCartReservationsService::class); - $carts->shouldReceive('expireOverdue') - ->once() - ->ordered() - ->andReturn(3); - - $logger = \Mockery::mock(LoggerInterface::class); - Log::shouldReceive('channel') - ->once() - ->with('commands') - ->andReturn($logger); - $logger->shouldReceive('info') - ->once() - ->with('Stock reservation cleanup completed.', [ - 'command' => 'reservations:expire', - 'expired_purchases' => 2, - 'expired_cart_reservations' => 3, - 'total_expired' => 5, - ]); - - $result = (new ExpireStockReservationsService($checkout, $carts))->expireOverdue(); + $result = app(ExpireStockReservationsService::class)->expireOverdue(); $this->assertSame([ - 'purchases' => 2, - 'cart_reservations' => 3, + 'purchases' => 0, + 'cart_reservations' => 0, + 'orphan_reservations' => 1, + 'failed' => 0, ], $result); $this->assertDatabaseHas('stock_reservations', [ 'id' => $reservation->id, @@ -87,19 +71,12 @@ class ExpireStockReservationsServiceTest extends TestCase throw new RuntimeException('Broken reservation.'); } - $logger = \Mockery::mock(LoggerInterface::class); - Log::shouldReceive('channel') - ->once() - ->with('commands') - ->andReturn($logger); - $logger->shouldReceive('error') - ->once() - ->with('Stock reservation cleanup failed.', [ - 'command' => 'reservations:expire', - 'expired_purchases' => 2, - 'expired_cart_reservations' => null, - 'exception' => $exception, - ]); + $reservation->update([ + 'status' => StockReservation::STATUS_EXPIRED, + 'expires_at' => null, + 'expired_at' => now(), + ]); + }); $service = new ExpireStockReservationsService( \Mockery::mock(ReleaseCheckoutService::class),