From 044d62a126cd7dc6a85fbce010fb6be9cf70deda Mon Sep 17 00:00:00 2001 From: Luca Tumedei Date: Wed, 17 Jul 2024 10:25:55 +0200 Subject: [PATCH] fix(StubClassFactory) handle PHPUnit version 10 differently from others --- tests/_support/StubClassFactory.php | 36 ++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 11 deletions(-) diff --git a/tests/_support/StubClassFactory.php b/tests/_support/StubClassFactory.php index 0bf5a1b15..0ebb8c234 100644 --- a/tests/_support/StubClassFactory.php +++ b/tests/_support/StubClassFactory.php @@ -12,6 +12,7 @@ class StubClassFactory { + private static ?int $phpunitVersion = null; private static string $classTemplate = 'class %1$s extends %2$s { public function __construct(%3$s) @@ -29,6 +30,17 @@ public static function tearDown(): void self::$constructorAssertions = []; } + private static function getPHPUnitVersion(): int + { + if (self::$phpunitVersion === null) { + self::$phpunitVersion = class_exists('PHPUnit\Runner\Version') ? + (int)\PHPUnit\Runner\Version::id() + : (int)\PHPUnit_Runner_Version::id(); + } + + return self::$phpunitVersion; + } + /** * @throws Exception */ @@ -37,23 +49,25 @@ public static function connectInvocationMocker(object $mock): void $mockClassName = get_class($mock); [$class, $parameters] = self::$stubParametersByClassName[$mockClassName]; $stub = Stub::makeEmpty($class, $parameters); - $phpuniStateProperty = null; - - try { - $phpuniStateProperty = Property::readPrivate($stub, '__phpunit_state'); - } catch (\Throwable $t) { - // PHPUnit < 10.0.0. - } + $phpunitVersion = self::getPHPUnitVersion(); - if ($phpuniStateProperty) { - // PHPUnit >= 10.0.0. - Property::setPrivateProperties($mock, ['__phpunit_state' => $phpuniStateProperty]); - } else { + if ($phpunitVersion < 10) { Property::setPrivateProperties($mock, [ '__phpunit_originalObject' => Property::readPrivate($stub, '__phpunit_originalObject'), '__phpunit_returnValueGeneration' => Property::readPrivate($stub, '__phpunit_returnValueGeneration'), '__phpunit_invocationMocker' => Property::readPrivate($stub, '__phpunit_invocationMocker'), ]); + } elseif ($phpunitVersion === 10) { + Property::setPrivateProperties($mock, [ + '__phpunit_returnValueGeneration' => Property::readPrivate($stub, '__phpunit_returnValueGeneration'), + '__phpunit_invocationMocker' => Property::readPrivate($stub, '__phpunit_invocationMocker'), + ]); + } else { + // PHPUnit >= 10.0.0. + Property::setPrivateProperties( + $mock, + ['__phpunit_state' => Property::readPrivate($stub, '__phpunit_state')] + ); } unset($stub);