class TimeWithNoRequestTest

Same name in other branches
  1. 11.x core/tests/Drupal/Tests/Component/Datetime/TimeWithNoRequestTest.php \Drupal\Tests\Component\Datetime\TimeWithNoRequestTest

Tests that getRequest(Micro)Time works when no underlying request exists.

@coversDefaultClass \Drupal\Component\Datetime\Time @group Datetime @runTestsInSeparateProcesses @preserveGlobalState disabled

Hierarchy

Expanded class hierarchy of TimeWithNoRequestTest

File

core/tests/Drupal/Tests/Component/Datetime/TimeWithNoRequestTest.php, line 18

Namespace

Drupal\Tests\Component\Datetime
View source
class TimeWithNoRequestTest extends TestCase {
    
    /**
     * The time class for testing.
     */
    protected Time $time;
    
    /**
     * {@inheritdoc}
     */
    protected function setUp() : void {
        parent::setUp();
        // We need to explicitly unset the $_SERVER variables, so that Time is
        // forced to look for current time.
        unset($_SERVER['REQUEST_TIME']);
        unset($_SERVER['REQUEST_TIME_FLOAT']);
        $this->time = new Time();
    }
    
    /**
     * Tests the getRequestTime method.
     *
     * @covers ::getRequestTime
     */
    public function testGetRequestTimeImmutable() : void {
        $requestTime = $this->time
            ->getRequestTime();
        sleep(2);
        $this->assertSame($requestTime, $this->time
            ->getRequestTime());
    }
    
    /**
     * Tests the getRequestMicroTime method.
     *
     * @covers ::getRequestMicroTime
     */
    public function testGetRequestMicroTimeImmutable() : void {
        $requestTime = $this->time
            ->getRequestMicroTime();
        usleep(20000);
        $this->assertSame($requestTime, $this->time
            ->getRequestMicroTime());
    }

}

Members

Title Sort descending Modifiers Object type Summary
TimeWithNoRequestTest::$time protected property The time class for testing.
TimeWithNoRequestTest::setUp protected function
TimeWithNoRequestTest::testGetRequestMicroTimeImmutable public function Tests the getRequestMicroTime method.
TimeWithNoRequestTest::testGetRequestTimeImmutable public function Tests the getRequestTime method.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.