Skip to content

Instantly share code, notes, and snippets.

@ShrwdFlrst
Created January 5, 2021 19:08
Show Gist options
  • Select an option

  • Save ShrwdFlrst/2ba9c562004a6f7bd704077714cf8998 to your computer and use it in GitHub Desktop.

Select an option

Save ShrwdFlrst/2ba9c562004a6f7bd704077714cf8998 to your computer and use it in GitHub Desktop.
Working days date arithmetic test
<?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;
}
}
@ShrwdFlrst
Copy link
Copy Markdown
Author

Output
Screenshot 2021-01-05 at 19 09 04

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment