Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix compatibility with PHPUnit 10.4 #47

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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]);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sebastianbergmann/phpunit@c8e2aa0 added a new parameter to Generator::testDouble()

}

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';
Copy link
Contributor Author

@W0rma W0rma Oct 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sebastianbergmann/phpunit@26a33f7 deprecated mockObjectForAbstractClass.
Seems like it will still be available in PHPUnit 11 but will be dropped in PHPUnit 12.

} 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
Loading