Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Rector\PHPUnit\Tests\PHPUnit120\Rector\MethodCall\ExplicitMockExpectsCallRector\Fixture;

use PHPUnit\Framework\TestCase;

final class SkipIfNotTestMethod extends TestCase
{
private function decorateMock(): void
{
$someClass = $this->createMock(\stdClass::class);

$someClass->method('some');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,15 @@

namespace Rector\PHPUnit\PHPUnit120\Rector\MethodCall;

use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\Variable;
use PHPStan\Type\ObjectType;
use Rector\PHPStan\ScopeFetcher;
use Rector\PHPUnit\Enum\PHPUnitClassName;
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer;
use Rector\Rector\AbstractRector;
use Rector\ValueObject\MethodName;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

Expand All @@ -30,7 +29,7 @@ public function __construct(
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Add explicit expects() to mocks, to make expectations count explicit',
'Add explicit expects() to method() mock calls, to make expectations count explicit',
[
new CodeSample(
<<<'CODE_SAMPLE'
Expand Down Expand Up @@ -69,39 +68,54 @@ public function testMe()
*/
public function getNodeTypes(): array
{
return [MethodCall::class];
return [ClassMethod::class];
}

/**
* @param MethodCall $node
* @param ClassMethod $node
*/
public function refactor(Node $node): Node|null
{
if (! $this->testsNodeAnalyzer->isInTestClass($node)) {
return null;
}

$scope = ScopeFetcher::fetch($node);
if ($scope->getFunctionName() === MethodName::SET_UP) {
if (! $this->testsNodeAnalyzer->isTestClassMethod($node)) {
return null;
}

if (! $node->var instanceof Variable) {
return null;
}
$hasChanged = false;

if (! $this->isName($node->name, 'method')) {
return null;
}
$this->traverseNodesWithCallable((array) $node->stmts, function (Node $node) use (&$hasChanged): ?MethodCall {
if (! $node instanceof MethodCall) {
return null;
}

if (! $this->isObjectType($node->var, new ObjectType(PHPUnitClassName::MOCK_OBJECT))) {
return null;
}
if (! $node->var instanceof Variable) {
return null;
}

$node->var = new MethodCall($node->var, 'expects', [
new Arg(new MethodCall(new Variable('this'), 'atLeastOnce')),
]);
if (! $this->isName($node->name, 'method')) {
return null;
}

if (! $this->isObjectType($node->var, new ObjectType(PHPUnitClassName::MOCK_OBJECT))) {
return null;
}

$node->var = new MethodCall($node->var, 'expects', [
new Arg(new MethodCall(new Variable('this'), 'atLeastOnce')),
]);

$hasChanged = true;

return $node;
});

if ($hasChanged) {
return $node;
}

return $node;
return null;
}
}