-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #56 from hughgrigg/54-diff-business-start-after-end
Improve negative interval handling
- Loading branch information
Showing
3 changed files
with
99 additions
and
2 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
namespace BusinessTime\Tests\Unit\BusinessTime; | ||
|
||
use BusinessTime\BusinessTime; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
/** | ||
* Test the BusinessTime::diffBusiness() method. | ||
*/ | ||
class DiffBusinessTest extends TestCase | ||
{ | ||
/** | ||
* Should be able to handle start date being after end date. | ||
*/ | ||
public function testStartAfterEndAbsolute() | ||
{ | ||
// Given we have a business time as a start; | ||
$start = new BusinessTime('Monday 11am'); | ||
|
||
// And an end time that is before that; | ||
$end = new BusinessTime('Monday 10am'); | ||
|
||
// When we get the absolute difference in business time; | ||
$diff = $start->diffBusiness($end); | ||
|
||
// Then it should still be correct. | ||
self::assertEquals(60, $diff->inMinutes()); | ||
} | ||
|
||
/** | ||
* Should be able to handle start date being after end date. | ||
*/ | ||
public function testStartAfterEndNonAbsolute() | ||
{ | ||
// Given we have a business time as a start; | ||
$start = new BusinessTime('Monday 11am'); | ||
|
||
// And an end time that is before that; | ||
$end = new BusinessTime('Monday 10am'); | ||
|
||
// When we get the non-absolute difference in business time; | ||
$diff = $start->diffBusiness($end, false); | ||
|
||
// Then it should still be correct (note weirdness with how DateInterval | ||
// handles negatives and units). | ||
self::assertEquals(-3600, $diff->seconds); | ||
self::assertEquals(60, $diff->inMinutes()); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
namespace BusinessTime\Tests\Unit\Interval; | ||
|
||
use BusinessTime\Interval; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class IntervalTest extends TestCase | ||
{ | ||
public function testPositiveInterval() | ||
{ | ||
// Given we have a positive number of seconds; | ||
$seconds = 3600; | ||
|
||
// When we construct and interval with it; | ||
/** @var Interval $interval */ | ||
$interval = Interval::seconds($seconds); | ||
|
||
// Then it should be correct. | ||
self::assertEquals(60, $interval->inMinutes()); | ||
} | ||
|
||
public function testNegativeInterval() | ||
{ | ||
// Given we have a negative number of seconds; | ||
$seconds = -3600; | ||
|
||
// When we construct and interval with it; | ||
/** @var Interval $interval */ | ||
$interval = Interval::seconds(0); | ||
$interval->seconds = $seconds; | ||
$interval->invert = 1; | ||
|
||
// Then it should be correct. | ||
self::assertEquals(-3600, $interval->seconds); | ||
self::assertEquals(60, $interval->inMinutes()); | ||
} | ||
} |