Skip to content

Commit

Permalink
Merge pull request #47 from W0rma/fix-compatibility-with-phpunit10-4
Browse files Browse the repository at this point in the history
Fix compatibility with PHPUnit 10.4
  • Loading branch information
Naktibalda committed Oct 7, 2023
2 parents 4aaeffd + c996399 commit f6bc56e
Showing 1 changed file with 19 additions and 12 deletions.
31 changes: 19 additions & 12 deletions src/Stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,12 @@ public static function constructEmptyExcept(

private static function generateMock()
{
return self::doGenerateMock(func_get_args());
$args = func_get_args();
if (version_compare(PHPUnitVersion::series(), '10.4', '>=') && !is_bool($args[1])) {
array_splice($args, 1, 0, [true]);
}

return self::doGenerateMock($args);
}

/**
Expand All @@ -433,26 +438,28 @@ private static function generateMockForAbstractClass(): object
private static function doGenerateMock($args, $isAbstract = false)
{
$testCase = self::extractTestCaseFromArgs($args);
$methodName = $isAbstract ? 'getMockForAbstractClass' : 'getMock';

// PHPUnit 10.4 changed method names
if (version_compare(PHPUnitVersion::series(), '10.4', '>=')) {
$methodName = $isAbstract ? 'mockObjectForAbstractClass' : 'testDouble';
} else {
$methodName = $isAbstract ? 'getMockForAbstractClass' : 'getMock';
}

// PHPUnit 10.3 changed the namespace
if (version_compare(PHPUnitVersion::series(), '10.3', '>=')) {
$generatorClass = new Generator();
} else {
$generatorClass = new LegacyGenerator();
}

// using PHPUnit 5.4 mocks registration
if (version_compare(PHPUnitVersion::series(), '5.4', '>=')
&& $testCase instanceof PHPUnitTestCase
) {
$mock = call_user_func_array([$generatorClass, $methodName], $args);
$testCase->registerMockObject($mock);
return $mock;
}
$mock = call_user_func_array([$generatorClass, $methodName], $args);

if ($testCase instanceof PHPUnitTestCase) {
$generatorClass = $testCase;
$testCase->registerMockObject($mock);
}
return call_user_func_array([$generatorClass, $methodName], $args);

return $mock;
}

private static function extractTestCaseFromArgs(&$args)
Expand Down

0 comments on commit f6bc56e

Please sign in to comment.