|
| 1 | +<?php |
| 2 | + |
| 3 | +use App\Models\User; |
| 4 | +use App\Modules\Core\Models\Tenant; |
| 5 | +use App\Modules\HR\Models\Employee; |
| 6 | +use App\Modules\HR\Models\FlexibleWorkArrangement; |
| 7 | +use Database\Seeders\RolePermissionSeeder; |
| 8 | + |
| 9 | +beforeEach(function () { |
| 10 | + $this->seed(RolePermissionSeeder::class); |
| 11 | + $this->tenant = Tenant::create(['name' => 'FWAcorp', 'slug' => 'fwa-corp-' . uniqid()]); |
| 12 | + $this->admin = User::factory()->create(['tenant_id' => $this->tenant->id]); |
| 13 | + $this->admin->assignRole('super-admin'); |
| 14 | + $this->actingAs($this->admin); |
| 15 | + app()->instance('tenant', $this->tenant); |
| 16 | +}); |
| 17 | + |
| 18 | +function makeFWAEmployee(): Employee |
| 19 | +{ |
| 20 | + return Employee::create([ |
| 21 | + 'tenant_id' => test()->tenant->id, |
| 22 | + 'user_id' => test()->admin->id, |
| 23 | + 'first_name' => 'Flex', |
| 24 | + 'last_name' => 'Worker-' . uniqid(), |
| 25 | + 'email' => 'fwa.' . uniqid() . '@test.com', |
| 26 | + 'status' => 'active', |
| 27 | + 'start_date' => now()->toDateString(), |
| 28 | + ]); |
| 29 | +} |
| 30 | + |
| 31 | +function makeFlexWork(array $attrs = []): FlexibleWorkArrangement |
| 32 | +{ |
| 33 | + $emp = makeFWAEmployee(); |
| 34 | + return FlexibleWorkArrangement::create([ |
| 35 | + 'tenant_id' => test()->tenant->id, |
| 36 | + 'employee_id' => $emp->id, |
| 37 | + 'arrangement_type' => 'remote', |
| 38 | + 'start_date' => now()->toDateString(), |
| 39 | + ...$attrs, |
| 40 | + ]); |
| 41 | +} |
| 42 | + |
| 43 | +it('index requires authentication', function () { |
| 44 | + $this->post('/logout'); |
| 45 | + $this->get('/hr/flexible-work')->assertRedirect('/login'); |
| 46 | +}); |
| 47 | + |
| 48 | +it('admin can list flexible work arrangements', function () { |
| 49 | + makeFlexWork(); |
| 50 | + $this->get('/hr/flexible-work')->assertOk(); |
| 51 | +}); |
| 52 | + |
| 53 | +it('store creates a flexible work arrangement', function () { |
| 54 | + $emp = makeFWAEmployee(); |
| 55 | + $this->post('/hr/flexible-work', [ |
| 56 | + 'employee_id' => $emp->id, |
| 57 | + 'arrangement_type' => 'hybrid', |
| 58 | + 'start_date' => now()->toDateString(), |
| 59 | + 'end_date' => now()->addMonths(6)->toDateString(), |
| 60 | + ])->assertRedirect(); |
| 61 | + |
| 62 | + expect(FlexibleWorkArrangement::where('employee_id', $emp->id)->where('arrangement_type', 'hybrid')->exists())->toBeTrue(); |
| 63 | +}); |
| 64 | + |
| 65 | +it('store validates required fields', function () { |
| 66 | + $this->postJson('/hr/flexible-work', [])->assertStatus(422)->assertJsonValidationErrors(['employee_id', 'arrangement_type', 'start_date']); |
| 67 | +}); |
| 68 | + |
| 69 | +it('show displays the arrangement', function () { |
| 70 | + $fwa = makeFlexWork(); |
| 71 | + $this->get("/hr/flexible-work/{$fwa->id}")->assertOk(); |
| 72 | +}); |
| 73 | + |
| 74 | +it('approve transitions to approved', function () { |
| 75 | + $fwa = makeFlexWork(); |
| 76 | + expect($fwa->is_pending)->toBeTrue(); |
| 77 | + |
| 78 | + $this->post("/hr/flexible-work/{$fwa->id}/approve")->assertRedirect(); |
| 79 | + |
| 80 | + $fwa->refresh(); |
| 81 | + expect($fwa->status)->toBe('approved'); |
| 82 | + expect($fwa->approved_by)->toBe(test()->admin->id); |
| 83 | +}); |
| 84 | + |
| 85 | +it('reject transitions to rejected with reason', function () { |
| 86 | + $fwa = makeFlexWork(); |
| 87 | + $this->post("/hr/flexible-work/{$fwa->id}/reject", ['reason' => 'Business needs'])->assertRedirect(); |
| 88 | + $fwa->refresh(); |
| 89 | + expect($fwa->status)->toBe('rejected'); |
| 90 | + expect($fwa->rejection_reason)->toBe('Business needs'); |
| 91 | +}); |
| 92 | + |
| 93 | +it('is_active returns true for approved arrangement without end date', function () { |
| 94 | + $fwa = makeFlexWork(['status' => 'approved']); |
| 95 | + expect($fwa->is_active)->toBeTrue(); |
| 96 | +}); |
| 97 | + |
| 98 | +it('is_active returns false for rejected arrangement', function () { |
| 99 | + $fwa = makeFlexWork(['status' => 'rejected']); |
| 100 | + expect($fwa->is_active)->toBeFalse(); |
| 101 | +}); |
| 102 | + |
| 103 | +it('destroy soft-deletes the arrangement', function () { |
| 104 | + $fwa = makeFlexWork(); |
| 105 | + $this->delete("/hr/flexible-work/{$fwa->id}")->assertRedirect(); |
| 106 | + expect(FlexibleWorkArrangement::find($fwa->id))->toBeNull(); |
| 107 | + expect(FlexibleWorkArrangement::withTrashed()->find($fwa->id))->not->toBeNull(); |
| 108 | +}); |
0 commit comments