98 lines
3.7 KiB
PHP
98 lines
3.7 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature\Http\Client\Doctor;
|
|
|
|
use App\Enums\DocumentStatusEnum;
|
|
use App\Enums\UserEnum;
|
|
use App\Models\Document;
|
|
use App\Models\DocumentAssignment;
|
|
use App\Models\Report;
|
|
use App\Models\User;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Illuminate\Http\UploadedFile;
|
|
use Illuminate\Support\Facades\Queue;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use Inertia\Testing\AssertableInertia as Assert;
|
|
use Tests\TestCase;
|
|
|
|
class ReportControllerTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
|
|
Queue::fake();
|
|
Storage::fake('public');
|
|
}
|
|
|
|
protected function doctor(): User
|
|
{
|
|
return User::factory()->create(['level' => UserEnum::Doctor->value]);
|
|
}
|
|
|
|
public function test_routes_require_authentication(): void
|
|
{
|
|
$this->get(route('dashboard.reports.index'))->assertRedirect(route('login'));
|
|
$this->post(route('dashboard.reports.store'))->assertRedirect(route('login'));
|
|
}
|
|
|
|
public function test_index_shows_reports_written_by_doctor_and_pending_cases(): void
|
|
{
|
|
$doctor = $this->doctor();
|
|
Report::factory()->count(2)->create(['doctor_id' => $doctor->id]);
|
|
Report::factory()->create();
|
|
$pending = Document::factory()->inProgress()->create();
|
|
DocumentAssignment::factory()->create(['document_id' => $pending->id, 'user_id' => $doctor->id]);
|
|
|
|
$this->actingAs($doctor)->get(route('dashboard.reports.index'))
|
|
->assertOk()
|
|
->assertInertia(fn (Assert $page) => $page->component('Dashboard/Reports/Index')->has('reports.data', 2)->has('pendingCases.data', 1));
|
|
}
|
|
|
|
public function test_index_shows_reports_of_requesters_documents_without_pending_cases(): void
|
|
{
|
|
$requester = User::factory()->create(['level' => UserEnum::Vet->value]);
|
|
$document = Document::factory()->create(['requester_id' => $requester->id]);
|
|
Report::factory()->create(['document_id' => $document->id]);
|
|
Report::factory()->create();
|
|
|
|
$this->actingAs($requester)->get(route('dashboard.reports.index'))
|
|
->assertOk()
|
|
->assertInertia(fn (Assert $page) => $page->has('reports.data', 1)->where('pendingCases', null));
|
|
}
|
|
|
|
public function test_store_submits_report_and_completes_document(): void
|
|
{
|
|
$doctor = $this->doctor();
|
|
$document = Document::factory()->inProgress()->create();
|
|
DocumentAssignment::factory()->create(['document_id' => $document->id, 'user_id' => $doctor->id]);
|
|
|
|
$this->actingAs($doctor)->post(route('dashboard.reports.store'), [
|
|
'document_id' => $document->id,
|
|
'finding' => 'یافته',
|
|
'files' => [UploadedFile::fake()->image('a.jpg')],
|
|
])->assertRedirect(route('dashboard.cases.index'))->assertSessionHas('success');
|
|
|
|
$this->assertDatabaseHas('reports', ['document_id' => $document->id, 'doctor_id' => $doctor->id, 'finding' => 'یافته']);
|
|
$this->assertSame(DocumentStatusEnum::Completed, $document->fresh()->status);
|
|
$this->assertDatabaseCount('media', 1);
|
|
}
|
|
|
|
public function test_store_validates_input(): void
|
|
{
|
|
$this->actingAs($this->doctor())->post(route('dashboard.reports.store'), [])->assertSessionHasErrors(['document_id', 'finding']);
|
|
}
|
|
|
|
public function test_store_rejects_unassigned_doctor(): void
|
|
{
|
|
$document = Document::factory()->inProgress()->create();
|
|
|
|
$this->actingAs($this->doctor())->post(route('dashboard.reports.store'), ['document_id' => $document->id, 'finding' => 'x'])
|
|
->assertSessionHasErrors('document');
|
|
|
|
$this->assertDatabaseCount('reports', 0);
|
|
}
|
|
}
|