id())->toBe($id) ->and($stock->stockAmount)->toBe($stockAmount) ->and($stock->campaignCode)->toBe($campaignCode) ->and($stock->couponCode)->toBe($couponCode) ->and($stock->stockAdjustment)->toBe($stockAdjustment) ->and($stock->updatedAt)->toBeNull() ->and($stock->updatedAt())->toBeNull(); }); it('hydrates a stock with several stock adjustments', function () use ($stockAmount, $campaignCode, $couponCode): void { // GIVEN $id = StockId::generate(); $severalAdjustments = [ new StockAdjustment(5, 'Inventory correction', ActorId::generate()), new StockAdjustment(10, 'Inventory correction', ActorId::generate()), ]; // WHEN $stock = Stock::hydrate( $id, $stockAmount, $campaignCode, $couponCode, $severalAdjustments, ); // THEN expect($stock->id())->toBe($id) ->and($stock->stockAmount)->toBe($stockAmount) ->and($stock->campaignCode)->toBe($campaignCode) ->and($stock->couponCode)->toBe($couponCode) ->and($stock->stockAdjustment)->toBe($severalAdjustments) ->and($stock->updatedAt)->toBeNull() ->and($stock->updatedAt())->toBeNull(); }); it('hydrates a stock without stock adjustement', function () use ($stockAmount, $campaignCode, $couponCode): void { // GIVEN $id = StockId::generate(); // WHEN $stock = Stock::hydrate( $id, $stockAmount, $campaignCode, $couponCode, ); // THEN expect($stock->id())->toBe($id) ->and($stock->stockAmount)->toBe($stockAmount) ->and($stock->campaignCode)->toBe($campaignCode) ->and($stock->couponCode)->toBe($couponCode) ->and($stock->stockAdjustment)->toBe([]) ->and($stock->updatedAt)->toBeNull() ->and($stock->updatedAt())->toBeNull(); }); it('rejects a stock with a non-stock-adjustment element', function () use ($stockAmount, $campaignCode, $couponCode): void { // GIVEN $id = StockId::generate(); $invalidAdjustment = [new StockAmount(5)]; // WHEN / THEN expect(fn (): Stock => Stock::hydrate( $id, $stockAmount, $campaignCode, $couponCode, $invalidAdjustment, ))->toThrow(\DomainException::class, 'Stock adjustment must be StockAdjustment instances.'); }); it('hydrates a stock with an explicit updatedAt date', function () use ($stockAmount, $campaignCode, $couponCode, $stockAdjustment): void { // GIVEN $updatedAt = new \DateTimeImmutable('2026-06-15 12:00:00', new \DateTimeZone('UTC')); // WHEN $stock = Stock::hydrate( StockId::generate(), $stockAmount, $campaignCode, $couponCode, $stockAdjustment, $updatedAt, ); // THEN expect($stock->updatedAt)->toBe($updatedAt) ->and($stock->updatedAt())->toBe($updatedAt); }); it('returns the creation date extracted from the id', function () use ($stockAmount, $campaignCode, $couponCode, $stockAdjustment): void { // GIVEN $expected = new \DateTimeImmutable('2024-06-15 12:34:56.789000', new \DateTimeZone('UTC')); $id = new StockId(Uuid::uuid7($expected)); $stock = Stock::hydrate( $id, $stockAmount, $campaignCode, $couponCode, $stockAdjustment ); // WHEN $result = $stock->createdAt(); // THEN expect($result->format('Y-m-d H:i:s.v'))->toBe($expected->format('Y-m-d H:i:s.v')); }); });