|
| 1 | +<?php |
| 2 | + |
| 3 | +use App\Models\User; |
| 4 | +use App\Modules\Core\Models\Tenant; |
| 5 | +use App\Modules\Finance\Models\WriteOff; |
| 6 | +use Database\Seeders\RolePermissionSeeder; |
| 7 | + |
| 8 | +beforeEach(function () { |
| 9 | + $this->seed(RolePermissionSeeder::class); |
| 10 | + $this->tenant = Tenant::create(['name' => 'WOcorp', 'slug' => 'wo-corp-' . uniqid()]); |
| 11 | + $this->admin = User::factory()->create(['tenant_id' => $this->tenant->id]); |
| 12 | + $this->admin->assignRole('super-admin'); |
| 13 | + $this->actingAs($this->admin); |
| 14 | + app()->instance('tenant', $this->tenant); |
| 15 | +}); |
| 16 | + |
| 17 | +function makeWriteOff(array $attrs = []): WriteOff |
| 18 | +{ |
| 19 | + return WriteOff::create([ |
| 20 | + 'tenant_id' => test()->tenant->id, |
| 21 | + 'amount' => 500.00, |
| 22 | + 'write_off_date' => now()->toDateString(), |
| 23 | + 'reason' => 'bad_debt', |
| 24 | + 'created_by' => test()->admin->id, |
| 25 | + ...$attrs, |
| 26 | + ]); |
| 27 | +} |
| 28 | + |
| 29 | +it('index requires authentication', function () { |
| 30 | + $this->post('/logout'); |
| 31 | + $this->get('/finance/write-offs')->assertRedirect('/login'); |
| 32 | +}); |
| 33 | + |
| 34 | +it('admin can list write-offs', function () { |
| 35 | + makeWriteOff(); |
| 36 | + $this->get('/finance/write-offs')->assertOk(); |
| 37 | +}); |
| 38 | + |
| 39 | +it('store creates a write-off', function () { |
| 40 | + $this->post('/finance/write-offs', [ |
| 41 | + 'amount' => 1000.00, |
| 42 | + 'write_off_date' => now()->toDateString(), |
| 43 | + 'reason' => 'dispute', |
| 44 | + ])->assertRedirect(); |
| 45 | + |
| 46 | + expect(WriteOff::where('tenant_id', test()->tenant->id)->where('reason', 'dispute')->exists())->toBeTrue(); |
| 47 | +}); |
| 48 | + |
| 49 | +it('store validates required fields', function () { |
| 50 | + $this->postJson('/finance/write-offs', [])->assertStatus(422)->assertJsonValidationErrors(['amount', 'write_off_date', 'reason']); |
| 51 | +}); |
| 52 | + |
| 53 | +it('show displays a write-off', function () { |
| 54 | + $wo = makeWriteOff(); |
| 55 | + $this->get("/finance/write-offs/{$wo->id}")->assertOk(); |
| 56 | +}); |
| 57 | + |
| 58 | +it('approve transitions to approved status', function () { |
| 59 | + $wo = makeWriteOff(); |
| 60 | + expect($wo->is_pending)->toBeTrue(); |
| 61 | + |
| 62 | + $this->post("/finance/write-offs/{$wo->id}/approve")->assertRedirect(); |
| 63 | + |
| 64 | + $wo->refresh(); |
| 65 | + expect($wo->is_approved)->toBeTrue(); |
| 66 | + expect($wo->approved_by)->toBe(test()->admin->id); |
| 67 | + expect($wo->approved_at)->not->toBeNull(); |
| 68 | + expect($wo->write_off_number)->not->toBeNull(); |
| 69 | +}); |
| 70 | + |
| 71 | +it('reverse transitions to reversed status', function () { |
| 72 | + $wo = makeWriteOff(['status' => 'approved']); |
| 73 | + $this->post("/finance/write-offs/{$wo->id}/reverse")->assertRedirect(); |
| 74 | + $wo->refresh(); |
| 75 | + expect($wo->status)->toBe('reversed'); |
| 76 | +}); |
| 77 | + |
| 78 | +it('write_off_number is generated on approval', function () { |
| 79 | + $wo = makeWriteOff(); |
| 80 | + $this->post("/finance/write-offs/{$wo->id}/approve")->assertRedirect(); |
| 81 | + $wo->refresh(); |
| 82 | + expect($wo->write_off_number)->toStartWith('WO-'); |
| 83 | +}); |
| 84 | + |
| 85 | +it('destroy soft-deletes the write-off', function () { |
| 86 | + $wo = makeWriteOff(); |
| 87 | + $this->delete("/finance/write-offs/{$wo->id}")->assertRedirect(); |
| 88 | + expect(WriteOff::find($wo->id))->toBeNull(); |
| 89 | + expect(WriteOff::withTrashed()->find($wo->id))->not->toBeNull(); |
| 90 | +}); |
0 commit comments