Skip to content

Commit 5ed96d3

Browse files
committed
add ability to mock the hrtime() function
1 parent 96cb23d commit 5ed96d3

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
6.2
5+
---
6+
7+
* Add support for mocking the `hrtime()` function
8+
49
6.1
510
---
611

ClockMock.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,19 @@ public static function gmdate($format, $timestamp = null)
9090
return \gmdate($format, $timestamp);
9191
}
9292

93+
public static function hrtime($asNumber = false)
94+
{
95+
$ns = (self::$now - (int) self::$now) * 1000000000;
96+
97+
if ($asNumber) {
98+
$number = sprintf('%d%d', (int) self::$now, $ns);
99+
100+
return PHP_INT_SIZE === 8 ? (int) $number : (float) $number;
101+
}
102+
103+
return [(int) $ns, (int) self::$now];
104+
}
105+
93106
public static function register($class)
94107
{
95108
$self = static::class;
@@ -137,6 +150,11 @@ function gmdate(\$format, \$timestamp = null)
137150
{
138151
return \\$self::gmdate(\$format, \$timestamp);
139152
}
153+
154+
function hrtime(\$asNumber = false)
155+
{
156+
return \\$self::hrtime(\$asNumber);
157+
}
140158
EOPHP
141159
);
142160
}

Tests/ClockMockTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,14 @@ public function testGmDate()
6969

7070
$this->assertSame('1555075769', gmdate('U'));
7171
}
72+
73+
public function testHrTime()
74+
{
75+
$this->assertSame([125000000, 1234567890], hrtime());
76+
}
77+
78+
public function testHrTimeAsNumber()
79+
{
80+
$this->assertSame(1234567890125000000, hrtime(true));
81+
}
7282
}

0 commit comments

Comments
 (0)