Created
January 5, 2021 19:08
-
-
Save ShrwdFlrst/2ba9c562004a6f7bd704077714cf8998 to your computer and use it in GitHub Desktop.
Working days date arithmetic test
This file contains hidden or 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
| <?php | |
| use Carbon\CarbonImmutable; | |
| use Carbon\CarbonPeriod; | |
| use PHPUnit\Framework\TestCase; | |
| class DatesTest extends TestCase | |
| { | |
| /** | |
| * @param CarbonImmutable $today | |
| * @dataProvider datesProvider | |
| */ | |
| public function test_date_arithmetic(CarbonImmutable $today): void | |
| { | |
| $transformed = $today | |
| ->subYearNoOverflow() | |
| ->addBusinessDays(18) | |
| ->toImmutable(); | |
| $reverted = $transformed->subBusinessDays(18)->addYear(); | |
| echo PHP_EOL . $today->format('Y-m-d D') . ' - ' . $reverted->format('Y-m-d D') . ' = ' . $today->diffInDays($reverted) . ' days'; | |
| self::assertLessThanOrEqual(7, $today->diffInDays($reverted)); | |
| self::assertTrue($today->greaterThanOrEqualTo($reverted), 'Failed on ' . $today->format('Y-m-d D')); | |
| echo PHP_EOL; | |
| } | |
| public function datesProvider() | |
| { | |
| $dates = []; | |
| $period = CarbonPeriod::create('2020-01-01', '2028-12-31'); | |
| foreach ($period as $today) { | |
| $today = $today->toImmutable(); | |
| $dates[] = [$today]; | |
| } | |
| return $dates; | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output
