Files
Verde-Web/tests/Feature/Api/V1/Admin/AdminHouseholdStoreTest.php

157 lines
4.8 KiB
PHP

<?php
namespace Tests\Feature\Api\V1\Admin;
use App\Models\Barangay;
use App\Models\Household;
use App\Models\User;
use Database\Seeders\RoleSeeder;
use Database\Seeders\SamplePsgcSeeder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
use Laravel\Sanctum\Sanctum;
use Tests\TestCase;
class AdminHouseholdStoreTest extends TestCase
{
use RefreshDatabase;
private User $admin;
private Barangay $barangay;
protected function setUp(): void
{
parent::setUp();
$this->seed([RoleSeeder::class, SamplePsgcSeeder::class]);
$this->admin = User::factory()->create([
'role' => User::ROLE_ADMIN,
'status' => User::STATUS_ACTIVE,
]);
$this->barangay = Barangay::first() ?? Barangay::factory()->create();
}
public function test_admin_can_create_household_with_new_resident(): void
{
Sanctum::actingAs($this->admin);
$response = $this->postJson('/api/v1/admin/households', [
'resident_type' => 'new',
'first_name' => 'Jane',
'last_name' => 'Smith',
'email' => 'jane.smith@example.com',
'phone' => '+639111111111',
'address_line' => '456 Quezon Ave',
'lat' => 14.6539,
'lng' => 121.0685,
'barangay_id' => $this->barangay->id,
'household_size' => 3,
]);
$response->assertCreated()
->assertJsonPath('data.address_line', '456 Quezon Ave')
->assertJsonPath('data.verification_status', 'pending')
->assertJsonPath('data.member_count', 1);
$this->assertDatabaseHas('users', [
'email' => 'jane.smith@example.com',
'role' => User::ROLE_RESIDENT,
'status' => User::STATUS_PENDING,
]);
$this->assertDatabaseHas('households', [
'address_line' => '456 Quezon Ave',
'verification_status' => 'pending',
]);
}
public function test_admin_can_create_household_with_existing_resident(): void
{
Sanctum::actingAs($this->admin);
$resident = User::factory()->create([
'role' => User::ROLE_RESIDENT,
'status' => User::STATUS_ACTIVE,
]);
$response = $this->postJson('/api/v1/admin/households', [
'resident_type' => 'existing',
'head_user_id' => $resident->id,
'address_line' => '789 Quezon Ave',
'lat' => 14.6539,
'lng' => 121.0685,
'barangay_id' => $this->barangay->id,
'household_size' => 2,
]);
$response->assertCreated()
->assertJsonPath('data.address_line', '789 Quezon Ave')
->assertJsonPath('data.verification_status', 'pending');
$this->assertDatabaseHas('households', [
'head_user_id' => $resident->id,
'address_line' => '789 Quezon Ave',
'verification_status' => 'pending',
]);
}
public function test_admin_cannot_create_household_with_existing_resident_already_in_household(): void
{
Sanctum::actingAs($this->admin);
$resident = User::factory()->create([
'role' => User::ROLE_RESIDENT,
'status' => User::STATUS_ACTIVE,
]);
// Create a household for them first
Household::factory()->create([
'head_user_id' => $resident->id,
'barangay_id' => $this->barangay->id,
]);
$response = $this->postJson('/api/v1/admin/households', [
'resident_type' => 'existing',
'head_user_id' => $resident->id,
'address_line' => '789 Quezon Ave',
'lat' => 14.6539,
'lng' => 121.0685,
'barangay_id' => $this->barangay->id,
'household_size' => 2,
]);
$response->assertStatus(422);
}
public function test_admin_can_create_household_with_proof_document(): void
{
Storage::fake('local');
Sanctum::actingAs($this->admin);
$file = UploadedFile::fake()->create('document.pdf', 500);
$response = $this->postJson('/api/v1/admin/households', [
'resident_type' => 'new',
'first_name' => 'Jane',
'last_name' => 'Smith',
'email' => 'jane.smith2@example.com',
'phone' => '+639111111112',
'address_line' => '456 Quezon Ave',
'lat' => 14.6539,
'lng' => 121.0685,
'barangay_id' => $this->barangay->id,
'household_size' => 3,
'proof' => $file,
]);
$response->assertCreated();
$household = Household::orderByDesc('id')->first();
$this->assertNotNull($household->proof_of_residency_path);
Storage::disk('local')->assertExists($household->proof_of_residency_path);
}
}