You are here

class TestTime in Automatic Updates 8.2

Test service for altering the request time.

Hierarchy

Expanded class hierarchy of TestTime

1 file declares its use of TestTime
ReadinessValidationTest.php in tests/src/Functional/ReadinessValidationTest.php
1 string reference to 'TestTime'
automatic_updates_test.services.yml in tests/modules/automatic_updates_test/automatic_updates_test.services.yml
tests/modules/automatic_updates_test/automatic_updates_test.services.yml
1 service uses TestTime
automatic_updates_test.time in tests/modules/automatic_updates_test/automatic_updates_test.services.yml
Drupal\automatic_updates_test\Datetime\TestTime

File

tests/modules/automatic_updates_test/src/Datetime/TestTime.php, line 11

Namespace

Drupal\automatic_updates_test\Datetime
View source
class TestTime extends Time {

  /**
   * The time service.
   *
   * @var \Drupal\Component\Datetime\Time
   */
  protected $decoratorTime;

  /**
   * Constructs an Updater object.
   *
   * @param \Drupal\Component\Datetime\Time $time
   *   The time service.
   * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
   *   The RequestStack object.
   */
  public function __construct(Time $time, RequestStack $request_stack) {
    $this->decoratorTime = $time;
    parent::__construct($request_stack);
  }

  /**
   * {@inheritdoc}
   */
  public function getRequestTime() : int {
    if ($faked_date = \Drupal::state()
      ->get('automatic_updates_test.fake_date_time')) {
      return \DateTime::createFromFormat('U', $faked_date)
        ->getTimestamp();
    }
    return $this->decoratorTime
      ->getRequestTime();
  }

  /**
   * Sets a fake time from an offset that will be used in the test.
   *
   * @param string $offset
   *   A date/time offset string as used by \DateTime::modify.
   */
  public static function setFakeTimeByOffset(string $offset) : void {
    $fake_time = (new \DateTime())
      ->modify($offset)
      ->format('U');
    \Drupal::state()
      ->set('automatic_updates_test.fake_date_time', $fake_time);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
TestTime::$decoratorTime protected property The time service.
TestTime::getRequestTime public function Returns the timestamp for the current request. Overrides Time::getRequestTime
TestTime::setFakeTimeByOffset public static function Sets a fake time from an offset that will be used in the test.
TestTime::__construct public function Constructs an Updater object. Overrides Time::__construct
Time::$requestStack protected property The request stack.
Time::getCurrentMicroTime public function Returns the current system time with microsecond precision. Overrides TimeInterface::getCurrentMicroTime
Time::getCurrentTime public function Returns the current system time as an integer. Overrides TimeInterface::getCurrentTime
Time::getRequestMicroTime public function Returns the timestamp for the current request with microsecond precision. Overrides TimeInterface::getRequestMicroTime