Files
PHP-App-Timekeeping/api/tests/unit/Models/WorkingHoursViewTest.php
T
2025-05-03 09:27:39 +02:00

153 lines
5.1 KiB
PHP

<?php
namespace TorstenHettstedt\TimekeepingApi\Tests\Unit\Models;
use Codeception\Attribute\DataProvider;
use Codeception\Test\Unit;
use DateInterval;
use DateTime;
use DateTimeInterface;
use TorstenHettstedt\TimekeepingApi\Models\PeriodDesignationEnum;
use TorstenHettstedt\TimekeepingApi\Models\WorkingHoursView;
class WorkingHoursViewTest extends Unit
{
/**
* @return array<array{DateTime,PeriodDesignationEnum,int,DateInterval,DateInterval,array<string,mixed>}>
*/
public function workingHoursProvider(): array
{
$date = new DateTime('2020-11-30');
$work_days = 15;
$total_hours = new DateInterval("PT32H22M");
$overtime = new DateInterval("PT22M");
$absence_time = clone $overtime;
$absence_time->invert = 1;
return [
[
$date,
PeriodDesignationEnum::WEEKLY,
$work_days,
$total_hours,
$overtime,
[
'period' => '2020#49',
'periodDesignation' => 'weekly',
'workingDays' => 15,
'totalHours' => '32:22:00',
'overtime' => '00:22:00'
],
],
[
$date,
PeriodDesignationEnum::WEEKLY,
$work_days,
$total_hours,
$absence_time,
[
'period' => '2020#49',
'periodDesignation' => 'weekly',
'workingDays' => 15,
'totalHours' => '32:22:00',
'overtime' => '-00:22:00'
],
],
[
$date,
PeriodDesignationEnum::MONTHLY,
$work_days,
$total_hours,
$overtime,
[
'period' => '2020-11',
'periodDesignation' => 'monthly',
'workingDays' => 15,
'totalHours' => '32:22:00',
'overtime' => '00:22:00'
],
],
[
$date,
PeriodDesignationEnum::MONTHLY,
$work_days,
$total_hours,
$absence_time,
[
'period' => '2020-11',
'periodDesignation' => 'monthly',
'workingDays' => 15,
'totalHours' => '32:22:00',
'overtime' => '-00:22:00'
],
],
[
$date,
PeriodDesignationEnum::YEARLY,
$work_days,
$total_hours,
$overtime,
[
'period' => '2020',
'periodDesignation' => 'yearly',
'workingDays' => 15,
'totalHours' => '32:22:00',
'overtime' => '00:22:00'
],
],
[
$date,
PeriodDesignationEnum::YEARLY,
$work_days,
$total_hours,
$absence_time,
[
'period' => '2020',
'periodDesignation' => 'yearly',
'workingDays' => 15,
'totalHours' => '32:22:00',
'overtime' => '-00:22:00'
],
],
];
}
/**
* @param DateTimeInterface $period
* @param PeriodDesignationEnum $periodDesignation
* @param int $workingDays
* @param DateInterval $totalHours
* @param DateInterval $overtime
* @param array<string, string> $shouldJson
*/
#[DataProvider('workingHoursProvider')]
public function testWorkingHoursViewWithContent(
DateTimeInterface $period,
PeriodDesignationEnum $periodDesignation,
int $workingDays,
DateInterval $totalHours,
DateInterval $overtime,
array $shouldJson
): void {
$obj = new WorkingHoursView($period, $periodDesignation, $workingDays, $totalHours, $overtime);
$this->assertInstanceOf(DateTimeInterface::class, $obj->getPeriod());
$this->assertEquals($period, $obj->getPeriod());
$this->assertInstanceOf(PeriodDesignationEnum::class, $obj->getPeriodDesignation());
$this->assertEquals($periodDesignation, $obj->getPeriodDesignation());
$this->assertEquals($workingDays, $obj->getWorkingDays());
$this->assertInstanceOf(DateInterval::class, $obj->getTotalHours());
$this->assertEquals($totalHours, $obj->getTotalHours());
$this->assertInstanceOf(DateInterval::class, $obj->getOvertime());
$this->assertEquals($overtime, $obj->getOvertime());
$json = $obj->jsonSerialize();
$this->assertEquals($shouldJson, $json);
}
}