TimeTest.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. namespace Drupal\Tests\Component\Datetime;
  3. use Drupal\Component\Datetime\Time;
  4. use PHPUnit\Framework\TestCase;
  5. use Symfony\Component\HttpFoundation\Request;
  6. /**
  7. * @coversDefaultClass \Drupal\Component\Datetime\Time
  8. * @group Datetime
  9. *
  10. * Isolate the tests to prevent side effects from altering system time.
  11. *
  12. * @runTestsInSeparateProcesses
  13. * @preserveGlobalState disabled
  14. */
  15. class TimeTest extends TestCase {
  16. /**
  17. * The mocked request stack.
  18. *
  19. * @var \Symfony\Component\HttpFoundation\RequestStack|\PHPUnit_Framework_MockObject_MockObject
  20. */
  21. protected $requestStack;
  22. /**
  23. * The mocked time class.
  24. *
  25. * @var \Drupal\Component\Datetime\Time
  26. */
  27. protected $time;
  28. /**
  29. * {@inheritdoc}
  30. */
  31. protected function setUp() {
  32. parent::setUp();
  33. $this->requestStack = $this->getMockBuilder('Symfony\Component\HttpFoundation\RequestStack')->getMock();
  34. $this->time = new Time($this->requestStack);
  35. }
  36. /**
  37. * Tests the getRequestTime method.
  38. *
  39. * @covers ::getRequestTime
  40. */
  41. public function testGetRequestTime() {
  42. $expected = 12345678;
  43. $request = Request::createFromGlobals();
  44. $request->server->set('REQUEST_TIME', $expected);
  45. // Mocks a the request stack getting the current request.
  46. $this->requestStack->expects($this->any())
  47. ->method('getCurrentRequest')
  48. ->willReturn($request);
  49. $this->assertEquals($expected, $this->time->getRequestTime());
  50. }
  51. /**
  52. * Tests the getRequestMicroTime method.
  53. *
  54. * @covers ::getRequestMicroTime
  55. */
  56. public function testGetRequestMicroTime() {
  57. $expected = 1234567.89;
  58. $request = Request::createFromGlobals();
  59. $request->server->set('REQUEST_TIME_FLOAT', $expected);
  60. // Mocks a the request stack getting the current request.
  61. $this->requestStack->expects($this->any())
  62. ->method('getCurrentRequest')
  63. ->willReturn($request);
  64. $this->assertEquals($expected, $this->time->getRequestMicroTime());
  65. }
  66. /**
  67. * Tests the getCurrentTime method.
  68. *
  69. * @covers ::getCurrentTime
  70. */
  71. public function testGetCurrentTime() {
  72. $expected = 12345678;
  73. $this->assertEquals($expected, $this->time->getCurrentTime());
  74. }
  75. /**
  76. * Tests the getCurrentMicroTime method.
  77. *
  78. * @covers ::getCurrentMicroTime
  79. */
  80. public function testGetCurrentMicroTime() {
  81. $expected = 1234567.89;
  82. $this->assertEquals($expected, $this->time->getCurrentMicroTime());
  83. }
  84. }
  85. namespace Drupal\Component\Datetime;
  86. /**
  87. * Shadow time() system call.
  88. *
  89. * @returns int
  90. */
  91. function time() {
  92. return 12345678;
  93. }
  94. /**
  95. * Shadow microtime system call.
  96. *
  97. * @returns float
  98. */
  99. function microtime() {
  100. return 1234567.89;
  101. }