ldc-cocotte/tests/Unit/Domain/Models/Entities/ShopperTest.php

237 lines
7.2 KiB
PHP

<?php
declare(strict_types=1);
namespace Tests\Unit\Domain\Models\Entities;
use Carbon\CarbonImmutable;
use Domain\Models\Entities\Shopper;
use Domain\Models\ValueObjects\Email;
use Domain\Models\ValueObjects\Entities\ShopperId;
use Domain\Models\ValueObjects\Matricule;
use Domain\Models\ValueObjects\ShopperEstablishmentCode;
use Domain\Models\ValueObjects\ShopperServiceCode;
use Domain\Models\ValueObjects\ShopperSiteCode;
use Ramsey\Uuid\Uuid;
describe('Shopper', function (): void {
it('can instantiate correctly and check active status (active)', function (): void {
// GIVEN
$id = new ShopperId(Uuid::uuid7());
$email = new Email('test@example.com');
$matricule = new Matricule('1234567');
$startDate = CarbonImmutable::now()->subDay();
$endDate = null;
$etablissement = new ShopperEstablishmentCode('ETAB01');
$site = new ShopperSiteCode('SITE');
$service = new ShopperServiceCode('101');
// WHEN
$entity = new Shopper(
$id,
$email,
$matricule,
'Nom',
'Prenom',
$startDate,
$endDate,
$etablissement,
$site,
$service
);
// THEN
expect($entity->id())->toBe($id)
->and($entity->email)->toBe($email)
->and($entity->matricule)->toBe($matricule)
->and($entity->nom)->toBe('Nom')
->and($entity->prenom)->toBe('Prenom')
->and($entity->startDate)->toBe($startDate)
->and($entity->endDate)->toBeNull()
->and($entity->etablissement)->toBe($etablissement)
->and($entity->site)->toBe($site)
->and($entity->service)->toBe($service)
->and($entity->isActive())->toBeTrue();
});
it('is inactive when endDate is in the past', function (): void {
// GIVEN
$endDate = CarbonImmutable::now()->subDay();
// WHEN
$entity = new Shopper(
new ShopperId(Uuid::uuid7()),
new Email('test@example.com'),
new Matricule('1234567'),
'Nom',
'Prenom',
CarbonImmutable::now()->subDays(2),
$endDate,
new ShopperEstablishmentCode('ETAB01'),
new ShopperSiteCode('SITE'),
new ShopperServiceCode('101')
);
// THEN
expect($entity->isActive())->toBeFalse();
});
it('is active when endDate is in the future', function (): void {
// GIVEN
$endDate = CarbonImmutable::now()->addDay();
// WHEN
$entity = new Shopper(
new ShopperId(Uuid::uuid7()),
new Email('test@example.com'),
new Matricule('1234567'),
'Nom',
'Prenom',
CarbonImmutable::now()->subDays(2),
$endDate,
new ShopperEstablishmentCode('ETAB01'),
new ShopperSiteCode('SITE'),
new ShopperServiceCode('101')
);
// THEN
expect($entity->isActive())->toBeTrue();
});
it('is inactive when startDate is in the future', function (): void {
// GIVEN
$startDate = CarbonImmutable::now()->addDay();
// WHEN
$entity = new Shopper(
new ShopperId(Uuid::uuid7()),
new Email('test@example.com'),
new Matricule('1234567'),
'Nom',
'Prenom',
$startDate,
null,
new ShopperEstablishmentCode('ETAB01'),
new ShopperSiteCode('SITE'),
new ShopperServiceCode('101')
);
// THEN
expect($entity->isActive())->toBeFalse();
});
it('is inactive when startDate is in the future even with future endDate', function (): void {
// GIVEN
$startDate = CarbonImmutable::now()->addDay();
$endDate = CarbonImmutable::now()->addDays(10);
// WHEN
$entity = new Shopper(
new ShopperId(Uuid::uuid7()),
new Email('test@example.com'),
new Matricule('1234567'),
'Nom',
'Prenom',
$startDate,
$endDate,
new ShopperEstablishmentCode('ETAB01'),
new ShopperSiteCode('SITE'),
new ShopperServiceCode('101')
);
// THEN
expect($entity->isActive())->toBeFalse();
});
it('is active when startDate equals now and endDate is null', function (): void {
// GIVEN
$now = CarbonImmutable::parse('2026-11-15 00:00:00');
CarbonImmutable::setTestNow($now);
try {
// WHEN
$entity = new Shopper(
new ShopperId(Uuid::uuid7()),
new Email('test@example.com'),
new Matricule('1234567'),
'Nom',
'Prenom',
$now,
null,
new ShopperEstablishmentCode('ETAB01'),
new ShopperSiteCode('SITE'),
new ShopperServiceCode('101')
);
// THEN
expect($entity->isActive())->toBeTrue();
} finally {
CarbonImmutable::setTestNow();
}
});
it('is inactive at the start of endDate', function (): void {
// GIVEN
$now = CarbonImmutable::parse('2026-11-15 00:00:01');
CarbonImmutable::setTestNow($now);
try {
// WHEN
$entity = new Shopper(
new ShopperId(Uuid::uuid7()),
new Email('test@example.com'),
new Matricule('1234567'),
'Nom',
'Prenom',
CarbonImmutable::parse('2026-11-01 00:00:00'),
CarbonImmutable::parse('2026-11-15 00:00:00'),
new ShopperEstablishmentCode('ETAB01'),
new ShopperSiteCode('SITE'),
new ShopperServiceCode('101')
);
// THEN
expect($entity->isActive())->toBeFalse();
} finally {
CarbonImmutable::setTestNow();
}
});
it('throws exception if nom is empty', function (): void {
// WHEN
$action = fn () => new Shopper(
new ShopperId(Uuid::uuid7()),
new Email('test@example.com'),
new Matricule('1234567'),
'',
'Prenom',
CarbonImmutable::now()->subDays(2),
null,
new ShopperEstablishmentCode('ETAB01'),
new ShopperSiteCode('SITE'),
new ShopperServiceCode('101')
);
// THEN
expect($action)->toThrow(\DomainException::class, 'nom cannot be empty');
});
it('throws exception if prenom is empty', function (): void {
// WHEN
$action = fn () => new Shopper(
new ShopperId(Uuid::uuid7()),
new Email('test@example.com'),
new Matricule('1234567'),
'Nom',
'',
CarbonImmutable::now()->subDays(2),
null,
new ShopperEstablishmentCode('ETAB01'),
new ShopperSiteCode('SITE'),
new ShopperServiceCode('101')
);
// THEN
expect($action)->toThrow(\DomainException::class, 'prenom cannot be empty');
});
});