|
4 | 4 |
|
5 | 5 | namespace QratorLabs\Smocky\Phpunit;
|
6 | 6 |
|
7 |
| -use PHPUnit\Framework\MockObject\Generator\Generator; |
| 7 | +use PHPUnit\Framework\MockObject\Generator as Generator_PHPUnit9; |
| 8 | +use PHPUnit\Framework\MockObject\Generator\Generator as Generator_PHPUnit1x; |
8 | 9 | use PHPUnit\Framework\MockObject\MockObject;
|
9 | 10 | use PHPUnit\Framework\TestCase;
|
10 | 11 | use QratorLabs\Smocky\EmptyClass;
|
| 12 | +use ReflectionException; |
11 | 13 | use ReflectionMethod;
|
12 | 14 |
|
13 | 15 | use function assert;
|
| 16 | +use function class_exists; |
14 | 17 |
|
15 | 18 | abstract class AbstractMocked
|
16 | 19 | {
|
17 | 20 | /**
|
18 | 21 | * @return MockObject&EmptyClass
|
| 22 | + * @throws ReflectionException |
19 | 23 | */
|
20 | 24 | protected static function createEmptyMock(TestCase $testCase, string $method): MockObject
|
21 | 25 | {
|
22 |
| - $args = [ |
23 |
| - EmptyClass::class, // string $type |
24 |
| - true, // bool $mockObject |
25 |
| - true, // bool $markAsMockObject |
26 |
| - [$method], // ?array $methods = [] |
27 |
| - [], // array $arguments = [] |
28 |
| - '', // string $mockClassName = '' |
29 |
| - false, // bool $callOriginalConstructor = true |
30 |
| - false, // bool $callOriginalClone = true |
31 |
| - true, // bool $callAutoload = true |
32 |
| - false, // bool $cloneArguments = true |
33 |
| - false, // bool $callOriginalMethods = false |
34 |
| - null, // object $proxyTarget = null |
35 |
| - false, // bool $allowMockingUnknownTypes = true |
36 |
| - true, // bool $returnValueGeneration = true |
37 |
| - ]; |
38 |
| - |
39 |
| - if (count($args) > (new ReflectionMethod(Generator::class, 'testDouble'))->getNumberOfParameters()) { |
40 |
| - // 10 -> 11 transition |
41 |
| - unset($args[2]); |
| 26 | + $generatorClass = class_exists(Generator_PHPUnit1x::class) |
| 27 | + ? Generator_PHPUnit1x::class |
| 28 | + : Generator_PHPUnit9::class; |
| 29 | + |
| 30 | + switch ($generatorClass) { |
| 31 | + case Generator_PHPUnit1x::class: |
| 32 | + $generatorMethod = 'testDouble'; |
| 33 | + $args = [ |
| 34 | + EmptyClass::class, // string $type |
| 35 | + true, // bool $mockObject |
| 36 | + true, // bool $markAsMockObject |
| 37 | + [$method], // ?array $methods = [] |
| 38 | + [], // array $arguments = [] |
| 39 | + '', // string $mockClassName = '' |
| 40 | + false, // bool $callOriginalConstructor = true |
| 41 | + false, // bool $callOriginalClone = true |
| 42 | + true, // bool $callAutoload = true |
| 43 | + false, // bool $cloneArguments = true |
| 44 | + false, // bool $callOriginalMethods = false |
| 45 | + null, // object $proxyTarget = null |
| 46 | + false, // bool $allowMockingUnknownTypes = true |
| 47 | + true, // bool $returnValueGeneration = true |
| 48 | + ]; |
| 49 | + if (count($args) > (new ReflectionMethod($generatorClass, 'testDouble'))->getNumberOfParameters()) { |
| 50 | + // 10 -> 11 transition |
| 51 | + unset($args[2]); |
| 52 | + } |
| 53 | + break; |
| 54 | + |
| 55 | + case Generator_PHPUnit9::class: |
| 56 | + $generatorMethod = 'getMock'; |
| 57 | + $args = [ |
| 58 | + EmptyClass::class, // string $type |
| 59 | + [$method], // $methods = [] |
| 60 | + [], // array $arguments = [] |
| 61 | + '', // string $mockClassName = '' |
| 62 | + false, // bool $callOriginalConstructor = true |
| 63 | + false, // bool $callOriginalClone = true |
| 64 | + true, // bool $callAutoload = true |
| 65 | + false, // bool $cloneArguments = true |
| 66 | + false, // bool $callOriginalMethods = false |
| 67 | + null, // object $proxyTarget = null |
| 68 | + false, // bool $allowMockingUnknownTypes = true |
| 69 | + true, // bool $returnValueGeneration = true |
| 70 | + ]; |
| 71 | + break; |
| 72 | + default: |
| 73 | + throw new ReflectionException('Unknown PHPUnit version'); |
42 | 74 | }
|
43 | 75 |
|
44 |
| - $mockObject = (new Generator())->testDouble(...$args); |
| 76 | + // @phpstan-ignore-next-line |
| 77 | + $mockObject = (new $generatorClass())->$generatorMethod(...$args); |
45 | 78 | assert($mockObject instanceof EmptyClass);
|
46 | 79 | assert($mockObject instanceof MockObject);
|
47 | 80 | $testCase->registerMockObject($mockObject);
|
|
0 commit comments