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

224 lines
6.9 KiB
PHP

<?php
declare(strict_types=1);
namespace Tests\Unit\Domain\Models\Entities;
use Carbon\CarbonImmutable;
use Domain\Models\Entities\Brand;
use Domain\Models\ValueObjects\Brand\BrandCode;
use Domain\Models\ValueObjects\Brand\BrandId;
use Domain\Models\ValueObjects\Brand\BrandName;
use Ramsey\Uuid\Uuid;
use Shared\ValueObjects\Email;
use Domain\Models\ValueObjects\PicturePath;
use Shared\ValueObjects\Rcs;
covers(Brand::class);
describe('brand', function (): void {
$code = new BrandCode('AAA01');
$name = new BrandName('Name');
$logo = new PicturePath('files/picture.jpg');
$rcs = new Rcs('RCS PARIS 111 222 333');
$contact = new Email('contact@example.com');
afterEach(function (): void {
CarbonImmutable::setTestNow();
});
it('creates a brand', function () use ($code, $name, $logo, $rcs, $contact): void {
// GIVEN
$id = BrandId::generate();
// WHEN
$brand = Brand::hydrate(
$id,
$code,
$name,
$logo,
$rcs,
$contact,
);
// THEN
expect($brand->id())->toBe($id)
->and($brand->code)->toBe($code)
->and($brand->name)->toBe($name)
->and($brand->logo)->toBe($logo)
->and($brand->rcs)->toBe($rcs)
->and($brand->contact)->toBe($contact)
->and($brand->updatedAt)->toBeNull()
->and($brand->updatedAt())->toBeNull();
});
it('creates a brand with an explicit updatedAt date', function () use ($code, $name, $logo, $rcs, $contact): void {
// GIVEN
$id = BrandId::generate();
$updatedAt = new \DateTimeImmutable('2026-06-15 12:00:00', new \DateTimeZone('UTC'));
// WHEN
$brand = Brand::hydrate(
$id,
$code,
$name,
$logo,
$rcs,
$contact,
$updatedAt,
);
// THEN
expect($brand->updatedAt)->toBe($updatedAt)
->and($brand->updatedAt())->toBe($updatedAt);
});
it('creates a new brand with a freshly generated id', function () use ($code, $name, $logo, $rcs, $contact): void {
// GIVEN / WHEN
$brand = Brand::create(
$code,
$name,
$logo,
$rcs,
$contact,
);
// THEN
expect($brand->id())->toBeInstanceOf(BrandId::class)
->and($brand->code)->toBe($code)
->and($brand->name)->toBe($name)
->and($brand->logo)->toBe($logo)
->and($brand->rcs)->toBe($rcs)
->and($brand->contact)->toBe($contact)
->and($brand->updatedAt)->toBeNull()
->and($brand->updatedAt())->toBeNull();
});
it('generates a different id for each created brand', function () use ($code, $name, $logo, $rcs, $contact): void {
// GIVEN / WHEN
$first = Brand::create(
$code,
$name,
$logo,
$rcs,
$contact,
);
$second = Brand::create(
$code,
$name,
$logo,
$rcs,
$contact,
);
// THEN
expect($first->id()->equals($second->id()))->toBeFalse();
});
it('updates a brand logo and sets updatedAt timestamp', function () use ($code, $name, $rcs, $contact): void {
// GIVEN
CarbonImmutable::setTestNow('2026-06-15 12:00:00 UTC');
$id = BrandId::generate();
$logoOld = new PicturePath('files/picture-old.jpg');
$logoNew = new PicturePath('files/picture-new.jpg');
$brand = Brand::hydrate(
$id,
$code,
$name,
$logoOld,
$rcs,
$contact,
);
// WHEN
$brand = $brand->changeLogo($logoNew);
// THEN
expect($brand->id())->toBe($id)
->and($brand->code)->toBe($code)
->and($brand->name)->toBe($name)
->and($brand->logo)->toBe($logoNew)
->and($brand->rcs)->toBe($rcs)
->and($brand->contact)->toBe($contact)
->and($brand->updatedAt)->not->toBeNull()
->and($brand->updatedAt()?->format('Y-m-d H:i:s'))->toBe('2026-06-15 12:00:00');
});
it('updates a brand rcs and sets updatedAt timestamp', function () use ($code, $name, $logo, $contact): void {
// GIVEN
CarbonImmutable::setTestNow('2026-06-15 14:30:00 UTC');
$id = BrandId::generate();
$rcsOld = new Rcs('RCS PARIS 111 222 333');
$rcsNew = new Rcs('RCS NANTES 444 555 666');
$brand = Brand::hydrate(
$id,
$code,
$name,
$logo,
$rcsOld,
$contact,
);
// WHEN
$brand = $brand->changeRcs($rcsNew);
// THEN
expect($brand->id())->toBe($id)
->and($brand->code)->toBe($code)
->and($brand->name)->toBe($name)
->and($brand->logo)->toBe($logo)
->and($brand->rcs)->toBe($rcsNew)
->and($brand->contact)->toBe($contact)
->and($brand->updatedAt)->not->toBeNull()
->and($brand->updatedAt()?->format('Y-m-d H:i:s'))->toBe('2026-06-15 14:30:00');
});
it('updates a brand contact and sets updatedAt timestamp', function () use ($code, $name, $logo, $rcs): void {
// GIVEN
CarbonImmutable::setTestNow('2026-06-15 16:45:00 UTC');
$id = BrandId::generate();
$contactOld = new Email('contact-old@example.com');
$contactNew = new Email('contact-new@example.com');
$brand = Brand::hydrate(
$id,
$code,
$name,
$logo,
$rcs,
$contactOld,
);
// WHEN
$brand = $brand->changeContact($contactNew);
// THEN
expect($brand->id())->toBe($id)
->and($brand->code)->toBe($code)
->and($brand->name)->toBe($name)
->and($brand->logo)->toBe($logo)
->and($brand->rcs)->toBe($rcs)
->and($brand->contact)->toBe($contactNew)
->and($brand->updatedAt)->not->toBeNull()
->and($brand->updatedAt()?->format('Y-m-d H:i:s'))->toBe('2026-06-15 16:45:00');
});
it('returns the creation date extracted from the id', function () use ($code, $name, $logo, $rcs, $contact): void {
// GIVEN
$expected = new \DateTimeImmutable('2024-06-15 12:34:56.789000', new \DateTimeZone('UTC'));
$id = new BrandId(Uuid::uuid7($expected));
$brand = Brand::hydrate(
$id,
$code,
$name,
$logo,
$rcs,
$contact,
);
// WHEN
$result = $brand->createdAt();
// THEN
expect($result->format('Y-m-d H:i:s.v'))->toBe($expected->format('Y-m-d H:i:s.v'));
});
});