From f281e89f122eb6179a33c6ec12599a6b698f5933 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yunus=20Emre=20Delig=C3=B6z?= Date: Sun, 7 Apr 2024 22:38:59 +0300 Subject: [PATCH] test(LoanService): refactor loan repayment in unit tests 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. --- tests/Unit/Services/LoanServiceTest.php | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/tests/Unit/Services/LoanServiceTest.php b/tests/Unit/Services/LoanServiceTest.php index 6ddb4cd..b39d462 100644 --- a/tests/Unit/Services/LoanServiceTest.php +++ b/tests/Unit/Services/LoanServiceTest.php @@ -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; @@ -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);