Files
headroom/backend/tests/Unit/AllocationValidationServiceTest.php
Santhosh Janardhanan 3324c4f156 feat(allocation): implement resource allocation feature
- Add AllocationController with CRUD + bulk endpoints
- Add AllocationValidationService for capacity/estimate validation
- Add AllocationMatrixService for optimized matrix queries
- Add AllocationPolicy for authorization
- Add AllocationResource for API responses
- Add frontend allocationService and matrix UI
- Add E2E tests for allocation matrix (20 tests)
- Add unit tests for validation service and policies
- Fix month format conversion (YYYY-MM to YYYY-MM-01)
2026-02-25 16:28:47 -05:00

118 lines
3.1 KiB
PHP

<?php
namespace Tests\Unit;
use App\Models\Project;
use App\Services\AllocationValidationService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class AllocationValidationServiceTest extends TestCase
{
use RefreshDatabase;
protected AllocationValidationService $service;
protected function setUp(): void
{
parent::setUp();
$this->service = new AllocationValidationService;
}
// 5.1.18 Unit test: Allocation validation service
public function test_validate_capacity_returns_zero_utilization_for_missing_team_member()
{
$result = $this->service->validateCapacity(
'non-existent-id',
'2026-02',
40
);
$this->assertTrue($result['valid']);
$this->assertNull($result['warning']);
$this->assertEquals(0, $result['utilization']);
}
public function test_validate_approved_estimate_returns_green_when_at_100_percent()
{
$project = Project::factory()->create([
'approved_estimate' => 100,
]);
$result = $this->service->validateApprovedEstimate(
$project->id,
'2026-02',
100
);
$this->assertTrue($result['valid']);
$this->assertEquals('green', $result['indicator']);
}
public function test_validate_approved_estimate_returns_yellow_when_under()
{
$project = Project::factory()->create([
'approved_estimate' => 100,
]);
$result = $this->service->validateApprovedEstimate(
$project->id,
'2026-02',
60
);
$this->assertTrue($result['valid']);
$this->assertEquals('yellow', $result['indicator']);
$this->assertStringContainsString('under by', $result['message']);
}
public function test_validate_approved_estimate_returns_red_when_over()
{
$project = Project::factory()->create([
'approved_estimate' => 100,
]);
$result = $this->service->validateApprovedEstimate(
$project->id,
'2026-02',
120
);
$this->assertTrue($result['valid']);
$this->assertEquals('red', $result['indicator']);
$this->assertStringContainsString('over', $result['message']);
}
public function test_validate_approved_estimate_returns_gray_when_no_estimate()
{
$project = Project::factory()->create([
'approved_estimate' => null,
]);
$result = $this->service->validateApprovedEstimate(
$project->id,
'2026-02',
40
);
$this->assertTrue($result['valid']);
$this->assertEquals('gray', $result['indicator']);
}
public function test_validate_approved_estimate_returns_gray_when_estimate_is_zero()
{
$project = Project::factory()->create([
'approved_estimate' => 0,
]);
$result = $this->service->validateApprovedEstimate(
$project->id,
'2026-02',
40
);
$this->assertTrue($result['valid']);
$this->assertEquals('gray', $result['indicator']);
}
}