All checks were successful
linter / quality (pull_request) Successful in 1m34s
security / Dependency Audit (pull_request) Successful in 1m30s
security / Static Analysis (pull_request) Successful in 1m32s
tests / ci (8.4) (pull_request) Successful in 1m25s
tests / ci (8.5) (pull_request) Successful in 1m58s
When no active approval workflow exists, ApprovalService::submit() was silently returning, leaving the request in Draft while showing a false success message. Now throws a RuntimeException as a safety net, and the Livewire component guards before creating any records and shows a clear error to the user.
125 lines
3.7 KiB
PHP
125 lines
3.7 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature;
|
|
|
|
use App\Enums\ApprovalStatus;
|
|
use App\Enums\JourneyMethod;
|
|
use App\Enums\TravelStatus;
|
|
use App\Models\ApprovalStep;
|
|
use App\Models\ApprovalWorkflow;
|
|
use App\Models\TravelRequest;
|
|
use App\Models\User;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Livewire\Livewire;
|
|
use Spatie\Permission\Models\Role;
|
|
use Tests\TestCase;
|
|
|
|
class TravelRequestSubmissionTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
private User $staff;
|
|
|
|
/** @var array<string, mixed> */
|
|
private array $validFormData;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
|
|
Role::firstOrCreate(['name' => 'staff']);
|
|
Role::firstOrCreate(['name' => 'travel_approver']);
|
|
Role::firstOrCreate(['name' => 'administrator']);
|
|
|
|
$this->staff = User::factory()->create();
|
|
$this->staff->assignRole('staff');
|
|
|
|
$this->validFormData = [
|
|
'emergencyFullName' => 'Jane Doe',
|
|
'emergencyPhone' => '0400000000',
|
|
'emergencyRelationship' => 'Spouse',
|
|
'reasonSummary' => 'Attending a medical conference in Sydney',
|
|
'journeys' => [
|
|
[
|
|
'origin' => 'Perth',
|
|
'destination' => 'Sydney',
|
|
'date' => '2026-04-01',
|
|
'time' => '09:00',
|
|
'method' => JourneyMethod::Air->value,
|
|
],
|
|
],
|
|
'costCodes' => [],
|
|
];
|
|
}
|
|
|
|
private function makeWorkflow(): ApprovalWorkflow
|
|
{
|
|
$workflow = ApprovalWorkflow::factory()->create(['is_active' => true]);
|
|
|
|
ApprovalStep::factory()->create([
|
|
'workflow_id' => $workflow->id,
|
|
'order' => 1,
|
|
'name' => 'Travel Approver Review',
|
|
'role' => 'travel_approver',
|
|
]);
|
|
|
|
return $workflow;
|
|
}
|
|
|
|
public function test_submitting_form_with_active_workflow_sets_status_to_pending(): void
|
|
{
|
|
$this->makeWorkflow();
|
|
|
|
Livewire::actingAs($this->staff)
|
|
->test('travel-request.create')
|
|
->set($this->validFormData)
|
|
->call('submit');
|
|
|
|
$request = TravelRequest::first();
|
|
$this->assertNotNull($request);
|
|
$this->assertSame(TravelStatus::Pending, $request->status);
|
|
$this->assertNotNull($request->submitted_at);
|
|
}
|
|
|
|
public function test_submitting_form_with_active_workflow_creates_pending_approval(): void
|
|
{
|
|
$this->makeWorkflow();
|
|
|
|
Livewire::actingAs($this->staff)
|
|
->test('travel-request.create')
|
|
->set($this->validFormData)
|
|
->call('submit');
|
|
|
|
$request = TravelRequest::first();
|
|
$this->assertCount(1, $request->approvals);
|
|
$this->assertSame(ApprovalStatus::Pending->value, $request->approvals->first()->status->value);
|
|
}
|
|
|
|
public function test_submitting_form_with_no_active_workflow_shows_error(): void
|
|
{
|
|
Livewire::actingAs($this->staff)
|
|
->test('travel-request.create')
|
|
->set($this->validFormData)
|
|
->call('submit')
|
|
->assertHasErrors(['workflow']);
|
|
|
|
$this->assertDatabaseCount('travel_requests', 0);
|
|
}
|
|
|
|
public function test_saving_draft_does_not_submit_for_approval(): void
|
|
{
|
|
$this->makeWorkflow();
|
|
|
|
Livewire::actingAs($this->staff)
|
|
->test('travel-request.create')
|
|
->set($this->validFormData)
|
|
->call('saveDraft');
|
|
|
|
$request = TravelRequest::first();
|
|
$this->assertNotNull($request);
|
|
$this->assertSame(TravelStatus::Draft, $request->status);
|
|
$this->assertNull($request->submitted_at);
|
|
$this->assertCount(0, $request->approvals);
|
|
}
|
|
}
|