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

287 lines
9.3 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 Shared\Helpers\UuidHelper;
describe('Shopper', function (): void {
it('can instantiate correctly and check active status (active)', function (): void {
// GIVEN
$id = new ShopperId(UuidHelper::v7());
$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('Europe/Paris')->subDay();
// WHEN
$entity = new Shopper(
new ShopperId(UuidHelper::v7()),
new Email('test@example.com'),
new Matricule('1234567'),
'Nom',
'Prenom',
CarbonImmutable::now('Europe/Paris')->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('Europe/Paris')->addDay();
// WHEN
$entity = new Shopper(
new ShopperId(UuidHelper::v7()),
new Email('test@example.com'),
new Matricule('1234567'),
'Nom',
'Prenom',
CarbonImmutable::now('Europe/Paris')->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('Europe/Paris')->addDay();
// WHEN
$entity = new Shopper(
new ShopperId(UuidHelper::v7()),
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('Europe/Paris')->addDay();
$endDate = CarbonImmutable::now('Europe/Paris')->addDays(10);
// WHEN
$entity = new Shopper(
new ShopperId(UuidHelper::v7()),
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', 'Europe/Paris');
CarbonImmutable::setTestNow($now);
try {
// WHEN
$entity = new Shopper(
new ShopperId(UuidHelper::v7()),
new Email('test@example.com'),
new Matricule('1234567'),
'Nom',
'Prenom',
CarbonImmutable::parse('2026-11-15', 'UTC'),
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:00', 'Europe/Paris');
CarbonImmutable::setTestNow($now);
try {
// WHEN
$entity = new Shopper(
new ShopperId(UuidHelper::v7()),
new Email('test@example.com'),
new Matricule('1234567'),
'Nom',
'Prenom',
CarbonImmutable::parse('2026-11-01', 'UTC'),
CarbonImmutable::parse('2026-11-15', 'UTC'),
new ShopperEstablishmentCode('ETAB01'),
new ShopperSiteCode('SITE'),
new ShopperServiceCode('101')
);
// THEN
expect($entity->isActive())->toBeFalse();
} finally {
CarbonImmutable::setTestNow();
}
});
it('activates shopper at Europe/Paris midnight even when UTC date is still previous day', function (): void {
// GIVEN: 2026-05-07 22:30:00 UTC === 2026-05-08 00:30:00 Europe/Paris
CarbonImmutable::setTestNow(CarbonImmutable::parse('2026-05-07 22:30:00', 'UTC'));
try {
$entity = new Shopper(
new ShopperId(UuidHelper::v7()),
new Email('test@example.com'),
new Matricule('1234567'),
'Nom',
'Prenom',
CarbonImmutable::parse('2026-05-08', 'UTC'),
null,
new ShopperEstablishmentCode('ETAB01'),
new ShopperSiteCode('SITE'),
new ShopperServiceCode('101')
);
// WHEN & THEN
expect($entity->isActive())->toBeTrue();
} finally {
CarbonImmutable::setTestNow();
}
});
it('deactivates shopper at Europe/Paris midnight on endDate even when UTC date is still previous day', function (): void {
// GIVEN: 2026-05-07 22:30:00 UTC === 2026-05-08 00:30:00 Europe/Paris
CarbonImmutable::setTestNow(CarbonImmutable::parse('2026-05-07 22:30:00', 'UTC'));
try {
$entity = new Shopper(
new ShopperId(UuidHelper::v7()),
new Email('test@example.com'),
new Matricule('1234567'),
'Nom',
'Prenom',
CarbonImmutable::parse('2026-05-01', 'UTC'),
CarbonImmutable::parse('2026-05-08', 'UTC'),
new ShopperEstablishmentCode('ETAB01'),
new ShopperSiteCode('SITE'),
new ShopperServiceCode('101')
);
// WHEN & THEN
expect($entity->isActive())->toBeFalse();
} finally {
CarbonImmutable::setTestNow();
}
});
it('throws exception if nom is empty', function (): void {
// WHEN
$action = fn () => new Shopper(
new ShopperId(UuidHelper::v7()),
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(UuidHelper::v7()),
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');
});
});