New-Thanasoft/thanasoft-back/tests/Feature/EmployeeAgendaApiTest.php

148 lines
4.8 KiB
PHP

<?php
namespace Tests\Feature;
use App\Models\Client;
use App\Models\Employee;
use App\Models\Intervention;
use App\Models\Thanatopractitioner;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Laravel\Sanctum\Sanctum;
use Tests\TestCase;
class EmployeeAgendaApiTest extends TestCase
{
use RefreshDatabase;
private User $authenticatedUser;
protected function setUp(): void
{
parent::setUp();
$this->authenticatedUser = User::factory()->create();
Sanctum::actingAs($this->authenticatedUser);
}
public function test_can_get_employee_agenda_filtered_by_month_and_status(): void
{
$client = Client::factory()->create();
$employee = Employee::create([
'first_name' => 'Jean',
'last_name' => 'Martin',
'email' => 'jean.martin@example.test',
'job_title' => 'Thanatopracteur',
'active' => true,
]);
$otherEmployee = Employee::create([
'first_name' => 'Paul',
'last_name' => 'Durand',
'email' => 'paul.durand@example.test',
'job_title' => 'Thanatopracteur',
'active' => true,
]);
$thanatopractitioner = Thanatopractitioner::create([
'employee_id' => $employee->id,
'authorization_number' => 'AUTH-001',
]);
$otherThanatopractitioner = Thanatopractitioner::create([
'employee_id' => $otherEmployee->id,
'authorization_number' => 'AUTH-002',
]);
$matchingIntervention = Intervention::create([
'client_id' => $client->id,
'type' => 'thanatopraxie',
'scheduled_at' => '2026-04-15 09:00:00',
'duration_min' => 90,
'status' => 'planifie',
'created_by' => $this->authenticatedUser->id,
]);
$sameEmployeeOutsideMonth = Intervention::create([
'client_id' => $client->id,
'type' => 'thanatopraxie',
'scheduled_at' => '2026-05-03 10:00:00',
'duration_min' => 60,
'status' => 'planifie',
'created_by' => $this->authenticatedUser->id,
]);
$sameMonthDifferentStatus = Intervention::create([
'client_id' => $client->id,
'type' => 'thanatopraxie',
'scheduled_at' => '2026-04-18 11:00:00',
'duration_min' => 45,
'status' => 'termine',
'created_by' => $this->authenticatedUser->id,
]);
$otherEmployeeIntervention = Intervention::create([
'client_id' => $client->id,
'type' => 'toilette_mortuaire',
'scheduled_at' => '2026-04-20 14:00:00',
'duration_min' => 30,
'status' => 'planifie',
'created_by' => $this->authenticatedUser->id,
]);
$matchingIntervention->practitioners()->attach($thanatopractitioner->id, [
'role' => 'principal',
'assigned_at' => now(),
]);
$sameEmployeeOutsideMonth->practitioners()->attach($thanatopractitioner->id, [
'role' => 'assistant',
'assigned_at' => now(),
]);
$sameMonthDifferentStatus->practitioners()->attach($thanatopractitioner->id, [
'role' => 'assistant',
'assigned_at' => now(),
]);
$otherEmployeeIntervention->practitioners()->attach($otherThanatopractitioner->id, [
'role' => 'principal',
'assigned_at' => now(),
]);
$response = $this->getJson(sprintf(
'/api/employees/%d/agenda?month=2026-04&status=planifie',
$employee->id
));
$response->assertStatus(200)
->assertJsonPath('employee.id', $employee->id)
->assertJsonPath('employee.thanatopractitioner_id', $thanatopractitioner->id)
->assertJsonPath('filters.month', '2026-04')
->assertJsonPath('meta.total', 1)
->assertJsonPath('meta.status_summary.planifie', 1)
->assertJsonCount(1, 'data')
->assertJsonPath('data.0.id', $matchingIntervention->id)
->assertJsonPath('data.0.status', 'planifie');
}
public function test_returns_empty_agenda_when_employee_has_no_thanatopractitioner(): void
{
$employee = Employee::create([
'first_name' => 'Luc',
'last_name' => 'Bernard',
'email' => 'luc.bernard@example.test',
'job_title' => 'Assistant',
'active' => true,
]);
$response = $this->getJson('/api/employees/' . $employee->id . '/agenda');
$response->assertStatus(200)
->assertJsonPath('employee.id', $employee->id)
->assertJsonPath('employee.full_name', 'Luc Bernard')
->assertJsonPath('meta.total', 0)
->assertJsonPath('data', []);
}
}