Skip to content

Commit 39663bb

Browse files
committed
fix(phpunit): fix support for phpunit-9/10/11
1 parent 42631f6 commit 39663bb

File tree

2 files changed

+56
-23
lines changed

2 files changed

+56
-23
lines changed

phpstan.neon.dist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@ parameters:
1313
- path: test/phpunit/Helpers/ClassWithMethods.php
1414
message: '/^Method .*\\ClassWithMethods::privateMethod\(\) is unused\.$/'
1515
- path: src/Phpunit/AbstractMocked.php
16-
message: '/^Parameter #\d+ \S+ of method PHPUnit\\Framework\\MockObject\\Generator\\Generator::testDouble\(\)/'
16+
message: '/^Class PHPUnit\\Framework\\MockObject(\\Generator)+ not found./'

src/Phpunit/AbstractMocked.php

Lines changed: 55 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,44 +4,77 @@
44

55
namespace QratorLabs\Smocky\Phpunit;
66

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;
89
use PHPUnit\Framework\MockObject\MockObject;
910
use PHPUnit\Framework\TestCase;
1011
use QratorLabs\Smocky\EmptyClass;
12+
use ReflectionException;
1113
use ReflectionMethod;
1214

1315
use function assert;
16+
use function class_exists;
1417

1518
abstract class AbstractMocked
1619
{
1720
/**
1821
* @return MockObject&EmptyClass
22+
* @throws ReflectionException
1923
*/
2024
protected static function createEmptyMock(TestCase $testCase, string $method): MockObject
2125
{
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');
4274
}
4375

44-
$mockObject = (new Generator())->testDouble(...$args);
76+
// @phpstan-ignore-next-line
77+
$mockObject = (new $generatorClass())->$generatorMethod(...$args);
4578
assert($mockObject instanceof EmptyClass);
4679
assert($mockObject instanceof MockObject);
4780
$testCase->registerMockObject($mockObject);

0 commit comments

Comments
 (0)