|
5 | 5 | namespace Tests\Unit\Filament\Resources; |
6 | 6 |
|
7 | 7 | use App\Filament\Resources\TimeEntryResource; |
| 8 | +use App\Models\Member; |
| 9 | +use App\Models\Organization; |
8 | 10 | use App\Models\TimeEntry; |
9 | 11 | use App\Models\User; |
10 | 12 | use Illuminate\Support\Facades\Config; |
@@ -50,4 +52,149 @@ public function test_can_see_edit_page_of_time_entry(): void |
50 | 52 | // Assert |
51 | 53 | $response->assertSuccessful(); |
52 | 54 | } |
| 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 | + } |
53 | 200 | } |
0 commit comments