From 220e721166331668ee278261c8a3c371f578d8c8 Mon Sep 17 00:00:00 2001 From: Martin Linzmayer Date: Mon, 29 Sep 2025 11:36:46 +0200 Subject: [PATCH 1/5] ref: remove phpunit bridge --- composer.json | 1 - 1 file changed, 1 deletion(-) diff --git a/composer.json b/composer.json index 0be4a6531..40e58869b 100644 --- a/composer.json +++ b/composer.json @@ -39,7 +39,6 @@ "phpbench/phpbench": "^1.0", "phpstan/phpstan": "^1.3", "phpunit/phpunit": "^8.5|^9.6", - "symfony/phpunit-bridge": "^5.2|^6.0|^7.0", "vimeo/psalm": "^4.17" }, "suggest": { From e188cfcd4dceaec0a19ba4d2a90febfe7d4b49f1 Mon Sep 17 00:00:00 2001 From: Martin Linzmayer Date: Mon, 29 Sep 2025 14:36:29 +0200 Subject: [PATCH 2/5] add vendored ClockMock --- phpunit.xml.dist | 6 +- src/State/HubAdapter.php | 6 + src/Util/ClockMock.php | 202 +++++++++++++++++++++ tests/BreadcrumbTest.php | 2 +- tests/ClientTest.php | 3 - tests/DsnTest.php | 3 - tests/Metrics/MetricsTest.php | 2 +- tests/OptionsTest.php | 3 - tests/Serializer/PayloadSerializerTest.php | 2 +- tests/Tracing/SpanTest.php | 2 +- tests/Tracing/TransactionContextTest.php | 3 - tests/Tracing/TransactionTest.php | 2 +- tests/Transport/HttpTransportTest.php | 2 +- tests/Transport/RateLimiterTest.php | 2 +- tests/bootstrap.php | 3 +- 15 files changed, 220 insertions(+), 23 deletions(-) create mode 100644 src/Util/ClockMock.php diff --git a/phpunit.xml.dist b/phpunit.xml.dist index d064f8775..45b16e93b 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -34,9 +34,9 @@ - - - + + + diff --git a/src/State/HubAdapter.php b/src/State/HubAdapter.php index 503153860..6d758145c 100644 --- a/src/State/HubAdapter.php +++ b/src/State/HubAdapter.php @@ -4,6 +4,7 @@ namespace Sentry\State; +use Sentry\Attachment\Attachment; use Sentry\Breadcrumb; use Sentry\CheckInStatus; use Sentry\ClientInterface; @@ -218,4 +219,9 @@ public function __sleep() { throw new \BadMethodCallException('Serializing instances of this class is forbidden.'); } + + public function addAttachment(Attachment $attachment): bool + { + return self::$instance->addAttachment($attachment); + } } diff --git a/src/Util/ClockMock.php b/src/Util/ClockMock.php new file mode 100644 index 000000000..ea3f18f18 --- /dev/null +++ b/src/Util/ClockMock.php @@ -0,0 +1,202 @@ + + * @author Dominic Tubach + */ +class ClockMock +{ + private static $now; + + public static function withClockMock($enable = null): ?bool + { + if ($enable === null) { + return self::$now !== null; + } + + self::$now = is_numeric($enable) ? (float) $enable : ($enable ? microtime(true) : null); + + return null; + } + + public static function time(): int + { + if (self::$now === null) { + return time(); + } + + return (int) self::$now; + } + + public static function sleep($s): int + { + if (self::$now === null) { + return sleep($s); + } + + self::$now += (int) $s; + + return 0; + } + + public static function usleep($us): void + { + if (self::$now === null) { + usleep($us); + } else { + self::$now += $us / 1000000; + } + } + + /** + * @return string|float + */ + public static function microtime($asFloat = false) + { + if (self::$now === null) { + return microtime($asFloat); + } + + if ($asFloat) { + return self::$now; + } + + return \sprintf('%0.6f00 %d', self::$now - (int) self::$now, (int) self::$now); + } + + public static function date($format, $timestamp = null): string + { + if ($timestamp === null) { + $timestamp = self::time(); + } + + return date($format, $timestamp); + } + + public static function gmdate($format, $timestamp = null): string + { + if ($timestamp === null) { + $timestamp = self::time(); + } + + return gmdate($format, $timestamp); + } + + /** + * @return array|int|float + */ + 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) self::$now, (int) $ns]; + } + + /** + * @return false|int + */ + public static function strtotime(string $datetime, ?int $timestamp = null) + { + if ($timestamp === null) { + $timestamp = self::time(); + } + + return strtotime($datetime, $timestamp); + } + + public static function register($class): void + { + $self = static::class; + + $mockedNs = [substr($class, 0, strrpos($class, '\\'))]; + if (strpos($class, '\\Tests\\') > 0) { + $ns = str_replace('\\Tests\\', '\\', $class); + $mockedNs[] = substr($ns, 0, strrpos($ns, '\\')); + } elseif (str_starts_with($class, 'Tests\\')) { + $mockedNs[] = substr($class, 6, strrpos($class, '\\') - 6); + } + foreach ($mockedNs as $ns) { + if (\function_exists($ns . '\time')) { + continue; + } + eval(<< Date: Mon, 29 Sep 2025 14:37:31 +0200 Subject: [PATCH 3/5] cleanup --- src/State/HubAdapter.php | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/State/HubAdapter.php b/src/State/HubAdapter.php index 6d758145c..503153860 100644 --- a/src/State/HubAdapter.php +++ b/src/State/HubAdapter.php @@ -4,7 +4,6 @@ namespace Sentry\State; -use Sentry\Attachment\Attachment; use Sentry\Breadcrumb; use Sentry\CheckInStatus; use Sentry\ClientInterface; @@ -219,9 +218,4 @@ public function __sleep() { throw new \BadMethodCallException('Serializing instances of this class is forbidden.'); } - - public function addAttachment(Attachment $attachment): bool - { - return self::$instance->addAttachment($attachment); - } } From 78bf1a0a829301c79dfe82d466488d4da4c90835 Mon Sep 17 00:00:00 2001 From: Martin Linzmayer Date: Mon, 29 Sep 2025 14:38:00 +0200 Subject: [PATCH 4/5] cleanup --- phpunit.xml.dist | 4 ---- 1 file changed, 4 deletions(-) diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 45b16e93b..8d5384d66 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -34,10 +34,6 @@ - - - - From ee9440ab4a6d31931665b0c1aabe14302a6d8736 Mon Sep 17 00:00:00 2001 From: Martin Linzmayer Date: Mon, 29 Sep 2025 14:53:30 +0200 Subject: [PATCH 5/5] exclude ClockMock --- phpstan.neon | 1 + psalm.xml.dist | 1 + 2 files changed, 2 insertions(+) diff --git a/phpstan.neon b/phpstan.neon index 91af9c48e..61e18b96c 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -10,6 +10,7 @@ parameters: excludePaths: - tests/resources - tests/Fixtures + - src/Util/ClockMock.php dynamicConstantNames: - Monolog\Logger::API bootstrapFiles: diff --git a/psalm.xml.dist b/psalm.xml.dist index ea60216a6..502f940b3 100644 --- a/psalm.xml.dist +++ b/psalm.xml.dist @@ -10,6 +10,7 @@ +