Add form request validation tests for various client and admin requests
This commit is contained in:
@@ -0,0 +1,96 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\Requests\Admin;
|
||||
|
||||
use App\Enums\ShiftDayEnum;
|
||||
use App\Enums\UserEnum;
|
||||
use App\Http\Requests\Admin\ShiftRequest;
|
||||
use App\Models\Shift;
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Tests\Concerns\ValidatesFormRequests;
|
||||
use Tests\TestCase;
|
||||
|
||||
class ShiftRequestTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase, ValidatesFormRequests;
|
||||
|
||||
protected function valid(array $overrides = []): array
|
||||
{
|
||||
return array_merge([
|
||||
'day_of_week' => ShiftDayEnum::Monday->value,
|
||||
'start_time' => '08:00',
|
||||
'end_time' => '16:00',
|
||||
'is_active' => true,
|
||||
], $overrides);
|
||||
}
|
||||
|
||||
public function test_passes_with_weekly_shift(): void
|
||||
{
|
||||
$this->assertPasses(ShiftRequest::class, $this->valid());
|
||||
}
|
||||
|
||||
public function test_passes_with_dated_shift_and_doctor(): void
|
||||
{
|
||||
$doctor = User::factory()->create(['level' => UserEnum::Doctor->value]);
|
||||
|
||||
$this->assertPasses(ShiftRequest::class, $this->valid(['day_of_week' => null, 'date' => '2026-09-21', 'user_id' => $doctor->id]));
|
||||
}
|
||||
|
||||
public function test_passes_with_overnight_shift(): void
|
||||
{
|
||||
$this->assertPasses(ShiftRequest::class, $this->valid(['start_time' => '22:00', 'end_time' => '02:00']));
|
||||
}
|
||||
|
||||
public function test_fails_without_day_of_week_or_date(): void
|
||||
{
|
||||
$errors = $this->formRequestErrors(ShiftRequest::class, $this->valid(['day_of_week' => null]));
|
||||
|
||||
$this->assertArrayHasKey('day_of_week', $errors);
|
||||
$this->assertArrayHasKey('date', $errors);
|
||||
}
|
||||
|
||||
public function test_fails_without_times_or_is_active(): void
|
||||
{
|
||||
$errors = $this->formRequestErrors(ShiftRequest::class, ['day_of_week' => ShiftDayEnum::Monday->value]);
|
||||
|
||||
$this->assertArrayHasKey('start_time', $errors);
|
||||
$this->assertArrayHasKey('end_time', $errors);
|
||||
$this->assertArrayHasKey('is_active', $errors);
|
||||
}
|
||||
|
||||
public function test_fails_with_invalid_day_time_format_or_equal_times(): void
|
||||
{
|
||||
$this->assertFailsOn('day_of_week', ShiftRequest::class, $this->valid(['day_of_week' => 'Monday']));
|
||||
$this->assertFailsOn('start_time', ShiftRequest::class, $this->valid(['start_time' => '8am']));
|
||||
$this->assertFailsOn('end_time', ShiftRequest::class, $this->valid(['end_time' => '08:00']));
|
||||
}
|
||||
|
||||
public function test_fails_when_user_is_not_a_doctor(): void
|
||||
{
|
||||
$vet = User::factory()->create(['level' => UserEnum::Vet->value]);
|
||||
|
||||
$this->assertFailsOn('user_id', ShiftRequest::class, $this->valid(['user_id' => $vet->id]));
|
||||
}
|
||||
|
||||
public function test_fails_when_shift_conflicts_with_doctors_existing_shift(): void
|
||||
{
|
||||
$doctor = User::factory()->create(['level' => UserEnum::Doctor->value]);
|
||||
Shift::factory()->weekly(ShiftDayEnum::Monday)->between('08:00', '12:00')->forDoctor($doctor)->create();
|
||||
|
||||
$this->assertFailsOn('start_time', ShiftRequest::class, $this->valid(['user_id' => $doctor->id, 'start_time' => '10:00', 'end_time' => '14:00']));
|
||||
}
|
||||
|
||||
public function test_conflict_check_ignores_shift_being_edited(): void
|
||||
{
|
||||
$doctor = User::factory()->create(['level' => UserEnum::Doctor->value]);
|
||||
$shift = Shift::factory()->weekly(ShiftDayEnum::Monday)->between('08:00', '12:00')->forDoctor($doctor)->create();
|
||||
|
||||
$this->assertPasses(ShiftRequest::class, $this->valid(['user_id' => $doctor->id, 'start_time' => '09:00', 'end_time' => '13:00']), [], null, ['shift' => $shift]);
|
||||
}
|
||||
|
||||
public function test_date_takes_precedence_and_clears_day_of_week(): void
|
||||
{
|
||||
$this->assertPasses(ShiftRequest::class, $this->valid(['day_of_week' => 'invalid-day', 'date' => '2026-09-21']));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user