create([ 'email' => 'manager@example.com', 'password' => bcrypt('password123'), 'role' => 'manager', 'active' => true, ]); $response = $this->postJson('/api/auth/login', [ 'email' => 'manager@example.com', 'password' => 'password123', ]); return $response->json('access_token'); } // 5.1.11 API test: POST /api/allocations creates allocation public function test_post_allocations_creates_allocation() { $token = $this->loginAsManager(); $role = Role::factory()->create(); $teamMember = TeamMember::factory()->create(['role_id' => $role->id, 'active' => true]); $project = Project::factory()->create(); $response = $this->withHeader('Authorization', "Bearer {$token}") ->postJson('/api/allocations', [ 'project_id' => $project->id, 'team_member_id' => $teamMember->id, 'month' => '2026-02', 'allocated_hours' => 40, ]); $response->assertStatus(201); $response->assertJson([ 'data' => [ 'project_id' => $project->id, 'team_member_id' => $teamMember->id, 'allocated_hours' => '40.00', ], ]); $this->assertDatabaseHas('allocations', [ 'project_id' => $project->id, 'team_member_id' => $teamMember->id, 'allocated_hours' => 40, ]); } // 5.1.12 API test: Validate hours >= 0 public function test_validate_hours_must_be_greater_than_or_equal_zero() { $token = $this->loginAsManager(); $role = Role::factory()->create(); $teamMember = TeamMember::factory()->create(['role_id' => $role->id]); $project = Project::factory()->create(); // Test with negative hours $response = $this->withHeader('Authorization', "Bearer {$token}") ->postJson('/api/allocations', [ 'project_id' => $project->id, 'team_member_id' => $teamMember->id, 'month' => '2026-02', 'allocated_hours' => -10, ]); $response->assertStatus(422); $response->assertJsonValidationErrors(['allocated_hours']); $response->assertJsonFragment([ 'allocated_hours' => ['The allocated hours field must be at least 0.'], ]); } // 5.1.13 API test: GET /api/allocations returns matrix public function test_get_allocations_returns_matrix() { $token = $this->loginAsManager(); $role = Role::factory()->create(); $teamMember = TeamMember::factory()->create(['role_id' => $role->id, 'active' => true]); $project = Project::factory()->create(); // Create allocation Allocation::factory()->create([ 'project_id' => $project->id, 'team_member_id' => $teamMember->id, 'month' => '2026-02', 'allocated_hours' => 40, ]); $response = $this->withHeader('Authorization', "Bearer {$token}") ->getJson('/api/allocations?month=2026-02'); $response->assertStatus(200); $response->assertJsonStructure([ 'data' => [ '*' => [ 'project_id', 'team_member_id', 'month', 'allocated_hours', ], ], ]); } // 5.1.14 API test: PUT /api/allocations/{id} updates public function test_put_allocations_updates() { $token = $this->loginAsManager(); $allocation = Allocation::factory()->create([ 'allocated_hours' => 40, ]); $response = $this->withHeader('Authorization', "Bearer {$token}") ->putJson("/api/allocations/{$allocation->id}", [ 'allocated_hours' => 60, ]); $response->assertStatus(200); $response->assertJson([ 'data' => [ 'id' => $allocation->id, 'allocated_hours' => '60.00', ], ]); $this->assertDatabaseHas('allocations', [ 'id' => $allocation->id, 'allocated_hours' => 60, ]); } // 5.1.15 API test: DELETE /api/allocations/{id} removes public function test_delete_allocation_removes() { $token = $this->loginAsManager(); $allocation = Allocation::factory()->create(); $response = $this->withHeader('Authorization', "Bearer {$token}") ->deleteJson("/api/allocations/{$allocation->id}"); $response->assertStatus(200); $response->assertJson([ 'message' => 'Allocation deleted successfully', ]); $this->assertDatabaseMissing('allocations', [ 'id' => $allocation->id, ]); } // 5.1.16 API test: POST /api/allocations/bulk creates multiple public function test_post_allocations_bulk_creates_multiple() { $token = $this->loginAsManager(); $role = Role::factory()->create(); $teamMember1 = TeamMember::factory()->create(['role_id' => $role->id, 'active' => true]); $teamMember2 = TeamMember::factory()->create(['role_id' => $role->id, 'active' => true]); $project = Project::factory()->create(); $response = $this->withHeader('Authorization', "Bearer {$token}") ->postJson('/api/allocations/bulk', [ 'allocations' => [ [ 'project_id' => $project->id, 'team_member_id' => $teamMember1->id, 'month' => '2026-02', 'allocated_hours' => 40, ], [ 'project_id' => $project->id, 'team_member_id' => $teamMember2->id, 'month' => '2026-02', 'allocated_hours' => 32, ], ], ]); $response->assertStatus(201); $response->assertJsonCount(2, 'data'); $this->assertDatabaseHas('allocations', [ 'project_id' => $project->id, 'team_member_id' => $teamMember1->id, 'allocated_hours' => 40, ]); $this->assertDatabaseHas('allocations', [ 'project_id' => $project->id, 'team_member_id' => $teamMember2->id, 'allocated_hours' => 32, ]); } // Test: Allocate zero hours is allowed public function test_allocate_zero_hours_is_allowed() { $token = $this->loginAsManager(); $role = Role::factory()->create(); $teamMember = TeamMember::factory()->create(['role_id' => $role->id]); $project = Project::factory()->create(); $response = $this->withHeader('Authorization', "Bearer {$token}") ->postJson('/api/allocations', [ 'project_id' => $project->id, 'team_member_id' => $teamMember->id, 'month' => '2026-02', 'allocated_hours' => 0, ]); $response->assertStatus(201); $response->assertJson([ 'data' => [ 'allocated_hours' => '0.00', ], ]); } // Test: Cannot update non-existent allocation public function test_cannot_update_nonexistent_allocation() { $token = $this->loginAsManager(); $response = $this->withHeader('Authorization', "Bearer {$token}") ->putJson('/api/allocations/nonexistent-id', [ 'allocated_hours' => 60, ]); $response->assertStatus(404); $response->assertJson([ 'message' => 'Allocation not found', ]); } // Test: Cannot delete non-existent allocation public function test_cannot_delete_nonexistent_allocation() { $token = $this->loginAsManager(); $response = $this->withHeader('Authorization', "Bearer {$token}") ->deleteJson('/api/allocations/nonexistent-id'); $response->assertStatus(404); } }