From f201784291d09b9b7629bbc50d5449c3299679cf Mon Sep 17 00:00:00 2001 From: Vassyli Date: Thu, 30 Mar 2017 11:03:45 +0200 Subject: [PATCH] Changed TimeKeeper to keep a permanent "now" state. --- src/TimeKeeper.php | 19 ++++++++++++++++--- tests/TimeKeeperTest.php | 2 +- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/src/TimeKeeper.php b/src/TimeKeeper.php index ac6af89..050bc72 100644 --- a/src/TimeKeeper.php +++ b/src/TimeKeeper.php @@ -22,6 +22,8 @@ class TimeKeeper private $secondsPerGameMinute; private $secondsPerGameSecond; + private $now; + /** * Construct a TimeKeeper with required configuration. * @param DateTime $gameEpoch When in real time is game day 0. @@ -41,6 +43,17 @@ class TimeKeeper $this->secondsPerGameHour = $this->secondsPerGameDay / 24; $this->secondsPerGameMinute = $this->secondsPerGameHour / 60; $this->secondsPerGameSecond = $this->secondsPerGameMinute / 60; + + $this->now = new DateTime(); + } + + /** + * Changes the "now" state of the TimeKeeper. + * @param DateTime $dateTime + */ + public function changeNow(DateTime $dateTime) + { + $this->now = $dateTime; } /** @@ -55,7 +68,7 @@ class TimeKeeper return true; } - $t1 = $this->gameTime(); + $t1 = $this->getGameTime(); $t2 = $this->convertToGameTime($lastInteractionTime); $d1 = $t1->format("Y-m-d"); $d2 = $t2->format("Y-m-d"); @@ -67,9 +80,9 @@ class TimeKeeper * Return the current game time. * @return DateTime */ - public function gameTime(): DateTime + public function getGameTime(): DateTime { - return $this->convertToGameTime(new DateTime()); + return $this->convertToGameTime($this->now); } /** diff --git a/tests/TimeKeeperTest.php b/tests/TimeKeeperTest.php index 5251481..fd82585 100644 --- a/tests/TimeKeeperTest.php +++ b/tests/TimeKeeperTest.php @@ -165,6 +165,6 @@ class TimeKeeperTests extends \PHPUnit_Framework_TestCase { public function testGameTimeSanity() { $keeper = new TimeKeeper($this->gameEpoch, $this->gameOffsetSeconds, $this->gameDaysPerDay); - $this->assertNotNull($keeper->gameTime()); + $this->assertNotNull($keeper->getGameTime()); } }