From 68b428803f2340abfd026b8e889ecd91fe64c393 Mon Sep 17 00:00:00 2001 From: Christian Flothmann Date: Wed, 17 Aug 2022 12:25:46 +0200 Subject: [PATCH] add ability to mock the hrtime() function --- src/Symfony/Bridge/PhpUnit/CHANGELOG.md | 5 +++++ src/Symfony/Bridge/PhpUnit/ClockMock.php | 18 ++++++++++++++++++ .../Bridge/PhpUnit/Tests/ClockMockTest.php | 10 ++++++++++ 3 files changed, 33 insertions(+) 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)); + } }