155 lines
5.2 KiB
PHP
155 lines
5.2 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Tests\State;
|
|
|
|
use ApiPlatform\Metadata\Get;
|
|
use App\Entity\Absence;
|
|
use App\Entity\AbsenceType;
|
|
use App\Entity\Contract;
|
|
use App\Entity\Employee;
|
|
use App\Entity\User;
|
|
use App\Enum\HalfDay;
|
|
use App\Repository\Contract\AbsenceReadRepositoryInterface;
|
|
use App\Repository\Contract\EmployeeScopedRepositoryInterface;
|
|
use App\Service\WorkHours\AbsenceSegmentsResolver;
|
|
use App\Service\WorkHours\WorkedHoursCreditPolicy;
|
|
use App\State\WorkHourDayContextProvider;
|
|
use DateTime;
|
|
use PHPUnit\Framework\TestCase;
|
|
use ReflectionObject;
|
|
use Symfony\Bundle\SecurityBundle\Security;
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
use Symfony\Component\HttpFoundation\RequestStack;
|
|
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
|
|
use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException;
|
|
|
|
/**
|
|
* @internal
|
|
*/
|
|
final class WorkHourDayContextProviderTest extends TestCase
|
|
{
|
|
private Security $security;
|
|
private EmployeeScopedRepositoryInterface $employeeRepository;
|
|
private AbsenceReadRepositoryInterface $absenceRepository;
|
|
private RequestStack $requestStack;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
$this->security = $this->createStub(Security::class);
|
|
$this->employeeRepository = $this->createStub(EmployeeScopedRepositoryInterface::class);
|
|
$this->absenceRepository = $this->createStub(AbsenceReadRepositoryInterface::class);
|
|
$this->requestStack = new RequestStack();
|
|
}
|
|
|
|
public function testThrowsWhenAnonymous(): void
|
|
{
|
|
$this->security->method('getUser')->willReturn(null);
|
|
|
|
$provider = new WorkHourDayContextProvider(
|
|
$this->security,
|
|
$this->requestStack,
|
|
$this->employeeRepository,
|
|
$this->absenceRepository,
|
|
new AbsenceSegmentsResolver(),
|
|
new WorkedHoursCreditPolicy()
|
|
);
|
|
|
|
$this->expectException(AccessDeniedHttpException::class);
|
|
$provider->provide(new Get());
|
|
}
|
|
|
|
public function testThrowsWhenDateFormatInvalid(): void
|
|
{
|
|
$this->requestStack->push(new Request(query: ['workDate' => '16-02-2026']));
|
|
$this->security->method('getUser')->willReturn(new User());
|
|
|
|
$provider = new WorkHourDayContextProvider(
|
|
$this->security,
|
|
$this->requestStack,
|
|
$this->employeeRepository,
|
|
$this->absenceRepository,
|
|
new AbsenceSegmentsResolver(),
|
|
new WorkedHoursCreditPolicy()
|
|
);
|
|
|
|
$this->expectException(UnprocessableEntityHttpException::class);
|
|
$provider->provide(new Get());
|
|
}
|
|
|
|
public function testBuildsRowsWithAbsenceCredits(): void
|
|
{
|
|
$user = new User();
|
|
$employee = $this->buildEmployee(1, Contract::TRACKING_TIME, 35);
|
|
$absence = $this->buildAbsence($employee, '2026-02-16', '2026-02-16', true);
|
|
|
|
$this->requestStack->push(new Request(query: ['workDate' => '2026-02-16']));
|
|
$this->security->method('getUser')->willReturn($user);
|
|
$this->employeeRepository->method('findScoped')->with($user)->willReturn([$employee]);
|
|
$this->absenceRepository->method('findByDateAndEmployees')->willReturn([$absence]);
|
|
|
|
$provider = new WorkHourDayContextProvider(
|
|
$this->security,
|
|
$this->requestStack,
|
|
$this->employeeRepository,
|
|
$this->absenceRepository,
|
|
new AbsenceSegmentsResolver(),
|
|
new WorkedHoursCreditPolicy()
|
|
);
|
|
|
|
$result = $provider->provide(new Get());
|
|
|
|
self::assertSame('2026-02-16', $result->workDate);
|
|
self::assertCount(1, $result->rows);
|
|
self::assertSame(1, $result->rows[0]['employeeId']);
|
|
self::assertSame('Maladie', $result->rows[0]['absenceLabel']);
|
|
self::assertSame('AM', $result->rows[0]['absenceHalf']);
|
|
self::assertSame(210, $result->rows[0]['creditedMinutes']);
|
|
}
|
|
|
|
private function buildEmployee(int $id, string $trackingMode, ?int $weeklyHours): Employee
|
|
{
|
|
$contract = new Contract()
|
|
->setName('Contrat')
|
|
->setTrackingMode($trackingMode)
|
|
->setWeeklyHours($weeklyHours)
|
|
;
|
|
$employee = new Employee()
|
|
->setFirstName('Jean')
|
|
->setLastName('Test')
|
|
->setContract($contract)
|
|
;
|
|
$this->setEntityId($employee, $id);
|
|
|
|
return $employee;
|
|
}
|
|
|
|
private function buildAbsence(Employee $employee, string $startDate, string $endDate, bool $countAsWorked): Absence
|
|
{
|
|
$type = new AbsenceType()
|
|
->setCode('MAL')
|
|
->setLabel('Maladie')
|
|
->setColor('#f00')
|
|
->setCountAsWorkedHours($countAsWorked)
|
|
;
|
|
|
|
return new Absence()
|
|
->setEmployee($employee)
|
|
->setType($type)
|
|
->setStartDate(new DateTime($startDate))
|
|
->setEndDate(new DateTime($endDate))
|
|
->setStartHalf(HalfDay::AM)
|
|
->setEndHalf(HalfDay::AM)
|
|
;
|
|
}
|
|
|
|
private function setEntityId(object $entity, int $id): void
|
|
{
|
|
$reflection = new ReflectionObject($entity);
|
|
$property = $reflection->getProperty('id');
|
|
$property->setAccessible(true);
|
|
$property->setValue($entity, $id);
|
|
}
|
|
}
|