Skip to content

Commit

Permalink
Add freeze(), travel() and scale() to DefaultClock
Browse files Browse the repository at this point in the history
  • Loading branch information
BenMorel committed Oct 4, 2017
1 parent c08e7e7 commit 6f84129
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 1 deletion.
54 changes: 53 additions & 1 deletion src/DefaultClock.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

namespace Brick\DateTime;

use Brick\DateTime\Clock\FixedClock;
use Brick\DateTime\Clock\OffsetClock;
use Brick\DateTime\Clock\ScaleClock;
use Brick\DateTime\Clock\SystemClock;

/**
Expand All @@ -26,7 +29,7 @@ private function __construct()
}

/**
* Returns the default clock.
* Gets the default clock.
*
* @return Clock
*/
Expand Down Expand Up @@ -60,4 +63,53 @@ public static function reset() : void
{
self::$clock = null;
}

/**
* Freezes time to a specific point in time.
*
* @param Instant|null $instant The time to freeze to.
*
* @return void
*/
public static function freeze(Instant $instant) : void
{
self::set(new FixedClock($instant));
}

/**
* Travels to a specific point in time, but allows time to continue moving forward from there.
*
* If the current default clock is frozen, you must `reset()` it first, or the time will stay frozen.
*
* @param Instant $instant
*
* @return void
*/
public static function travel(Instant $instant) : void
{
$clock = self::get();
$offset = Duration::between($clock->getTime(), $instant);

self::set(new OffsetClock($clock, $offset));
}

/**
* Makes time move at a given pace.
*
* - a scale > 1 makes the time move at an accelerated pace;
* - a scale == 1 makes the time move at the normal pace;
* - a scale == 0 freezes the current time;
* - a scale < 0 makes the time move backwards.
*
* If the current default clock is frozen, you must `reset()` it first, or the time will stay frozen.
* Multiple calls to `scale()` will result in a clock with the combined scales.
*
* @param int $timeScale The time scale.
*
* @return void
*/
public static function scale(int $timeScale) : void
{
self::set(new ScaleClock(self::get(), $timeScale));
}
}
52 changes: 52 additions & 0 deletions tests/DefaultClockTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

declare(strict_types=1);

namespace Brick\DateTime\Tests;

use Brick\DateTime\Clock\FixedClock;
use Brick\DateTime\DefaultClock;
use Brick\DateTime\Instant;

/**
* Unit tests for class DefaultClock.
*/
class DefaultClockTest extends AbstractTestCase
{
public function tearDown()
{
DefaultClock::reset();
}

public function testFreeze()
{
DefaultClock::freeze(Instant::of(123456, 5000));
$this->assertInstantIs(123456, 5000, Instant::now());
}

public function testTravel()
{
$fixedClock = new FixedClock(Instant::of(1000, 0));
DefaultClock::set($fixedClock);
$this->assertInstantIs(1000, 0, Instant::now());

DefaultClock::travel(Instant::of(-1000));
$this->assertInstantIs(-1000, 0, Instant::now());

$fixedClock->move(2);
$this->assertInstantIs(-998, 0, Instant::now());
}

public function testScale()
{
$fixedClock = new FixedClock(Instant::of(1000, 0));
DefaultClock::set($fixedClock);
$this->assertInstantIs(1000, 0, Instant::now());

DefaultClock::scale(60);
$this->assertInstantIs(1000, 0, Instant::now());

$fixedClock->move(2);
$this->assertInstantIs(1120, 0, Instant::now());
}
}

0 comments on commit 6f84129

Please sign in to comment.