Skip to content

Commit

Permalink
test(LoanService): refactor loan repayment in unit tests
Browse files Browse the repository at this point in the history
Refactor the way the loan repayments are performed in the unit tests of LoanService. The `scheduledRepayments` method is replaced with `Collection::times` for better readability and efficiency. The expected number of repayments is also updated accordingly.
  • Loading branch information
deligoez committed Apr 7, 2024
1 parent d6d8d01 commit f281e89
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions tests/Unit/Services/LoanServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Illuminate\Support\Carbon;
use App\Models\ReceivedRepayment;
use App\Models\ScheduledRepayment;
use Illuminate\Support\Collection;
use PHPUnit\Framework\Attributes\Test;
use App\Exceptions\AlreadyRepaidException;
use PHPUnit\Framework\Attributes\DataProvider;
Expand Down Expand Up @@ -249,19 +250,17 @@ public function can_pay_multiple_scheduled_payments_on_the_same_day(): void
);

// 2. Act
$loan->scheduledRepayments()->each(callback: function (ScheduledRepayment $scheduledRepayment) use ($loan): void {
LoanFacade::repayLoan(
loan: $loan,
amount: $scheduledRepayment->amount,
currencyCode: $loan->currency_code,
receivedAt: now()
);
});
Collection::times(number: 6, callback: fn () => LoanFacade::repayLoan(
loan: $loan,
amount: 1000,
currencyCode: $loan->currency_code,
receivedAt: now()
));

// 3. Assert
$this->assertEquals(expected: PaymentStatus::REPAID, actual: $loan->status);

$this->assertEquals(expected: 3, actual: $loan->receivedRepayments()->count());
$this->assertEquals(expected: 6, actual: $loan->receivedRepayments()->count());

$loan->scheduledRepayments()->each(callback: function (ScheduledRepayment $scheduledRepayment): void {
$this->assertEquals(expected: PaymentStatus::REPAID, actual: $scheduledRepayment->status);
Expand Down

0 comments on commit f281e89

Please sign in to comment.