|
| 1 | +<?php |
| 2 | + |
| 3 | +use App\Models\User; |
| 4 | +use App\Modules\Core\Models\Tenant; |
| 5 | +use App\Modules\QualityControl\Models\NonConformanceReport; |
| 6 | +use App\Modules\QualityControl\Models\QcChecklist; |
| 7 | +use App\Modules\QualityControl\Models\QcChecklistItem; |
| 8 | +use App\Modules\QualityControl\Models\QcInspection; |
| 9 | +use App\Modules\QualityControl\Models\QcInspectionResult; |
| 10 | +use Database\Seeders\RolePermissionSeeder; |
| 11 | + |
| 12 | +beforeEach(function () { |
| 13 | + $this->seed(RolePermissionSeeder::class); |
| 14 | + $this->tenant = Tenant::create(['name' => 'QC Module Corp', 'slug' => 'qc-module-corp']); |
| 15 | + $this->user = User::factory()->create(['tenant_id' => $this->tenant->id]); |
| 16 | + $this->user->assignRole('super-admin'); |
| 17 | + $this->actingAs($this->user); |
| 18 | + app()->instance('tenant', $this->tenant); |
| 19 | +}); |
| 20 | + |
| 21 | +function makeQcChecklist(string $name = 'Test Checklist', string $category = 'process'): QcChecklist |
| 22 | +{ |
| 23 | + return QcChecklist::create([ |
| 24 | + 'tenant_id' => test()->tenant->id, |
| 25 | + 'name' => $name, |
| 26 | + 'category' => $category, |
| 27 | + 'is_active' => true, |
| 28 | + 'created_by' => test()->user->id, |
| 29 | + ]); |
| 30 | +} |
| 31 | + |
| 32 | +function makeQcInspection(?QcChecklist $cl = null): QcInspection |
| 33 | +{ |
| 34 | + $checklist = $cl ?? makeQcChecklist(); |
| 35 | + |
| 36 | + return QcInspection::create([ |
| 37 | + 'tenant_id' => test()->tenant->id, |
| 38 | + 'checklist_id' => $checklist->id, |
| 39 | + 'inspector_id' => test()->user->id, |
| 40 | + 'status' => 'pending', |
| 41 | + ]); |
| 42 | +} |
| 43 | + |
| 44 | +// Test 1: QC dashboard renders |
| 45 | +it('qc dashboard renders', function () { |
| 46 | + $this->get('/quality/dashboard')->assertStatus(200); |
| 47 | +}); |
| 48 | + |
| 49 | +// Test 2: Checklists index renders |
| 50 | +it('checklists index renders', function () { |
| 51 | + $this->get('/quality/checklists')->assertStatus(200); |
| 52 | +}); |
| 53 | + |
| 54 | +// Test 3: Can create a checklist |
| 55 | +it('can create a checklist', function () { |
| 56 | + $this->post('/quality/checklists', [ |
| 57 | + 'name' => 'Incoming Inspection', |
| 58 | + 'description' => 'Check incoming materials', |
| 59 | + 'category' => 'incoming', |
| 60 | + 'is_active' => true, |
| 61 | + ])->assertRedirect(); |
| 62 | + |
| 63 | + expect(QcChecklist::where('name', 'Incoming Inspection')->exists())->toBeTrue(); |
| 64 | +}); |
| 65 | + |
| 66 | +// Test 4: Checklist store validates required fields |
| 67 | +it('checklist store validates required fields', function () { |
| 68 | + $this->postJson('/quality/checklists', [ |
| 69 | + 'name' => '', |
| 70 | + 'category' => 'invalid_category', |
| 71 | + ])->assertStatus(422) |
| 72 | + ->assertJsonValidationErrors(['name', 'category']); |
| 73 | +}); |
| 74 | + |
| 75 | +// Test 5: Can add item to checklist |
| 76 | +it('can add item to checklist', function () { |
| 77 | + $checklist = makeQcChecklist('Items Test'); |
| 78 | + |
| 79 | + $this->post("/quality/checklists/{$checklist->id}/items", [ |
| 80 | + 'description' => 'Check surface finish', |
| 81 | + 'check_type' => 'visual', |
| 82 | + 'is_required' => true, |
| 83 | + 'sequence' => 1, |
| 84 | + ])->assertRedirect(); |
| 85 | + |
| 86 | + expect($checklist->items()->count())->toBe(1); |
| 87 | +}); |
| 88 | + |
| 89 | +// Test 6: Inspections index renders |
| 90 | +it('inspections index renders', function () { |
| 91 | + $this->get('/quality/inspections')->assertStatus(200); |
| 92 | +}); |
| 93 | + |
| 94 | +// Test 7: Can create an inspection |
| 95 | +it('can create an inspection', function () { |
| 96 | + $checklist = makeQcChecklist('Create Inspection Test'); |
| 97 | + |
| 98 | + $this->post('/quality/inspections', [ |
| 99 | + 'checklist_id' => $checklist->id, |
| 100 | + ])->assertRedirect(); |
| 101 | + |
| 102 | + expect(QcInspection::where('checklist_id', $checklist->id)->exists())->toBeTrue(); |
| 103 | +}); |
| 104 | + |
| 105 | +// Test 8: Can start an inspection |
| 106 | +it('can start an inspection', function () { |
| 107 | + $inspection = makeQcInspection(); |
| 108 | + |
| 109 | + $this->post("/quality/inspections/{$inspection->id}/start") |
| 110 | + ->assertRedirect(); |
| 111 | + |
| 112 | + $fresh = $inspection->fresh(); |
| 113 | + expect($fresh->status)->toBe('in_progress'); |
| 114 | + expect($fresh->started_at)->not->toBeNull(); |
| 115 | +}); |
| 116 | + |
| 117 | +// Test 9: Submit inspection results sets status to passed when all pass |
| 118 | +it('submit inspection results sets status to passed when all pass', function () { |
| 119 | + $checklist = makeQcChecklist('All Pass Test'); |
| 120 | + $item1 = QcChecklistItem::create([ |
| 121 | + 'tenant_id' => $this->tenant->id, |
| 122 | + 'checklist_id' => $checklist->id, |
| 123 | + 'description' => 'Item 1', |
| 124 | + 'check_type' => 'pass_fail', |
| 125 | + 'is_required' => true, |
| 126 | + 'sequence' => 1, |
| 127 | + ]); |
| 128 | + $item2 = QcChecklistItem::create([ |
| 129 | + 'tenant_id' => $this->tenant->id, |
| 130 | + 'checklist_id' => $checklist->id, |
| 131 | + 'description' => 'Item 2', |
| 132 | + 'check_type' => 'pass_fail', |
| 133 | + 'is_required' => true, |
| 134 | + 'sequence' => 2, |
| 135 | + ]); |
| 136 | + |
| 137 | + $inspection = makeQcInspection($checklist); |
| 138 | + $inspection->start(); |
| 139 | + |
| 140 | + $this->post("/quality/inspections/{$inspection->id}/results", [ |
| 141 | + 'results' => [ |
| 142 | + ['checklist_item_id' => $item1->id, 'result' => 'pass', 'measured_value' => null, 'notes' => null], |
| 143 | + ['checklist_item_id' => $item2->id, 'result' => 'pass', 'measured_value' => null, 'notes' => null], |
| 144 | + ], |
| 145 | + ])->assertRedirect(); |
| 146 | + |
| 147 | + expect($inspection->fresh()->status)->toBe('passed'); |
| 148 | +}); |
| 149 | + |
| 150 | +// Test 10: Submit with a fail result sets status to failed |
| 151 | +it('submit with a fail result sets status to failed', function () { |
| 152 | + $checklist = makeQcChecklist('Fail Test'); |
| 153 | + $item1 = QcChecklistItem::create([ |
| 154 | + 'tenant_id' => $this->tenant->id, |
| 155 | + 'checklist_id' => $checklist->id, |
| 156 | + 'description' => 'Item A', |
| 157 | + 'check_type' => 'pass_fail', |
| 158 | + 'is_required' => true, |
| 159 | + 'sequence' => 1, |
| 160 | + ]); |
| 161 | + $item2 = QcChecklistItem::create([ |
| 162 | + 'tenant_id' => $this->tenant->id, |
| 163 | + 'checklist_id' => $checklist->id, |
| 164 | + 'description' => 'Item B', |
| 165 | + 'check_type' => 'pass_fail', |
| 166 | + 'is_required' => true, |
| 167 | + 'sequence' => 2, |
| 168 | + ]); |
| 169 | + |
| 170 | + $inspection = makeQcInspection($checklist); |
| 171 | + $inspection->start(); |
| 172 | + |
| 173 | + $this->post("/quality/inspections/{$inspection->id}/results", [ |
| 174 | + 'results' => [ |
| 175 | + ['checklist_item_id' => $item1->id, 'result' => 'pass', 'measured_value' => null, 'notes' => null], |
| 176 | + ['checklist_item_id' => $item2->id, 'result' => 'fail', 'measured_value' => null, 'notes' => 'Surface scratch found'], |
| 177 | + ], |
| 178 | + ])->assertRedirect(); |
| 179 | + |
| 180 | + expect($inspection->fresh()->status)->toBe('failed'); |
| 181 | +}); |
| 182 | + |
| 183 | +// Test 11: NCRs index renders |
| 184 | +it('ncrs index renders', function () { |
| 185 | + $this->get('/quality/ncrs')->assertStatus(200); |
| 186 | +}); |
| 187 | + |
| 188 | +// Test 12: Can create an NCR with generated number |
| 189 | +it('can create an ncr with generated number', function () { |
| 190 | + $this->post('/quality/ncrs', [ |
| 191 | + 'title' => 'Defective batch found', |
| 192 | + 'description' => 'Batch #42 failed dimensional check', |
| 193 | + 'severity' => 'major', |
| 194 | + ])->assertRedirect(); |
| 195 | + |
| 196 | + $ncr = NonConformanceReport::where('title', 'Defective batch found')->first(); |
| 197 | + expect($ncr)->not->toBeNull(); |
| 198 | + expect($ncr->ncr_number)->toBe('NCR-0001'); |
| 199 | +}); |
0 commit comments