Created
November 16, 2015 08:50
-
-
Save luxifer/e0ded6be1393717affe6 to your computer and use it in GitHub Desktop.
period overlap in php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ ./bin/phpunit overlap_test.php | |
PHPUnit 5.0.8 by Sebastian Bergmann and contributors. | |
............. 13 / 13 (100%) | |
Time: 140 ms, Memory: 3.50Mb | |
OK (13 tests, 13 assertions) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Period | |
{ | |
public $startDate; | |
public $endDate; | |
public function __construct(\DateTime $startDate, \DateTime $endDate) | |
{ | |
$this->startDate = $startDate; | |
$this->endDate = $endDate; | |
} | |
public function overlap(Period $period) | |
{ | |
return $this->endDate > $period->startDate && $this->startDate < $period->endDate; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require __DIR__.'/vendor/autoload.php'; | |
require __DIR__.'/overlap.php'; | |
class PeriodTest extends \PHPUnit_Framework_TestCase | |
{ | |
protected static $reference; | |
protected function setUp() | |
{ | |
static::$reference = new \Period(new \DateTime('2015-11-16'), new \DateTime('2015-11-23')); | |
} | |
/** | |
* @dataProvider overlapPeriod | |
*/ | |
public function testOverlap($period) | |
{ | |
$this->assertTrue(static::$reference->overlap($period)); | |
} | |
/** | |
* @dataProvider doNotOverlapPeriod | |
*/ | |
public function testDoesNotOverlap($period) | |
{ | |
$this->assertFalse(static::$reference->overlap($period)); | |
} | |
public function doNotOverlapPeriod() | |
{ | |
return [ | |
[new \Period(new \DateTime('2015-11-07'), new \DateTime('2015-11-13'))], | |
[new \Period(new \DateTime('2015-11-25'), new \DateTime('2015-11-31'))], | |
[new \Period(new \DateTime('2015-11-07'), new \DateTime('2015-11-16'))], | |
[new \Period(new \DateTime('2015-11-23'), new \DateTime('2015-11-31'))], | |
]; | |
} | |
public function overlapPeriod() | |
{ | |
return [ | |
[new \Period(new \DateTime('2015-11-07'), new \DateTime('2015-11-18'))], | |
[new \Period(new \DateTime('2015-11-18'), new \DateTime('2015-11-25'))], | |
[new \Period(new \DateTime('2015-11-16'), new \DateTime('2015-11-18'))], | |
[new \Period(new \DateTime('2015-11-16'), new \DateTime('2015-11-25'))], | |
[new \Period(new \DateTime('2015-11-16'), new \DateTime('2015-11-23'))], | |
[new \Period(new \DateTime('2015-11-07'), new \DateTime('2015-11-23'))], | |
[new \Period(new \DateTime('2015-11-18'), new \DateTime('2015-11-23'))], | |
[new \Period(new \DateTime('2015-11-18'), new \DateTime('2015-11-20'))], | |
[new \Period(new \DateTime('2015-11-13'), new \DateTime('2015-11-25'))], | |
]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment