Files
dualfroz 5763d58d02 fix: return 0 from length() when excluded boundaries leave a period empty
Excluding the boundaries of a one or two unit long period pushes the
included start past the included end. diff()->days is unsigned and the
HOUR/MINUTE/SECOND branch applied abs() outright, so that backwards range
was measured as a positive distance and the unconditional +1 inflated it
further: a one day period reported 1, 2, 2 and 3 for EXCLUDE_NONE, START,
END and ALL, while iterating the same objects yielded nothing.

Return 0 up front when the included start is past the included end. This
matches what the MONTH and YEAR precisions already returned through
iterator_count(), and lets the now unreachable abs() go.
2026-09-03 14:39:22 +02:00

64 lines
2.4 KiB
PHP

<?php
use Spatie\Period\Boundaries;
use Spatie\Period\Period;
it('exclude none', function () {
$period = Period::make('2018-01-01', '2018-01-31', boundaries: Boundaries::EXCLUDE_NONE());
expect($period->isStartExcluded())->toBeFalse();
expect($period->isEndExcluded())->toBeFalse();
});
it('exclude start', function () {
$period = Period::make('2018-01-01', '2018-01-31', boundaries: Boundaries::EXCLUDE_START());
expect($period->isStartExcluded())->toBeTrue();
expect($period->isEndExcluded())->toBeFalse();
});
it('exclude end', function () {
$period = Period::make('2018-01-01', '2018-01-31', boundaries: Boundaries::EXCLUDE_END());
expect($period->isStartExcluded())->toBeFalse();
expect($period->isEndExcluded())->toBeTrue();
});
it('exclude all', function () {
$period = Period::make('2018-01-01', '2018-01-31', boundaries: Boundaries::EXCLUDE_ALL());
expect($period->isStartExcluded())->toBeTrue();
expect($period->isEndExcluded())->toBeTrue();
});
it('length with boundaries', function ($expectedAmount, Period $period) {
expect($period->length())->toEqual($expectedAmount);
})->with('periods_with_amounts_of_included_dates');
it('length is zero when excluding boundaries leaves nothing', function (Period $period) {
expect(iterator_count($period))->toEqual(0);
expect($period->length())->toEqual(0);
})->with('periods_left_empty_by_excluded_boundaries');
it('overlap with excluded boundaries', function () {
$a = Period::make('2018-01-01', '2018-01-05', boundaries: Boundaries::EXCLUDE_END());
$b = Period::make('2018-01-05', '2018-01-10');
expect($a->overlapsWith($b))->toBeFalse();
$a = Period::make('2018-01-01', '2018-01-05');
$b = Period::make('2018-01-05', '2018-01-10', boundaries: Boundaries::EXCLUDE_START());
expect($a->overlapsWith($b))->toBeFalse();
$a = Period::make('2018-01-01', '2018-01-05');
$b = Period::make('2018-01-05', '2018-01-10');
expect($a->overlapsWith($b))->toBeTrue();
});
it('subtract with boundaries', function (string $period, string $subtract, string $result) {
expect(Period::fromString($period)->subtract(Period::fromString($subtract))[0]->asString())->toEqual($result);
})->with('boundaries_for_subtract');
it('overlap with boundaries', function (string $period, string $overlap, string $result) {
expect(Period::fromString($period)->overlap(Period::fromString($overlap))->asString())->toEqual($result);
})->with('boundaries_for_overlap');