Skip to content

Commit 660eece

Browse files
committed
[phpunit 12] Add ExplicitMockExpectsCallRector
1 parent db0d1d5 commit 660eece

File tree

9 files changed

+176
-6
lines changed

9 files changed

+176
-6
lines changed

rules-tests/PHPUnit120/Rector/MethodCall/AssertIsTypeMethodCallRector/AssertIsTypeMethodCallRectorTest.php renamed to rules-tests/PHPUnit120/Rector/Class_/AssertIsTypeMethodCallRector/AssertIsTypeMethodCallRectorTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
namespace Rector\PHPUnit\Tests\PHPUnit120\Rector\MethodCall\AssertIsTypeMethodCallRector;
5+
namespace Rector\PHPUnit\Tests\PHPUnit120\Rector\Class_\AssertIsTypeMethodCallRector;
66

77
use Iterator;
88
use PHPUnit\Framework\Attributes\DataProvider;

rules-tests/PHPUnit120/Rector/MethodCall/AssertIsTypeMethodCallRector/Fixture/fixture.php.inc renamed to rules-tests/PHPUnit120/Rector/Class_/AssertIsTypeMethodCallRector/Fixture/fixture.php.inc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Rector\PHPUnit\Tests\PHPUnit120\Rector\MethodCall\AssertIsTypeMethodCallRector\Fixture;
3+
namespace Rector\PHPUnit\Tests\PHPUnit120\Rector\Class_\AssertIsTypeMethodCallRector\Fixture;
44

55
use PHPUnit\Framework\TestCase;
66

@@ -31,7 +31,7 @@ final class Fixture extends TestCase
3131
-----
3232
<?php
3333

34-
namespace Rector\PHPUnit\Tests\PHPUnit120\Rector\MethodCall\AssertIsTypeMethodCallRector\Fixture;
34+
namespace Rector\PHPUnit\Tests\PHPUnit120\Rector\Class_\AssertIsTypeMethodCallRector\Fixture;
3535

3636
use PHPUnit\Framework\TestCase;
3737

rules-tests/PHPUnit120/Rector/MethodCall/AssertIsTypeMethodCallRector/Fixture/static_fixture.php.inc renamed to rules-tests/PHPUnit120/Rector/Class_/AssertIsTypeMethodCallRector/Fixture/static_fixture.php.inc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Rector\PHPUnit\Tests\PHPUnit120\Rector\MethodCall\AssertIsTypeMethodCallRector\Fixture;
3+
namespace Rector\PHPUnit\Tests\PHPUnit120\Rector\Class_\AssertIsTypeMethodCallRector\Fixture;
44

55
use PHPUnit\Framework\TestCase;
66

@@ -31,7 +31,7 @@ final class StaticFixture extends TestCase
3131
-----
3232
<?php
3333

34-
namespace Rector\PHPUnit\Tests\PHPUnit120\Rector\MethodCall\AssertIsTypeMethodCallRector\Fixture;
34+
namespace Rector\PHPUnit\Tests\PHPUnit120\Rector\Class_\AssertIsTypeMethodCallRector\Fixture;
3535

3636
use PHPUnit\Framework\TestCase;
3737

rules-tests/PHPUnit120/Rector/MethodCall/AssertIsTypeMethodCallRector/config/configured_rule.php renamed to rules-tests/PHPUnit120/Rector/Class_/AssertIsTypeMethodCallRector/config/configured_rule.php

File renamed without changes.
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\MethodCall\ExplicitMockExpectsCallRector;
6+
7+
use Iterator;
8+
use PHPUnit\Framework\Attributes\DataProvider;
9+
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
10+
11+
final class ExplicitMockExpectsCallRectorTest 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: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\PHPUnit120\Rector\MethodCall\ExplicitMockExpectsCallRector\Fixture;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
final class Fixture extends TestCase
8+
{
9+
public function testMethod(): void
10+
{
11+
$someClass = $this->createMock(\stdClass::class);
12+
13+
$someClass->method('some');
14+
}
15+
}
16+
17+
?>
18+
-----
19+
<?php
20+
21+
namespace Rector\PHPUnit\Tests\PHPUnit120\Rector\MethodCall\ExplicitMockExpectsCallRector\Fixture;
22+
23+
use PHPUnit\Framework\TestCase;
24+
25+
final class Fixture extends TestCase
26+
{
27+
public function testMethod(): void
28+
{
29+
$someClass = $this->createMock(\stdClass::class);
30+
31+
$someClass->expects($this->atLeastOnce())->method('some');
32+
}
33+
}
34+
35+
?>
36+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\Config\RectorConfig;
6+
7+
return RectorConfig::configure()
8+
->withRules([\Rector\PHPUnit\PHPUnit120\Rector\MethodCall\ExplicitMockExpectsCallRector::class]);

