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
1 change: 1 addition & 0 deletions config/set/phpunit/phpunit60.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ services:
- 'PHPUnit_Framework_MockObject_MockObject'

Rector\PHPUnit\Rector\ClassMethod\AddDoesNotPerformAssertionToNonAssertingTestRector: null
Rector\PHPUnit\Rector\MethodCall\GetMockBuilderGetMockToCreateMockRector: null
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

declare(strict_types=1);

namespace Rector\PHPUnit\Rector\MethodCall;

use PhpParser\Node;
use PhpParser\Node\Expr\MethodCall;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\CodeSample;
use Rector\RectorDefinition\RectorDefinition;

/**
* @see https://github.com/lmc-eu/steward/pull/187/files#diff-c7e8c65e59b8b4ff8b54325814d4ba55L80
*
* @see \Rector\PHPUnit\Tests\Rector\MethodCall\GetMockBuilderGetMockToCreateMockRector\GetMockBuilderGetMockToCreateMockRectorTest
*/
final class GetMockBuilderGetMockToCreateMockRector extends AbstractRector
{
public function getDefinition(): RectorDefinition
{
return new RectorDefinition('Remove getMockBuilder() to createMock()', [
new CodeSample(
<<<'PHP'
class SomeTest extends \PHPUnit\Framework\TestCase
{
public function test()
{
$applicationMock = $this->getMockBuilder('SomeClass')
->disableOriginalConstructor()
->getMock();
}
}
PHP
,
<<<'PHP'
class SomeTest extends \PHPUnit\Framework\TestCase
{
public function test()
{
$applicationMock = $this->createMock('SomeClass');
}
}
PHP

),
]);
}

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

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

if (! $node->var instanceof MethodCall) {
return null;
}

if ($this->isName($node->var->name, 'disableOriginalConstructor')) {
$getMockBuilderMethodCall = $node->var->var; // null;
} else {
$getMockBuilderMethodCall = $node->var;
}

/** @var MethodCall|null $getMockBuilderMethodCall */
if ($getMockBuilderMethodCall === null) {
return null;
}

if (! $this->isName($getMockBuilderMethodCall->name, 'getMockBuilder')) {
return null;
}

$args = $getMockBuilderMethodCall->args;
$thisVariable = $getMockBuilderMethodCall->var;

return new MethodCall($thisVariable, 'createMock', $args);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Rector\PHPUnit\Tests\Rector\MethodCall\GetMockBuilderGetMockToCreateMockRector\Fixture;

class SomeTest extends \PHPUnit\Framework\TestCase
{
public function test()
{
$applicationMock = $this->getMockBuilder('SomeClass')
->disableOriginalConstructor()
->getMock();
}
}

?>
-----
<?php

namespace Rector\PHPUnit\Tests\Rector\MethodCall\GetMockBuilderGetMockToCreateMockRector\Fixture;

class SomeTest extends \PHPUnit\Framework\TestCase
{
public function test()
{
$applicationMock = $this->createMock('SomeClass');
}
}

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

namespace Rector\PHPUnit\Tests\Rector\MethodCall\GetMockBuilderGetMockToCreateMockRector\Fixture;

class GetMockOnly extends \PHPUnit\Framework\TestCase
{
public function test()
{
$applicationMock = $this->getMockBuilder('SomeClass')
->getMock();
}
}

?>
-----
<?php

namespace Rector\PHPUnit\Tests\Rector\MethodCall\GetMockBuilderGetMockToCreateMockRector\Fixture;

class GetMockOnly extends \PHPUnit\Framework\TestCase
{
public function test()
{
$applicationMock = $this->createMock('SomeClass');
}
}

?>
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\GetMockBuilderGetMockToCreateMockRector;

use Iterator;
use Rector\PHPUnit\Rector\MethodCall\GetMockBuilderGetMockToCreateMockRector;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

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

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

protected function getRectorClass(): string
{
return GetMockBuilderGetMockToCreateMockRector::class;
}
}