mirror of
https://github.com/rectorphp/rector.git
synced 2025-01-17 21:38:22 +01:00
[MockistaToMocker] init
This commit is contained in:
parent
3383d3d301
commit
a6598e5e82
@ -130,7 +130,8 @@
|
||||
"Rector\\VendorLocker\\": "packages/vendor-locker/src",
|
||||
"Rector\\Performance\\": "rules/performance/src",
|
||||
"Rector\\Naming\\": "rules/naming/src",
|
||||
"Rector\\Order\\": "rules/order/src"
|
||||
"Rector\\Order\\": "rules/order/src",
|
||||
"Rector\\MockistaToMockery\\": "rules/mockista-to-mockery/src"
|
||||
}
|
||||
},
|
||||
"autoload-dev": {
|
||||
@ -199,7 +200,8 @@
|
||||
"Rector\\Utils\\PHPStanStaticTypeMapperChecker\\": "utils/phpstan-static-type-mapper-checker/src",
|
||||
"Rector\\Performance\\Tests\\": "rules/performance/tests",
|
||||
"Rector\\Naming\\Tests\\": "rules/naming/tests",
|
||||
"Rector\\Order\\Tests\\": "rules/order/tests"
|
||||
"Rector\\Order\\Tests\\": "rules/order/tests",
|
||||
"Rector\\MockistaToMockery\\Tests\\": "rules/mockista-to-mockery/tests"
|
||||
},
|
||||
"classmap": [
|
||||
"rules/cakephp/tests/Rector/Name/ImplicitShortClassNameUseStatementRector/Source",
|
||||
|
3
config/set/framework-migration/mockista-to-mockery.yaml
Normal file
3
config/set/framework-migration/mockista-to-mockery.yaml
Normal file
@ -0,0 +1,3 @@
|
||||
services:
|
||||
Rector\MockistaToMockery\Rector\Class_\MockeryTearDownRector: null
|
||||
Rector\MockistaToMockery\Rector\ClassMethod\MockistaMockToMockeryMockRector: null
|
@ -1,4 +1,4 @@
|
||||
# All 483 Rectors Overview
|
||||
# All 485 Rectors Overview
|
||||
|
||||
- [Projects](#projects)
|
||||
- [General](#general)
|
||||
@ -22,6 +22,7 @@
|
||||
- [JMS](#jms) (2)
|
||||
- [Laravel](#laravel) (6)
|
||||
- [Legacy](#legacy) (2)
|
||||
- [MockistaToMockery](#mockistatomockery) (2)
|
||||
- [MysqlToMysqli](#mysqltomysqli) (4)
|
||||
- [Naming](#naming) (1)
|
||||
- [Nette](#nette) (11)
|
||||
@ -4129,6 +4130,56 @@ Change functions to static calls, so composer can autoload them
|
||||
|
||||
<br>
|
||||
|
||||
## MockistaToMockery
|
||||
|
||||
### `MockeryTearDownRector`
|
||||
|
||||
- class: [`Rector\MockistaToMockery\Rector\Class_\MockeryTearDownRector`](/../master/rules/mockista-to-mockery/src/Rector/Class_/MockeryTearDownRector.php)
|
||||
- [test fixtures](/../master/rules/mockista-to-mockery/tests/Rector/Class_/MockeryTearDownRector/Fixture)
|
||||
|
||||
Add Mockery::close() in tearDown() method if not yet
|
||||
|
||||
```diff
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class SomeTest extends TestCase
|
||||
{
|
||||
+ protected function tearDown(): void
|
||||
+ {
|
||||
+ Mockery::close();
|
||||
+ }
|
||||
public function test()
|
||||
{
|
||||
$mockUser = mock(User::class);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
<br>
|
||||
|
||||
### `MockistaMockToMockeryMockRector`
|
||||
|
||||
- class: [`Rector\MockistaToMockery\Rector\ClassMethod\MockistaMockToMockeryMockRector`](/../master/rules/mockista-to-mockery/src/Rector/ClassMethod/MockistaMockToMockeryMockRector.php)
|
||||
- [test fixtures](/../master/rules/mockista-to-mockery/tests/Rector/ClassMethod/MockistaMockToMockeryMockRector/Fixture)
|
||||
|
||||
Change functions to static calls, so composer can autoload them
|
||||
|
||||
```diff
|
||||
class SomeTest
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
- $mockUser = mock(User::class);
|
||||
- $mockUser->getId()->once->andReturn(1);
|
||||
- $mockUser->freeze();
|
||||
+ $mockUser = Mockery::mock(User::class);
|
||||
+ $mockUser->expects()->getId()->once()->andReturn(1);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
<br>
|
||||
|
||||
## MysqlToMysqli
|
||||
|
||||
### `MysqlAssignToMysqliRector`
|
||||
|
@ -13,6 +13,7 @@ use PhpParser\Node\Name;
|
||||
use PhpParser\Node\Stmt\ClassLike;
|
||||
use PhpParser\Node\Stmt\Interface_;
|
||||
use PhpParser\Node\Stmt\Trait_;
|
||||
use Rector\Core\Contract\Rector\RectorInterface;
|
||||
use Rector\Core\Exception\ShouldNotHappenException;
|
||||
use Rector\Core\PhpParser\Printer\BetterStandardPrinter;
|
||||
use Rector\NodeNameResolver\Contract\NodeNameResolverInterface;
|
||||
@ -214,9 +215,11 @@ final class NodeNameResolver
|
||||
}
|
||||
|
||||
$backtrace = debug_backtrace();
|
||||
if (isset($backtrace[1])) {
|
||||
$fileInfo = new SmartFileInfo($backtrace[1]['file']);
|
||||
$fileAndLine = $fileInfo->getRelativeFilePathFromCwd() . ':' . $backtrace[1]['line'];
|
||||
$rectorBacktrace = $this->matchRectorBacktraceCall($backtrace);
|
||||
|
||||
if ($rectorBacktrace) {
|
||||
$fileInfo = new SmartFileInfo($rectorBacktrace['file']);
|
||||
$fileAndLine = $fileInfo->getRelativeFilePathFromCwd() . ':' . $rectorBacktrace['line'];
|
||||
|
||||
$message .= PHP_EOL . PHP_EOL;
|
||||
$message .= sprintf('Look at %s', $fileAndLine);
|
||||
@ -224,4 +227,22 @@ final class NodeNameResolver
|
||||
|
||||
throw new ShouldNotHappenException($message);
|
||||
}
|
||||
|
||||
private function matchRectorBacktraceCall(array $backtrace): ?array
|
||||
{
|
||||
foreach ($backtrace as $singleTrace) {
|
||||
if (! isset($singleTrace['object'])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// match a Rector class
|
||||
if (! is_a($singleTrace['object'], RectorInterface::class)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
return $singleTrace;
|
||||
}
|
||||
|
||||
return $backtrace[1] ?? null;
|
||||
}
|
||||
}
|
||||
|
9
rules/mockista-to-mockery/config/config.yaml
Normal file
9
rules/mockista-to-mockery/config/config.yaml
Normal file
@ -0,0 +1,9 @@
|
||||
services:
|
||||
_defaults:
|
||||
public: true
|
||||
autowire: true
|
||||
|
||||
Rector\MockistaToMockery\:
|
||||
resource: '../src'
|
||||
exclude:
|
||||
- '../src/Rector/**/*Rector.php'
|
41
rules/mockista-to-mockery/src/MockistaDetector.php
Normal file
41
rules/mockista-to-mockery/src/MockistaDetector.php
Normal file
@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\MockistaToMockery;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr\FuncCall;
|
||||
use PhpParser\Node\Stmt\Class_;
|
||||
use Rector\Core\PhpParser\Node\BetterNodeFinder;
|
||||
use Rector\NodeNameResolver\NodeNameResolver;
|
||||
|
||||
final class MockistaDetector
|
||||
{
|
||||
/**
|
||||
* @var BetterNodeFinder
|
||||
*/
|
||||
private $betterNodeFinder;
|
||||
|
||||
/**
|
||||
* @var NodeNameResolver
|
||||
*/
|
||||
private $nodeNameResolver;
|
||||
|
||||
public function __construct(BetterNodeFinder $betterNodeFinder, NodeNameResolver $nodeNameResolver)
|
||||
{
|
||||
$this->betterNodeFinder = $betterNodeFinder;
|
||||
$this->nodeNameResolver = $nodeNameResolver;
|
||||
}
|
||||
|
||||
public function isInClass(Class_ $class): bool
|
||||
{
|
||||
return (bool) $this->betterNodeFinder->findFirst((array) $class->stmts, function (Node $node) {
|
||||
if (! $node instanceof FuncCall) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $this->nodeNameResolver->isName($node, 'mock');
|
||||
});
|
||||
}
|
||||
}
|
@ -0,0 +1,300 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\MockistaToMockery\Rector\ClassMethod;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr\Assign;
|
||||
use PhpParser\Node\Expr\FuncCall;
|
||||
use PhpParser\Node\Expr\MethodCall;
|
||||
use PhpParser\Node\Expr\PropertyFetch;
|
||||
use PhpParser\Node\Expr\Variable;
|
||||
use PhpParser\Node\Stmt\ClassMethod;
|
||||
use Rector\Core\Rector\AbstractPHPUnitRector;
|
||||
use Rector\Core\RectorDefinition\CodeSample;
|
||||
use Rector\Core\RectorDefinition\RectorDefinition;
|
||||
use Rector\NodeTypeResolver\Node\AttributeKey;
|
||||
use ReflectionMethod;
|
||||
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
|
||||
|
||||
/**
|
||||
* @see \Rector\MockistaToMockery\Tests\Rector\ClassMethod\MockistaMockToMockeryMockRector\MockistaMockToMockeryMockRectorTest
|
||||
*/
|
||||
final class MockistaMockToMockeryMockRector extends AbstractPHPUnitRector
|
||||
{
|
||||
/**
|
||||
* @var string[]
|
||||
*/
|
||||
private const METHODS_TO_REMOVE = ['freeze', 'assertExpectations'];
|
||||
|
||||
/**
|
||||
* @var string[]
|
||||
*/
|
||||
private $mockVariableTypesByNames = [];
|
||||
|
||||
public function getDefinition(): RectorDefinition
|
||||
{
|
||||
return new RectorDefinition('Change functions to static calls, so composer can autoload them', [
|
||||
new CodeSample(
|
||||
<<<'PHP'
|
||||
class SomeTest
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
$mockUser = mock(User::class);
|
||||
$mockUser->getId()->once->andReturn(1);
|
||||
$mockUser->freeze();
|
||||
}
|
||||
}
|
||||
PHP
|
||||
,
|
||||
<<<'PHP'
|
||||
class SomeTest
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
$mockUser = Mockery::mock(User::class);
|
||||
$mockUser->expects()->getId()->once()->andReturn(1);
|
||||
}
|
||||
}
|
||||
PHP
|
||||
),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
public function getNodeTypes(): array
|
||||
{
|
||||
return [ClassMethod::class];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ClassMethod $node
|
||||
*/
|
||||
public function refactor(Node $node): ?Node
|
||||
{
|
||||
if (! $this->isInTestClass($node)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$this->replaceMockWithMockerMockAndCollectMockVariableName($node);
|
||||
$this->replaceMethodCallOncePropertyFetch($node);
|
||||
$this->removeUnusedMethodCalls($node);
|
||||
$this->replaceMethodCallWithExpects($node);
|
||||
|
||||
$this->switchWithAnyArgsAndOnceTwice($node);
|
||||
|
||||
return $node;
|
||||
}
|
||||
|
||||
private function collectMockVariableName(FuncCall $funcCall): void
|
||||
{
|
||||
$parentNode = $funcCall->getAttribute(AttributeKey::PARENT_NODE);
|
||||
if (! $parentNode instanceof Assign) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (! $parentNode->var instanceof Variable) {
|
||||
return;
|
||||
}
|
||||
|
||||
/** @var Variable $variable */
|
||||
$variable = $parentNode->var;
|
||||
|
||||
/** @var string $variableName */
|
||||
$variableName = $this->getName($variable);
|
||||
|
||||
$type = $funcCall->args[0]->value;
|
||||
$mockedType = $this->getValue($type);
|
||||
|
||||
$this->mockVariableTypesByNames[$variableName] = $mockedType;
|
||||
}
|
||||
|
||||
private function isMethodCallOrPropertyFetchOnMockVariable(Node $node): bool
|
||||
{
|
||||
if (! $node instanceof MethodCall && ! $this->isPropertyFetchDisguisedAsMethodCall($node)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (! $node->var instanceof Variable) {
|
||||
return false;
|
||||
}
|
||||
|
||||
/** @var string $variableName */
|
||||
$variableName = $this->getName($node->var);
|
||||
|
||||
return isset($this->mockVariableTypesByNames[$variableName]);
|
||||
}
|
||||
|
||||
private function replaceMockWithMockerMockAndCollectMockVariableName(ClassMethod $classMethod): void
|
||||
{
|
||||
$this->traverseNodesWithCallable((array) $classMethod->stmts, function (Node $node) {
|
||||
if (! $this->isFuncCallName($node, 'mock')) {
|
||||
return null;
|
||||
}
|
||||
|
||||
/** @var FuncCall $node */
|
||||
$this->collectMockVariableName($node);
|
||||
|
||||
return $this->createStaticCall('Mockery', 'mock', $node->args);
|
||||
});
|
||||
}
|
||||
|
||||
private function removeUnusedMethodCalls(ClassMethod $classMethod): void
|
||||
{
|
||||
$this->traverseNodesWithCallable((array) $classMethod->stmts, function (Node $node) {
|
||||
if (! $this->isMethodCallOrPropertyFetchOnMockVariable($node)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (! $this->isNames($node->name, self::METHODS_TO_REMOVE)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$this->removeNode($node);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* $mock->getMethod()->once()
|
||||
* ↓
|
||||
* $mock->expects()->getMethod()->once()
|
||||
*/
|
||||
private function replaceMethodCallWithExpects(ClassMethod $classMethod): void
|
||||
{
|
||||
$this->traverseNodesWithCallable((array) $classMethod->stmts, function (Node $node) {
|
||||
if (! $this->isMethodCallOrPropertyFetchOnMockVariable($node)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// skip assigns
|
||||
$parent = $node->getAttribute(AttributeKey::PARENT_NODE);
|
||||
if ($parent instanceof Assign) {
|
||||
return null;
|
||||
}
|
||||
|
||||
/** @var MethodCall|PropertyFetch $node */
|
||||
if ($this->isNames($node->name, self::METHODS_TO_REMOVE)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if ($this->isNames($node->name, ['expects', 'allows'])) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// probably method mock
|
||||
$expectedMethodCall = new MethodCall($node->var, 'expects');
|
||||
$methodCall = new MethodCall($expectedMethodCall, $node->name);
|
||||
|
||||
if ($node instanceof PropertyFetch) {
|
||||
return $methodCall;
|
||||
}
|
||||
|
||||
$methodCall->args = $node->args;
|
||||
|
||||
return $this->decorateWithAnyArgs($node, $methodCall);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* $mock->getMethod()->once
|
||||
* ↓
|
||||
* $mock->getMethod()->once()
|
||||
*/
|
||||
private function replaceMethodCallOncePropertyFetch(ClassMethod $classMethod): void
|
||||
{
|
||||
$this->traverseNodesWithCallable((array) $classMethod->stmts, function (Node $node) {
|
||||
if (! $node instanceof PropertyFetch) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (! $this->isNames($node->name, ['once', 'twice'])) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return new MethodCall($node->var, $node->name);
|
||||
});
|
||||
}
|
||||
|
||||
private function isPropertyFetchDisguisedAsMethodCall(Node $node): bool
|
||||
{
|
||||
if (! $node instanceof PropertyFetch) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($node->var instanceof MethodCall) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$variableName = $this->getName($node->var);
|
||||
if (! isset($this->mockVariableTypesByNames[$variableName])) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$mockVariableType = $this->mockVariableTypesByNames[$variableName];
|
||||
$propertyName = $this->getName($node->name);
|
||||
if ($propertyName === null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return method_exists($mockVariableType, $propertyName);
|
||||
}
|
||||
|
||||
/**
|
||||
* $mock->someMethodWithArgs()->once()
|
||||
* ↓
|
||||
* $mock->expects()->someMethodWithArgs()->withAnyArgs()->once()
|
||||
*/
|
||||
private function decorateWithAnyArgs(MethodCall $originalMethodCall, MethodCall $expectsMethodCall): MethodCall
|
||||
{
|
||||
$variableName = $this->getName($originalMethodCall->var);
|
||||
$mockVariableType = $this->mockVariableTypesByNames[$variableName];
|
||||
|
||||
$methodName = $this->getName($originalMethodCall->name);
|
||||
if ($methodName === null) {
|
||||
return $expectsMethodCall;
|
||||
}
|
||||
|
||||
if (! method_exists($mockVariableType, $methodName)) {
|
||||
return $expectsMethodCall;
|
||||
}
|
||||
|
||||
$methodReflection = new ReflectionMethod($mockVariableType, $methodName);
|
||||
if ($methodReflection->getNumberOfRequiredParameters() === 0) {
|
||||
return $expectsMethodCall;
|
||||
}
|
||||
|
||||
return new MethodCall($expectsMethodCall, 'withAnyArgs');
|
||||
}
|
||||
|
||||
/**
|
||||
* Order correction for @see replaceMethodCallWithExpects()
|
||||
*/
|
||||
private function switchWithAnyArgsAndOnceTwice(ClassMethod $classMethod): void
|
||||
{
|
||||
$this->traverseNodesWithCallable((array) $classMethod->stmts, function (Node $node) {
|
||||
if (! $node instanceof MethodCall) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (! $this->isNames($node->name, ['once', 'twice'])) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (! $node->var instanceof MethodCall) {
|
||||
return null;
|
||||
}
|
||||
|
||||
/** @var MethodCall $previousMethodCall */
|
||||
$previousMethodCall = $node->var;
|
||||
if (! $this->isName($previousMethodCall->name, 'withAnyArgs')) {
|
||||
return null;
|
||||
}
|
||||
|
||||
[$node->name, $previousMethodCall->name] = [$previousMethodCall->name, $node->name];
|
||||
});
|
||||
}
|
||||
}
|
@ -0,0 +1,130 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\MockistaToMockery\Rector\Class_;
|
||||
|
||||
use PhpParser\Builder\Method;
|
||||
use PhpParser\BuilderHelpers;
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr\StaticCall;
|
||||
use PhpParser\Node\Stmt;
|
||||
use PhpParser\Node\Stmt\Class_;
|
||||
use PhpParser\Node\Stmt\ClassMethod;
|
||||
use Rector\Core\Rector\AbstractRector;
|
||||
use Rector\Core\RectorDefinition\CodeSample;
|
||||
use Rector\Core\RectorDefinition\RectorDefinition;
|
||||
use Rector\MockistaToMockery\MockistaDetector;
|
||||
|
||||
/**
|
||||
* @see \Rector\MockistaToMockery\Tests\Rector\Class_\MockeryTearDownRector\MockeryTearDownRectorTest
|
||||
*/
|
||||
final class MockeryTearDownRector extends AbstractRector
|
||||
{
|
||||
/**
|
||||
* @var MockistaDetector
|
||||
*/
|
||||
private $mockistaDetector;
|
||||
|
||||
public function __construct(MockistaDetector $mockistaDetector)
|
||||
{
|
||||
$this->mockistaDetector = $mockistaDetector;
|
||||
}
|
||||
|
||||
public function getDefinition(): RectorDefinition
|
||||
{
|
||||
return new RectorDefinition('Add Mockery::close() in tearDown() method if not yet', [
|
||||
new CodeSample(
|
||||
<<<'PHP'
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class SomeTest extends TestCase
|
||||
{
|
||||
public function test()
|
||||
{
|
||||
$mockUser = mock(User::class);
|
||||
}
|
||||
}
|
||||
PHP
|
||||
,
|
||||
<<<'PHP'
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class SomeTest extends TestCase
|
||||
{
|
||||
protected function tearDown(): void
|
||||
{
|
||||
Mockery::close();
|
||||
}
|
||||
public function test()
|
||||
{
|
||||
$mockUser = mock(User::class);
|
||||
}
|
||||
}
|
||||
PHP
|
||||
|
||||
),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
public function getNodeTypes(): array
|
||||
{
|
||||
return [Class_::class];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Class_ $node
|
||||
*/
|
||||
public function refactor(Node $node): ?Node
|
||||
{
|
||||
if (! $this->mockistaDetector->isInClass($node)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$tearDownClassMethod = $node->getMethod('tearDown');
|
||||
if ($tearDownClassMethod === null) {
|
||||
$node->stmts[] = $this->createTearDownMethodWithMockeryClose();
|
||||
} elseif (! $this->containsMockeryClose($tearDownClassMethod)) {
|
||||
$tearDownClassMethod->stmts[] = $this->createMockeryClose();
|
||||
}
|
||||
|
||||
return $node;
|
||||
}
|
||||
|
||||
private function createTearDownMethodWithMockeryClose(): ClassMethod
|
||||
{
|
||||
$tearDownClassMethodBuilder = new Method('tearDown');
|
||||
$tearDownClassMethodBuilder->setReturnType('void');
|
||||
$tearDownClassMethodBuilder->makeProtected();
|
||||
|
||||
$staticCall = $this->createMockeryClose();
|
||||
$tearDownClassMethodBuilder->addStmt($staticCall);
|
||||
|
||||
return $tearDownClassMethodBuilder->getNode();
|
||||
}
|
||||
|
||||
private function containsMockeryClose(ClassMethod $classMethod): bool
|
||||
{
|
||||
return (bool) $this->betterNodeFinder->findFirst((array) $classMethod->stmts, function (Node $node) {
|
||||
if (! $node instanceof StaticCall) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (! $this->isName($node->class, 'Mockery')) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $this->isName($node->name, 'close');
|
||||
});
|
||||
}
|
||||
|
||||
private function createMockeryClose(): Stmt
|
||||
{
|
||||
$staticCall = $this->createStaticCall('Mockery', 'close');
|
||||
|
||||
return BuilderHelpers::normalizeStmt($staticCall);
|
||||
}
|
||||
}
|
@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
namespace Rector\MockistaToMockery\Tests\Rector\ClassMethod\MockistaMockToMockeryMockRector\Fixture;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class SomeTest extends TestCase
|
||||
{
|
||||
public function testSomething()
|
||||
{
|
||||
$mockUser = mock(User::class);
|
||||
$mockUser->getId()->once->andReturn(1);
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\MockistaToMockery\Tests\Rector\ClassMethod\MockistaMockToMockeryMockRector\Fixture;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class SomeTest extends TestCase
|
||||
{
|
||||
public function testSomething()
|
||||
{
|
||||
$mockUser = \Mockery::mock(User::class);
|
||||
$mockUser->expects()->getId()->once()->andReturn(1);
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
namespace Rector\MockistaToMockery\Tests\Rector\ClassMethod\MockistaMockToMockeryMockRector\Fixture;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class MethodArgs extends TestCase
|
||||
{
|
||||
public function testSomething()
|
||||
{
|
||||
$mockUser = mock(User::class);
|
||||
$mockUser->getId(1);
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\MockistaToMockery\Tests\Rector\ClassMethod\MockistaMockToMockeryMockRector\Fixture;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class MethodArgs extends TestCase
|
||||
{
|
||||
public function testSomething()
|
||||
{
|
||||
$mockUser = \Mockery::mock(User::class);
|
||||
$mockUser->expects()->getId(1);
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
namespace Rector\MockistaToMockery\Tests\Rector\ClassMethod\MockistaMockToMockeryMockRector\Fixture;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Rector\MockistaToMockery\Tests\Rector\ClassMethod\MockistaMockToMockeryMockRector\Source\User;
|
||||
|
||||
class MethodWithArgsWithoutAny extends TestCase
|
||||
{
|
||||
public function testSomething()
|
||||
{
|
||||
$mockUser = mock(User::class);
|
||||
$mockUser->methodWithArg()->once();
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\MockistaToMockery\Tests\Rector\ClassMethod\MockistaMockToMockeryMockRector\Fixture;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Rector\MockistaToMockery\Tests\Rector\ClassMethod\MockistaMockToMockeryMockRector\Source\User;
|
||||
|
||||
class MethodWithArgsWithoutAny extends TestCase
|
||||
{
|
||||
public function testSomething()
|
||||
{
|
||||
$mockUser = \Mockery::mock(User::class);
|
||||
$mockUser->expects()->methodWithArg()->once()->withAnyArgs();
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
namespace Rector\MockistaToMockery\Tests\Rector\ClassMethod\MockistaMockToMockeryMockRector\Fixture;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class MethodsToRemove extends TestCase
|
||||
{
|
||||
public function testSomething()
|
||||
{
|
||||
$mockUser = mock(User::class);
|
||||
$mockUser->freeze();
|
||||
$mockUser->assertExpectations();
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\MockistaToMockery\Tests\Rector\ClassMethod\MockistaMockToMockeryMockRector\Fixture;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class MethodsToRemove extends TestCase
|
||||
{
|
||||
public function testSomething()
|
||||
{
|
||||
$mockUser = \Mockery::mock(User::class);
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
namespace Rector\MockistaToMockery\Tests\Rector\ClassMethod\MockistaMockToMockeryMockRector\Fixture;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Rector\MockistaToMockery\Tests\Rector\ClassMethod\MockistaMockToMockeryMockRector\Source\User;
|
||||
|
||||
class PropertyOrMethod extends TestCase
|
||||
{
|
||||
public function testSomething()
|
||||
{
|
||||
$mockUser = mock(User::class);
|
||||
$mockUser->insert->once;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\MockistaToMockery\Tests\Rector\ClassMethod\MockistaMockToMockeryMockRector\Fixture;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Rector\MockistaToMockery\Tests\Rector\ClassMethod\MockistaMockToMockeryMockRector\Source\User;
|
||||
|
||||
class PropertyOrMethod extends TestCase
|
||||
{
|
||||
public function testSomething()
|
||||
{
|
||||
$mockUser = \Mockery::mock(User::class);
|
||||
$mockUser->expects()->insert()->once();
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
namespace Rector\MockistaToMockery\Tests\Rector\ClassMethod\MockistaMockToMockeryMockRector\Fixture;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class SetValue extends TestCase
|
||||
{
|
||||
public function testSomething()
|
||||
{
|
||||
$mockUser = mock(User::class);
|
||||
$mockUser->id = 5;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\MockistaToMockery\Tests\Rector\ClassMethod\MockistaMockToMockeryMockRector\Fixture;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class SetValue extends TestCase
|
||||
{
|
||||
public function testSomething()
|
||||
{
|
||||
$mockUser = \Mockery::mock(User::class);
|
||||
$mockUser->id = 5;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
namespace Rector\MockistaToMockery\Tests\Rector\ClassMethod\MockistaMockToMockeryMockRector\Fixture;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Rector\MockistaToMockery\Tests\Rector\ClassMethod\MockistaMockToMockeryMockRector\Source\User;
|
||||
|
||||
class Twice extends TestCase
|
||||
{
|
||||
public function testSomething()
|
||||
{
|
||||
$mockUser = mock(User::class);
|
||||
$mockUser->insert->twice;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\MockistaToMockery\Tests\Rector\ClassMethod\MockistaMockToMockeryMockRector\Fixture;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Rector\MockistaToMockery\Tests\Rector\ClassMethod\MockistaMockToMockeryMockRector\Source\User;
|
||||
|
||||
class Twice extends TestCase
|
||||
{
|
||||
public function testSomething()
|
||||
{
|
||||
$mockUser = \Mockery::mock(User::class);
|
||||
$mockUser->expects()->insert()->twice();
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace Rector\MockistaToMockery\Tests\Rector\ClassMethod\MockistaMockToMockeryMockRector\Fixture;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Rector\MockistaToMockery\Tests\Rector\ClassMethod\MockistaMockToMockeryMockRector\Source\User;
|
||||
|
||||
class UseValue extends TestCase
|
||||
{
|
||||
public function testSomething()
|
||||
{
|
||||
$mockUser = mock(User::class);
|
||||
$mockUser->id = 5;
|
||||
$mockUser->run($mockUser->id);
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\MockistaToMockery\Tests\Rector\ClassMethod\MockistaMockToMockeryMockRector\Fixture;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Rector\MockistaToMockery\Tests\Rector\ClassMethod\MockistaMockToMockeryMockRector\Source\User;
|
||||
|
||||
class UseValue extends TestCase
|
||||
{
|
||||
public function testSomething()
|
||||
{
|
||||
$mockUser = \Mockery::mock(User::class);
|
||||
$mockUser->id = 5;
|
||||
$mockUser->expects()->run($mockUser->id);
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\MockistaToMockery\Tests\Rector\ClassMethod\MockistaMockToMockeryMockRector;
|
||||
|
||||
use Iterator;
|
||||
use Rector\Core\Testing\PHPUnit\AbstractRectorTestCase;
|
||||
use Rector\MockistaToMockery\Rector\ClassMethod\MockistaMockToMockeryMockRector;
|
||||
|
||||
final class MockistaMockToMockeryMockRectorTest 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 MockistaMockToMockeryMockRector::class;
|
||||
}
|
||||
}
|
@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\MockistaToMockery\Tests\Rector\ClassMethod\MockistaMockToMockeryMockRector\Source;
|
||||
|
||||
final class User
|
||||
{
|
||||
public function insert()
|
||||
{
|
||||
}
|
||||
|
||||
public function methodWithArg($values)
|
||||
{
|
||||
}
|
||||
}
|
@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
namespace Rector\MockistaToMockery\Tests\Rector\Class_\MockeryTearDownRector\Fixture;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class AddToExistingTearDownTest extends TestCase
|
||||
{
|
||||
public function test()
|
||||
{
|
||||
$mockUser = mock(User::class);
|
||||
}
|
||||
protected function tearDown(): void
|
||||
{
|
||||
$value = 5;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\MockistaToMockery\Tests\Rector\Class_\MockeryTearDownRector\Fixture;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class AddToExistingTearDownTest extends TestCase
|
||||
{
|
||||
public function test()
|
||||
{
|
||||
$mockUser = mock(User::class);
|
||||
}
|
||||
protected function tearDown(): void
|
||||
{
|
||||
$value = 5;
|
||||
\Mockery::close();
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
namespace Rector\MockistaToMockery\Tests\Rector\Class_\MockeryTearDownRector\Fixture;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class SomeTest extends TestCase
|
||||
{
|
||||
public function test()
|
||||
{
|
||||
$mockUser = mock(User::class);
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\MockistaToMockery\Tests\Rector\Class_\MockeryTearDownRector\Fixture;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class SomeTest extends TestCase
|
||||
{
|
||||
public function test()
|
||||
{
|
||||
$mockUser = mock(User::class);
|
||||
}
|
||||
protected function tearDown(): void
|
||||
{
|
||||
\Mockery::close();
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace Rector\MockistaToMockery\Tests\Rector\Class_\MockeryTearDownRector\Fixture;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class SkipExisting extends TestCase
|
||||
{
|
||||
public function test()
|
||||
{
|
||||
$mockUser = \mock(User::class);
|
||||
}
|
||||
protected function tearDown(): void
|
||||
{
|
||||
\Mockery::close();
|
||||
}
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\MockistaToMockery\Tests\Rector\Class_\MockeryTearDownRector;
|
||||
|
||||
use Iterator;
|
||||
use Rector\Core\Testing\PHPUnit\AbstractRectorTestCase;
|
||||
use Rector\MockistaToMockery\Rector\Class_\MockeryTearDownRector;
|
||||
|
||||
final class MockeryTearDownRectorTest 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 MockeryTearDownRector::class;
|
||||
}
|
||||
}
|
10
stubs/Mockista/DummyMock.php
Normal file
10
stubs/Mockista/DummyMock.php
Normal file
@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Mockery;
|
||||
|
||||
class DummyMock implements MockInterface
|
||||
{
|
||||
|
||||
}
|
14
stubs/Mockista/MockInterface.php
Normal file
14
stubs/Mockista/MockInterface.php
Normal file
@ -0,0 +1,14 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Mockery;
|
||||
|
||||
if (class_exists('Mockery\MockerInterface')) {
|
||||
return;
|
||||
}
|
||||
|
||||
interface MockInterface
|
||||
{
|
||||
|
||||
}
|
10
stubs/Mockista/mock_function.php
Normal file
10
stubs/Mockista/mock_function.php
Normal file
@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
if (function_exists('mock')) {
|
||||
return;
|
||||
}
|
||||
|
||||
function mock(): \Mockery\MockInterface
|
||||
{
|
||||
return new DummyMock();
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user