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