diff --git a/src/Symfony/Bridge/PhpUnit/CHANGELOG.md b/src/Symfony/Bridge/PhpUnit/CHANGELOG.md index 9b8a974ecc5e6..e7e3e29862bd4 100644 --- a/src/Symfony/Bridge/PhpUnit/CHANGELOG.md +++ b/src/Symfony/Bridge/PhpUnit/CHANGELOG.md @@ -1,6 +1,11 @@ CHANGELOG ========= +6.2 +--- + + * Add support for mocking the `hrtime()` function + 6.1 --- diff --git a/src/Symfony/Bridge/PhpUnit/ClockMock.php b/src/Symfony/Bridge/PhpUnit/ClockMock.php index 7280d44dc16f8..d2e150084bdda 100644 --- a/src/Symfony/Bridge/PhpUnit/ClockMock.php +++ b/src/Symfony/Bridge/PhpUnit/ClockMock.php @@ -90,6 +90,19 @@ public static function gmdate($format, $timestamp = null) return \gmdate($format, $timestamp); } + public static function hrtime($asNumber = false) + { + $ns = (self::$now - (int) self::$now) * 1000000000; + + if ($asNumber) { + $number = sprintf('%d%d', (int) self::$now, $ns); + + return PHP_INT_SIZE === 8 ? (int) $number : (float) $number; + } + + return [(int) $ns, (int) self::$now]; + } + public static function register($class) { $self = static::class; @@ -137,6 +150,11 @@ function gmdate(\$format, \$timestamp = null) { return \\$self::gmdate(\$format, \$timestamp); } + +function hrtime(\$asNumber = false) +{ + return \\$self::hrtime(\$asNumber); +} EOPHP ); } diff --git a/src/Symfony/Bridge/PhpUnit/Tests/ClockMockTest.php b/src/Symfony/Bridge/PhpUnit/Tests/ClockMockTest.php index 5af0617ba5a7f..de39909681a1d 100644 --- a/src/Symfony/Bridge/PhpUnit/Tests/ClockMockTest.php +++ b/src/Symfony/Bridge/PhpUnit/Tests/ClockMockTest.php @@ -69,4 +69,14 @@ public function testGmDate() $this->assertSame('1555075769', gmdate('U')); } + + public function testHrTime() + { + $this->assertSame([125000000, 1234567890], hrtime()); + } + + public function testHrTimeAsNumber() + { + $this->assertSame(1234567890125000000, hrtime(true)); + } }