171 lines
5.8 KiB
PHP
171 lines
5.8 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature\Api\V1\Team;
|
|
|
|
use App\Models\CollectionTeam;
|
|
use App\Models\Truck;
|
|
use App\Models\User;
|
|
use Database\Seeders\RoleSeeder;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Laravel\Sanctum\Sanctum;
|
|
use Tests\TestCase;
|
|
|
|
class TeamCrudTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
private User $admin;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
$this->seed(RoleSeeder::class);
|
|
$this->admin = User::factory()->create([
|
|
'role' => User::ROLE_ADMIN,
|
|
'status' => User::STATUS_ACTIVE,
|
|
]);
|
|
}
|
|
|
|
public function test_admin_can_create_truck_and_team(): void
|
|
{
|
|
Sanctum::actingAs($this->admin);
|
|
|
|
$truck = $this->postJson('/api/v1/admin/trucks', [
|
|
'plate_number' => 'NCR-1234',
|
|
'model' => 'Isuzu Forward',
|
|
'capacity_kg' => 5000,
|
|
]);
|
|
$truck->assertCreated();
|
|
$truckId = Truck::first()->id;
|
|
|
|
$driver = User::factory()->create(['role' => User::ROLE_DRIVER, 'status' => User::STATUS_ACTIVE]);
|
|
$scanner = User::factory()->create(['role' => User::ROLE_SCANNER, 'status' => User::STATUS_ACTIVE]);
|
|
|
|
$response = $this->postJson('/api/v1/admin/teams', [
|
|
'name' => 'QC Team A',
|
|
'driver_id' => $driver->id,
|
|
'scanner_id' => $scanner->id,
|
|
'truck_id' => $truckId,
|
|
]);
|
|
|
|
$response->assertCreated()
|
|
->assertJsonPath('data.name', 'QC Team A');
|
|
}
|
|
|
|
public function test_double_assigned_driver_rejected(): void
|
|
{
|
|
Sanctum::actingAs($this->admin);
|
|
$driver = User::factory()->create(['role' => User::ROLE_DRIVER]);
|
|
|
|
$this->postJson('/api/v1/admin/teams', [
|
|
'name' => 'Team 1', 'driver_id' => $driver->id, 'status' => 'active'
|
|
])->assertCreated();
|
|
|
|
$response = $this->postJson('/api/v1/admin/teams', [
|
|
'name' => 'Team 2', 'driver_id' => $driver->id, 'status' => 'active'
|
|
]);
|
|
|
|
$response->assertStatus(422);
|
|
$this->assertNotEmpty($response->json('errors.conflicts'));
|
|
}
|
|
|
|
public function test_override_conflicts_lets_admin_force_assign(): void
|
|
{
|
|
Sanctum::actingAs($this->admin);
|
|
$driver = User::factory()->create(['role' => User::ROLE_DRIVER]);
|
|
|
|
$this->postJson('/api/v1/admin/teams', [
|
|
'name' => 'Team 1', 'driver_id' => $driver->id, 'status' => 'active'
|
|
])->assertCreated();
|
|
|
|
$this->postJson('/api/v1/admin/teams', [
|
|
'name' => 'Team 2', 'driver_id' => $driver->id, 'override_conflicts' => true, 'status' => 'active'
|
|
])->assertCreated();
|
|
}
|
|
|
|
public function test_admin_can_update_team_helpers(): void
|
|
{
|
|
Sanctum::actingAs($this->admin);
|
|
$helper1 = User::factory()->create(['role' => User::ROLE_HELPER]);
|
|
$helper2 = User::factory()->create(['role' => User::ROLE_HELPER]);
|
|
|
|
$created = $this->postJson('/api/v1/admin/teams', [
|
|
'name' => 'Team', 'helper_ids' => [$helper1->id],
|
|
]);
|
|
$uuid = $created->json('data.id');
|
|
|
|
$response = $this->patchJson("/api/v1/admin/teams/{$uuid}", [
|
|
'name' => 'Team',
|
|
'helper_ids' => [$helper1->id, $helper2->id],
|
|
]);
|
|
|
|
$response->assertOk();
|
|
$team = CollectionTeam::where('uuid', $uuid)->first();
|
|
$this->assertSame(2, $team->helpers()->count());
|
|
}
|
|
|
|
public function test_admin_can_create_team_with_random_helper_assignment(): void
|
|
{
|
|
Sanctum::actingAs($this->admin);
|
|
|
|
// Create 3 active helper users
|
|
User::factory()->count(3)->create(['role' => User::ROLE_HELPER, 'status' => User::STATUS_ACTIVE]);
|
|
|
|
$response = $this->postJson('/api/v1/admin/teams', [
|
|
'name' => 'Random Team',
|
|
'helper_count' => 2,
|
|
'helper_assignment_mode' => 'random',
|
|
]);
|
|
|
|
$response->assertCreated();
|
|
$team = CollectionTeam::where('name', 'Random Team')->first();
|
|
$this->assertSame(2, $team->helpers()->count());
|
|
}
|
|
|
|
public function test_admin_cannot_create_team_with_random_helper_assignment_if_insufficient_available_helpers(): void
|
|
{
|
|
Sanctum::actingAs($this->admin);
|
|
|
|
// Only 1 helper available
|
|
User::factory()->create(['role' => User::ROLE_HELPER, 'status' => User::STATUS_ACTIVE]);
|
|
|
|
$response = $this->postJson('/api/v1/admin/teams', [
|
|
'name' => 'Unsatisfied Random Team',
|
|
'helper_count' => 2,
|
|
'helper_assignment_mode' => 'random',
|
|
]);
|
|
|
|
$response->assertStatus(422)
|
|
->assertJsonValidationErrors(['helper_count']);
|
|
}
|
|
|
|
public function test_admin_can_create_team_with_manual_helper_assignment(): void
|
|
{
|
|
Sanctum::actingAs($this->admin);
|
|
|
|
$helper1 = User::factory()->create(['role' => User::ROLE_HELPER, 'status' => User::STATUS_ACTIVE]);
|
|
$helper2 = User::factory()->create(['role' => User::ROLE_HELPER, 'status' => User::STATUS_ACTIVE]);
|
|
|
|
$response = $this->postJson('/api/v1/admin/teams', [
|
|
'name' => 'Manual Team',
|
|
'helper_count' => 2,
|
|
'helper_assignment_mode' => 'manual',
|
|
'helper_ids' => [$helper1->id, $helper2->id],
|
|
]);
|
|
|
|
$response->assertCreated();
|
|
$team = CollectionTeam::where('name', 'Manual Team')->first();
|
|
$assignedHelperIds = $team->helpers()->pluck('user_id')->toArray();
|
|
$this->assertContains($helper1->id, $assignedHelperIds);
|
|
$this->assertContains($helper2->id, $assignedHelperIds);
|
|
}
|
|
|
|
public function test_resident_blocked(): void
|
|
{
|
|
$resident = User::factory()->create(['role' => User::ROLE_RESIDENT, 'status' => User::STATUS_ACTIVE]);
|
|
Sanctum::actingAs($resident);
|
|
|
|
$this->getJson('/api/v1/admin/teams')->assertStatus(403);
|
|
}
|
|
}
|