feat: 🔨 working on Stock and associated VO/Entities

This commit is contained in:
K Muller 2026-09-23 15:05:27 +02:00
parent ab1923c05b
commit 2065340fc2
11 changed files with 382 additions and 0 deletions

View file

@ -0,0 +1,48 @@
<?php
declare(strict_types=1);
namespace Domain\Models\Entities;
use Domain\Models\ValueObjects\Actor\ActorId;
use Shared\DDD\Models\AggregateRoot;
use Shared\DDD\Models\HasTimestampIdentifier;
/**
* @implements \Shared\DDD\Models\AggregateRoot<\Domain\Models\ValueObjects\Actor\ActorId>
*/
final readonly class Actor implements AggregateRoot, HasTimestampIdentifier
{
private function __construct(
public ActorId $id,
public string $displayName,
public ?\DateTimeInterface $updatedAt = null,
) {}
public function id(): ActorId
{
return $this->id;
}
public function createdAt(): \DateTimeInterface
{
return $this->id->toDateTime();
}
public function updatedAt(): ?\DateTimeInterface
{
return $this->updatedAt;
}
public static function hydrate(
ActorId $id,
string $displayName,
?\DateTimeInterface $updatedAt = null,
): self {
return new self(
id: $id,
displayName: $displayName,
updatedAt: $updatedAt,
);
}
}

View file

@ -0,0 +1,61 @@
<?php
declare(strict_types=1);
namespace Domain\Models\Entities;
use Domain\Models\ValueObjects\ProcessingCode;
use Domain\Models\ValueObjects\Stock\StockAdjustment;
use Domain\Models\ValueObjects\Stock\StockAmount;
use Domain\Models\ValueObjects\Stock\StockId;
use Shared\DDD\Models\AggregateRoot;
use Shared\DDD\Models\HasTimestampIdentifier;
/**
* @implements \Shared\DDD\Models\AggregateRoot<\Domain\Models\ValueObjects\Stock\StockId>
*/
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,
public ?\DateTimeInterface $updatedAt = null,
) {
}
public function id(): StockId
{
return $this->id;
}
public function createdAt(): \DateTimeInterface
{
return $this->id->toDateTime();
}
public function updatedAt(): ?\DateTimeInterface
{
return $this->updatedAt;
}
public static function hydrate(
StockId $id,
StockAmount $stockAmount,
StockAdjustment $stockAdjustment,
CampaignCode $campaignCode,
ProcessingCode $processingCode,
?\DateTimeInterface $updatedAt = null,
): self {
return new self(
id: $id,
stockAmount: $stockAmount,
stockAdjustment: $stockAdjustment,
campaignCode: $campaignCode,
processingCode: $processingCode,
updatedAt: $updatedAt,
);
}
}

View file

@ -0,0 +1,16 @@
<?php
declare(strict_types=1);
namespace Domain\Models\ValueObjects\Actor;
use Ramsey\Uuid\Uuid;
use Shared\DDD\Models\Uuidv7EntityId;
final readonly class ActorId extends Uuidv7EntityId
{
public static function generate(): self
{
return new self(Uuid::uuid7());
}
}

View file

@ -0,0 +1,21 @@
<?php
declare(strict_types=1);
namespace Domain\Models\ValueObjects\Stock;
use Domain\Models\ValueObjects\Actor\ActorId;
final readonly class StockAdjustment
{
public function __construct(
public int $quantity,
public string $reason,
public ActorId $actorId,
public ?\DateTimeInterface $updatedAt = null,
) {
if ($this->quantity <= 0 ) {
throw new \DomainException('Stock Amount should be more than 0.');
}
}
}

View file

@ -0,0 +1,16 @@
<?php
declare(strict_types=1);
namespace Domain\Models\ValueObjects\Stock;
final readonly class StockAmount
{
public function __construct(
public int $value,
) {
if ($this->value < 0 ) {
throw new \DomainException('Stock Amount should be positive.');
}
}
}

View file

@ -0,0 +1,16 @@
<?php
declare(strict_types=1);
namespace Domain\Models\ValueObjects\Stock;
use Ramsey\Uuid\Uuid;
use Shared\DDD\Models\Uuidv7EntityId;
final readonly class StockId extends Uuidv7EntityId
{
public static function generate(): self
{
return new self(Uuid::uuid7());
}
}

View file

