From 39663bb445b3afce9afd69b35186c32285f875cf Mon Sep 17 00:00:00 2001 From: Ilia Urvachev Date: Wed, 21 Feb 2024 15:23:13 +0100 Subject: [PATCH] fix(phpunit): fix support for phpunit-9/10/11 --- phpstan.neon.dist | 2 +- src/Phpunit/AbstractMocked.php | 77 ++++++++++++++++++++++++---------- 2 files changed, 56 insertions(+), 23 deletions(-) diff --git a/phpstan.neon.dist b/phpstan.neon.dist index 35d8e80..a27c2bb 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -13,4 +13,4 @@ parameters: - path: test/phpunit/Helpers/ClassWithMethods.php message: '/^Method .*\\ClassWithMethods::privateMethod\(\) is unused\.$/' - path: src/Phpunit/AbstractMocked.php - message: '/^Parameter #\d+ \S+ of method PHPUnit\\Framework\\MockObject\\Generator\\Generator::testDouble\(\)/' \ No newline at end of file + message: '/^Class PHPUnit\\Framework\\MockObject(\\Generator)+ not found./' \ No newline at end of file diff --git a/src/Phpunit/AbstractMocked.php b/src/Phpunit/AbstractMocked.php index d50ed8a..9fe5a64 100644 --- a/src/Phpunit/AbstractMocked.php +++ b/src/Phpunit/AbstractMocked.php @@ -4,44 +4,77 @@ namespace QratorLabs\Smocky\Phpunit; -use PHPUnit\Framework\MockObject\Generator\Generator; +use PHPUnit\Framework\MockObject\Generator as Generator_PHPUnit9; +use PHPUnit\Framework\MockObject\Generator\Generator as Generator_PHPUnit1x; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; use QratorLabs\Smocky\EmptyClass; +use ReflectionException; use ReflectionMethod; use function assert; +use function class_exists; abstract class AbstractMocked { /** * @return MockObject&EmptyClass + * @throws ReflectionException */ protected static function createEmptyMock(TestCase $testCase, string $method): MockObject { - $args = [ - EmptyClass::class, // string $type - true, // bool $mockObject - true, // bool $markAsMockObject - [$method], // ?array $methods = [] - [], // array $arguments = [] - '', // string $mockClassName = '' - false, // bool $callOriginalConstructor = true - false, // bool $callOriginalClone = true - true, // bool $callAutoload = true - false, // bool $cloneArguments = true - false, // bool $callOriginalMethods = false - null, // object $proxyTarget = null - false, // bool $allowMockingUnknownTypes = true - true, // bool $returnValueGeneration = true - ]; - - if (count($args) > (new ReflectionMethod(Generator::class, 'testDouble'))->getNumberOfParameters()) { - // 10 -> 11 transition - unset($args[2]); + $generatorClass = class_exists(Generator_PHPUnit1x::class) + ? Generator_PHPUnit1x::class + : Generator_PHPUnit9::class; + + switch ($generatorClass) { + case Generator_PHPUnit1x::class: + $generatorMethod = 'testDouble'; + $args = [ + EmptyClass::class, // string $type + true, // bool $mockObject + true, // bool $markAsMockObject + [$method], // ?array $methods = [] + [], // array $arguments = [] + '', // string $mockClassName = '' + false, // bool $callOriginalConstructor = true + false, // bool $callOriginalClone = true + true, // bool $callAutoload = true + false, // bool $cloneArguments = true + false, // bool $callOriginalMethods = false + null, // object $proxyTarget = null + false, // bool $allowMockingUnknownTypes = true + true, // bool $returnValueGeneration = true + ]; + if (count($args) > (new ReflectionMethod($generatorClass, 'testDouble'))->getNumberOfParameters()) { + // 10 -> 11 transition + unset($args[2]); + } + break; + + case Generator_PHPUnit9::class: + $generatorMethod = 'getMock'; + $args = [ + EmptyClass::class, // string $type + [$method], // $methods = [] + [], // array $arguments = [] + '', // string $mockClassName = '' + false, // bool $callOriginalConstructor = true + false, // bool $callOriginalClone = true + true, // bool $callAutoload = true + false, // bool $cloneArguments = true + false, // bool $callOriginalMethods = false + null, // object $proxyTarget = null + false, // bool $allowMockingUnknownTypes = true + true, // bool $returnValueGeneration = true + ]; + break; + default: + throw new ReflectionException('Unknown PHPUnit version'); } - $mockObject = (new Generator())->testDouble(...$args); + // @phpstan-ignore-next-line + $mockObject = (new $generatorClass())->$generatorMethod(...$args); assert($mockObject instanceof EmptyClass); assert($mockObject instanceof MockObject); $testCase->registerMockObject($mockObject);