rules/PHPUnit120/Rector/Class_/AssertIsTypeMethodCallRector.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
/**
1818
* @see https://github.com/sebastianbergmann/phpunit/issues/6053
1919
* @see https://github.com/sebastianbergmann/phpunit/blob/12.0.0/ChangeLog-12.0.md
20-
* @see \Rector\PHPUnit\Tests\PHPUnit120\Rector\MethodCall\AssertIsTypeMethodCallRector\AssertIsTypeMethodCallRectorTest
20+
*
21+
* @see \Rector\PHPUnit\Tests\PHPUnit120\Rector\Class_\AssertIsTypeMethodCallRector\AssertIsTypeMethodCallRectorTest
2122
*/
2223
final class AssertIsTypeMethodCallRector extends AbstractRector
2324
{
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\PHPUnit\PHPUnit120\Rector\MethodCall;
6+
7+
use PhpParser\Node;
8+
use PHPStan\Type\ObjectType;
9+
use Rector\PHPUnit\Enum\PHPUnitClassName;
10+
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer;
11+
use Rector\Rector\AbstractRector;
12+
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
13+
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
14+
15+
/**
16+
* @see \Rector\PHPUnit\Tests\PHPUnit120\Rector\MethodCall\ExplicitMockExpectsCallRector\ExplicitMockExpectsCallRectorTest
17+
*/
18+
final class ExplicitMockExpectsCallRector extends AbstractRector
19+
{
20+
public function __construct(
21+
private readonly TestsNodeAnalyzer $testsNodeAnalyzer,
22+
) {
23+
}
24+
25+
public function getRuleDefinition(): RuleDefinition
26+
{
27+
return new RuleDefinition(
28+
'Add explicit expects() to mocks, to make expectations count explicit',
29+
[
30+
new CodeSample(
31+
<<<'CODE_SAMPLE'
32+
use PHPUnit\Framework\TestCase;
33+
34+
final class SomeClass extends TestCase
35+
{
36+
public function testMe()
37+
{
38+
$someMock = $this->createMock(\stdClass::class);
39+
$someMock->method('some');
40+
}
41+
}
42+
CODE_SAMPLE
43+
,
44+
<<<'CODE_SAMPLE'
45+
use PHPUnit\Framework\TestCase;
46+
47+
final class SomeClass extends TestCase
48+
{
49+
public function testMe()
50+
{
51+
$someMock = $this->createMock(\stdClass::class);
52+
$someMock->expects($this->atLeastOnce())->method('some');
53+
}
54+
}
55+
CODE_SAMPLE
56+
,
57+
),
58+
],
59+
);
60+
}
61+
62+
/**
63+
* @return array<class-string<Node>>
64+
*/
65+
public function getNodeTypes(): array
66+
{
67+
return [Node\Expr\MethodCall::class];
68+
}
69+
70+
/**
71+
* @param Node\Expr\MethodCall $node
72+
*/
73+
public function refactor(Node $node): Node|null
74+
{
75+
if (! $this->testsNodeAnalyzer->isInTestClass($node)) {
76+
return null;
77+
}
78+
79+
if (! $node->var instanceof Node\Expr\Variable && ! $node->var instanceof Node\Expr\PropertyFetch) {
80+
return null;
81+
}
82+
83+
if (! $this->isName($node->name, 'method')) {
84+
return null;
85+
}
86+
87+
if (! $this->isObjectType($node->var, new ObjectType(PHPUnitClassName::MOCK_OBJECT))) {
88+
return null;
89+
}
90+
91+
$node->var = new Node\Expr\MethodCall($node->var, 'expects', [
92+
new Node\Arg(new Node\Expr\MethodCall(new Node\Expr\Variable('this'), 'atLeastOnce')),
93+
]);
94+
95+
return $node;
96+
}
97+
}

0 commit comments

Comments
 (0)