Skip to content

Commit

Permalink
Fix bug wit null element in diff
Browse files Browse the repository at this point in the history
  • Loading branch information
brendt committed Feb 1, 2019
1 parent 4b0145b commit 40f1f98
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

All notable changes to `period` will be documented in this file

## 1.1.1 - 2019-02-01

- Fix bug with null element in diff

## 1.1.0 - 2019-01-26

- Make Period iterable
Expand Down
6 changes: 5 additions & 1 deletion src/Period.php
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,11 @@ public function diff(Period ...$periods): PeriodCollection
if (count($periods) === 1 && ! $this->overlapsWith($periods[0])) {
$collection = new PeriodCollection();

$collection[] = $this->gap($periods[0]);
$gap = $this->gap($periods[0]);

if ($gap !== null) {
$collection[] = $gap;
}

return $collection;
}
Expand Down
18 changes: 15 additions & 3 deletions tests/PeriodTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ public function it_can_determine_if_two_periods_touch_each_other()
{
$this->assertTrue(
Period::make('2018-01-01', '2018-01-01')
->touchesWith(Period::make('2018-01-02', '2018-01-02'))
->touchesWith(Period::make('2018-01-02', '2018-01-02'))
);

$this->assertTrue(
Period::make('2018-01-02', '2018-01-02')
->touchesWith(Period::make('2018-01-01', '2018-01-01'))
->touchesWith(Period::make('2018-01-01', '2018-01-01'))
);

$this->assertFalse(
Expand All @@ -48,7 +48,7 @@ public function it_can_determine_if_two_periods_touch_each_other()

$this->assertFalse(
Period::make('2018-01-03', '2018-01-03')
->touchesWith(Period::make('2018-01-01', '2018-01-01'))
->touchesWith(Period::make('2018-01-01', '2018-01-01'))
);
}

Expand Down Expand Up @@ -523,6 +523,18 @@ public function its_iterator_returns_immutable_dates()
$this->assertInstanceOf(DateTimeImmutable::class, current($period));
}

/** @test */
public function diff_filters_out_null_object_if_no_gap()
{
$a = Period::make('2019-02-01', '2019-02-01');

$b = Period::make('2019-02-02', '2019-02-02');

$diff = $a->diff($b);

$this->assertEmpty($diff);
}

public function expectedPeriodLengths()
{
return [
Expand Down

0 comments on commit 40f1f98

Please sign in to comment.