Skip to content

Commit

Permalink
Merge pull request #7 from chinmaypurav/fix/no-future-dates-allowed-f…
Browse files Browse the repository at this point in the history
…or-normal-transactions

Feat: no future dates allowed for normal transactions
  • Loading branch information
chinmaypurav authored Sep 28, 2024
2 parents e65996f + 411ffe2 commit 5cfefd7
Show file tree
Hide file tree
Showing 8 changed files with 186 additions and 2 deletions.
4 changes: 3 additions & 1 deletion app/Filament/Concerns/IncomeExpenseResourceTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ public static function form(Form $form): Form

DateTimePicker::make('transacted_at')
->label('Transacted Date')
->default(now()),
->default(now())
->beforeOrEqual(now())
->required(),

TextInput::make('amount')
->required()
Expand Down
3 changes: 2 additions & 1 deletion app/Filament/Resources/AccountResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ public static function form(Form $form): Form

DatePicker::make('initial_date')
->label('Initial Balance Date')
->default(now())
->default(today())
->beforeOrEqual(today())
->required(),
]);
}
Expand Down
7 changes: 7 additions & 0 deletions database/factories/AccountFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,11 @@ public function definition(): array
'user_id' => User::factory(),
];
}

public function tomorrow(): self
{
return $this->state(fn (array $attributes) => [
'initial_date' => Carbon::now()->addDay(),
]);
}
}
7 changes: 7 additions & 0 deletions database/factories/ExpenseFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,11 @@ public function today(): self
'transacted_at' => Carbon::now(),
]);
}

public function tomorrow(): self
{
return $this->state(fn (array $attributes) => [
'transacted_at' => Carbon::now()->addDay(),
]);
}
}
7 changes: 7 additions & 0 deletions database/factories/IncomeFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,11 @@ public function today(): self
'transacted_at' => Carbon::now(),
]);
}

public function tomorrow(): self
{
return $this->state(fn (array $attributes) => [
'transacted_at' => Carbon::now()->addDay(),
]);
}
}
48 changes: 48 additions & 0 deletions tests/Feature/Account/AccountValidationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

use App\Filament\Resources\AccountResource;
use App\Models\Account;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Carbon;

use function Pest\Livewire\livewire;

uses(RefreshDatabase::class);

beforeEach(function () {
$this->user = User::factory()->create();
$this->actingAs($this->user);
Carbon::setTestNow(now());
});

it('cannot add future date as initial date', function () {
$newData = Account::factory()->tomorrow()->make();

livewire(AccountResource\Pages\CreateAccount::class)
->fillForm([
'name' => $newData->name,
'account_type' => $newData->account_type,
'current_balance' => $newData->current_balance,
'initial_date' => $newData->initial_date,
])
->call('create')
->assertHasFormErrors([
'initial_date',
]);
});

it('cannot update future date as initial date', function () {
$account = Account::factory()->for($this->user)->create();

livewire(AccountResource\Pages\EditAccount::class, [
'record' => $account->getRouteKey(),
])
->fillForm([
'initial_date' => today()->addDay(),
])
->call('save')
->assertHasFormErrors([
'initial_date',
]);
});
56 changes: 56 additions & 0 deletions tests/Feature/Expense/ExpenseValidationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

use App\Filament\Resources\ExpenseResource;
use App\Models\Account;
use App\Models\Category;
use App\Models\Expense;
use App\Models\Person;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Carbon;

use function Pest\Livewire\livewire;

uses(RefreshDatabase::class);

beforeEach(function () {
$this->user = User::factory()->create();
$this->actingAs($this->user);
Carbon::setTestNow(now());
});

it('cannot add future date as transacted_at', function () {
$newData = Expense::factory()->tomorrow()->make();
$account = Account::factory()->create();
$person = Person::factory()->create();
$category = Category::factory()->create();

livewire(ExpenseResource\Pages\CreateExpense::class)
->fillForm([
'description' => $newData->description,
'person_id' => $person->id,
'account_id' => $account->id,
'category_id' => $category->id,
'amount' => $newData->amount,
'transacted_at' => $newData->transacted_at,
])
->call('create')
->assertHasFormErrors([
'transacted_at',
]);
});

it('cannot update future date as transacted_at', function () {
$expense = Expense::factory()->for($this->user)->create();

livewire(ExpenseResource\Pages\EditExpense::class, [
'record' => $expense->getRouteKey(),
])
->fillForm([
'transacted_at' => today()->addDay(),
])
->call('save')
->assertHasFormErrors([
'transacted_at',
]);
});
56 changes: 56 additions & 0 deletions tests/Feature/Income/IncomeValidationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

use App\Filament\Resources\IncomeResource;
use App\Models\Account;
use App\Models\Category;
use App\Models\Income;
use App\Models\Person;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Carbon;

use function Pest\Livewire\livewire;

uses(RefreshDatabase::class);

beforeEach(function () {
$this->user = User::factory()->create();
$this->actingAs($this->user);
Carbon::setTestNow(now());
});

it('cannot add future date as transacted_at', function () {
$newData = Income::factory()->tomorrow()->make();
$account = Account::factory()->create();
$person = Person::factory()->create();
$category = Category::factory()->create();

livewire(IncomeResource\Pages\CreateIncome::class)
->fillForm([
'description' => $newData->description,
'person_id' => $person->id,
'account_id' => $account->id,
'category_id' => $category->id,
'amount' => $newData->amount,
'transacted_at' => $newData->transacted_at,
])
->call('create')
->assertHasFormErrors([
'transacted_at',
]);
});

it('cannot update future date as transacted_at', function () {
$income = Income::factory()->for($this->user)->create();

livewire(IncomeResource\Pages\EditIncome::class, [
'record' => $income->getRouteKey(),
])
->fillForm([
'transacted_at' => today()->addDay(),
])
->call('save')
->assertHasFormErrors([
'transacted_at',
]);
});

0 comments on commit 5cfefd7

Please sign in to comment.