Add form request validation tests for various client and admin requests
This commit is contained in:
@@ -0,0 +1,114 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\Requests\Client;
|
||||
|
||||
use App\Enums\DocumentModalityEnum;
|
||||
use App\Enums\DocumentPriorityEnum;
|
||||
use App\Enums\PetGenderEnum;
|
||||
use App\Enums\PetTypeEnum;
|
||||
use App\Enums\UserEnum;
|
||||
use App\Http\Requests\Client\FastRequestRequest;
|
||||
use App\Models\Pet;
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Http\UploadedFile;
|
||||
use Tests\Concerns\ValidatesFormRequests;
|
||||
use Tests\TestCase;
|
||||
|
||||
class FastRequestRequestTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase, ValidatesFormRequests;
|
||||
|
||||
protected User $user;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->user = User::factory()->create();
|
||||
}
|
||||
|
||||
protected function valid(array $overrides = []): array
|
||||
{
|
||||
return array_merge([
|
||||
'modality' => DocumentModalityEnum::Mri->value,
|
||||
'priority' => DocumentPriorityEnum::High->name,
|
||||
'pet' => ['name' => 'Rex', 'type' => PetTypeEnum::Dog->name],
|
||||
], $overrides);
|
||||
}
|
||||
|
||||
public function test_passes_with_new_pet(): void
|
||||
{
|
||||
$this->assertPasses(FastRequestRequest::class, $this->valid([
|
||||
'note' => 'n',
|
||||
'pet' => ['name' => 'Rex', 'type' => PetTypeEnum::Dog->name, 'gender' => PetGenderEnum::Male->name, 'breed' => 'b', 'age' => 3, 'weight' => 20, 'sterilized' => true],
|
||||
]), [], $this->user);
|
||||
}
|
||||
|
||||
public function test_passes_with_own_existing_pet_and_no_pet_payload(): void
|
||||
{
|
||||
$pet = Pet::factory()->create(['owner_id' => $this->user->id]);
|
||||
|
||||
$this->assertPasses(FastRequestRequest::class, ['modality' => DocumentModalityEnum::Mri->value, 'priority' => DocumentPriorityEnum::Medium->name, 'pet_id' => $pet->id], [], $this->user);
|
||||
}
|
||||
|
||||
public function test_passes_with_doctor_and_image_files(): void
|
||||
{
|
||||
$doctor = User::factory()->create(['level' => UserEnum::Doctor->value]);
|
||||
|
||||
$this->assertPasses(FastRequestRequest::class, $this->valid(['doctor_id' => $doctor->id]), [
|
||||
'history_files' => [UploadedFile::fake()->image('h.jpg')],
|
||||
'scan_files' => [UploadedFile::fake()->image('s.png')],
|
||||
], $this->user);
|
||||
}
|
||||
|
||||
public function test_fails_without_modality_priority_or_pet(): void
|
||||
{
|
||||
$errors = $this->formRequestErrors(FastRequestRequest::class, [], [], $this->user);
|
||||
|
||||
$this->assertArrayHasKey('modality', $errors);
|
||||
$this->assertArrayHasKey('priority', $errors);
|
||||
$this->assertArrayHasKey('pet', $errors);
|
||||
$this->assertArrayHasKey('pet.name', $errors);
|
||||
$this->assertArrayHasKey('pet.type', $errors);
|
||||
}
|
||||
|
||||
public function test_fails_with_invalid_modality_or_priority(): void
|
||||
{
|
||||
$this->assertFailsOn('modality', FastRequestRequest::class, $this->valid(['modality' => 'xray']), [], $this->user);
|
||||
$this->assertFailsOn('priority', FastRequestRequest::class, $this->valid(['priority' => DocumentPriorityEnum::High->value]), [], $this->user);
|
||||
}
|
||||
|
||||
public function test_fails_with_pet_of_another_user(): void
|
||||
{
|
||||
$pet = Pet::factory()->create();
|
||||
|
||||
$this->assertFailsOn('pet_id', FastRequestRequest::class, $this->valid(['pet_id' => $pet->id]), [], $this->user);
|
||||
}
|
||||
|
||||
public function test_fails_with_invalid_pet_type_gender_age_or_weight(): void
|
||||
{
|
||||
$this->assertFailsOn('pet.type', FastRequestRequest::class, $this->valid(['pet' => ['name' => 'x', 'type' => PetTypeEnum::Dog->value]]), [], $this->user);
|
||||
$this->assertFailsOn('pet.gender', FastRequestRequest::class, $this->valid(['pet' => ['name' => 'x', 'type' => 'Dog', 'gender' => 'Alien']]), [], $this->user);
|
||||
$this->assertFailsOn('pet.age', FastRequestRequest::class, $this->valid(['pet' => ['name' => 'x', 'type' => 'Dog', 'age' => 101]]), [], $this->user);
|
||||
$this->assertFailsOn('pet.weight', FastRequestRequest::class, $this->valid(['pet' => ['name' => 'x', 'type' => 'Dog', 'weight' => -1]]), [], $this->user);
|
||||
}
|
||||
|
||||
public function test_fails_with_non_doctor_as_doctor_id(): void
|
||||
{
|
||||
$vet = User::factory()->create(['level' => UserEnum::Vet->value]);
|
||||
|
||||
$this->assertFailsOn('doctor_id', FastRequestRequest::class, $this->valid(['doctor_id' => $vet->id]), [], $this->user);
|
||||
}
|
||||
|
||||
public function test_fails_with_non_image_or_oversized_files(): void
|
||||
{
|
||||
$this->assertFailsOn('history_files.0', FastRequestRequest::class, $this->valid(), ['history_files' => [UploadedFile::fake()->create('a.pdf', 10, 'application/pdf')]], $this->user);
|
||||
$this->assertFailsOn('scan_files.0', FastRequestRequest::class, $this->valid(), ['scan_files' => [UploadedFile::fake()->image('a.jpg')->size(10241)]], $this->user);
|
||||
}
|
||||
|
||||
public function test_fails_when_note_exceeds_limit(): void
|
||||
{
|
||||
$this->assertFailsOn('note', FastRequestRequest::class, $this->valid(['note' => str_repeat('a', 1001)]), [], $this->user);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user