Ajout des notification + page employé (#6)
Some checks failed
Auto Tag Develop / tag (push) Has been cancelled
Some checks failed
Auto Tag Develop / tag (push) Has been cancelled
| Numéro du ticket | Titre du ticket | |------------------|-----------------| | | | ## Description de la PR ## Modification du .env ## Check list - [ ] Pas de régression - [ ] TU/TI/TF rédigée - [ ] TU/TI/TF OK - [ ] CHANGELOG modifié Reviewed-on: #6 Co-authored-by: tristan <tristan@yuno.malio.fr> Co-committed-by: tristan <tristan@yuno.malio.fr>
This commit was merged in pull request #6.
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Tests\Service\Contracts;
|
||||
|
||||
use App\Entity\Contract;
|
||||
use App\Entity\Employee;
|
||||
use App\Enum\ContractNature;
|
||||
use App\Service\Contracts\EmployeeContractChangeRequestFactory;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
final class EmployeeContractChangeRequestFactoryTest extends TestCase
|
||||
{
|
||||
public function testCreatesRequestFromEmployeePayload(): void
|
||||
{
|
||||
$factory = new EmployeeContractChangeRequestFactory();
|
||||
$employee = $this->buildEmployee()
|
||||
->setContractNature('CDD')
|
||||
->setContractStartDate('2026-03-01')
|
||||
->setContractEndDate('2026-03-10')
|
||||
->setContractPaidLeaveSettled(true)
|
||||
;
|
||||
|
||||
$request = $factory->fromEmployee($employee);
|
||||
|
||||
self::assertSame(ContractNature::CDD, $request->contractNature);
|
||||
self::assertSame('2026-03-01', $request->contractStartDate?->format('Y-m-d'));
|
||||
self::assertSame('2026-03-10', $request->contractEndDate?->format('Y-m-d'));
|
||||
self::assertTrue($request->contractPaidLeaveSettled);
|
||||
self::assertTrue($request->hasPeriodChangeRequest());
|
||||
}
|
||||
|
||||
public function testThrowsOnInvalidContractNature(): void
|
||||
{
|
||||
$factory = new EmployeeContractChangeRequestFactory();
|
||||
$employee = $this->buildEmployee()->setContractNature('XYZ');
|
||||
|
||||
$this->expectException(UnprocessableEntityHttpException::class);
|
||||
$this->expectExceptionMessage('contractNature must be one of CDI, CDD, INTERIM.');
|
||||
$factory->fromEmployee($employee);
|
||||
}
|
||||
|
||||
public function testThrowsOnInvalidDateFormat(): void
|
||||
{
|
||||
$factory = new EmployeeContractChangeRequestFactory();
|
||||
$employee = $this->buildEmployee()->setContractStartDate('01/03/2026');
|
||||
|
||||
$this->expectException(UnprocessableEntityHttpException::class);
|
||||
$this->expectExceptionMessage('contractStartDate must use Y-m-d format.');
|
||||
$factory->fromEmployee($employee);
|
||||
}
|
||||
|
||||
private function buildEmployee(): Employee
|
||||
{
|
||||
$contract = new Contract()
|
||||
->setName('35h')
|
||||
->setTrackingMode(Contract::TRACKING_TIME)
|
||||
->setWeeklyHours(35)
|
||||
;
|
||||
|
||||
return new Employee()
|
||||
->setFirstName('Alice')
|
||||
->setLastName('Martin')
|
||||
->setContract($contract)
|
||||
;
|
||||
}
|
||||
}
|
||||
118
tests/Service/Contracts/EmployeeContractPeriodValidatorTest.php
Normal file
118
tests/Service/Contracts/EmployeeContractPeriodValidatorTest.php
Normal file
@@ -0,0 +1,118 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Tests\Service\Contracts;
|
||||
|
||||
use App\Entity\Contract;
|
||||
use App\Entity\Employee;
|
||||
use App\Entity\EmployeeContractPeriod;
|
||||
use App\Enum\ContractNature;
|
||||
use App\Service\Contracts\EmployeeContractPeriodValidator;
|
||||
use DateTimeImmutable;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
final class EmployeeContractPeriodValidatorTest extends TestCase
|
||||
{
|
||||
private EmployeeContractPeriodValidator $validator;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->validator = new EmployeeContractPeriodValidator();
|
||||
}
|
||||
|
||||
public function testAssertPeriodDatesRejectsEndBeforeStart(): void
|
||||
{
|
||||
$this->expectException(UnprocessableEntityHttpException::class);
|
||||
$this->expectExceptionMessage('contractEndDate cannot be before contractStartDate.');
|
||||
|
||||
$this->validator->assertPeriodDates(
|
||||
new DateTimeImmutable('2026-03-10'),
|
||||
new DateTimeImmutable('2026-03-01'),
|
||||
ContractNature::CDD
|
||||
);
|
||||
}
|
||||
|
||||
public function testAssertPeriodDatesRejectsMissingEndDateForCdd(): void
|
||||
{
|
||||
$this->expectException(UnprocessableEntityHttpException::class);
|
||||
$this->expectExceptionMessage('contractEndDate is required for CDD and INTERIM.');
|
||||
|
||||
$this->validator->assertPeriodDates(
|
||||
new DateTimeImmutable('2026-03-01'),
|
||||
null,
|
||||
ContractNature::CDD
|
||||
);
|
||||
}
|
||||
|
||||
public function testAssertPeriodDatesRejectsEndDateForCdiWhenNotAllowed(): void
|
||||
{
|
||||
$this->expectException(UnprocessableEntityHttpException::class);
|
||||
$this->expectExceptionMessage('contractEndDate must be empty for CDI.');
|
||||
|
||||
$this->validator->assertPeriodDates(
|
||||
new DateTimeImmutable('2026-03-01'),
|
||||
new DateTimeImmutable('2026-03-10'),
|
||||
ContractNature::CDI
|
||||
);
|
||||
}
|
||||
|
||||
public function testAssertCloseEndDateCanBeAppliedRejectsIncrease(): void
|
||||
{
|
||||
$this->expectException(UnprocessableEntityHttpException::class);
|
||||
$this->expectExceptionMessage('contractEndDate cannot be increased on current contract.');
|
||||
|
||||
$this->validator->assertCloseEndDateCanBeApplied(
|
||||
new DateTimeImmutable('2026-03-01'),
|
||||
new DateTimeImmutable('2026-03-10'),
|
||||
new DateTimeImmutable('2026-03-11'),
|
||||
ContractNature::CDI
|
||||
);
|
||||
}
|
||||
|
||||
public function testAssertNextStartDateCompatibleRejectsWhenNotAfterCurrentOpenStart(): void
|
||||
{
|
||||
$currentPeriod = $this->buildCurrentPeriod('2026-03-05', null);
|
||||
|
||||
$this->expectException(UnprocessableEntityHttpException::class);
|
||||
$this->expectExceptionMessage('contractStartDate must be after current contract start date.');
|
||||
|
||||
$this->validator->assertNextStartDateCompatible(new DateTimeImmutable('2026-03-05'), $currentPeriod);
|
||||
}
|
||||
|
||||
public function testAssertNextStartDateCompatibleRejectsWhenNotAfterCurrentClosedEnd(): void
|
||||
{
|
||||
$currentPeriod = $this->buildCurrentPeriod('2026-03-01', '2026-03-10');
|
||||
|
||||
$this->expectException(UnprocessableEntityHttpException::class);
|
||||
$this->expectExceptionMessage('contractStartDate must be after current contract end date.');
|
||||
|
||||
$this->validator->assertNextStartDateCompatible(new DateTimeImmutable('2026-03-10'), $currentPeriod);
|
||||
}
|
||||
|
||||
private function buildCurrentPeriod(string $startDate, ?string $endDate): EmployeeContractPeriod
|
||||
{
|
||||
$contract = new Contract()
|
||||
->setName('35h')
|
||||
->setTrackingMode(Contract::TRACKING_TIME)
|
||||
->setWeeklyHours(35)
|
||||
;
|
||||
$employee = new Employee()
|
||||
->setFirstName('Test')
|
||||
->setLastName('User')
|
||||
->setContract($contract)
|
||||
;
|
||||
|
||||
return new EmployeeContractPeriod()
|
||||
->setEmployee($employee)
|
||||
->setContract($contract)
|
||||
->setStartDate(new DateTimeImmutable($startDate))
|
||||
->setEndDate(null !== $endDate ? new DateTimeImmutable($endDate) : null)
|
||||
->setContractNature(ContractNature::CDI)
|
||||
;
|
||||
}
|
||||
}
|
||||
@@ -43,9 +43,9 @@ final class WorkedHoursCreditPolicyTest extends TestCase
|
||||
$policy = new WorkedHoursCreditPolicy($this->buildResolverStub());
|
||||
$absence = $this->buildAbsence(trackMode: Contract::TRACKING_PRESENCE, weeklyHours: null, countAsWorked: true);
|
||||
|
||||
$units = $policy->computeCreditedPresenceUnits($absence, '2026-02-16', true, false);
|
||||
|
||||
self::assertSame(0.5, $units);
|
||||
// Forfait : les absences ne créditent jamais de présence, seules les checkboxes comptent.
|
||||
self::assertSame(0.0, $policy->computeCreditedPresenceUnits($absence, '2026-02-16', true, false));
|
||||
self::assertSame(0.0, $policy->computeCreditedPresenceUnits($absence, '2026-02-16', true, true));
|
||||
}
|
||||
|
||||
public function testNoCreditWhenAbsenceTypeDoesNotCount(): void
|
||||
|
||||
264
tests/State/EmployeeWriteProcessorTest.php
Normal file
264
tests/State/EmployeeWriteProcessorTest.php
Normal file
@@ -0,0 +1,264 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Tests\State;
|
||||
|
||||
use ApiPlatform\Metadata\Delete;
|
||||
use ApiPlatform\Metadata\Patch;
|
||||
use ApiPlatform\State\ProcessorInterface;
|
||||
use App\Entity\Contract;
|
||||
use App\Entity\Employee;
|
||||
use App\Entity\EmployeeContractPeriod;
|
||||
use App\Enum\ContractNature;
|
||||
use App\Repository\Contract\EmployeeContractPeriodReadRepositoryInterface;
|
||||
use App\Service\Contracts\EmployeeContractChangeRequestFactory;
|
||||
use App\Service\Contracts\EmployeeContractPeriodManagerInterface;
|
||||
use App\State\EmployeeWriteProcessor;
|
||||
use DateTimeImmutable;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Doctrine\ORM\UnitOfWork;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use ReflectionProperty;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
final class EmployeeWriteProcessorTest extends TestCase
|
||||
{
|
||||
public function testDelegatesCloseOnlyRequestToPeriodManager(): void
|
||||
{
|
||||
$employee = $this->buildEmployeeWithId(42);
|
||||
$employee->setContractEndDate('2026-03-05');
|
||||
$employee->setContractPaidLeaveSettled(true);
|
||||
$contract = $employee->getContract();
|
||||
self::assertInstanceOf(Contract::class, $contract);
|
||||
|
||||
$todayPeriod = new EmployeeContractPeriod()
|
||||
->setEmployee($employee)
|
||||
->setContract($contract)
|
||||
->setStartDate(new DateTimeImmutable('2026-03-01'))
|
||||
->setEndDate(null)
|
||||
->setContractNature(ContractNature::CDI)
|
||||
;
|
||||
|
||||
$persistProcessor = $this->createMock(ProcessorInterface::class);
|
||||
$removeProcessor = $this->createStub(ProcessorInterface::class);
|
||||
$entityManager = $this->createStub(EntityManagerInterface::class);
|
||||
$periodRepository = $this->createMock(EmployeeContractPeriodReadRepositoryInterface::class);
|
||||
$changeRequestFactory = new EmployeeContractChangeRequestFactory();
|
||||
$periodManager = $this->createMock(EmployeeContractPeriodManagerInterface::class);
|
||||
|
||||
$persistProcessor
|
||||
->expects(self::once())
|
||||
->method('process')
|
||||
->willReturn($employee)
|
||||
;
|
||||
$periodRepository
|
||||
->expects(self::once())
|
||||
->method('findOneCoveringDate')
|
||||
->with($employee, self::isInstanceOf(DateTimeImmutable::class))
|
||||
->willReturn($todayPeriod)
|
||||
;
|
||||
$periodManager
|
||||
->expects(self::once())
|
||||
->method('closeCurrentPeriod')
|
||||
->with(
|
||||
$todayPeriod,
|
||||
self::callback(static fn (DateTimeImmutable $value): bool => '2026-03-05' === $value->format('Y-m-d')),
|
||||
true
|
||||
)
|
||||
;
|
||||
$periodManager
|
||||
->expects(self::never())
|
||||
->method('createNextPeriod')
|
||||
;
|
||||
|
||||
$this->mockOriginalContract($entityManager, $employee, $contract);
|
||||
|
||||
$processor = new EmployeeWriteProcessor(
|
||||
$persistProcessor,
|
||||
$removeProcessor,
|
||||
$entityManager,
|
||||
$periodRepository,
|
||||
$changeRequestFactory,
|
||||
$periodManager
|
||||
);
|
||||
|
||||
$result = $processor->process($employee, new Patch());
|
||||
|
||||
self::assertSame($employee, $result);
|
||||
}
|
||||
|
||||
public function testDelegatesNonCloseRequestToCreateNextPeriod(): void
|
||||
{
|
||||
$employee = $this->buildEmployeeWithId(43);
|
||||
$employee->setContractStartDate('2026-03-06');
|
||||
$contract = $employee->getContract();
|
||||
self::assertInstanceOf(Contract::class, $contract);
|
||||
|
||||
$todayPeriod = new EmployeeContractPeriod()
|
||||
->setEmployee($employee)
|
||||
->setContract($contract)
|
||||
->setStartDate(new DateTimeImmutable('2026-03-01'))
|
||||
->setEndDate(null)
|
||||
->setContractNature(ContractNature::CDI)
|
||||
;
|
||||
|
||||
$persistProcessor = $this->createMock(ProcessorInterface::class);
|
||||
$removeProcessor = $this->createStub(ProcessorInterface::class);
|
||||
$entityManager = $this->createStub(EntityManagerInterface::class);
|
||||
$periodRepository = $this->createMock(EmployeeContractPeriodReadRepositoryInterface::class);
|
||||
$changeRequestFactory = new EmployeeContractChangeRequestFactory();
|
||||
$periodManager = $this->createMock(EmployeeContractPeriodManagerInterface::class);
|
||||
|
||||
$persistProcessor
|
||||
->expects(self::once())
|
||||
->method('process')
|
||||
->willReturn($employee)
|
||||
;
|
||||
$periodRepository
|
||||
->expects(self::once())
|
||||
->method('findOneCoveringDate')
|
||||
->with($employee, self::isInstanceOf(DateTimeImmutable::class))
|
||||
->willReturn($todayPeriod)
|
||||
;
|
||||
$periodManager
|
||||
->expects(self::once())
|
||||
->method('createNextPeriod')
|
||||
->with(
|
||||
employee: $employee,
|
||||
contract: $contract,
|
||||
startDate: self::callback(static fn (DateTimeImmutable $value): bool => '2026-03-06' === $value->format('Y-m-d')),
|
||||
endDate: null,
|
||||
nature: ContractNature::CDI,
|
||||
todayPeriod: $todayPeriod
|
||||
)
|
||||
;
|
||||
$periodManager
|
||||
->expects(self::never())
|
||||
->method('closeCurrentPeriod')
|
||||
;
|
||||
|
||||
$this->mockOriginalContract($entityManager, $employee, $contract);
|
||||
|
||||
$processor = new EmployeeWriteProcessor(
|
||||
$persistProcessor,
|
||||
$removeProcessor,
|
||||
$entityManager,
|
||||
$periodRepository,
|
||||
$changeRequestFactory,
|
||||
$periodManager
|
||||
);
|
||||
|
||||
$result = $processor->process($employee, new Patch());
|
||||
|
||||
self::assertSame($employee, $result);
|
||||
}
|
||||
|
||||
public function testSkipsPeriodOperationsWhenContractAndPeriodPayloadAreUnchanged(): void
|
||||
{
|
||||
$employee = $this->buildEmployeeWithId(44);
|
||||
$contract = $employee->getContract();
|
||||
self::assertInstanceOf(Contract::class, $contract);
|
||||
|
||||
$persistProcessor = $this->createMock(ProcessorInterface::class);
|
||||
$removeProcessor = $this->createStub(ProcessorInterface::class);
|
||||
$entityManager = $this->createStub(EntityManagerInterface::class);
|
||||
$periodRepository = $this->createMock(EmployeeContractPeriodReadRepositoryInterface::class);
|
||||
$changeRequestFactory = new EmployeeContractChangeRequestFactory();
|
||||
$periodManager = $this->createMock(EmployeeContractPeriodManagerInterface::class);
|
||||
|
||||
$persistProcessor
|
||||
->expects(self::once())
|
||||
->method('process')
|
||||
->willReturn($employee)
|
||||
;
|
||||
$periodRepository->expects(self::never())->method('findOneCoveringDate');
|
||||
$periodManager->expects(self::never())->method('closeCurrentPeriod');
|
||||
$periodManager->expects(self::never())->method('createNextPeriod');
|
||||
|
||||
$this->mockOriginalContract($entityManager, $employee, $contract);
|
||||
|
||||
$processor = new EmployeeWriteProcessor(
|
||||
$persistProcessor,
|
||||
$removeProcessor,
|
||||
$entityManager,
|
||||
$periodRepository,
|
||||
$changeRequestFactory,
|
||||
$periodManager
|
||||
);
|
||||
|
||||
$result = $processor->process($employee, new Patch());
|
||||
|
||||
self::assertSame($employee, $result);
|
||||
}
|
||||
|
||||
public function testDeleteOperationDelegatesToRemoveProcessor(): void
|
||||
{
|
||||
$employee = $this->buildEmployeeWithId(45);
|
||||
|
||||
$persistProcessor = $this->createMock(ProcessorInterface::class);
|
||||
$removeProcessor = $this->createMock(ProcessorInterface::class);
|
||||
$entityManager = $this->createStub(EntityManagerInterface::class);
|
||||
$periodRepository = $this->createStub(EmployeeContractPeriodReadRepositoryInterface::class);
|
||||
$changeRequestFactory = new EmployeeContractChangeRequestFactory();
|
||||
$periodManager = $this->createStub(EmployeeContractPeriodManagerInterface::class);
|
||||
|
||||
$persistProcessor->expects(self::never())->method('process');
|
||||
$removeProcessor
|
||||
->expects(self::once())
|
||||
->method('process')
|
||||
->with($employee, self::isInstanceOf(Delete::class), [], [])
|
||||
->willReturn(null)
|
||||
;
|
||||
|
||||
$processor = new EmployeeWriteProcessor(
|
||||
$persistProcessor,
|
||||
$removeProcessor,
|
||||
$entityManager,
|
||||
$periodRepository,
|
||||
$changeRequestFactory,
|
||||
$periodManager
|
||||
);
|
||||
|
||||
$result = $processor->process($employee, new Delete());
|
||||
|
||||
self::assertNull($result);
|
||||
}
|
||||
|
||||
private function buildEmployeeWithId(int $id): Employee
|
||||
{
|
||||
$contract = new Contract()
|
||||
->setName('39h')
|
||||
->setTrackingMode(Contract::TRACKING_TIME)
|
||||
->setWeeklyHours(39)
|
||||
;
|
||||
|
||||
$employee = new Employee()
|
||||
->setFirstName('John')
|
||||
->setLastName('Doe')
|
||||
->setContract($contract)
|
||||
;
|
||||
|
||||
$ref = new ReflectionProperty($employee, 'id');
|
||||
$ref->setValue($employee, $id);
|
||||
|
||||
return $employee;
|
||||
}
|
||||
|
||||
private function mockOriginalContract(EntityManagerInterface $entityManager, Employee $employee, Contract $contract): void
|
||||
{
|
||||
$unitOfWork = $this->createStub(UnitOfWork::class);
|
||||
$unitOfWork
|
||||
->method('getOriginalEntityData')
|
||||
->with($employee)
|
||||
->willReturn(['contract' => $contract])
|
||||
;
|
||||
|
||||
$entityManager
|
||||
->method('getUnitOfWork')
|
||||
->willReturn($unitOfWork)
|
||||
;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user