Skip to content

Commit

Permalink
Add failing test case for range subtraction and fix it
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Stříbrný committed Jun 7, 2017
1 parent 657258b commit f9a410d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/DateRange.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,9 @@ public function subtract($b)
}

if (
($b->from && $this->includes($b->from))
($b->from && $this->from != $b->from && $this->includes($b->from))
||
($this->to && $b->includes($this->to))
($this->to && $this->to != $b->to && $b->includes($this->to))
) {
$from = $this->from;
$to = (clone $b->from)->modify('-1 day');
Expand Down
7 changes: 6 additions & 1 deletion tests/DateRange/Subtract.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ $range2 = \Danoha\DateRange::wrap([NULL, new \DateTime('2017-12-01'),]);
$range3 = \Danoha\DateRange::wrap([new \DateTime('2017-01-01'), NULL,]);
$range4 = \Danoha\DateRange::wrap([new \DateTime('2016-06-01'), new \DateTime('2017-05-31'),]);
$range5 = \Danoha\DateRange::wrap([new \DateTime('2017-06-01'), new \DateTime('2018-05-31'),]);
$range6 = \Danoha\DateRange::wrap([new \DateTime('2017-01-01'), new \DateTime('2017-05-31'),]);

Assert::equal([
['from' => new \DateTime('2017-12-02'), 'to' => NULL,],
Expand All @@ -34,4 +35,8 @@ Assert::same([], $range1->subtract($range1)->unwrap());

Assert::equal([
['from' => new \DateTime('2016-06-01'), 'to' => new \DateTime('2017-05-31'),],
], $range4->subtract($range5)->unwrap());
], $range4->subtract($range5)->unwrap());

Assert::equal([
['from' => new \DateTime('2017-06-01'), 'to' => NULL,],
], $range3->subtract($range6)->unwrap());

0 comments on commit f9a410d

Please sign in to comment.