migrant-web/tests/Feature/EmployerMessageWebTest.php

121 lines
3.7 KiB
PHP

<?php
namespace Tests\Feature;
use App\Models\Conversation;
use App\Models\Message;
use App\Models\User;
use App\Models\EmployerProfile;
use App\Models\Worker;
use App\Models\JobPost;
use App\Models\JobApplication;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
use Inertia\Testing\AssertableInertia as Assert;
class EmployerMessageWebTest extends TestCase
{
use RefreshDatabase;
protected $worker;
protected $employer;
protected $jobPost;
protected function setUp(): void
{
parent::setUp();
// Create a test worker
$this->worker = Worker::create([
'name' => 'Jane Smith',
'email' => 'jane@example.com',
'phone' => '+971500000001',
'password' => bcrypt('password'),
'status' => 'Available',
'nationality' => 'Ethiopian',
'age' => 28,
'salary' => 1500.00,
'availability' => 'Immediate',
'experience' => '2 Years',
'religion' => 'Christian',
'bio' => 'Experienced housemaid seeking employment.',
]);
// Create an employer user
$this->employer = User::create([
'name' => 'John Employer',
'email' => 'employer_test@example.com',
'password' => bcrypt('password'),
'role' => 'employer',
]);
// Create employer profile
EmployerProfile::create([
'user_id' => $this->employer->id,
'company_name' => 'John Ltd',
'phone' => '+971500000002',
'country' => 'United Arab Emirates',
'verification_status' => 'approved',
'language' => 'English',
'notifications' => true,
]);
// Create a Job Post
$this->jobPost = JobPost::create([
'employer_id' => $this->employer->id,
'title' => 'Housekeeper',
'location' => 'Dubai',
'salary' => 1800,
'job_type' => 'full-time',
'status' => 'open',
'start_date' => '2026-08-01',
'description' => 'Test Job Description',
'requirements' => 'Test Requirements',
]);
}
/**
* Test show conversation auto-creates JobApplication and passes it as a prop.
*/
public function test_show_conversation_auto_creates_application_and_passes_prop()
{
// Authenticate session
session(['user' => (object)[
'id' => $this->employer->id,
'name' => $this->employer->name,
'email' => $this->employer->email,
'role' => 'employer',
'subscription_status' => 'active',
]]);
$conversation = Conversation::create([
'employer_id' => $this->employer->id,
'worker_id' => $this->worker->id,
]);
// Ensure no application exists initially
$this->assertDatabaseMissing('job_applications', [
'worker_id' => $this->worker->id,
]);
$response = $this->actingAs($this->employer)
->get(route('employer.messages.show', ['id' => $conversation->id]));
$response->assertStatus(200);
// Verify JobApplication was auto-created in database
$this->assertDatabaseHas('job_applications', [
'worker_id' => $this->worker->id,
'status' => 'applied',
]);
// Verify Inertia response has the 'application' prop
$response->assertInertia(fn (Assert $page) => $page
->component('Employer/Messages/Show')
->has('application')
->where('application.status', 'applied')
->has('application.status_history')
);
}
}