Skip to content

Commit

Permalink
Fixed timezone problem in unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
korridor committed Sep 30, 2024
1 parent 8b50f33 commit 78ea8a6
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 10 deletions.
6 changes: 3 additions & 3 deletions database/factories/TimeEntryFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,8 @@ public function startWithDuration(Carbon $start, int $durationInSeconds): self
{
return $this->state(function (array $attributes) use ($start, $durationInSeconds): array {
return [
'start' => $start->utc(),
'end' => $start->copy()->addSeconds($durationInSeconds),
'start' => $start->copy()->utc(),
'end' => $start->copy()->utc()->addSeconds($durationInSeconds),
];
});
}
Expand All @@ -157,7 +157,7 @@ public function start(Carbon $start): self
{
return $this->state(function (array $attributes) use ($start): array {
return [
'start' => $start->utc(),
'start' => $start->copy()->utc(),
];
});
}
Expand Down
2 changes: 2 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ protected function setUp(): void
$mock->shouldReceive('getTrialUntil')->andReturn(null);
$mock->shouldReceive('isBlocked')->andReturn(false);
});
// Note: The following line can be used to test timezone edge cases.
// $this->travelTo(Carbon::now()->timezone('Europe/Vienna')->setHour(0)->setMinute(59)->setSecond(0));
}

protected function mockPrivateStorage(): void
Expand Down
8 changes: 4 additions & 4 deletions tests/Unit/Endpoint/Api/V1/TimeEntryEndpointTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -473,8 +473,8 @@ public function test_aggregate_endpoint_groups_by_two_groups(): void
'time-entries:view:all',
]);
$project = Project::factory()->forOrganization($data->organization)->create();
$day1 = Carbon::now()->timezone($data->user->timezone)->subDays(1)->utc();
$day2 = Carbon::now()->timezone($data->user->timezone)->subDays(3)->utc();
$day1 = Carbon::now()->timezone($data->user->timezone)->subDays(1);
$day2 = Carbon::now()->timezone($data->user->timezone)->subDays(3);
$timeEntry1NoProject = TimeEntry::factory()->forOrganization($data->organization)->forMember($data->member)->startWithDuration($day1, 10)->create();
$timeEntry2NoProject = TimeEntry::factory()->forOrganization($data->organization)->forMember($data->member)->startWithDuration($day2, 10)->create();
$timeEntry1WithProject = TimeEntry::factory()->forOrganization($data->organization)->forMember($data->member)->forProject($project)->startWithDuration($day1, 10)->create();
Expand Down Expand Up @@ -552,8 +552,8 @@ public function test_aggregate_endpoint_groups_by_two_groups_with_fill_gaps_argu
'time-entries:view:all',
]);
$project = Project::factory()->forOrganization($data->organization)->create();
$day1 = Carbon::now()->timezone($data->user->timezone)->subDays(1)->utc();
$day2 = Carbon::now()->timezone($data->user->timezone)->subDays(3)->utc();
$day1 = Carbon::now()->timezone($data->user->timezone)->subDays(1);
$day2 = Carbon::now()->timezone($data->user->timezone)->subDays(3);
$timeEntry1NoProject = TimeEntry::factory()->forOrganization($data->organization)->forMember($data->member)->startWithDuration($day1, 10)->create();
$timeEntry2NoProject = TimeEntry::factory()->forOrganization($data->organization)->forMember($data->member)->startWithDuration($day2, 10)->create();
$timeEntry1WithProject = TimeEntry::factory()->forOrganization($data->organization)->forMember($data->member)->forProject($project)->startWithDuration($day1, 10)->create();
Expand Down
7 changes: 4 additions & 3 deletions tests/Unit/Service/TimeEntryAggregationServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,14 +140,15 @@ public function test_aggregate_time_entries_by_project_and_description(): void
public function test_aggregate_time_entries_empty_state_by_day_and_project_with_filled_gaps(): void
{
// Arrange
$timezone = 'Europe/Vienna';
$query = TimeEntry::query();

// Act
$result = $this->service->getAggregatedTimeEntries(
$query,
TimeEntryAggregationType::Day,
TimeEntryAggregationType::Project,
'Europe/Vienna',
$timezone,
Weekday::Monday,
true,
Carbon::now()->subDays(2)->utc(),
Expand All @@ -161,14 +162,14 @@ public function test_aggregate_time_entries_empty_state_by_day_and_project_with_
'grouped_type' => 'day',
'grouped_data' => [
[
'key' => Carbon::now()->subDays(2)->utc()->format('Y-m-d'),
'key' => Carbon::now()->subDays(2)->timezone($timezone)->format('Y-m-d'),
'seconds' => 0,
'cost' => 0,
'grouped_type' => 'project',
'grouped_data' => [],
],
[
'key' => Carbon::now()->subDay()->utc()->format('Y-m-d'),
'key' => Carbon::now()->subDay()->timezone($timezone)->format('Y-m-d'),
'seconds' => 0,
'cost' => 0,
'grouped_type' => 'project',
Expand Down

0 comments on commit 78ea8a6

Please sign in to comment.