-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add frequency iteration logic with accompanying unit tests
Implemented `getRemainingIterations` in the `Frequency` enum to calculate remaining occurrences based on specified frequencies. Added comprehensive unit tests to ensure correct behavior for daily, weekly, monthly, quarterly, yearly, and one-time frequencies.
- Loading branch information
1 parent
d850016
commit 5ff27fc
Showing
2 changed files
with
91 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?php | ||
|
||
use App\Enums\Frequency; | ||
use Illuminate\Support\Carbon; | ||
|
||
it('returns correct remaining occurrences for daily frequency', function () { | ||
$start = Carbon::create(2023, 1, 1); | ||
$end = Carbon::create(2023, 1, 10); | ||
$iterationsLeft = 5; | ||
|
||
$remainingOccurrences = Frequency::DAILY->getRemainingIterations($start, $end, $iterationsLeft); | ||
|
||
expect($remainingOccurrences)->toEqual(5); | ||
}); | ||
|
||
it('returns correct remaining occurrences for weekly frequency', function () { | ||
$start = Carbon::create(2023, 1, 1); | ||
$end = Carbon::create(2023, 2, 1); // 4 weeks | ||
$iterationsLeft = 2; | ||
|
||
$remainingOccurrences = Frequency::WEEKLY->getRemainingIterations($start, $end, $iterationsLeft); | ||
|
||
expect($remainingOccurrences)->toEqual(2); | ||
}); | ||
|
||
it('returns correct remaining occurrences for monthly frequency', function () { | ||
$start = Carbon::create(2023, 1, 1); | ||
$end = Carbon::create(2023, 6, 1); // 5 months | ||
$iterationsLeft = 10; | ||
|
||
$remainingOccurrences = Frequency::MONTHLY->getRemainingIterations($start, $end, $iterationsLeft); | ||
|
||
expect($remainingOccurrences)->toEqual(5); | ||
}); | ||
|
||
it('returns correct remaining occurrences for quarterly frequency', function () { | ||
$start = Carbon::create(2023, 1, 1); | ||
$end = Carbon::create(2024, 1, 1); // 4 quarters | ||
$iterationsLeft = 3; | ||
|
||
$remainingOccurrences = Frequency::QUARTERLY->getRemainingIterations($start, $end, $iterationsLeft); | ||
|
||
expect($remainingOccurrences)->toEqual(3); | ||
}); | ||
|
||
it('returns correct remaining occurrences for yearly frequency', function () { | ||
$start = Carbon::create(2020, 1, 1); | ||
$end = Carbon::create(2025, 1, 1); // 5 years | ||
$iterationsLeft = 2; | ||
|
||
$remainingOccurrences = Frequency::YEARLY->getRemainingIterations($start, $end, $iterationsLeft); | ||
|
||
expect($remainingOccurrences)->toEqual(2); | ||
}); | ||
|
||
it('returns one for once frequency', function () { | ||
$start = Carbon::create(2023, 1, 1); | ||
$end = Carbon::create(2023, 12, 31); | ||
$iterationsLeft = 10; | ||
|
||
$remainingOccurrences = Frequency::ONCE->getRemainingIterations($start, $end, $iterationsLeft); | ||
|
||
expect($remainingOccurrences)->toEqual(1); | ||
}); | ||
|
||
it('returns the minimum of iterations left or the differences', function () { | ||
$start = Carbon::create(2023, 1, 1); | ||
$end = Carbon::create(2023, 1, 5); // 4 days | ||
$iterationsLeft = 2; | ||
|
||
$remainingOccurrences = Frequency::DAILY->getRemainingIterations($start, $end, $iterationsLeft); | ||
|
||
expect($remainingOccurrences)->toEqual(2); | ||
}); |