Skip to content

Commit

Permalink
fix(StubClassFactory) handle PHPUnit version 10 differently from others
Browse files Browse the repository at this point in the history
  • Loading branch information
lucatume committed Jul 17, 2024
1 parent e9056ec commit 044d62a
Showing 1 changed file with 25 additions and 11 deletions.
36 changes: 25 additions & 11 deletions tests/_support/StubClassFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
*/
Expand All @@ -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);
Expand Down

0 comments on commit 044d62a

Please sign in to comment.