class TimeTest

Same name in other branches
  1. 8.9.x core/tests/Drupal/Tests/Component/Datetime/TimeTest.php \Drupal\Tests\Component\Datetime\TimeTest
  2. 10 core/tests/Drupal/Tests/Component/Datetime/TimeTest.php \Drupal\Tests\Component\Datetime\TimeTest
  3. 11.x core/tests/Drupal/Tests/Component/Datetime/TimeTest.php \Drupal\Tests\Component\Datetime\TimeTest

@coversDefaultClass \Drupal\Component\Datetime\Time @group Datetime

Isolate the tests to prevent side effects from altering system time.

@runTestsInSeparateProcesses @preserveGlobalState disabled

Hierarchy

  • class \Drupal\Tests\Component\Datetime\TimeTest extends \PHPUnit\Framework\TestCase

Expanded class hierarchy of TimeTest

File

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

Namespace

Drupal\Tests\Component\Datetime
View source
class TimeTest extends TestCase {
    
    /**
     * The mocked request stack.
     *
     * @var \Symfony\Component\HttpFoundation\RequestStack|\PHPUnit\Framework\MockObject\MockObject
     */
    protected $requestStack;
    
    /**
     * The mocked time class.
     *
     * @var \Drupal\Component\Datetime\Time
     */
    protected $time;
    
    /**
     * {@inheritdoc}
     */
    protected function setUp() : void {
        parent::setUp();
        $this->requestStack = $this->getMockBuilder('Symfony\\Component\\HttpFoundation\\RequestStack')
            ->getMock();
        $this->time = new Time($this->requestStack);
    }
    
    /**
     * Tests the getRequestTime method.
     *
     * @covers ::getRequestTime
     */
    public function testGetRequestTime() {
        $expected = 12345678;
        $request = Request::createFromGlobals();
        $request->server
            ->set('REQUEST_TIME', $expected);
        // Mocks a the request stack getting the current request.
        $this->requestStack
            ->expects($this->any())
            ->method('getCurrentRequest')
            ->willReturn($request);
        $this->assertEquals($expected, $this->time
            ->getRequestTime());
    }
    
    /**
     * Tests the getRequestMicroTime method.
     *
     * @covers ::getRequestMicroTime
     */
    public function testGetRequestMicroTime() {
        $expected = 1234567.89;
        $request = Request::createFromGlobals();
        $request->server
            ->set('REQUEST_TIME_FLOAT', $expected);
        // Mocks a the request stack getting the current request.
        $this->requestStack
            ->expects($this->any())
            ->method('getCurrentRequest')
            ->willReturn($request);
        $this->assertEquals($expected, $this->time
            ->getRequestMicroTime());
    }
    
    /**
     * @covers ::getRequestTime
     */
    public function testGetRequestTimeNoRequest() {
        $expected = 12345678;
        unset($_SERVER['REQUEST_TIME']);
        $this->assertEquals($expected, $this->time
            ->getRequestTime());
        $_SERVER['REQUEST_TIME'] = 23456789;
        $this->assertEquals(23456789, $this->time
            ->getRequestTime());
    }
    
    /**
     * @covers ::getRequestMicroTime
     */
    public function testGetRequestMicroTimeNoRequest() {
        $expected = 1234567.89;
        unset($_SERVER['REQUEST_TIME_FLOAT']);
        $this->assertEquals($expected, $this->time
            ->getRequestMicroTime());
        $_SERVER['REQUEST_TIME_FLOAT'] = 2345678.9;
        $this->assertEquals(2345678.9, $this->time
            ->getRequestMicroTime());
    }
    
    /**
     * Tests the getCurrentTime method.
     *
     * @covers ::getCurrentTime
     */
    public function testGetCurrentTime() {
        $expected = 12345678;
        $this->assertEquals($expected, $this->time
            ->getCurrentTime());
    }
    
    /**
     * Tests the getCurrentMicroTime method.
     *
     * @covers ::getCurrentMicroTime
     */
    public function testGetCurrentMicroTime() {
        $expected = 1234567.89;
        $this->assertEquals($expected, $this->time
            ->getCurrentMicroTime());
    }

}

Members

Title Sort descending Modifiers Object type Summary
TimeTest::$requestStack protected property The mocked request stack.
TimeTest::$time protected property The mocked time class.
TimeTest::setUp protected function
TimeTest::testGetCurrentMicroTime public function Tests the getCurrentMicroTime method.
TimeTest::testGetCurrentTime public function Tests the getCurrentTime method.
TimeTest::testGetRequestMicroTime public function Tests the getRequestMicroTime method.
TimeTest::testGetRequestMicroTimeNoRequest public function @covers ::getRequestMicroTime
TimeTest::testGetRequestTime public function Tests the getRequestTime method.
TimeTest::testGetRequestTimeNoRequest public function @covers ::getRequestTime

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