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
34 changes: 33 additions & 1 deletion docs/AllRectorsOverview.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# All 465 Rectors Overview
# All 466 Rectors Overview

- [Projects](#projects)
- [General](#general)
Expand Down Expand Up @@ -5333,6 +5333,38 @@ Turns true/false comparisons to their method name alternatives in PHPUnit TestCa

<br>

### `CreateMockToCreateStubRector`

- class: [`Rector\PHPUnit\Rector\MethodCall\CreateMockToCreateStubRector`](/../master/rules/phpunit/src/Rector/MethodCall/CreateMockToCreateStubRector.php)
- [test fixtures](/../master/rules/phpunit/tests/Rector/MethodCall/CreateMockToCreateStubRector/Fixture)

Replaces createMock() with createStub() when relevant

```diff
use PHPUnit\Framework\TestCase

class MyTest extends TestCase
{
public function testItBehavesAsExpected(): void
{
- $stub = $this->createMock(\Exception::class);
+ $stub = $this->createStub(\Exception::class);
$stub->method('getMessage')
->willReturn('a message');

$mock = $this->createMock(\Exception::class);
$mock->expects($this->once())
->method('getMessage')
->willReturn('a message');

self::assertSame('a message', $stub->getMessage());
self::assertSame('a message', $mock->getMessage());
}
}
```

<br>

### `DelegateExceptionArgumentsRector`

- class: [`Rector\PHPUnit\Rector\DelegateExceptionArgumentsRector`](/../master/rules/phpunit/src/Rector/DelegateExceptionArgumentsRector.php)
Expand Down
5 changes: 5 additions & 0 deletions packages/node-type-resolver/src/Node/AttributeKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,9 @@ final class AttributeKey
* @var string
*/
public const PHP_DOC_INFO = PhpDocInfo::class;

/**
* @var string
*/
public const METHOD_CALL_NODE_CALLER_NAME = 'method_call_variable_name';
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Rector\NodeCollector\NodeVisitor\NodeCollectorNodeVisitor;
use Rector\NodeTypeResolver\NodeVisitor\FileInfoNodeVisitor;
use Rector\NodeTypeResolver\NodeVisitor\FunctionMethodAndClassNodeVisitor;
use Rector\NodeTypeResolver\NodeVisitor\MethodCallNodeVisitor;
use Rector\NodeTypeResolver\NodeVisitor\NamespaceNodeVisitor;
use Rector\NodeTypeResolver\NodeVisitor\ParentAndNextNodeVisitor;
use Rector\NodeTypeResolver\NodeVisitor\PhpDocInfoNodeVisitor;
Expand Down Expand Up @@ -70,6 +71,11 @@ final class NodeScopeAndMetadataDecorator
*/
private $phpDocInfoNodeVisitor;

/**
* @var MethodCallNodeVisitor
*/
private $methodCallNodeVisitor;

public function __construct(
NodeScopeResolver $nodeScopeResolver,
ParentAndNextNodeVisitor $parentAndNextNodeVisitor,
Expand All @@ -80,7 +86,8 @@ public function __construct(
FileInfoNodeVisitor $fileInfoNodeVisitor,
NodeCollectorNodeVisitor $nodeCollectorNodeVisitor,
PhpDocInfoNodeVisitor $phpDocInfoNodeVisitor,
Configuration $configuration
Configuration $configuration,
MethodCallNodeVisitor $methodCallNodeVisitor
) {
$this->nodeScopeResolver = $nodeScopeResolver;
$this->parentAndNextNodeVisitor = $parentAndNextNodeVisitor;
Expand All @@ -92,6 +99,7 @@ public function __construct(
$this->nodeCollectorNodeVisitor = $nodeCollectorNodeVisitor;
$this->configuration = $configuration;
$this->phpDocInfoNodeVisitor = $phpDocInfoNodeVisitor;
$this->methodCallNodeVisitor = $methodCallNodeVisitor;
}

/**
Expand Down Expand Up @@ -125,6 +133,7 @@ public function decorateNodesFromFile(array $nodes, string $filePath, bool $need
$nodeTraverser->addVisitor($this->parentAndNextNodeVisitor);
$nodeTraverser->addVisitor($this->functionMethodAndClassNodeVisitor);
$nodeTraverser->addVisitor($this->namespaceNodeVisitor);
$nodeTraverser->addVisitor($this->methodCallNodeVisitor);
$nodeTraverser->addVisitor($this->phpDocInfoNodeVisitor);

$nodes = $nodeTraverser->traverse($nodes);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

declare(strict_types=1);

namespace Rector\NodeTypeResolver\NodeVisitor;

use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Name;
use PhpParser\NodeVisitorAbstract;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\NodeTypeResolver\Node\AttributeKey;

final class MethodCallNodeVisitor extends NodeVisitorAbstract
{
/**
* @var Expr|Name
*/
private $callerNode;

/**
* @var NodeNameResolver
*/
private $nodeNameResolver;

public function __construct(NodeNameResolver $nodeNameResolver)
{
$this->nodeNameResolver = $nodeNameResolver;
}

/**
* @return int|Node|void|null
*/
public function enterNode(Node $node)
{
$this->processMethodCall($node);

return $node;
}

private function processMethodCall(Node $node): void
{
if (! $node instanceof MethodCall) {
return;
}

$callerNode = $node->var;
if ($callerNode instanceof MethodCall) {
while ($callerNode instanceof MethodCall) {
$callerNode = $callerNode->var;
}
}

if ($callerNode instanceof StaticCall) {
while ($callerNode instanceof StaticCall) {
$callerNode = $callerNode->class;
}
}

$this->callerNode = $callerNode;
$currentCallerName = $this->nodeNameResolver->getName($this->callerNode);

$node->setAttribute(AttributeKey::METHOD_CALL_NODE_CALLER_NAME, $currentCallerName);
}
}
122 changes: 122 additions & 0 deletions rules/phpunit/src/Rector/MethodCall/CreateMockToCreateStubRector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
<?php

declare(strict_types=1);

namespace Rector\PHPUnit\Rector\MethodCall;

use PhpParser\Node;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Identifier;
use Rector\Core\PhpParser\Node\Manipulator\MethodCallManipulator;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\RectorDefinition\CodeSample;
use Rector\Core\RectorDefinition\RectorDefinition;
use Rector\NodeTypeResolver\Node\AttributeKey;

/**
* @see https://github.com/sebastianbergmann/phpunit/issues/3120
*
* @see \Rector\PHPUnit\Tests\Rector\MethodCall\CreateMockToCreateStubRector\CreateMockToCreateStubRectorTest
*/
final class CreateMockToCreateStubRector extends AbstractRector
{
/**
* @var MethodCallManipulator
*/
private $methodCallManipulator;

public function __construct(MethodCallManipulator $methodCallManipulator)
{
$this->methodCallManipulator = $methodCallManipulator;
}

public function getDefinition(): RectorDefinition
{
return new RectorDefinition('Replaces createMock() with createStub() when relevant', [
new CodeSample(
<<<'PHP'
use PHPUnit\Framework\TestCase

class MyTest extends TestCase
{
public function testItBehavesAsExpected(): void
{
$stub = $this->createMock(\Exception::class);
$stub->method('getMessage')
->willReturn('a message');

$mock = $this->createMock(\Exception::class);
$mock->expects($this->once())
->method('getMessage')
->willReturn('a message');

self::assertSame('a message', $stub->getMessage());
self::assertSame('a message', $mock->getMessage());
}
}
PHP
,
<<<'PHP'
use PHPUnit\Framework\TestCase

class MyTest extends TestCase
{
public function testItBehavesAsExpected(): void
{
$stub = $this->createStub(\Exception::class);
$stub->method('getMessage')
->willReturn('a message');

$mock = $this->createMock(\Exception::class);
$mock->expects($this->once())
->method('getMessage')
->willReturn('a message');

self::assertSame('a message', $stub->getMessage());
self::assertSame('a message', $mock->getMessage());
}
}
PHP
),
]);
}

/**
* @return string[]
*/
public function getNodeTypes(): array
{
return [MethodCall::class];
}

/**
* @param MethodCall $node
*/
public function refactor(Node $node): ?Node
{
if (! $this->isName($node->name, 'createMock')) {
return null;
}

$parentNode = $node->getAttribute(AttributeKey::PARENT_NODE);
if (! $parentNode instanceof Assign) {
return null;
}

$mockVariable = $parentNode->var;
if (! $mockVariable instanceof Variable) {
return null;
}

$methodCallNamesOnVariable = $this->methodCallManipulator->findMethodCallNamesOnVariable($mockVariable);
if (in_array('expects', $methodCallNamesOnVariable, true)) {
return null;
}

$node->name = new Identifier('createStub');

return $node;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace Rector\PHPUnit\Tests\Rector\MethodCall\CreateMockToCreateStubRector;

use Iterator;
use Rector\Core\Testing\PHPUnit\AbstractRectorTestCase;
use Rector\PHPUnit\Rector\MethodCall\CreateMockToCreateStubRector;

final class CreateMockToCreateStubRectorTest extends AbstractRectorTestCase
{
/**
* @dataProvider provideData()
*/
public function test(string $file): void
{
$this->doTestFile($file);
}

public function provideData(): Iterator
{
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

protected function getRectorClass(): string
{
return CreateMockToCreateStubRector::class;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Rector\phpunit\Tests\Rector\MethodCall\CreateMockToCreateStubRector\Fixture;

use PHPUnit\Framework\TestCase;

class MyTest extends TestCase
{
public function testItBehavesAsExpected(): void
{
$stub = $this->createMock(\Exception::class);
$stub->method('getMessage')
->willReturn('a message');
}
}

?>
-----
<?php

namespace Rector\phpunit\Tests\Rector\MethodCall\CreateMockToCreateStubRector\Fixture;

use PHPUnit\Framework\TestCase;

class MyTest extends TestCase
{
public function testItBehavesAsExpected(): void
{
$stub = $this->createStub(\Exception::class);
$stub->method('getMessage')
->willReturn('a message');
}
}

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

namespace Rector\phpunit\Tests\Rector\MethodCall\CreateMockToCreateStubRector\Fixture;

use PHPUnit\Framework\TestCase;

class SkipWithExpects extends TestCase
{
public function testItBehavesAsExpected(): void
{
$mock = $this->createMock(\Exception::class);
$mock->expects($this->once())
->method('getMessage')
->willReturn('a message');
}
}
Loading