From c10f629b065af8d53ca6b3bf9969c3789e03fb6c Mon Sep 17 00:00:00 2001 From: chinmaypurav Date: Tue, 24 Sep 2024 22:40:04 +0530 Subject: [PATCH] transfer should have different creditor and debtor accounts --- app/Filament/Resources/TransferResource.php | 2 + .../Transfer/TransferValidationTest.php | 56 +++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 tests/Feature/Transfer/TransferValidationTest.php diff --git a/app/Filament/Resources/TransferResource.php b/app/Filament/Resources/TransferResource.php index b662c11..a627feb 100644 --- a/app/Filament/Resources/TransferResource.php +++ b/app/Filament/Resources/TransferResource.php @@ -50,10 +50,12 @@ public static function form(Form $form): Form Select::make('creditor_id') ->options($accounts) + ->different('debtor_id') ->required(), Select::make('debtor_id') ->options($accounts) + ->different('creditor_id') ->required(), TextInput::make('description') diff --git a/tests/Feature/Transfer/TransferValidationTest.php b/tests/Feature/Transfer/TransferValidationTest.php new file mode 100644 index 0000000..5946af0 --- /dev/null +++ b/tests/Feature/Transfer/TransferValidationTest.php @@ -0,0 +1,56 @@ +user = User::factory()->create(); + $this->actingAs($this->user); + Carbon::setTestNow(now()); +}); + +it('cannot have same account as debtor and creditor on save', function () { + $newData = Transfer::factory()->for($this->user)->make(); + $account = Account::factory()->create(); + + livewire(TransferResource\Pages\CreateTransfer::class) + ->fillForm([ + 'debtor_id' => $account->id, + 'creditor_id' => $account->id, + 'description' => $newData->description, + 'amount' => $newData->amount, + 'transacted_at' => $newData->transacted_at, + ]) + ->call('create') + ->assertHasFormErrors([ + 'creditor_id', + 'debtor_id', + ]); +}); + +it('cannot have same account as debtor and creditor on update', function () { + $account = Account::factory()->create(); + + $transfer = Transfer::factory()->for($this->user)->create(); + + livewire(TransferResource\Pages\EditTransfer::class, [ + 'record' => $transfer->getRouteKey(), + ]) + ->fillForm([ + 'creditor_id' => $account->id, + 'debtor_id' => $account->id, + ]) + ->call('save') + ->assertHasFormErrors([ + 'creditor_id', + 'debtor_id', + ]); +});