ldc-mesbr-app/tests/Unit/Domain/Models/Entities/StockTest.php
2026-10-12 17:08:07 +02:00

148 lines
4.9 KiB
PHP

<?php
declare(strict_types=1);
namespace Tests\Unit\Domain\Models\Entities;
use Domain\Models\Entities\Stock;
use Domain\Models\ValueObjects\Actor\ActorId;
use Domain\Models\ValueObjects\Campaign\CampaignCode;
use Domain\Models\ValueObjects\Coupon\CouponCode;
use Domain\Models\ValueObjects\Stock\StockAdjustment;
use Domain\Models\ValueObjects\Stock\StockAmount;
use Domain\Models\ValueObjects\Stock\StockId;
use Ramsey\Uuid\Uuid;
covers(Stock::class);
describe('stock', function (): void {
$stockAmount = new StockAmount(10);
$campaignCode = new CampaignCode('AAA01');
$couponCode = new CouponCode('AAA01');
$stockAdjustment = [new StockAdjustment(5, 'Inventory correction', ActorId::generate())];
it('hydrates a stock', function () use ($stockAmount, $campaignCode, $couponCode, $stockAdjustment): void {
// GIVEN
$id = StockId::generate();
// WHEN
$stock = Stock::hydrate(
$id,
$stockAmount,
$campaignCode,
$couponCode,
$stockAdjustment,
);
// 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($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'));
});
});