Add form request validation tests for various client and admin requests
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\Requests\Client;
|
||||
|
||||
use App\Http\Requests\Client\ReportSubmitRequest;
|
||||
use App\Models\Document;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Http\UploadedFile;
|
||||
use Tests\Concerns\ValidatesFormRequests;
|
||||
use Tests\TestCase;
|
||||
|
||||
class ReportSubmitRequestTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase, ValidatesFormRequests;
|
||||
|
||||
protected function valid(array $overrides = []): array
|
||||
{
|
||||
return array_merge(['document_id' => Document::factory()->create()->id, 'finding' => 'یافتهها'], $overrides);
|
||||
}
|
||||
|
||||
public function test_passes_with_document_and_finding(): void
|
||||
{
|
||||
$this->assertPasses(ReportSubmitRequest::class, $this->valid());
|
||||
}
|
||||
|
||||
public function test_passes_with_optional_sections_and_image_files(): void
|
||||
{
|
||||
$this->assertPasses(ReportSubmitRequest::class, $this->valid([
|
||||
'technique' => 't',
|
||||
'diagnose' => 'd',
|
||||
'recommendation' => 'r',
|
||||
]), ['files' => [UploadedFile::fake()->image('a.jpg'), UploadedFile::fake()->image('b.png')]]);
|
||||
}
|
||||
|
||||
public function test_fails_without_document_or_finding(): void
|
||||
{
|
||||
$errors = $this->formRequestErrors(ReportSubmitRequest::class, []);
|
||||
|
||||
$this->assertArrayHasKey('document_id', $errors);
|
||||
$this->assertArrayHasKey('finding', $errors);
|
||||
}
|
||||
|
||||
public function test_fails_with_unknown_document(): void
|
||||
{
|
||||
$this->assertFailsOn('document_id', ReportSubmitRequest::class, $this->valid(['document_id' => 999]));
|
||||
}
|
||||
|
||||
public function test_fails_when_section_exceeds_limit(): void
|
||||
{
|
||||
$this->assertFailsOn('finding', ReportSubmitRequest::class, $this->valid(['finding' => str_repeat('a', 10001)]));
|
||||
}
|
||||
|
||||
public function test_fails_with_non_image_file(): void
|
||||
{
|
||||
$this->assertFailsOn('files.0', ReportSubmitRequest::class, $this->valid(), ['files' => [UploadedFile::fake()->create('a.pdf', 10, 'application/pdf')]]);
|
||||
}
|
||||
|
||||
public function test_fails_with_file_over_ten_megabytes(): void
|
||||
{
|
||||
$this->assertFailsOn('files.0', ReportSubmitRequest::class, $this->valid(), ['files' => [UploadedFile::fake()->image('a.jpg')->size(10241)]]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user