ldc-cocotte/tests/Integration/WithApp/App/Models/EmployeeTest.php

73 lines
3 KiB
PHP
Raw Normal View History

<?php
declare(strict_types=1);
namespace Tests\Integration\WithApp\App\Models;
use App\Models\Employee;
use Domain\Models\ValueObjects\Code;
use Domain\Models\ValueObjects\Matricule;
use Domain\Models\ValueObjects\ShopperEstablishmentCode;
use Domain\Models\ValueObjects\ShopperServiceCode;
use Domain\Models\ValueObjects\ShopperSiteCode;
use Illuminate\Support\Facades\Date;
describe('Employee model active status', function (): void {
it('evaluates is_active and active scope consistently across start_date and end_date', function (): void {
// GIVEN
$frozenNow = Date::parse('2026-11-15 10:00:00');
Date::setTestNow($frozenNow);
try {
$createEmployee = static function (
string $matricule,
string $code,
?string $startDate,
?string $endDate,
): Employee {
return Employee::query()->create([
'matricule' => new Matricule($matricule),
'code' => new Code($code),
'nom' => 'Dupont',
'prenom' => 'Jean',
'start_date' => $startDate,
'end_date' => $endDate,
'etablissement' => new ShopperEstablishmentCode('ETAB01'),
'site' => new ShopperSiteCode('SITE'),
'service' => new ShopperServiceCode('101'),
]);
};
$activeNoDates = $createEmployee('1000001', 'ABCDEF1', null, null);
$activeStartedToday = $createEmployee('1000002', 'ABCDEF2', '2026-11-15', null);
$activeInWindow = $createEmployee('1000003', 'ABCDEF3', '2026-11-01', '2026-11-16');
$inactiveFutureStart = $createEmployee('1000004', 'ABCDEF4', '2026-11-16', null);
$inactiveFutureBoth = $createEmployee('1000005', 'ABCDEF5', '2026-11-16', '2026-11-30');
$inactiveEndedToday = $createEmployee('1000006', 'ABCDEF6', '2026-11-01', '2026-11-15');
$inactiveEndedPast = $createEmployee('1000007', 'ABCDEF7', '2026-11-01', '2026-11-14');
// WHEN
$activeIds = Employee::query()
->active()
->pluck('id')
->all();
// THEN
expect($activeNoDates->is_active)->toBeTrue()
->and($activeStartedToday->is_active)->toBeTrue()
->and($activeInWindow->is_active)->toBeTrue()
->and($inactiveFutureStart->is_active)->toBeFalse()
->and($inactiveFutureBoth->is_active)->toBeFalse()
->and($inactiveEndedToday->is_active)->toBeFalse()
->and($inactiveEndedPast->is_active)->toBeFalse()
->and($activeIds)->toEqualCanonicalizing([
$activeNoDates->id,
$activeStartedToday->id,
$activeInWindow->id,
]);
} finally {
Date::setTestNow();
}
});
});