default time entry type filter to work for legacy reports

This commit is contained in:
Gregor Vostrak
2026-07-24 15:36:51 +02:00
parent cbcd1e51f6
commit c0c8fee6be
2 changed files with 97 additions and 2 deletions

View File

@@ -132,8 +132,12 @@ class ReportPropertiesDto implements Castable
$dto->roundingType = isset($data->roundingType) ? TimeEntryRoundingType::from($data->roundingType) : null; $dto->roundingType = isset($data->roundingType) ? TimeEntryRoundingType::from($data->roundingType) : null;
// Note: roundingMinutes was added later so it is possible that the value is missing in persisted reports in the DB // Note: roundingMinutes was added later so it is possible that the value is missing in persisted reports in the DB
$dto->roundingMinutes = isset($data->roundingMinutes) ? (int) $data->roundingMinutes : null; $dto->roundingMinutes = isset($data->roundingMinutes) ? (int) $data->roundingMinutes : null;
// Note: timeEntryType was added later so it is possible that the value is missing in persisted reports in the DB // Note: timeEntryType was added later, reports persisted before that are missing the value and default to "work"
$dto->timeEntryType = isset($data->timeEntryType) ? TimeEntryType::from($data->timeEntryType) : null; if (property_exists($data, 'timeEntryType')) {
$dto->timeEntryType = $data->timeEntryType !== null ? TimeEntryType::from($data->timeEntryType) : null;
} else {
$dto->timeEntryType = TimeEntryType::Work;
}
return $dto; return $dto;
} }

View File

@@ -0,0 +1,91 @@
<?php
declare(strict_types=1);
namespace Tests\Unit\Service\Dto;
use App\Enums\TimeEntryType;
use App\Models\Report;
use App\Service\Dto\ReportPropertiesDto;
use PHPUnit\Framework\Attributes\CoversClass;
use Tests\TestCase;
#[CoversClass(ReportPropertiesDto::class)]
class ReportPropertiesDtoTest extends TestCase
{
/**
* @return array<string, mixed>
*/
private function getBaseProperties(): array
{
return [
'group' => 'project',
'subGroup' => 'task',
'historyGroup' => 'day',
'weekStart' => 'monday',
'timezone' => 'Europe/Vienna',
'start' => '2024-01-01T00:00:00Z',
'end' => '2024-01-31T00:00:00Z',
'active' => null,
'memberIds' => null,
'billable' => null,
'clientIds' => null,
'projectIds' => null,
'tagIds' => null,
'taskIds' => null,
];
}
/**
* @param array<string, mixed> $properties
*/
private function castFromJson(array $properties): ReportPropertiesDto
{
$json = json_encode($properties);
$this->assertIsString($json);
return ReportPropertiesDto::castUsing([])->get(new Report, 'properties', $json, []);
}
public function test_time_entry_type_defaults_to_work_if_the_value_is_missing_in_the_persisted_report(): void
{
// Arrange
$properties = $this->getBaseProperties();
// Act
$dto = $this->castFromJson($properties);
// Assert
$this->assertSame(TimeEntryType::Work, $dto->timeEntryType);
}
public function test_time_entry_type_is_null_if_the_persisted_report_has_it_set_to_null(): void
{
// Arrange
$properties = $this->getBaseProperties();
$properties['timeEntryType'] = null;
// Act
$dto = $this->castFromJson($properties);
// Assert
$this->assertNull($dto->timeEntryType);
}
public function test_time_entry_type_is_casted_to_the_enum_if_the_persisted_report_has_a_value(): void
{
// Arrange
$propertiesWork = $this->getBaseProperties();
$propertiesWork['timeEntryType'] = 'work';
$propertiesBreak = $this->getBaseProperties();
$propertiesBreak['timeEntryType'] = 'break';
// Act
$dtoWork = $this->castFromJson($propertiesWork);
$dtoBreak = $this->castFromJson($propertiesBreak);
// Assert
$this->assertSame(TimeEntryType::Work, $dtoWork->timeEntryType);
$this->assertSame(TimeEntryType::Break, $dtoBreak->timeEntryType);
}
}