Files
radicall/tests/Feature/Http/Admin/ReportControllerTest.php

92 lines
4.0 KiB
PHP

<?php
namespace Tests\Feature\Http\Admin;
use App\Enums\ReportStatusEnum;
use App\Enums\UserEnum;
use App\Models\Document;
use App\Models\Report;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Inertia\Testing\AssertableInertia as Assert;
use Tests\Concerns\InteractsWithAdmin;
use Tests\TestCase;
class ReportControllerTest extends TestCase
{
use InteractsWithAdmin, RefreshDatabase;
protected function payload(array $overrides = []): array
{
return array_merge([
'document_id' => Document::factory()->create()->id,
'doctor_id' => User::factory()->create()->id,
'status' => ReportStatusEnum::Approved->name,
'body' => 'متن',
'submitted_at' => '2026-09-21 10:00:00',
], $overrides);
}
public function test_guest_and_non_admin_are_blocked(): void
{
$this->get(route('admin.reports.index'))->assertRedirect(route('login'));
$this->actingAs(User::factory()->create())->get(route('admin.reports.index'))->assertNotFound();
}
public function test_index_renders_with_filters(): void
{
$admin = $this->adminWithoutPermissions();
$document = Document::factory()->create(['code' => '123456']);
$doctor = User::factory()->create(['level' => UserEnum::Doctor->value]);
Report::factory()->draft()->create(['created_at' => now()->subMinute()]);
Report::factory()->approved()->create(['document_id' => $document->id, 'doctor_id' => $doctor->id]);
$this->actingAs($admin)->get(route('admin.reports.index'))->assertOk()->assertInertia(fn (Assert $page) => $page
->component('Reports/index')
->has('reports.data', 2)
->where('reports.data.1.doctor', null)
->where('reports.data.0.doctor.id', $doctor->id));
$this->actingAs($admin)->get(route('admin.reports.index', ['status' => ReportStatusEnum::Draft->value]))->assertInertia(fn (Assert $page) => $page->has('reports.data', 1));
$this->actingAs($admin)->get(route('admin.reports.index', ['search' => '1234']))->assertInertia(fn (Assert $page) => $page->has('reports.data', 1));
}
public function test_store_creates_report(): void
{
$payload = $this->payload();
$this->actingAs($this->adminWithoutPermissions())->post(route('admin.reports.store'), $payload)->assertOk()->assertSessionHas('success');
$this->assertDatabaseHas('reports', ['document_id' => $payload['document_id'], 'doctor_id' => $payload['doctor_id'], 'status' => ReportStatusEnum::Approved->value, 'body' => 'متن']);
}
public function test_store_validates_input(): void
{
$this->actingAs($this->adminWithoutPermissions())->postJson(route('admin.reports.store'), [])->assertUnprocessable()->assertJsonValidationErrors(['document_id', 'doctor_id', 'status', 'body', 'submitted_at']);
}
public function test_edit_renders_report_prop_for_missing_page(): void
{
$report = Report::factory()->create();
$this->actingAs($this->adminWithoutPermissions())->get(route('admin.reports.edit', $report))->assertInertia(fn (Assert $page) => $page->component('Reports/edit', false)->where('report.id', $report->id));
}
public function test_update_changes_report(): void
{
$report = Report::factory()->draft()->create();
$this->actingAs($this->adminWithoutPermissions())->put(route('admin.reports.update', $report), $this->payload(['status' => ReportStatusEnum::Rejected->name, 'body' => 'جدید']))->assertOk();
$this->assertDatabaseHas('reports', ['id' => $report->id, 'status' => ReportStatusEnum::Rejected->value, 'body' => 'جدید']);
}
public function test_destroy_deletes_report(): void
{
$report = Report::factory()->create();
$this->actingAs($this->adminWithoutPermissions())->delete(route('admin.reports.destroy', $report))->assertOk();
$this->assertDatabaseMissing('reports', ['id' => $report->id]);
}
}