ldc-arcadia-genesis-php-client/tests/Unit/Lib/ApiDto/EmployeeTest.php

158 lines
6.3 KiB
PHP

<?php
declare(strict_types=1);
namespace Tests\Unit\Lib\ApiDto;
use DateTimeImmutable;
use DateTimeInterface;
use Uwil\ArcadiaApiPhpClient\Lib\ApiDto\Dto;
use Uwil\ArcadiaApiPhpClient\Lib\ApiDto\Dto\Employee;
use Uwil\ArcadiaApiPhpClient\Lib\ApiValueObject\ArcadiaEmployeeId;
use Uwil\ArcadiaApiPhpClient\Lib\ApiValueObject\EstablishmentCode;
use Uwil\ArcadiaApiPhpClient\Lib\ApiValueObject\ServiceId;
use Uwil\ArcadiaApiPhpClient\Lib\ApiValueObject\SiteId;
/**
* Creates a complete Employee object graph.
*/
function createCompleteEmployee(
string $matricule = '1234567',
string $lastname = 'Dupont',
string $firstname = 'Jean',
?DateTimeInterface $startDate = null,
?DateTimeInterface $endDate = null,
?DateTimeInterface $plannedEndDate = null,
string $establishment = 'EST01',
string $site = 'LDCSER',
string $service = '097',
?DateTimeInterface $createdAt = null,
?DateTimeInterface $updatedAt = null,
): Employee {
return new Employee(
matriculePublique: new ArcadiaEmployeeId($matricule),
lastname: $lastname,
firstname: $firstname,
start_date: $startDate ?? new DateTimeImmutable('2023-01-15T08:00:00+00:00'),
end_date: $endDate ?? new DateTimeImmutable('2025-12-31T18:00:00+00:00'),
planned_end_date: $plannedEndDate ?? new DateTimeImmutable('2025-06-30T17:00:00+00:00'),
establishment: new EstablishmentCode($establishment),
site: new SiteId($site),
service: new ServiceId($service),
created_at: $createdAt ?? new DateTimeImmutable('2023-01-01T00:00:00+00:00'),
updated_at: $updatedAt ?? new DateTimeImmutable('2024-01-01T10:00:00+00:00'),
);
}
describe('Employee DTO test', function (): void {
it('can instantiate Employee with complete object graph', function (): void {
// GIVEN
$startDate = new DateTimeImmutable('2023-01-15T08:00:00+00:00');
$endDate = new DateTimeImmutable('2025-12-31T18:00:00+00:00');
$plannedEndDate = new DateTimeImmutable('2025-06-30T17:00:00+00:00');
$createdAt = new DateTimeImmutable('2023-01-01T00:00:00+00:00');
$updatedAt = new DateTimeImmutable('2024-01-01T10:00:00+00:00');
// WHEN
$employee = createCompleteEmployee(
matricule: '1234567',
lastname: 'Dupont',
firstname: 'Jean',
startDate: $startDate,
endDate: $endDate,
plannedEndDate: $plannedEndDate,
establishment: 'EST01',
site: 'LDCSER',
service: '097',
createdAt: $createdAt,
updatedAt: $updatedAt,
);
// THEN
expect($employee)->toBeInstanceOf(Employee::class)
->and($employee)->toBeInstanceOf(Dto::class)
->and($employee->matriculePublique)->toBeInstanceOf(ArcadiaEmployeeId::class)
->and($employee->matriculePublique->value)->toBe('1234567')
->and($employee->lastname)->toBe('Dupont')
->and($employee->firstname)->toBe('Jean')
->and($employee->start_date)->toBe($startDate)
->and($employee->end_date)->toBe($endDate)
->and($employee->planned_end_date)->toBe($plannedEndDate)
->and($employee->establishment)->toBeInstanceOf(EstablishmentCode::class)
->and($employee->establishment->value)->toBe('EST01')
->and($employee->site)->toBeInstanceOf(SiteId::class)
->and($employee->site->value)->toBe('LDCSER')
->and($employee->service)->toBeInstanceOf(ServiceId::class)
->and($employee->service->value)->toBe('097')
->and($employee->created_at)->toBe($createdAt)
->and($employee->updated_at)->toBe($updatedAt);
});
it('can jsonSerialize complete Employee object graph', function (): void {
// GIVEN
$employee = createCompleteEmployee();
// WHEN
$serialized = $employee->jsonSerialize();
// THEN
expect($serialized)->toBe([
'matriculePublique' => '1234567',
'lastname' => 'Dupont',
'firstname' => 'Jean',
'start_date' => '2023-01-15T08:00:00+00:00',
'end_date' => '2025-12-31T18:00:00+00:00',
'planned_end_date' => '2025-06-30T17:00:00+00:00',
'establishment' => 'EST01',
'site' => 'LDCSER',
'service' => '097',
'created_at' => '2023-01-01T00:00:00+00:00',
'updated_at' => '2024-01-01T10:00:00+00:00',
]);
$jsonEncoded = json_encode($employee);
expect($jsonEncoded)->toBeString();
expect(json_decode((string) $jsonEncoded, true))->toBe($serialized);
});
it('can instantiate Employee with nullable fields set to null', function (): void {
// GIVEN
$startDate = new DateTimeImmutable('2023-06-01T09:00:00+00:00');
$createdAt = new DateTimeImmutable('2023-06-01T09:00:00+00:00');
// WHEN
$employee = new Employee(
matriculePublique: new ArcadiaEmployeeId('9876543'),
lastname: 'Martin',
firstname: 'Claire',
start_date: $startDate,
end_date: null,
planned_end_date: null,
establishment: new EstablishmentCode('EST02'),
site: new SiteId('SLELA'),
service: new ServiceId('098'),
created_at: $createdAt,
updated_at: null,
);
// THEN
expect($employee->end_date)->toBeNull()
->and($employee->planned_end_date)->toBeNull()
->and($employee->updated_at)->toBeNull();
$serialized = $employee->jsonSerialize();
expect($serialized)->toBe([
'matriculePublique' => '9876543',
'lastname' => 'Martin',
'firstname' => 'Claire',
'start_date' => '2023-06-01T09:00:00+00:00',
'end_date' => null,
'planned_end_date' => null,
'establishment' => 'EST02',
'site' => 'SLELA',
'service' => '098',
'created_at' => '2023-06-01T09:00:00+00:00',
'updated_at' => null,
]);
});
});