TimeTest.php in Drupal 9
File
core/tests/Drupal/Tests/Component/Datetime/TimeTest.php
View source
<?php
namespace Drupal\Tests\Component\Datetime;
use Drupal\Component\Datetime\Time;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Request;
class TimeTest extends TestCase {
protected $requestStack;
protected $time;
protected function setUp() : void {
parent::setUp();
$this->requestStack = $this
->getMockBuilder('Symfony\\Component\\HttpFoundation\\RequestStack')
->getMock();
$this->time = new Time($this->requestStack);
}
public function testGetRequestTime() {
$expected = 12345678;
$request = Request::createFromGlobals();
$request->server
->set('REQUEST_TIME', $expected);
$this->requestStack
->expects($this
->any())
->method('getCurrentRequest')
->willReturn($request);
$this
->assertEquals($expected, $this->time
->getRequestTime());
}
public function testGetRequestMicroTime() {
$expected = 1234567.89;
$request = Request::createFromGlobals();
$request->server
->set('REQUEST_TIME_FLOAT', $expected);
$this->requestStack
->expects($this
->any())
->method('getCurrentRequest')
->willReturn($request);
$this
->assertEquals($expected, $this->time
->getRequestMicroTime());
}
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());
}
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());
}
public function testGetCurrentTime() {
$expected = 12345678;
$this
->assertEquals($expected, $this->time
->getCurrentTime());
}
public function testGetCurrentMicroTime() {
$expected = 1234567.89;
$this
->assertEquals($expected, $this->time
->getCurrentMicroTime());
}
}
namespace Drupal\Component\Datetime;
function time() {
return 12345678;
}
function microtime() {
return 1234567.89;
}