Skip to content

Commit ed6bbc7

Browse files
authored
[PHPUnit120] Add AnyMatcherToNewAnyInvokedCountRector for deprecated any() matcher assign (#764)
1 parent 2db2e4c commit ed6bbc7

6 files changed

Lines changed: 202 additions & 0 deletions

File tree

‎config/sets/composer-based.php‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
use Rector\PHPUnit\PHPUnit110\Rector\Class_\NamedArgumentForDataProviderRector;
3131
use Rector\PHPUnit\PHPUnit110\Rector\ClassMethod\ExpectsParamToMockObjectRector;
3232
use Rector\PHPUnit\PHPUnit110\Rector\ClassMethod\MockObjectArgCreateStubToCreateMockRector;
33+
use Rector\PHPUnit\PHPUnit120\Rector\Assign\AnyMatcherToNewAnyInvokedCountRector;
3334
use Rector\PHPUnit\PHPUnit120\Rector\CallLike\CreateStubInCoalesceArgRector;
3435
use Rector\PHPUnit\PHPUnit120\Rector\CallLike\CreateStubOverCreateMockArgRector;
3536
use Rector\PHPUnit\PHPUnit120\Rector\Class_\AllowMockObjectsForDataProviderRector;
@@ -106,6 +107,9 @@
106107
// the TestCase::__construct() is final since PHPUnit 12.0.3
107108
RemoveOverrideFinalConstructTestCaseRector::class,
108109

110+
// the any() matcher was deprecated in PHPUnit 12.5
111+
AnyMatcherToNewAnyInvokedCountRector::class,
112+
109113
// the AllowMockObjectsWithoutExpectations attribute exists since PHPUnit 12.5.2
110114
AllowMockObjectsWhereParentClassRector::class,
111115
AllowMockObjectsForDataProviderRector::class,
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\PHPUnit\Tests\PHPUnit120\Rector\Assign\AnyMatcherToNewAnyInvokedCountRector;
6+
7+
use Iterator;
8+
use PHPUnit\Framework\Attributes\DataProvider;
9+
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
10+
11+
final class AnyMatcherToNewAnyInvokedCountRectorTest extends AbstractRectorTestCase
12+
{
13+
#[DataProvider('provideData')]
14+
public function test(string $filePath): void
15+
{
16+
$this->doTestFile($filePath);
17+
}
18+
19+
public static function provideData(): Iterator
20+
{
21+
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
22+
}
23+
24+
public function provideConfigFilePath(): string
25+
{
26+
return __DIR__ . '/config/configured_rule.php';
27+
}
28+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\PHPUnit120\Rector\Assign\AnyMatcherToNewAnyInvokedCountRector\Fixture;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
final class Fixture extends TestCase
8+
{
9+
public function test(): void
10+
{
11+
$matcher = $this->any();
12+
}
13+
}
14+
15+
?>
16+
-----
17+
<?php
18+
19+
namespace Rector\PHPUnit\Tests\PHPUnit120\Rector\Assign\AnyMatcherToNewAnyInvokedCountRector\Fixture;
20+
21+
use PHPUnit\Framework\TestCase;
22+
23+
final class Fixture extends TestCase
24+
{
25+
public function test(): void
26+
{
27+
$matcher = new \PHPUnit\Framework\MockObject\Rule\AnyInvokedCount();
28+
}
29+
}
30+
31+
?>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\PHPUnit120\Rector\Assign\AnyMatcherToNewAnyInvokedCountRector\Fixture;
4+
5+
final class SkipNonTestCase
6+
{
7+
public function run(): void
8+
{
9+
$matcher = $this->any();
10+
}
11+
12+
public function any(): string
13+
{
14+
return 'any';
15+
}
16+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\Config\RectorConfig;
6+
use Rector\PHPUnit\PHPUnit120\Rector\Assign\AnyMatcherToNewAnyInvokedCountRector;
7+
8+
return static function (RectorConfig $rectorConfig): void {
9+
$rectorConfig->rule(AnyMatcherToNewAnyInvokedCountRector::class);
10+
};
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\PHPUnit\PHPUnit120\Rector\Assign;
6+
7+
use PhpParser\Node;
8+
use PhpParser\Node\Expr\Assign;
9+
use PhpParser\Node\Expr\MethodCall;
10+
use PhpParser\Node\Expr\New_;
11+
use PhpParser\Node\Name\FullyQualified;
12+
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer;
13+
use Rector\Rector\AbstractRector;
14+
use Rector\VersionBonding\Contract\ComposerPackageConstraintInterface;
15+
use Rector\VersionBonding\ValueObject\ComposerPackageConstraint;
16+
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
17+
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
18+
19+
/**
20+
* The TestCase::any() method was deprecated in PHPUnit 12.5
21+
*
22+
* @see https://github.com/sebastianbergmann/phpunit/issues/6461
23+
*
24+
* @see \Rector\PHPUnit\Tests\PHPUnit120\Rector\Assign\AnyMatcherToNewAnyInvokedCountRector\AnyMatcherToNewAnyInvokedCountRectorTest
25+
*/
26+
final class AnyMatcherToNewAnyInvokedCountRector extends AbstractRector implements ComposerPackageConstraintInterface
27+
{
28+
private const string ANY_INVOKED_COUNT_CLASS = 'PHPUnit\Framework\MockObject\Rule\AnyInvokedCount';
29+
30+
public function __construct(
31+
private readonly TestsNodeAnalyzer $testsNodeAnalyzer
32+
) {
33+
}
34+
35+
public function provideComposerPackageConstraint(): ComposerPackageConstraint
36+
{
37+
return new ComposerPackageConstraint('phpunit/phpunit', '>=12.5');
38+
}
39+
40+
public function getRuleDefinition(): RuleDefinition
41+
{
42+
return new RuleDefinition(
43+
'Change deprecated `$this->any()` matcher assign to direct `new AnyInvokedCount()`',
44+
[
45+
new CodeSample(
46+
<<<'CODE_SAMPLE'
47+
use PHPUnit\Framework\TestCase;
48+
49+
final class SomeTest extends TestCase
50+
{
51+
public function test(): void
52+
{
53+
$matcher = $this->any();
54+
}
55+
}
56+
CODE_SAMPLE
57+
,
58+
<<<'CODE_SAMPLE'
59+
use PHPUnit\Framework\MockObject\Rule\AnyInvokedCount;
60+
use PHPUnit\Framework\TestCase;
61+
62+
final class SomeTest extends TestCase
63+
{
64+
public function test(): void
65+
{
66+
$matcher = new AnyInvokedCount();
67+
}
68+
}
69+
CODE_SAMPLE
70+
),
71+
]
72+
);
73+
}
74+
75+
/**
76+
* @return array<class-string<Node>>
77+
*/
78+
public function getNodeTypes(): array
79+
{
80+
return [Assign::class];
81+
}
82+
83+
/**
84+
* @param Assign $node
85+
*/
86+
public function refactor(Node $node): ?Assign
87+
{
88+
if (! $node->expr instanceof MethodCall) {
89+
return null;
90+
}
91+
92+
$methodCall = $node->expr;
93+
if ($methodCall->isFirstClassCallable()) {
94+
return null;
95+
}
96+
97+
if (! $this->isName($methodCall->name, 'any')) {
98+
return null;
99+
}
100+
101+
if ($methodCall->getArgs() !== []) {
102+
return null;
103+
}
104+
105+
if (! $this->testsNodeAnalyzer->isPHPUnitTestCaseCall($methodCall)) {
106+
return null;
107+
}
108+
109+
$node->expr = new New_(new FullyQualified(self::ANY_INVOKED_COUNT_CLASS));
110+
111+
return $node;
112+
}
113+
}

0 commit comments

Comments
 (0)