@ -0,0 +1,53 @@
<?php
declare(strict_types=1);
namespace Tests\Unit\Domain\Models\Entities;
use Domain\Models\Entities\Actor;
use Domain\Models\ValueObjects\Actor\ActorId;
use Ramsey\Uuid\Uuid;
covers(Actor::class);
describe('actor', function (): void {
it('hydrates an actor', function (): void {
// GIVEN
$id = ActorId::generate();
$displayName = 'John Doe';
// WHEN
$actor = Actor::hydrate($id, $displayName);
// THEN
expect($actor->id())->toBe($id)
->and($actor->displayName)->toBe($displayName)
->and($actor->updatedAt)->toBeNull()
->and($actor->updatedAt())->toBeNull();
});
it('hydrates an actor with an explicit updatedAt date', function (): void {
// GIVEN
$updatedAt = new \DateTimeImmutable('2026-06-15 12:00:00', new \DateTimeZone('UTC'));
// WHEN
$actor = Actor::hydrate(ActorId::generate(), 'John Doe', $updatedAt);
// THEN
expect($actor->updatedAt)->toBe($updatedAt)
->and($actor->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 ActorId(Uuid::uuid7($expected));
$actor = Actor::hydrate($id, 'John Doe');
// WHEN
$result = $actor->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,28 @@
<?php
declare(strict_types=1);
namespace Tests\Unit\Domain\Models\ValueObjects\Actor;
use Domain\Models\ValueObjects\Actor\ActorId;
covers(ActorId::class);
describe('actor id', function (): void {
it('generates a unique actor id', function (): void {
// WHEN
$id = ActorId::generate();
// THEN
expect($id)->toBeInstanceOf(ActorId::class);
});
it('generates different ids on each call', function (): void {
// WHEN
$first = ActorId::generate();
$second = ActorId::generate();
// THEN
expect((string) $first)->not->toBe((string) $second);
});
});

View file

@ -0,0 +1,56 @@
<?php
declare(strict_types=1);
namespace Tests\Unit\Domain\Models\ValueObjects\Stock;
use Domain\Models\ValueObjects\Actor\ActorId;
use Domain\Models\ValueObjects\Stock\StockAdjustment;
covers(StockAdjustment::class);
describe('stock adjustment', function (): void {
it('creates a valid stock adjustment', function (): void {
// GIVEN
$quantity = 10;
$reason = 'Inventory correction';
$actorId = ActorId::generate();
// WHEN
$adjustment = new StockAdjustment($quantity, $reason, $actorId);
// THEN
expect($adjustment)->toBeInstanceOf(StockAdjustment::class)
->and($adjustment->quantity)->toBe($quantity)
->and($adjustment->reason)->toBe($reason)
->and($adjustment->actorId)->toBe($actorId)
->and($adjustment->updatedAt)->toBeNull();
});
it('creates a stock adjustment with an explicit updatedAt date', function (): void {
// GIVEN
$updatedAt = new \DateTimeImmutable('2026-06-15 12:00:00', new \DateTimeZone('UTC'));
// WHEN
$adjustment = new StockAdjustment(10, 'Inventory correction', ActorId::generate(), $updatedAt);
// THEN
expect($adjustment->updatedAt)->toBe($updatedAt);
});
it('rejects a quantity of zero', function (): void {
// WHEN
$action = fn (): StockAdjustment => new StockAdjustment(0, 'Inventory correction', ActorId::generate());
// THEN
expect($action)->toThrow(\DomainException::class, 'Stock Amount should be more than 0.');
});
it('rejects a negative quantity', function (): void {
// WHEN
$action = fn (): StockAdjustment => new StockAdjustment(-5, 'Inventory correction', ActorId::generate());
// THEN
expect($action)->toThrow(\DomainException::class, 'Stock Amount should be more than 0.');
});
});

View file

@ -0,0 +1,39 @@
<?php
declare(strict_types=1);
namespace Tests\Unit\Domain\Models\ValueObjects\Stock;
use Domain\Models\ValueObjects\Stock\StockAmount;
covers(StockAmount::class);
describe('stock amount', function (): void {
it('creates a valid stock amount', function (): void {
// GIVEN
$value = 10;
// WHEN
$amount = new StockAmount($value);
// THEN
expect($amount)->toBeInstanceOf(StockAmount::class)
->and($amount->value)->toBe($value);
});
it('accepts a zero value', function (): void {
// WHEN
$amount = new StockAmount(0);
// THEN
expect($amount->value)->toBe(0);
});
it('rejects a negative value', function (): void {
// WHEN
$action = fn (): StockAmount => new StockAmount(-1);
// THEN
expect($action)->toThrow(\DomainException::class, 'Stock Amount should be positive.');
});
});

View file

@ -0,0 +1,28 @@
<?php
declare(strict_types=1);
namespace Tests\Unit\Domain\Models\ValueObjects\Stock;
use Domain\Models\ValueObjects\Stock\StockId;
covers(StockId::class);
describe('stock id', function (): void {
it('generates a unique stock id', function (): void {
// WHEN
$id = StockId::generate();
// THEN
expect($id)->toBeInstanceOf(StockId::class);
});
it('generates different ids on each call', function (): void {
// WHEN
$first = StockId::generate();
$second = StockId::generate();
// THEN
expect((string) $first)->not->toBe((string) $second);
});
});