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
2 changes: 2 additions & 0 deletions config/sets/phpunit120.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Rector\PHPUnit\PHPUnit120\Rector\CallLike\CreateStubOverCreateMockArgRector;
use Rector\PHPUnit\PHPUnit120\Rector\Class_\AssertIsTypeMethodCallRector;
use Rector\PHPUnit\PHPUnit120\Rector\Class_\RemoveOverrideFinalConstructTestCaseRector;
use Rector\PHPUnit\PHPUnit120\Rector\ClassMethod\ExpressionCreateMockToCreateStubRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rules([
Expand All @@ -14,6 +15,7 @@

// stubs over mocks
CreateStubOverCreateMockArgRector::class,
ExpressionCreateMockToCreateStubRector::class,

// experimental, from PHPUnit 12.5.2
// @see https://github.com/sebastianbergmann/phpunit/commit/24c208d6a340c3071f28a9b5cce02b9377adfd43
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Rector\PHPUnit\Tests\PHPUnit120\Rector\CallLike\CreateStubOverCreateMockArgRector\Source;

final class InstanceWithMock
{
public function __construct(private $object)
{
}

public function getInner(): object
{
return $this->object;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Rector\PHPUnit\Tests\PHPUnit120\Rector\ClassMethod\ExpressionCreateMockToCreateStubRector;

use Iterator;
use PHPUnit\Framework\Attributes\DataProvider;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class ExpressionCreateMockToCreateStubRectorTest extends AbstractRectorTestCase
{
#[DataProvider('provideData')]
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

public static function provideData(): Iterator
{
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule.php';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace Rector\PHPUnit\Tests\PHPUnit120\Rector\ClassMethod\ExpressionCreateMockToCreateStubRector\Fixture;

use PHPUnit\Framework\TestCase;
use Rector\PHPUnit\Tests\PHPUnit120\Rector\ClassMethod\ExpressionCreateMockToCreateStubRector\Source\ClassWithDependency;

final class SomeTest extends TestCase
{
public function test()
{
$mock = $this->createMock(\stdClass::class);

$someObject = new ClassWithDependency($mock);
$this->assertSame($mock, $someObject->getDependency());
}
}

?>
-----
<?php

namespace Rector\PHPUnit\Tests\PHPUnit120\Rector\ClassMethod\ExpressionCreateMockToCreateStubRector\Fixture;

use PHPUnit\Framework\TestCase;
use Rector\PHPUnit\Tests\PHPUnit120\Rector\ClassMethod\ExpressionCreateMockToCreateStubRector\Source\ClassWithDependency;

final class SomeTest extends TestCase
{
public function test()
{
$mock = $this->createStub(\stdClass::class);

$someObject = new ClassWithDependency($mock);
$this->assertSame($mock, $someObject->getDependency());
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Rector\PHPUnit\Tests\PHPUnit120\Rector\ClassMethod\ExpressionCreateMockToCreateStubRector\Fixture;

use PHPUnit\Framework\TestCase;
use Rector\PHPUnit\Tests\PHPUnit120\Rector\ClassMethod\ExpressionCreateMockToCreateStubRector\Source\ClassWithDependency;

final class SkipMocking extends TestCase
{
public function test()
{
$mock = $this->createMock(\stdClass::class);
$mock->expects($this->once())->method('someMethod')->willReturn('someValue');

$someObject = new ClassWithDependency($mock);
$this->assertSame($mock, $someObject->getDependency());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Rector\PHPUnit\Tests\PHPUnit120\Rector\ClassMethod\ExpressionCreateMockToCreateStubRector\Fixture;

use PHPUnit\Framework\TestCase;
use Rector\PHPUnit\Tests\PHPUnit120\Rector\ClassMethod\ExpressionCreateMockToCreateStubRector\Source\ClassWithDependency;

final class SkipPropertyForExternalScope extends TestCase
{
private \PHPUnit\Framework\MockObject\MockObject $mock;

public function test()
{
$this->mock = $this->createMock(\stdClass::class);

$someObject = new ClassWithDependency($this->mock);
$this->assertSame($this->mock, $someObject->getDependency());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Rector\PHPUnit\Tests\PHPUnit120\Rector\ClassMethod\ExpressionCreateMockToCreateStubRector\Fixture;

use PHPUnit\Framework\TestCase;
use Rector\PHPUnit\Tests\PHPUnit120\Rector\ClassMethod\ExpressionCreateMockToCreateStubRector\Source\ClassWithDependency;

final class SkipUsedOutsideArg extends TestCase
{
public function test()
{
$mock = $this->createMock(\stdClass::class);

if ($mock instanceof \stdClass) {
// do something
}

$someObject = new ClassWithDependency($mock);
$this->assertSame($mock, $someObject->getDependency());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Rector\PHPUnit\Tests\PHPUnit120\Rector\ClassMethod\ExpressionCreateMockToCreateStubRector\Source;

final class ClassWithDependency
{
public function __construct(
private $dependency,
) {
}

public function getDependency()
{
return $this->dependency;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\PHPUnit\PHPUnit120\Rector\ClassMethod\ExpressionCreateMockToCreateStubRector;

return RectorConfig::configure()
->withRules(rules: [ExpressionCreateMockToCreateStubRector::class]);
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
<?php

declare(strict_types=1);

namespace Rector\PHPUnit\PHPUnit120\Rector\ClassMethod;

use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\CallLike;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\New_;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Identifier;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Expression;
use Rector\PhpParser\Node\BetterNodeFinder;
use Rector\PHPUnit\CodeQuality\NodeAnalyser\AssignedMocksCollector;
use Rector\PHPUnit\CodeQuality\NodeFinder\VariableFinder;
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @see \Rector\PHPUnit\Tests\PHPUnit120\Rector\ClassMethod\ExpressionCreateMockToCreateStubRector\ExpressionCreateMockToCreateStubRectorTest
*/
final class ExpressionCreateMockToCreateStubRector extends AbstractRector
{
public function __construct(
private readonly AssignedMocksCollector $assignedMocksCollector,
private readonly TestsNodeAnalyzer $testsNodeAnalyzer,
private readonly VariableFinder $variableFinder,
private readonly BetterNodeFinder $betterNodeFinder,
) {
}

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Replace createMock() assigned to variable that is only used as arg with no expectations, to createStub()',
[
new CodeSample(
<<<'CODE_SAMPLE'
use PHPUnit\Framework\TestCase;

final class SomeTest extends TestCase
{
public function test(): void
{
$mock = $this->createMock(SomeClass::class);

$someObject = new SomeClass($mock);
$this->assertSame($mock, $someObject->getDependency());
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
use PHPUnit\Framework\TestCase;

final class SomeTest extends TestCase
{
public function test(): void
{
$mock = $this->createStub(SomeClass::class);

$someObject = new SomeClass($mock);
$this->assertSame($mock, $someObject->getDependency());
}
}
CODE_SAMPLE
),

]
);
}

public function getNodeTypes(): array
{
return [ClassMethod::class];
}

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

if ($node->stmts === null || count($node->stmts) < 2) {
return null;
}

$hasChanged = false;

foreach ($node->stmts as $stmt) {
if (! $stmt instanceof Expression) {
continue;
}

if (! $stmt->expr instanceof Assign) {
continue;
}

$typeArg = $this->assignedMocksCollector->matchCreateMockArgAssignedToVariable($stmt->expr);
if (! $typeArg instanceof Arg) {
continue;
}

/** @var Assign $assign */
$assign = $stmt->expr;

if (! $assign->var instanceof Variable) {
continue;
}

$assignedVariable = $assign->var;
$variableName = $this->getName($assignedVariable);
if ($variableName === null) {
continue;
}

// find variable usages outside call like and inside it
$usedVariables = $this->variableFinder->find($node, $variableName);

// used variable in calls
/** @var array<StaticCall|MethodCall|New_> $callLikes */
$callLikes = $this->betterNodeFinder->findInstancesOfScoped($node->stmts, [CallLike::class]);

$callLikeUsedVariables = $this->collectVariableInCallLikeArg($callLikes, $variableName);

if (count($usedVariables) - 1 !== count($callLikeUsedVariables)) {
continue;
}

// here we can flip the createMock() to createStub()

if (! $assign->expr instanceof MethodCall) {
continue;
}

$methodCall = $assign->expr;
$methodCall->name = new Identifier('createStub');

$hasChanged = true;
}

if ($hasChanged) {
return $node;
}

return null;
}

/**
* @param CallLike[] $callLikes
* @return Variable[]
*/
private function collectVariableInCallLikeArg(array $callLikes, string $variableName): array
{
$callLikeUsedVariables = [];

foreach ($callLikes as $callLike) {
if ($callLike->isFirstClassCallable()) {
continue;
}

foreach ($callLike->getArgs() as $arg) {
if (! $arg->value instanceof Variable) {
continue;
}

if (! $this->isName($arg->value, $variableName)) {
continue;
}

$callLikeUsedVariables[] = $arg->value;
}
}

return $callLikeUsedVariables;
}
}