0553451de8c408753dfa2ea529937695f3693d8e
[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 Drupal\Tests\UnitTestCase;
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 UnitTestCase {
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->getMock('Symfony\Component\HttpFoundation\RequestStack');
41
42     $this->time = new Time($this->requestStack);
43   }
44
45   /**
46    * Tests the getRequestTime method.
47    *
48    * @covers ::getRequestTime
49    */
50   public function testGetRequestTime() {
51     $expected = 12345678;
52
53     $request = Request::createFromGlobals();
54     $request->server->set('REQUEST_TIME', $expected);
55
56     // Mocks a the request stack getting the current request.
57     $this->requestStack->expects($this->any())
58       ->method('getCurrentRequest')
59       ->willReturn($request);
60
61     $this->assertEquals($expected, $this->time->getRequestTime());
62   }
63
64   /**
65    * Tests the getRequestMicroTime method.
66    *
67    * @covers ::getRequestMicroTime
68    */
69   public function testGetRequestMicroTime() {
70     $expected = 1234567.89;
71
72     $request = Request::createFromGlobals();
73     $request->server->set('REQUEST_TIME_FLOAT', $expected);
74
75     // Mocks a the request stack getting the current request.
76     $this->requestStack->expects($this->any())
77       ->method('getCurrentRequest')
78       ->willReturn($request);
79
80     $this->assertEquals($expected, $this->time->getRequestMicroTime());
81   }
82
83   /**
84    * Tests the getCurrentTime method.
85    *
86    * @covers ::getCurrentTime
87    */
88   public function testGetCurrentTime() {
89     $expected = 12345678;
90     $this->assertEquals($expected, $this->time->getCurrentTime());
91   }
92
93   /**
94    * Tests the getCurrentMicroTime method.
95    *
96    * @covers ::getCurrentMicroTime
97    */
98   public function testGetCurrentMicroTime() {
99     $expected = 1234567.89;
100     $this->assertEquals($expected, $this->time->getCurrentMicroTime());
101   }
102
103 }
104
105 namespace Drupal\Component\Datetime;
106
107 /**
108  * Shadow time() system call.
109  *
110  * @returns int
111  */
112 function time() {
113   return 12345678;
114 }
115
116 /**
117  * Shadow microtime system call.
118  *
119  * @returns float
120  */
121 function microtime() {
122   return 1234567.89;
123 }