Skip to content

Commit 7cb2270

Browse files
committed
[phpunit] [9.0] resolve WithConsecutiveRector naming ad-hoc to PHPUnit real code
1 parent 6e9d843 commit 7cb2270

File tree

3 files changed

+32
-43
lines changed

3 files changed

+32
-43
lines changed

src/Composer/ProjectPackageVersionResolver.php

Lines changed: 0 additions & 36 deletions
This file was deleted.

src/Enum/PHPUnitClassName.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,9 @@ final class PHPUnitClassName
2020
* @var string
2121
*/
2222
public const ASSERT = 'PHPUnit\Framework\Assert';
23+
24+
/**
25+
* @var string
26+
*/
27+
public const INVOCATION_ORDER = 'PHPUnit\Framework\MockObject\Rule\InvocationOrder';
2328
}

src/NodeFactory/MatcherInvocationCountMethodCallNodeFactory.php

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,28 @@
77
use PhpParser\Node\Expr\MethodCall;
88
use PhpParser\Node\Expr\Variable;
99
use PhpParser\Node\Identifier;
10-
use Rector\PHPUnit\Composer\ProjectPackageVersionResolver;
10+
use PHPStan\Reflection\ReflectionProvider;
1111
use Rector\PHPUnit\Enum\ConsecutiveVariable;
12+
use Rector\PHPUnit\Enum\PHPUnitClassName;
1213

14+
/**
15+
* Handle silent rename "getInvocationCount()" to "numberOfInvocations()" in PHPUnit 10
16+
* https://github.com/sebastianbergmann/phpunit/commit/2ba8b7fded44a1a75cf5712a3b7310a8de0b6bb8#diff-3b464bb32b9187dd2d047fd1c21773aa32c19b20adebc083e1a49267c524a980
17+
*/
1318
final readonly class MatcherInvocationCountMethodCallNodeFactory
1419
{
20+
/**
21+
* @var string
22+
*/
23+
private const GET_INVOCATION_COUNT = 'getInvocationCount';
24+
25+
/**
26+
* @var string
27+
*/
28+
private const NUMBER_OF_INVOCATIONS = 'numberOfInvocations';
29+
1530
public function __construct(
16-
private ProjectPackageVersionResolver $projectPackageVersionResolver
31+
private ReflectionProvider $reflectionProvider,
1732
) {
1833
}
1934

@@ -28,14 +43,19 @@ public function create(): MethodCall
2843

2944
private function getInvocationMethodName(): string
3045
{
31-
$projectPHPUnitVersion = $this->projectPackageVersionResolver->findPackageVersion('phpunit/phpunit');
46+
if (! $this->reflectionProvider->hasClass(PHPUnitClassName::INVOCATION_ORDER)) {
47+
// fallback to PHPUnit 9
48+
return self::GET_INVOCATION_COUNT;
49+
}
50+
51+
$invocationOrderClassReflection = $this->reflectionProvider->getClass(PHPUnitClassName::INVOCATION_ORDER);
3252

33-
if ($projectPHPUnitVersion === null || version_compare($projectPHPUnitVersion, '10.0', '>=')) {
34-
// phpunit 10 naming
35-
return 'numberOfInvocations';
53+
// phpunit 10 naming
54+
if ($invocationOrderClassReflection->hasNativeMethod(self::GET_INVOCATION_COUNT)) {
55+
return self::GET_INVOCATION_COUNT;
3656
}
3757

3858
// phpunit 9 naming
39-
return 'getInvocationCount';
59+
return self::NUMBER_OF_INVOCATIONS;
4060
}
4161
}

0 commit comments

Comments
 (0)