Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions lib/Date.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@ public function isAfter(self $other): bool
return $this->toDateTime() > $other->toDateTime();
}

public function isAfterOrEqualTo(self $other): bool
{
return $this->isAfter($other) || $this->equals($other);
}

public function isBetween(self $start, self $end): bool
{
return !$this->isBefore($start) && !$this->isAfter($end);
Expand Down
53 changes: 40 additions & 13 deletions tests/unit/DateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ public static function provideValidIntegerComponentsAndExpectedStringRepresentat
];
}

#[DataProvider('provideValidIntegerComponentsAndExpectedStringRepresentationsData')]
#[Test]
#[DataProvider('provideValidIntegerComponentsAndExpectedStringRepresentationsData')]
public function it_can_be_constructed_from_integer_components(
int $year,
int $month,
Expand Down Expand Up @@ -87,8 +87,8 @@ public static function provideDateTimesAndExpectedStringRepresentationsData(): i
}
}

#[DataProvider('provideDateTimesAndExpectedStringRepresentationsData')]
#[Test]
#[DataProvider('provideDateTimesAndExpectedStringRepresentationsData')]
public function it_can_be_constructed_from_a_date_time_instance(
\DateTimeImmutable $dateTime,
string $expectedStringRepresentation
Expand Down Expand Up @@ -117,8 +117,8 @@ public static function provideDateTimeStringsAndExpectedStringRepresentationsDat
];
}

#[DataProvider('provideDateTimeStringsAndExpectedStringRepresentationsData')]
#[Test]
#[DataProvider('provideDateTimeStringsAndExpectedStringRepresentationsData')]
public function it_can_be_constructed_from_a_formatted_string(
string $dateTimeString,
string $expectedStringRepresentation
Expand All @@ -142,8 +142,8 @@ public static function provideInvalidYearMonthDayStrings(): iterable
yield ['2024'];
}

#[DataProvider('provideInvalidYearMonthDayStrings')]
#[Test]
#[DataProvider('provideInvalidYearMonthDayStrings')]
public function it_throws_when_constructing_from_a_formatted_string_if_invalid_format(string $value): void
{
$this->expectException(\InvalidArgumentException::class);
Expand Down Expand Up @@ -219,8 +219,8 @@ public function it_equals_other_dates_with_the_same_value(): void
$this->assertFalse($dateDifferentValue3->equals($date));
}

#[DataProvider('provideDatesForDayAfterIncrementData')]
#[Test]
#[DataProvider('provideDatesForDayAfterIncrementData')]
public function it_will_correctly_increment_for_following_day(string $currentDate, string $expectedDate): void
{
$this->assertSame(
Expand All @@ -241,8 +241,8 @@ public static function provideDatesForDayAfterIncrementData(): iterable
yield ['2020-04-30', '2020-05-01'];
}

#[DataProvider('provideDatesForDayBeforeIncrementData')]
#[Test]
#[DataProvider('provideDatesForDayBeforeIncrementData')]
public function it_will_correctly_increment_for_previous_day(string $currentDate, string $expectedDate): void
{
$this->assertSame(
Expand All @@ -261,8 +261,8 @@ public static function provideDatesForDayBeforeIncrementData(): iterable
}
}

#[DataProvider('provideValuesForBeforeComparisonData')]
#[Test]
#[DataProvider('provideValuesForBeforeComparisonData')]
public function it_will_return_correct_values_for_before_comparison(
string $before,
string $after,
Expand All @@ -288,8 +288,8 @@ public static function provideValuesForBeforeComparisonData(): iterable
yield ['2019-01-02', '2019-01-01', false];
}

#[DataProvider('provideValuesForBeforeOrEqualToComparisonData')]
#[Test]
#[DataProvider('provideValuesForBeforeOrEqualToComparisonData')]
public function it_will_return_correct_values_for_before_or_equal_to_comparison(
string $before,
string $after,
Expand All @@ -315,8 +315,8 @@ public static function provideValuesForBeforeOrEqualToComparisonData(): iterable
yield ['2019-01-02', '2019-01-01', false];
}

#[DataProvider('provideValuesForAfterComparisonData')]
#[Test]
#[DataProvider('provideValuesForAfterComparisonData')]
public function it_will_return_correct_values_for_after_comparison(
string $before,
string $after,
Expand All @@ -342,6 +342,33 @@ public static function provideValuesForAfterComparisonData(): iterable
yield ['2019-01-02', '2019-01-01', true];
}

#[Test]
#[DataProvider('provideValuesForAfterOrEqualToComparisonData')]
public function it_will_return_correct_values_for_after_or_equal_to_comparison(
string $before,
string $after,
bool $expectation
): void {
$this->assertSame(
$expectation,
Date::fromYearMonthDayString($before)->isAfterOrEqualTo(Date::fromYearMonthDayString($after))
);
}

/**
* @return iterable<array{string, string, bool}>
*/
public static function provideValuesForAfterOrEqualToComparisonData(): iterable
{
yield ['2019-01-01', '2019-02-01', false];
yield ['2020-01-01', '2021-01-01', false];
yield ['2019-01-01', '2019-01-02', false];
yield ['2018-01-01', '2018-01-01', true];
yield ['2019-02-01', '2019-01-01', true];
yield ['2021-01-01', '2020-01-01', true];
yield ['2019-01-02', '2019-01-01', true];
}

#[Test]
public function it_will_return_correct_values_for_between_comparison(): void
{
Expand All @@ -355,8 +382,8 @@ public function it_will_return_correct_values_for_between_comparison(): void
$this->assertFalse(Date::fromYearMonthDay(2019, 7, 29)->isBetween($startDate, $endDate));
}

#[DataProvider('provideDatesAndDayCountsForDiffComparisonData')]
#[Test]
#[DataProvider('provideDatesAndDayCountsForDiffComparisonData')]
public function it_will_calculate_difference_between_two_days(
string $start,
string $end,
Expand Down Expand Up @@ -406,8 +433,8 @@ public static function provideNumberOfDaysOffsets(): iterable
yield [10];
}

#[DataProvider('provideNumberOfDaysOffsets')]
#[Test]
#[DataProvider('provideNumberOfDaysOffsets')]
public function it_can_be_offset_by_days(int $days): void
{
$date = Date::fromYearMonthDay(2022, 5, 1);
Expand Down Expand Up @@ -437,8 +464,8 @@ public static function provideNumberOfMonthsOffsets(): iterable
yield [10];
}

#[DataProvider('provideNumberOfMonthsOffsets')]
#[Test]
#[DataProvider('provideNumberOfMonthsOffsets')]
public function it_can_be_offset_by_months(int $days): void
{
$date = Date::fromYearMonthDay(2022, 5, 1);
Expand Down Expand Up @@ -468,8 +495,8 @@ public static function provideNumberOfYearsOffsets(): iterable
yield [10];
}

#[DataProvider('provideNumberOfYearsOffsets')]
#[Test]
#[DataProvider('provideNumberOfYearsOffsets')]
public function it_can_be_offset_by_years(int $days): void
{
$date = Date::fromYearMonthDay(2022, 5, 1);
Expand Down