[PHPUnit] Improve GetMockRector

This commit is contained in:
TomasVotruba 2020-01-17 14:46:48 +01:00
parent 760d0eff21
commit e343b39633
5 changed files with 78 additions and 8 deletions

View File

@ -13,6 +13,9 @@ use Rector\RectorDefinition\CodeSample;
use Rector\RectorDefinition\RectorDefinition;
/**
* @see https://github.com/sebastianbergmann/phpunit/blob/5.7.0/src/Framework/TestCase.php#L1623
* @see https://github.com/sebastianbergmann/phpunit/blob/6.0.0/src/Framework/TestCase.php#L1452
*
* @see \Rector\PHPUnit\Tests\Rector\GetMockRector\GetMockRectorTest
*/
final class GetMockRector extends AbstractPHPUnitRector
@ -45,8 +48,15 @@ final class GetMockRector extends AbstractPHPUnitRector
return null;
}
if (count($node->args) !== 1) {
return null;
if ($node instanceof MethodCall) {
if ($node->var instanceof MethodCall) {
return null;
}
}
// narrow args to one
if (count($node->args) > 1) {
$node->args = [$node->args[0]];
}
$node->name = new Identifier('createMock');

View File

@ -7,9 +7,6 @@ final class MyGetMockTest extends \PHPUnit\Framework\TestCase
public function test()
{
$firstMock = $this->getMock('SomeClass');
$secondMock = $this->buildMock()
->getMock();
}
}
@ -24,9 +21,6 @@ final class MyGetMockTest extends \PHPUnit\Framework\TestCase
public function test()
{
$firstMock = $this->createMock('SomeClass');
$secondMock = $this->buildMock()
->getMock();
}
}

View File

@ -0,0 +1,27 @@
<?php
namespace Rector\PHPUnit\Tests\Rector\GetMockRector\Fixture;
final class GetMockMultiTest extends \PHPUnit\Framework\TestCase
{
public function test()
{
$firstMock = $this->getMock('LDUser', [], [], '', false);
}
}
?>
-----
<?php
namespace Rector\PHPUnit\Tests\Rector\GetMockRector\Fixture;
final class GetMockMultiTest extends \PHPUnit\Framework\TestCase
{
public function test()
{
$firstMock = $this->createMock('LDUser');
}
}
?>

View File

@ -0,0 +1,12 @@
<?php
namespace Rector\PHPUnit\Tests\Rector\GetMockRector\Fixture;
final class SkipBuildMockTest extends \PHPUnit\Framework\TestCase
{
public function test()
{
$secondMock = $this->buildMock()
->getMock();
}
}

View File

@ -0,0 +1,27 @@
<?php
namespace Rector\PHPUnit\Tests\Rector\GetMockRector\Fixture;
final class StaticCallTest extends \PHPUnit\Framework\TestCase
{
public function test()
{
$firstMock = self::getMock('SomeClass');
}
}
?>
-----
<?php
namespace Rector\PHPUnit\Tests\Rector\GetMockRector\Fixture;
final class StaticCallTest extends \PHPUnit\Framework\TestCase
{
public function test()
{
$firstMock = self::createMock('SomeClass');
}
}
?>