Skip to content

Commit

Permalink
Add LocalDateRange::withStart() / withEnd()
Browse files Browse the repository at this point in the history
  • Loading branch information
BenMorel committed Jun 29, 2021
1 parent 50e7baa commit 4d0a530
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/LocalDateRange.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,22 @@ public function getIntersectionWith(LocalDateRange $that) : LocalDateRange
return new LocalDateRange($intersectStart, $intersectEnd);
}

/**
* @throws DateTimeException If the start date is after the end date.
*/
public function withStart(LocalDate $start): LocalDateRange
{
return LocalDateRange::of($start, $this->end);
}

/**
* @throws DateTimeException If the end date is before the start date.
*/
public function withEnd(LocalDate $end): LocalDateRange
{
return LocalDateRange::of($this->start, $end);
}

/**
* Returns an iterator for all the dates contained in this range.
*
Expand Down
61 changes: 61 additions & 0 deletions tests/LocalDateRangeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -295,4 +295,65 @@ public function testGetIntersectionInvalidParams(): void

$aRange->getIntersectionWith($bRange);
}

/**
* @dataProvider providerWithStart
*/
public function testWithStart(string $originalRange, string $start, ?string $expectedRange): void
{
$originalRange = LocalDateRange::parse($originalRange);

if ($expectedRange === null) {
$this->expectException(DateTimeException::class);
}

$actualRange = $originalRange->withStart(LocalDate::parse($start));

if ($expectedRange !== null) {
$this->assertSame($expectedRange, (string) $actualRange);
}
}

public function providerWithStart(): array
{
return [
['2021-06-15/2021-07-07', '2021-05-29', '2021-05-29/2021-07-07'],
['2021-06-15/2021-07-07', '2021-06-14', '2021-06-14/2021-07-07'],
['2021-06-15/2021-07-07', '2021-06-15', '2021-06-15/2021-07-07'],
['2021-06-15/2021-07-07', '2021-06-16', '2021-06-16/2021-07-07'],
['2021-06-15/2021-07-07', '2021-07-06', '2021-07-06/2021-07-07'],
['2021-06-15/2021-07-07', '2021-07-07', '2021-07-07/2021-07-07'],
['2021-06-15/2021-07-07', '2021-07-08', null],
];
}

/**
* @dataProvider providerWithEnd
*/
public function testWithEnd(string $originalRange, string $end, ?string $expectedRange): void
{
$originalRange = LocalDateRange::parse($originalRange);

if ($expectedRange === null) {
$this->expectException(DateTimeException::class);
}

$actualRange = $originalRange->withEnd(LocalDate::parse($end));

if ($expectedRange !== null) {
$this->assertSame($expectedRange, (string) $actualRange);
}
}

public function providerWithEnd(): array
{
return [
['2021-06-15/2021-07-07', '2021-06-14', null],
['2021-06-15/2021-07-07', '2021-06-15', '2021-06-15/2021-06-15'],
['2021-06-15/2021-07-07', '2021-06-16', '2021-06-15/2021-06-16'],
['2021-06-15/2021-07-07', '2021-07-06', '2021-06-15/2021-07-06'],
['2021-06-15/2021-07-07', '2021-07-07', '2021-06-15/2021-07-07'],
['2021-06-15/2021-07-07', '2021-07-08', '2021-06-15/2021-07-08'],
];
}
}

0 comments on commit 4d0a530

Please sign in to comment.