feat: ✨ add stock entity

This commit is contained in:
K Muller 2026-09-23 15:31:51 +02:00
parent 2065340fc2
commit 41c8c2cc07
9 changed files with 157 additions and 19 deletions

View file

@ -4,6 +4,7 @@ declare(strict_types=1);
namespace Domain\Models\Entities;
use Domain\Models\ValueObjects\Actor\ActorDisplayName;
use Domain\Models\ValueObjects\Actor\ActorId;
use Shared\DDD\Models\AggregateRoot;
use Shared\DDD\Models\HasTimestampIdentifier;
@ -15,9 +16,10 @@ final readonly class Actor implements AggregateRoot, HasTimestampIdentifier
{
private function __construct(
public ActorId $id,
public string $displayName,
public ActorDisplayName $displayName,
public ?\DateTimeInterface $updatedAt = null,
) {}
) {
}
public function id(): ActorId
{
@ -36,7 +38,7 @@ final readonly class Actor implements AggregateRoot, HasTimestampIdentifier
public static function hydrate(
ActorId $id,
string $displayName,
ActorDisplayName $displayName,
?\DateTimeInterface $updatedAt = null,
): self {
return new self(

View file

@ -4,6 +4,7 @@ declare(strict_types=1);
namespace Domain\Models\Entities;
use Domain\Models\ValueObjects\Campaign\CampaignCode;
use Domain\Models\ValueObjects\ProcessingCode;
use Domain\Models\ValueObjects\Stock\StockAdjustment;
use Domain\Models\ValueObjects\Stock\StockAmount;
@ -19,12 +20,12 @@ final readonly class Stock implements AggregateRoot, HasTimestampIdentifier
private function __construct(
public StockId $id,
public StockAmount $stockAmount,
public StockAdjustment $stockAdjustment,
public CampaignCode $campaignCode,
public ProcessingCode $processingCode,
/** @var array<int, StockAdjustment> $stockAdjustment */
public array $stockAdjustment,
public ?\DateTimeInterface $updatedAt = null,
) {
}
) {}
public function id(): StockId
{
@ -41,20 +42,23 @@ final readonly class Stock implements AggregateRoot, HasTimestampIdentifier
return $this->updatedAt;
}
/**
* @param array<int, StockAdjustment> $stockAdjustment
*/
public static function hydrate(
StockId $id,
StockAmount $stockAmount,
StockAdjustment $stockAdjustment,
CampaignCode $campaignCode,
ProcessingCode $processingCode,
array $stockAdjustment = [],
?\DateTimeInterface $updatedAt = null,
): self {
return new self(
id: $id,
stockAmount: $stockAmount,
stockAdjustment: $stockAdjustment,
campaignCode: $campaignCode,
processingCode: $processingCode,
stockAdjustment: $stockAdjustment,
updatedAt: $updatedAt,
);
}

View file

@ -0,0 +1,18 @@
<?php
declare(strict_types=1);
namespace Domain\Models\ValueObjects\Actor;
final readonly class ActorDisplayName
{
public function __construct(
public string $value,
) {
$trimmed = trim($value);
if ($trimmed === '') {
throw new \DomainException('Display name cannot be empty.');
}
}
}

View file

@ -12,10 +12,16 @@ final readonly class StockAdjustment
public int $quantity,
public string $reason,
public ActorId $actorId,
public ?\DateTimeInterface $updatedAt = null,
public ?\DateTimeInterface $createdAt = null,
) {
if ($this->quantity <= 0 ) {
if ($this->quantity <= 0) {
throw new \DomainException('Stock Amount should be more than 0.');
}
$trimmed = trim($reason);
if ($trimmed === '') {
throw new \DomainException('Reason cannot be empty.');
}
}
}

View file

@ -9,7 +9,7 @@ final readonly class StockAmount
public function __construct(
public int $value,
) {
if ($this->value < 0 ) {
if ($this->value < 0) {
throw new \DomainException('Stock Amount should be positive.');
}
}

View file

@ -5,6 +5,7 @@ declare(strict_types=1);
namespace Tests\Unit\Domain\Models\Entities;
use Domain\Models\Entities\Actor;
use Domain\Models\ValueObjects\Actor\ActorDisplayName;
use Domain\Models\ValueObjects\Actor\ActorId;
use Ramsey\Uuid\Uuid;
@ -14,7 +15,7 @@ describe('actor', function (): void {
it('hydrates an actor', function (): void {
// GIVEN
$id = ActorId::generate();
$displayName = 'John Doe';
$displayName = new ActorDisplayName('John Doe');
// WHEN
$actor = Actor::hydrate($id, $displayName);
@ -31,7 +32,7 @@ describe('actor', function (): void {
$updatedAt = new \DateTimeImmutable('2026-06-15 12:00:00', new \DateTimeZone('UTC'));
// WHEN
$actor = Actor::hydrate(ActorId::generate(), 'John Doe', $updatedAt);
$actor = Actor::hydrate(ActorId::generate(), new ActorDisplayName('John Doe'), $updatedAt);
// THEN
expect($actor->updatedAt)->toBe($updatedAt)
@ -42,7 +43,7 @@ describe('actor', function (): void {
// GIVEN
$expected = new \DateTimeImmutable('2024-06-15 12:34:56.789000', new \DateTimeZone('UTC'));
$id = new ActorId(Uuid::uuid7($expected));
$actor = Actor::hydrate($id, 'John Doe');
$actor = Actor::hydrate($id, new ActorDisplayName('John Doe'));
// WHEN
$result = $actor->createdAt();

View file

@ -0,0 +1,77 @@
<?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\ProcessingCode;
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 {
it('hydrates a stock', function (): void {
// GIVEN
$id = StockId::generate();
$stockAmount = new StockAmount(10);
$stockAdjustment = [new StockAdjustment(5, 'Inventory correction', ActorId::generate())];
$campaignCode = new CampaignCode('AAA01');
$processingCode = new ProcessingCode('PC1');
// WHEN
$stock = Stock::hydrate($id, $stockAmount, $campaignCode, $processingCode, $stockAdjustment);
// THEN
expect($stock->id())->toBe($id)
->and($stock->stockAmount)->toBe($stockAmount)
->and($stock->stockAdjustment)->toBe($stockAdjustment)
->and($stock->campaignCode)->toBe($campaignCode)
->and($stock->processingCode)->toBe($processingCode)
->and($stock->updatedAt)->toBeNull()
->and($stock->updatedAt())->toBeNull();
});
it('hydrates a stock with an explicit updatedAt date', function (): void {
// GIVEN
$updatedAt = new \DateTimeImmutable('2026-06-15 12:00:00', new \DateTimeZone('UTC'));
// WHEN
$stock = Stock::hydrate(
StockId::generate(),
new StockAmount(10),
new CampaignCode('AAA01'),
new ProcessingCode('PC1'),
[new StockAdjustment(5, 'Inventory correction', ActorId::generate())],
$updatedAt,
);
// THEN
expect($stock->updatedAt)->toBe($updatedAt)
->and($stock->updatedAt())->toBe($updatedAt);
});
it('returns the creation date extracted from the id', function (): 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,
new StockAmount(10),
new CampaignCode('AAA01'),
new ProcessingCode('PC1'),
[new StockAdjustment(5, 'Inventory correction', ActorId::generate())],
);
// 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'));
});
});

View file

@ -0,0 +1,30 @@
<?php
declare(strict_types=1);
namespace Tests\Unit\Domain\Models\ValueObjects\Actor;
use Domain\Models\ValueObjects\Actor\ActorDisplayName;
covers(ActorDisplayName::class);
describe('actor displayname', function (): void {
it('creates a valid actor displayname', function (): void {
// GIVEN
$value = 'Johnny';
// WHEN
$name = new ActorDisplayName($value);
// THEN
expect($name->value)->toBe($value);
});
it('rejects invalid actor displayname', function (): void {
// GIVEN
$value = '';
// GIVEN / THEN
expect(fn (): ActorDisplayName => new ActorDisplayName($value))->toThrow(\DomainException::class);
});
});

View file

@ -24,18 +24,18 @@ describe('stock adjustment', function (): void {
->and($adjustment->quantity)->toBe($quantity)
->and($adjustment->reason)->toBe($reason)
->and($adjustment->actorId)->toBe($actorId)
->and($adjustment->updatedAt)->toBeNull();
->and($adjustment->createdAt)->toBeNull();
});
it('creates a stock adjustment with an explicit updatedAt date', function (): void {
it('creates a stock adjustment with an explicit createdAt date', function (): void {
// GIVEN
$updatedAt = new \DateTimeImmutable('2026-06-15 12:00:00', new \DateTimeZone('UTC'));
$createdAt = new \DateTimeImmutable('2026-06-15 12:00:00', new \DateTimeZone('UTC'));
// WHEN
$adjustment = new StockAdjustment(10, 'Inventory correction', ActorId::generate(), $updatedAt);
$adjustment = new StockAdjustment(10, 'Inventory correction', ActorId::generate(), $createdAt);
// THEN
expect($adjustment->updatedAt)->toBe($updatedAt);
expect($adjustment->createdAt)->toBe($createdAt);
});
it('rejects a quantity of zero', function (): void {