mirror of https://github.com/rectorphp/rector.git
[MockeryToProphecy] Deprecate micro set as not practical (#1899)
This commit is contained in:
parent
77a4582d50
commit
320cdcd8de
|
@ -1,4 +1,4 @@
|
|||
# 514 Rules Overview
|
||||
# 512 Rules Overview
|
||||
|
||||
<br>
|
||||
|
||||
|
@ -42,8 +42,6 @@
|
|||
|
||||
- [EarlyReturn](#earlyreturn) (11)
|
||||
|
||||
- [MockeryToProphecy](#mockerytoprophecy) (2)
|
||||
|
||||
- [MysqlToMysqli](#mysqltomysqli) (4)
|
||||
|
||||
- [Naming](#naming) (6)
|
||||
|
@ -6236,40 +6234,6 @@ Changes Single return of || to early returns
|
|||
|
||||
<br>
|
||||
|
||||
## MockeryToProphecy
|
||||
|
||||
### MockeryCloseRemoveRector
|
||||
|
||||
Removes mockery close from test classes
|
||||
|
||||
- class: [`Rector\MockeryToProphecy\Rector\StaticCall\MockeryCloseRemoveRector`](../rules/MockeryToProphecy/Rector/StaticCall/MockeryCloseRemoveRector.php)
|
||||
|
||||
```diff
|
||||
public function tearDown() : void
|
||||
{
|
||||
- \Mockery::close();
|
||||
}
|
||||
```
|
||||
|
||||
<br>
|
||||
|
||||
### MockeryCreateMockToProphizeRector
|
||||
|
||||
Changes mockery mock creation to Prophesize
|
||||
|
||||
- class: [`Rector\MockeryToProphecy\Rector\ClassMethod\MockeryCreateMockToProphizeRector`](../rules/MockeryToProphecy/Rector/ClassMethod/MockeryCreateMockToProphizeRector.php)
|
||||
|
||||
```diff
|
||||
-$mock = \Mockery::mock('MyClass');
|
||||
+$mock = $this->prophesize('MyClass');
|
||||
+
|
||||
$service = new Service();
|
||||
-$service->injectDependency($mock);
|
||||
+$service->injectDependency($mock->reveal());
|
||||
```
|
||||
|
||||
<br>
|
||||
|
||||
## MysqlToMysqli
|
||||
|
||||
### MysqlAssignToMysqliRector
|
||||
|
|
|
@ -1,37 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\MockeryToProphecy\Rector\ClassMethod\MockeryToProphecyRector\Fixture;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class MockCreation extends TestCase
|
||||
{
|
||||
public function testSomething()
|
||||
{
|
||||
$mock = \Mockery::mock('MyClass');
|
||||
|
||||
$service = new Service();
|
||||
$service->injectDependency($mock);
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\Tests\MockeryToProphecy\Rector\ClassMethod\MockeryToProphecyRector\Fixture;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class MockCreation extends TestCase
|
||||
{
|
||||
public function testSomething()
|
||||
{
|
||||
$mock = $this->prophesize('MyClass');
|
||||
|
||||
$service = new Service();
|
||||
$service->injectDependency($mock->reveal());
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -1,33 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\MockeryToProphecy\Rector\ClassMethod\MockeryToProphecyRector\Fixture;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class MockCreationParameter extends TestCase
|
||||
{
|
||||
public function testSomething()
|
||||
{
|
||||
$service = new Service();
|
||||
$service->injectDependency(\Mockery::mock('MyClass'));
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\Tests\MockeryToProphecy\Rector\ClassMethod\MockeryToProphecyRector\Fixture;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class MockCreationParameter extends TestCase
|
||||
{
|
||||
public function testSomething()
|
||||
{
|
||||
$service = new Service();
|
||||
$service->injectDependency($this->prophesize('MyClass')->reveal());
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -1,33 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Tests\MockeryToProphecy\Rector\ClassMethod\MockeryToProphecyRector;
|
||||
|
||||
use Iterator;
|
||||
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
|
||||
use Symplify\SmartFileSystem\SmartFileInfo;
|
||||
|
||||
final class MockeryToProphecyRectorTest extends AbstractRectorTestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider provideData()
|
||||
*/
|
||||
public function test(SmartFileInfo $file): void
|
||||
{
|
||||
$this->doTestFileInfo($file);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Iterator<SmartFileInfo>
|
||||
*/
|
||||
public function provideData(): Iterator
|
||||
{
|
||||
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
|
||||
}
|
||||
|
||||
public function provideConfigFilePath(): string
|
||||
{
|
||||
return __DIR__ . '/config/configured_rule.php';
|
||||
}
|
||||
}
|
|
@ -1,11 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use Rector\MockeryToProphecy\Rector\ClassMethod\MockeryCreateMockToProphizeRector;
|
||||
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
|
||||
|
||||
return static function (ContainerConfigurator $containerConfigurator): void {
|
||||
$services = $containerConfigurator->services();
|
||||
$services->set(MockeryCreateMockToProphizeRector::class);
|
||||
};
|
|
@ -1,30 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\MockeryToProphecy\Rector\StaticCall\MockeryToProphecyRector\Fixture;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class MockeryClose extends TestCase
|
||||
{
|
||||
public function tearDown(): void
|
||||
{
|
||||
\Mockery::close();
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\Tests\MockeryToProphecy\Rector\StaticCall\MockeryToProphecyRector\Fixture;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class MockeryClose extends TestCase
|
||||
{
|
||||
public function tearDown(): void
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -1,33 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Tests\MockeryToProphecy\Rector\StaticCall\MockeryToProphecyRector;
|
||||
|
||||
use Iterator;
|
||||
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
|
||||
use Symplify\SmartFileSystem\SmartFileInfo;
|
||||
|
||||
final class MockeryToProphecyRectorTest extends AbstractRectorTestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider provideData()
|
||||
*/
|
||||
public function test(SmartFileInfo $file): void
|
||||
{
|
||||
$this->doTestFileInfo($file);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Iterator<SmartFileInfo>
|
||||
*/
|
||||
public function provideData(): Iterator
|
||||
{
|
||||
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
|
||||
}
|
||||
|
||||
public function provideConfigFilePath(): string
|
||||
{
|
||||
return __DIR__ . '/config/configured_rule.php';
|
||||
}
|
||||
}
|
|
@ -1,11 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use Rector\MockeryToProphecy\Rector\StaticCall\MockeryCloseRemoveRector;
|
||||
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
|
||||
|
||||
return static function (ContainerConfigurator $containerConfigurator): void {
|
||||
$services = $containerConfigurator->services();
|
||||
$services->set(MockeryCloseRemoveRector::class);
|
||||
};
|
|
@ -1,61 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\MockeryToProphecy\Collector;
|
||||
|
||||
use PhpParser\Node\Arg;
|
||||
use PhpParser\Node\Expr\Assign;
|
||||
use PhpParser\Node\Expr\FuncCall;
|
||||
use PhpParser\Node\Expr\StaticCall;
|
||||
use PhpParser\Node\Expr\Variable;
|
||||
use Rector\Core\PhpParser\Node\Value\ValueResolver;
|
||||
use Rector\NodeNameResolver\NodeNameResolver;
|
||||
use Rector\NodeTypeResolver\Node\AttributeKey;
|
||||
|
||||
final class MockVariableCollector
|
||||
{
|
||||
public function __construct(
|
||||
private readonly NodeNameResolver $nodeNameResolver,
|
||||
private readonly ValueResolver $valueResolver
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, class-string>
|
||||
*/
|
||||
public function collectMockVariableName(FuncCall | StaticCall $node): array
|
||||
{
|
||||
$mockVariableTypesByNames = [];
|
||||
|
||||
$parentNode = $node->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->nodeNameResolver->getName($variable);
|
||||
|
||||
if (! isset($node->args[0])) {
|
||||
return [];
|
||||
}
|
||||
|
||||
if (! $node->args[0] instanceof Arg) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$type = $node->args[0]->value;
|
||||
|
||||
$mockedType = $this->valueResolver->getValue($type);
|
||||
$mockVariableTypesByNames[$variableName] = $mockedType;
|
||||
|
||||
return $mockVariableTypesByNames;
|
||||
}
|
||||
}
|
|
@ -1,151 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\MockeryToProphecy\Rector\ClassMethod;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Arg;
|
||||
use PhpParser\Node\Expr\MethodCall;
|
||||
use PhpParser\Node\Expr\StaticCall;
|
||||
use PhpParser\Node\Expr\Variable;
|
||||
use PhpParser\Node\Stmt\ClassMethod;
|
||||
use PHPStan\Type\ObjectType;
|
||||
use Rector\Core\Rector\AbstractRector;
|
||||
use Rector\MockeryToProphecy\Collector\MockVariableCollector;
|
||||
use Rector\NodeTypeResolver\Node\AttributeKey;
|
||||
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer;
|
||||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
||||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
||||
|
||||
/**
|
||||
* @see \Rector\Tests\MockeryToProphecy\Rector\ClassMethod\MockeryToProphecyRector\MockeryToProphecyRectorTest
|
||||
*/
|
||||
final class MockeryCreateMockToProphizeRector extends AbstractRector
|
||||
{
|
||||
/**
|
||||
* @var array<string, class-string>
|
||||
*/
|
||||
private array $mockVariableTypesByNames = [];
|
||||
|
||||
public function __construct(
|
||||
private readonly MockVariableCollector $mockVariableCollector,
|
||||
private readonly TestsNodeAnalyzer $testsNodeAnalyzer
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<class-string<Node>>
|
||||
*/
|
||||
public function getNodeTypes(): array
|
||||
{
|
||||
return [ClassMethod::class];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ClassMethod $node
|
||||
*/
|
||||
public function refactor(Node $node): ?Node
|
||||
{
|
||||
if (! $this->testsNodeAnalyzer->isInTestClass($node)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$this->replaceMockCreationsAndCollectVariableNames($node);
|
||||
$this->revealMockArguments($node);
|
||||
|
||||
return $node;
|
||||
}
|
||||
|
||||
public function getRuleDefinition(): RuleDefinition
|
||||
{
|
||||
return new RuleDefinition(
|
||||
'Changes mockery mock creation to Prophesize',
|
||||
[
|
||||
new CodeSample(
|
||||
<<<'CODE_SAMPLE'
|
||||
$mock = \Mockery::mock('MyClass');
|
||||
$service = new Service();
|
||||
$service->injectDependency($mock);
|
||||
CODE_SAMPLE
|
||||
,
|
||||
<<<'CODE_SAMPLE'
|
||||
$mock = $this->prophesize('MyClass');
|
||||
|
||||
$service = new Service();
|
||||
$service->injectDependency($mock->reveal());
|
||||
CODE_SAMPLE
|
||||
),
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
private function replaceMockCreationsAndCollectVariableNames(ClassMethod $classMethod): void
|
||||
{
|
||||
if ($classMethod->stmts === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->traverseNodesWithCallable($classMethod->stmts, function (Node $node): ?MethodCall {
|
||||
if (! $node instanceof StaticCall) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$callerType = $this->nodeTypeResolver->getType($node->class);
|
||||
if (! $callerType->isSuperTypeOf(new ObjectType('Mockery'))->yes()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (! $this->isName($node->name, 'mock')) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$collectedVariableTypesByNames = $this->mockVariableCollector->collectMockVariableName($node);
|
||||
|
||||
/** @var array<string, class-string> $result */
|
||||
$result = [...$this->mockVariableTypesByNames, ...$collectedVariableTypesByNames];
|
||||
$this->mockVariableTypesByNames = $result;
|
||||
|
||||
$parentNode = $node->getAttribute(AttributeKey::PARENT_NODE);
|
||||
if ($parentNode instanceof Arg) {
|
||||
$prophesizeMethodCall = $this->createProphesizeMethodCall($node);
|
||||
return $this->nodeFactory->createMethodCall($prophesizeMethodCall, 'reveal');
|
||||
}
|
||||
|
||||
return $this->createProphesizeMethodCall($node);
|
||||
});
|
||||
}
|
||||
|
||||
private function revealMockArguments(ClassMethod $classMethod): void
|
||||
{
|
||||
if ($classMethod->stmts === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->traverseNodesWithCallable($classMethod->stmts, function (Node $node): ?Arg {
|
||||
if (! $node instanceof Arg) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (! $node->value instanceof Variable) {
|
||||
return null;
|
||||
}
|
||||
|
||||
/** @var string $variableName */
|
||||
$variableName = $this->getName($node->value);
|
||||
|
||||
if (! isset($this->mockVariableTypesByNames[$variableName])) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$methodCall = $this->nodeFactory->createMethodCall($node->value, 'reveal');
|
||||
$node->value = $methodCall;
|
||||
return $node;
|
||||
});
|
||||
}
|
||||
|
||||
private function createProphesizeMethodCall(StaticCall $staticCall): MethodCall
|
||||
{
|
||||
return $this->nodeFactory->createLocalMethodCall('prophesize', [$staticCall->args[0]]);
|
||||
}
|
||||
}
|
|
@ -1,78 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\MockeryToProphecy\Rector\StaticCall;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr\StaticCall;
|
||||
use PHPStan\Type\ObjectType;
|
||||
use Rector\Core\Rector\AbstractRector;
|
||||
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer;
|
||||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
||||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
||||
|
||||
/**
|
||||
* @see \Rector\Tests\MockeryToProphecy\Rector\StaticCall\MockeryToProphecyRector\MockeryToProphecyRectorTest
|
||||
*/
|
||||
final class MockeryCloseRemoveRector extends AbstractRector
|
||||
{
|
||||
public function __construct(
|
||||
private readonly TestsNodeAnalyzer $testsNodeAnalyzer
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<class-string<Node>>
|
||||
*/
|
||||
public function getNodeTypes(): array
|
||||
{
|
||||
return [StaticCall::class];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param StaticCall $node
|
||||
*/
|
||||
public function refactor(Node $node): ?Node
|
||||
{
|
||||
if (! $this->testsNodeAnalyzer->isInTestClass($node)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$callerType = $this->nodeTypeResolver->getType($node->class);
|
||||
if (! $callerType->isSuperTypeOf(new ObjectType('Mockery'))->yes()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (! $this->isName($node->name, 'close')) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$this->removeNode($node);
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public function getRuleDefinition(): RuleDefinition
|
||||
{
|
||||
return new RuleDefinition(
|
||||
'Removes mockery close from test classes',
|
||||
[
|
||||
new CodeSample(
|
||||
<<<'CODE_SAMPLE'
|
||||
public function tearDown() : void
|
||||
{
|
||||
\Mockery::close();
|
||||
}
|
||||
CODE_SAMPLE
|
||||
,
|
||||
<<<'CODE_SAMPLE'
|
||||
public function tearDown() : void
|
||||
{
|
||||
}
|
||||
CODE_SAMPLE
|
||||
),
|
||||
]
|
||||
);
|
||||
}
|
||||
}
|
|
@ -1,10 +0,0 @@
|
|||
<?php
|
||||
|
||||
if (function_exists('mock')) {
|
||||
return;
|
||||
}
|
||||
|
||||
function mock(): \Mockery\MockInterface
|
||||
{
|
||||
return new DummyMock();
|
||||
}
|
Loading…
Reference in New Issue