226 lines
7 KiB
PHP
226 lines
7 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 {
|
|
beforeEach(function (): void {
|
|
$this->code = new BrandCode('AAA01');
|
|
$this->name = new BrandName('Name');
|
|
$this->logo = new PicturePath('files/picture.jpg');
|
|
$this->rcs = new Rcs('RCS PARIS 111 222 333');
|
|
$this->contact = new Email('contact@example.com');
|
|
});
|
|
|
|
afterEach(function (): void {
|
|
CarbonImmutable::setTestNow();
|
|
});
|
|
|
|
it('creates a brand', function (): void {
|
|
// GIVEN
|
|
$id = BrandId::generate();
|
|
|
|
// WHEN
|
|
$brand = Brand::hydrate(
|
|
$id,
|
|
$this->code,
|
|
$this->name,
|
|
$this->logo,
|
|
$this->rcs,
|
|
$this->contact
|
|
);
|
|
|
|
// THEN
|
|
expect($brand->id())->toBe($id)
|
|
->and($brand->code)->toBe($this->code)
|
|
->and($brand->name)->toBe($this->name)
|
|
->and($brand->logo)->toBe($this->logo)
|
|
->and($brand->rcs)->toBe($this->rcs)
|
|
->and($brand->contact)->toBe($this->contact)
|
|
->and($brand->updatedAt)->toBeNull()
|
|
->and($brand->updatedAt())->toBeNull();
|
|
});
|
|
|
|
it('creates a brand with an explicit updatedAt date', function (): void {
|
|
// GIVEN
|
|
$id = BrandId::generate();
|
|
$updatedAt = new \DateTimeImmutable('2026-06-15 12:00:00', new \DateTimeZone('UTC'));
|
|
|
|
// WHEN
|
|
$brand = Brand::hydrate(
|
|
$id,
|
|
$this->code,
|
|
$this->name,
|
|
$this->logo,
|
|
$this->rcs,
|
|
$this->contact,
|
|
$updatedAt
|
|
);
|
|
|
|
// THEN
|
|
expect($brand->updatedAt)->toBe($updatedAt)
|
|
->and($brand->updatedAt())->toBe($updatedAt);
|
|
});
|
|
|
|
it('creates a new brand with a freshly generated id', function (): void {
|
|
// GIVEN / WHEN
|
|
$brand = Brand::create(
|
|
$this->code,
|
|
$this->name,
|
|
$this->logo,
|
|
$this->rcs,
|
|
$this->contact
|
|
);
|
|
|
|
// THEN
|
|
expect($brand->id())->toBeInstanceOf(BrandId::class)
|
|
->and($brand->code)->toBe($this->code)
|
|
->and($brand->name)->toBe($this->name)
|
|
->and($brand->logo)->toBe($this->logo)
|
|
->and($brand->rcs)->toBe($this->rcs)
|
|
->and($brand->contact)->toBe($this->contact)
|
|
->and($brand->updatedAt)->toBeNull()
|
|
->and($brand->updatedAt())->toBeNull();
|
|
});
|
|
|
|
it('generates a different id for each created brand', function (): void {
|
|
// GIVEN / WHEN
|
|
$first = Brand::create(
|
|
$this->code,
|
|
$this->name,
|
|
$this->logo,
|
|
$this->rcs,
|
|
$this->contact
|
|
);
|
|
$second = Brand::create(
|
|
$this->code,
|
|
$this->name,
|
|
$this->logo,
|
|
$this->rcs,
|
|
$this->contact
|
|
);
|
|
|
|
// THEN
|
|
expect($first->id()->equals($second->id()))->toBeFalse();
|
|
});
|
|
|
|
it('updates a brand logo and sets updatedAt timestamp', function (): 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,
|
|
$this->code,
|
|
$this->name,
|
|
$logoOld,
|
|
$this->rcs,
|
|
$this->contact
|
|
);
|
|
|
|
// WHEN
|
|
$brand = $brand->changeLogo($logoNew);
|
|
|
|
// THEN
|
|
expect($brand->id())->toBe($id)
|
|
->and($brand->code)->toBe($this->code)
|
|
->and($brand->name)->toBe($this->name)
|
|
->and($brand->logo)->toBe($logoNew)
|
|
->and($brand->rcs)->toBe($this->rcs)
|
|
->and($brand->contact)->toBe($this->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 (): 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,
|
|
$this->code,
|
|
$this->name,
|
|
$this->logo,
|
|
$rcsOld,
|
|
$this->contact
|
|
);
|
|
|
|
// WHEN
|
|
$brand = $brand->changeRcs($rcsNew);
|
|
|
|
// THEN
|
|
expect($brand->id())->toBe($id)
|
|
->and($brand->code)->toBe($this->code)
|
|
->and($brand->name)->toBe($this->name)
|
|
->and($brand->logo)->toBe($this->logo)
|
|
->and($brand->rcs)->toBe($rcsNew)
|
|
->and($brand->contact)->toBe($this->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 (): 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,
|
|
$this->code,
|
|
$this->name,
|
|
$this->logo,
|
|
$this->rcs,
|
|
$contactOld
|
|
);
|
|
|
|
// WHEN
|
|
$brand = $brand->changeContact($contactNew);
|
|
|
|
// THEN
|
|
expect($brand->id())->toBe($id)
|
|
->and($brand->code)->toBe($this->code)
|
|
->and($brand->name)->toBe($this->name)
|
|
->and($brand->logo)->toBe($this->logo)
|
|
->and($brand->rcs)->toBe($this->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 (): 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,
|
|
$this->code,
|
|
$this->name,
|
|
$this->logo,
|
|
$this->rcs,
|
|
$this->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'));
|
|
});
|
|
});
|