|
| 1 | +<?php |
| 2 | + |
| 3 | +use App\Models\User; |
| 4 | +use App\Modules\Core\Models\Tenant; |
| 5 | +use App\Modules\Finance\Models\IntercompanyTransaction; |
| 6 | +use Database\Seeders\RolePermissionSeeder; |
| 7 | + |
| 8 | +beforeEach(function () { |
| 9 | + $this->seed(RolePermissionSeeder::class); |
| 10 | + $this->tenant = Tenant::create(['name' => 'ICcorp', 'slug' => 'ic-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 makeICTransaction(array $attrs = []): IntercompanyTransaction |
| 18 | +{ |
| 19 | + return IntercompanyTransaction::create([ |
| 20 | + 'tenant_id' => test()->tenant->id, |
| 21 | + 'from_entity' => 'Parent Co', |
| 22 | + 'to_entity' => 'Subsidiary A', |
| 23 | + 'amount' => 10000.00, |
| 24 | + 'transaction_date' => now()->toDateString(), |
| 25 | + 'transaction_type' => 'recharge', |
| 26 | + 'created_by' => test()->admin->id, |
| 27 | + ...$attrs, |
| 28 | + ]); |
| 29 | +} |
| 30 | + |
| 31 | +it('index requires authentication', function () { |
| 32 | + $this->post('/logout'); |
| 33 | + $this->get('/finance/intercompany')->assertRedirect('/login'); |
| 34 | +}); |
| 35 | + |
| 36 | +it('admin can list intercompany transactions', function () { |
| 37 | + makeICTransaction(); |
| 38 | + $this->get('/finance/intercompany')->assertOk(); |
| 39 | +}); |
| 40 | + |
| 41 | +it('store creates an intercompany transaction', function () { |
| 42 | + $this->post('/finance/intercompany', [ |
| 43 | + 'from_entity' => 'HQ', |
| 44 | + 'to_entity' => 'Branch', |
| 45 | + 'amount' => 5000.00, |
| 46 | + 'transaction_date' => now()->toDateString(), |
| 47 | + 'transaction_type' => 'loan', |
| 48 | + ])->assertRedirect(); |
| 49 | + |
| 50 | + expect(IntercompanyTransaction::where('tenant_id', test()->tenant->id)->where('transaction_type', 'loan')->exists())->toBeTrue(); |
| 51 | +}); |
| 52 | + |
| 53 | +it('store validates required fields', function () { |
| 54 | + $this->postJson('/finance/intercompany', [])->assertStatus(422)->assertJsonValidationErrors(['from_entity', 'to_entity', 'amount', 'transaction_date', 'transaction_type']); |
| 55 | +}); |
| 56 | + |
| 57 | +it('show displays a transaction', function () { |
| 58 | + $tx = makeICTransaction(); |
| 59 | + $this->get("/finance/intercompany/{$tx->id}")->assertOk(); |
| 60 | +}); |
| 61 | + |
| 62 | +it('post transitions status to posted', function () { |
| 63 | + $tx = makeICTransaction(); |
| 64 | + expect($tx->is_draft)->toBeTrue(); |
| 65 | + |
| 66 | + $this->post("/finance/intercompany/{$tx->id}/post")->assertRedirect(); |
| 67 | + |
| 68 | + $tx->refresh(); |
| 69 | + expect($tx->is_posted)->toBeTrue(); |
| 70 | + expect($tx->transaction_number)->not->toBeNull(); |
| 71 | +}); |
| 72 | + |
| 73 | +it('reconcile transitions status to reconciled', function () { |
| 74 | + $tx = makeICTransaction(['status' => 'posted']); |
| 75 | + $this->post("/finance/intercompany/{$tx->id}/reconcile")->assertRedirect(); |
| 76 | + $tx->refresh(); |
| 77 | + expect($tx->status)->toBe('reconciled'); |
| 78 | +}); |
| 79 | + |
| 80 | +it('reverse transitions status to reversed', function () { |
| 81 | + $tx = makeICTransaction(['status' => 'posted']); |
| 82 | + $this->post("/finance/intercompany/{$tx->id}/reverse")->assertRedirect(); |
| 83 | + $tx->refresh(); |
| 84 | + expect($tx->status)->toBe('reversed'); |
| 85 | +}); |
| 86 | + |
| 87 | +it('transaction_number starts with IC- after posting', function () { |
| 88 | + $tx = makeICTransaction(); |
| 89 | + $this->post("/finance/intercompany/{$tx->id}/post")->assertRedirect(); |
| 90 | + $tx->refresh(); |
| 91 | + expect($tx->transaction_number)->toStartWith('IC-'); |
| 92 | +}); |
| 93 | + |
| 94 | +it('destroy soft-deletes the transaction', function () { |
| 95 | + $tx = makeICTransaction(); |
| 96 | + $this->delete("/finance/intercompany/{$tx->id}")->assertRedirect(); |
| 97 | + expect(IntercompanyTransaction::find($tx->id))->toBeNull(); |
| 98 | + expect(IntercompanyTransaction::withTrashed()->find($tx->id))->not->toBeNull(); |
| 99 | +}); |
0 commit comments