ldc-mesbr-app/tests/Unit/Domain/Models/Entities/BrandTest.php
2026-09-15 09:24:18 +02:00

185 lines
6.1 KiB
PHP

<?php
declare(strict_types=1);
namespace Tests\Unit\Domain\Models\Entities;
use DateTimeImmutable;
use DateTimeZone;
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\Models\ValueObjects\Email;
use Shared\Models\ValueObjects\PicturePath;
use Shared\Models\ValueObjects\RCS;
use stdClass;
covers(Brand::class);
describe('brand', function (): void {
it('creates a brand', function (): void {
// GIVEN
$id = BrandId::generate();
$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');
// WHEN
$brand = new Brand($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);
});
it('updates a brand logo', function (): void {
// GIVEN
$id = BrandId::generate();
$code = new BrandCode('AAA01');
$name = new BrandName('Name');
$logoOld = new PicturePath('files/picture-old.jpg');
$logoNew = new PicturePath('files/picture-new.jpg');
$rcs = new RCS('RCS PARIS 111 222 333');
$contact = new Email('contact@example.com');
$brand = new Brand($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);
});
it('updates a brand rcs', function (): void {
// GIVEN
$id = BrandId::generate();
$code = new BrandCode('AAA01');
$name = new BrandName('Name');
$logo = new PicturePath('files/picture.jpg');
$rcsOld = new RCS('RCS PARIS 111 222 333');
$rcsNew = new RCS('RCS NANTES 444 555 666');
$contact = new Email('contact@example.com');
$brand = new Brand($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);
});
it('updates a brand contact', function (): void {
// GIVEN
$id = BrandId::generate();
$code = new BrandCode('AAA01');
$name = new BrandName('Name');
$logo = new PicturePath('files/picture.jpg');
$rcs = new RCS('RCS PARIS 111 222 333');
$contactOld = new Email('contact-old@example.com');
$contactNew = new Email('contact-new@example.com');
$brand = new Brand($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);
});
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 = new Brand(
$id,
new BrandCode('AAA01'),
new BrandName('Name'),
new PicturePath('files/picture.jpg'),
new RCS('RCS PARIS 111 222 333'),
new Email('contact@example.com'),
);
// 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'));
});
it('considers two brands with the same id as equal', function (): void {
// GIVEN
$id = BrandId::generate();
// WHEN
$first = new Brand(
$id,
new BrandCode('AAA01'),
new BrandName('Name 1'),
new PicturePath('files/picture-1.jpg'),
new RCS('RCS PARIS 111 222 333'),
new Email('contact@example.com'),
);
$second = new Brand(
$id,
new BrandCode('BBB02'),
new BrandName('Name 2'),
new PicturePath('files/picture-2.jpg'),
new RCS('RCS NANTES 444 555 666'),
new Email('contact@example.com'),
);
// THEN
expect($first->equals($second))->toBeTrue();
});
it('considers a brand as not equal to', function (mixed $other): void {
// GIVEN
$brand = new Brand(
BrandId::generate(),
new BrandCode('AAA01'),
new BrandName('Name'),
new PicturePath('files/picture.jpg'),
new RCS('RCS PARIS 111 222 333'),
new Email('contact@example.com'),
);
// WHEN
$result = $brand->equals($other);
// THEN
expect($result)->toBeFalse();
})->with([
'different brand id' => new Brand(
BrandId::generate(),
new BrandCode('AAA01'),
new BrandName('Name'),
new PicturePath('files/picture.jpg'),
new RCS('RCS PARIS 111 222 333'),
new Email('contact@example.com'),
),
'non-brand value' => new stdClass(),
]);
});