Skip to content

Commit 42975ea

Browse files
committed
add tests for admin time entry create and edit
Tests creating time entries via the admin panel, including verifying that user_id and organization_id are correctly derived from member_id. Also tests editing and reassigning time entries to different members. https://claude.ai/code/session_01CVi5hmyiPDpvEzzTD7RPr7
1 parent f153877 commit 42975ea

File tree

1 file changed

+147
-0
lines changed

1 file changed

+147
-0
lines changed

tests/Unit/Filament/Resources/TimeEntryResourceTest.php

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
namespace Tests\Unit\Filament\Resources;
66

77
use App\Filament\Resources\TimeEntryResource;
8+
use App\Models\Member;
9+
use App\Models\Organization;
810
use App\Models\TimeEntry;
911
use App\Models\User;
1012
use Illuminate\Support\Facades\Config;
@@ -50,4 +52,149 @@ public function test_can_see_edit_page_of_time_entry(): void
5052
// Assert
5153
$response->assertSuccessful();
5254
}
55+
56+
public function test_can_see_create_page_of_time_entry(): void
57+
{
58+
// Act
59+
$response = Livewire::test(TimeEntryResource\Pages\CreateTimeEntry::class);
60+
61+
// Assert
62+
$response->assertSuccessful();
63+
}
64+
65+
public function test_can_create_time_entry(): void
66+
{
67+
// Arrange
68+
$organization = Organization::factory()->create();
69+
$user = User::factory()->create();
70+
$member = Member::factory()
71+
->forOrganization($organization)
72+
->forUser($user)
73+
->create();
74+
75+
// Act
76+
$response = Livewire::test(TimeEntryResource\Pages\CreateTimeEntry::class)
77+
->fillForm([
78+
'description' => 'Test time entry',
79+
'billable' => true,
80+
'start' => '2024-01-01 08:00:00',
81+
'end' => '2024-01-01 10:00:00',
82+
'member_id' => $member->getKey(),
83+
])
84+
->call('create')
85+
->assertHasNoFormErrors();
86+
87+
// Assert
88+
$response->assertSuccessful();
89+
$timeEntry = TimeEntry::where('description', 'Test time entry')->first();
90+
$this->assertNotNull($timeEntry);
91+
$this->assertSame($member->getKey(), $timeEntry->member_id);
92+
$this->assertSame($user->getKey(), $timeEntry->user_id);
93+
$this->assertSame($organization->getKey(), $timeEntry->organization_id);
94+
$this->assertTrue($timeEntry->billable);
95+
}
96+
97+
public function test_can_create_time_entry_and_derives_user_and_organization_from_member(): void
98+
{
99+
// Arrange
100+
$organization = Organization::factory()->create();
101+
$user = User::factory()->create();
102+
$member = Member::factory()
103+
->forOrganization($organization)
104+
->forUser($user)
105+
->create();
106+
$otherUser = User::factory()->create();
107+
$otherOrganization = Organization::factory()->create();
108+
109+
// Act
110+
$response = Livewire::test(TimeEntryResource\Pages\CreateTimeEntry::class)
111+
->fillForm([
112+
'description' => 'Derived fields test',
113+
'billable' => false,
114+
'start' => '2024-03-01 09:00:00',
115+
'end' => '2024-03-01 11:00:00',
116+
'member_id' => $member->getKey(),
117+
'user_id' => $otherUser->getKey(),
118+
'organization_id' => $otherOrganization->getKey(),
119+
])
120+
->call('create')
121+
->assertHasNoFormErrors();
122+
123+
// Assert
124+
$response->assertSuccessful();
125+
$timeEntry = TimeEntry::where('description', 'Derived fields test')->first();
126+
$this->assertNotNull($timeEntry);
127+
$this->assertSame($user->getKey(), $timeEntry->user_id);
128+
$this->assertSame($organization->getKey(), $timeEntry->organization_id);
129+
}
130+
131+
public function test_can_update_time_entry(): void
132+
{
133+
// Arrange
134+
$organization = Organization::factory()->create();
135+
$user = User::factory()->create();
136+
$member = Member::factory()
137+
->forOrganization($organization)
138+
->forUser($user)
139+
->create();
140+
$timeEntry = TimeEntry::factory()->forMember($member)->create();
141+
142+
// Act
143+
$response = Livewire::test(TimeEntryResource\Pages\EditTimeEntry::class, ['record' => $timeEntry->getKey()])
144+
->fillForm([
145+
'description' => 'Updated description',
146+
'billable' => true,
147+
'start' => '2024-02-01 08:00:00',
148+
'end' => '2024-02-01 12:00:00',
149+
'member_id' => $member->getKey(),
150+
])
151+
->call('save')
152+
->assertHasNoFormErrors();
153+
154+
// Assert
155+
$response->assertSuccessful();
156+
$timeEntry->refresh();
157+
$this->assertSame('Updated description', $timeEntry->description);
158+
$this->assertTrue($timeEntry->billable);
159+
$this->assertSame($user->getKey(), $timeEntry->user_id);
160+
$this->assertSame($organization->getKey(), $timeEntry->organization_id);
161+
}
162+
163+
public function test_update_time_entry_derives_user_and_organization_from_new_member(): void
164+
{
165+
// Arrange
166+
$organization = Organization::factory()->create();
167+
$user = User::factory()->create();
168+
$member = Member::factory()
169+
->forOrganization($organization)
170+
->forUser($user)
171+
->create();
172+
$timeEntry = TimeEntry::factory()->create();
173+
174+
$newOrganization = Organization::factory()->create();
175+
$newUser = User::factory()->create();
176+
$newMember = Member::factory()
177+
->forOrganization($newOrganization)
178+
->forUser($newUser)
179+
->create();
180+
181+
// Act
182+
$response = Livewire::test(TimeEntryResource\Pages\EditTimeEntry::class, ['record' => $timeEntry->getKey()])
183+
->fillForm([
184+
'description' => 'Reassigned entry',
185+
'billable' => false,
186+
'start' => '2024-02-01 08:00:00',
187+
'end' => '2024-02-01 12:00:00',
188+
'member_id' => $newMember->getKey(),
189+
])
190+
->call('save')
191+
->assertHasNoFormErrors();
192+
193+
// Assert
194+
$response->assertSuccessful();
195+
$timeEntry->refresh();
196+
$this->assertSame($newMember->getKey(), $timeEntry->member_id);
197+
$this->assertSame($newUser->getKey(), $timeEntry->user_id);
198+
$this->assertSame($newOrganization->getKey(), $timeEntry->organization_id);
199+
}
53200
}

0 commit comments

Comments
 (0)