WIP add createMock to createStub rector

This commit is contained in:
Grégoire Paris 2020-03-22 15:38:35 +01:00 committed by TomasVotruba
parent 2da2c94cd2
commit 88f27689eb
3 changed files with 179 additions and 0 deletions

View File

@ -0,0 +1,102 @@
<?php
declare(strict_types=1);
namespace Rector\PHPUnit\Rector\MethodCall;
use PhpParser\Node;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\MethodCall;
use Rector\Core\PhpParser\Node\Manipulator\AssignManipulator;
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;
}
dump($this->methodCallManipulator->findMethodCallNamesOnVariable($parentNode->var));
return $node;
}
}

View File

@ -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;
}
}

View File

@ -0,0 +1,47 @@
<?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');
$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
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');
$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());
}
}
?>