|
| 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