Changed isNewDay to accept null instead of DateTime

Fixes #93
This commit is contained in:
Vassyli
2017-03-30 11:00:10 +02:00
parent 5668c08f45
commit 39b9ec318a
2 changed files with 25 additions and 9 deletions
+6 -8
View File
@@ -46,13 +46,15 @@ class TimeKeeper
/**
* Returns whether a user who is interating with the game now and last
* interacted at $lastInteractionTime should experience a New Day event.
* @param DateTime $lastInteractionTime
* @param DateTime|null $lastInteractionTime
* @return bool
*/
public function isNewDay(DateTime $lastInteractionTime): bool
public function isNewDay(?DateTime $lastInteractionTime): bool
{
if ($lastInteractionTime == null) {
return true;
}
$t1 = $this->gameTime();
$t2 = $this->convertToGameTime($lastInteractionTime);
$d1 = $t1->format("Y-m-d");
@@ -75,9 +77,7 @@ class TimeKeeper
* @param DateTime $time Game time to convert.
* @return DateTime Real time corresponding to game time $time.
*/
public function convertFromGameTime(
DateTime $time
): DateTime {
public function convertFromGameTime(DateTime $time): DateTime {
// Game dates are in the distant past, better not use getTimestamp().
$i = $this->theBeginning->diff($time);
@@ -96,9 +96,7 @@ class TimeKeeper
* @param DateTime $time Real time to convert.
* @return DateTime Game time corresponding to real time $time.
*/
public function convertToGameTime(
DateTime $time
): DateTime {
public function convertToGameTime(DateTime $time): DateTime {
$timeUnix = $time->getTimestamp();
$epochUnix = $this->adjustedEpoch->getTimestamp();
+19 -1
View File
@@ -63,7 +63,25 @@ class TimeKeeperTests extends \PHPUnit_Framework_TestCase {
$this->assertEquals('0000-01-02', $converted->format('Y-m-d'));
}
public function testDetectsNewDay() {
public function testIfIsNewDayReturnsTrueWithNullAsLastInteractionTime() {
$keeper = new TimeKeeper($this->gameEpoch, $this->gameOffsetSeconds, 4);
$this->assertTrue($keeper->isNewDay(null));
}
public function testIfIsNewDayReturnsFalseIfLastInteractionTimeWasJustRecently()
{
// game days per day: 4, each day 6h.
$keeper = new TimeKeeper($this->gameEpoch, $this->gameOffsetSeconds, 4);
$now = new \DateTime();
$nowMinus1Second = $now->sub(new \DateInterval("PT1S"));
$newMinus1Minute = $now->sub(new \DateInterval("PT1M"));
$newMinus1Hour = $now->sub(new \DateInterval("PT1H"));
$oldPLus1H = $now->sub(new \DateInterval("PT5H59M59S"));
$this->assertFalse($keeper->isNewDay($now));
$this->assertFalse($keeper->isNewDay($nowMinus1Second));
}
public function testConvertToRespectsGameOffset() {