Skip to content

Commit

Permalink
[CodeQuality] Add SingleWithConsecutiveToWithRector
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Sep 29, 2024
1 parent 4c3932b commit 7d29f98
Show file tree
Hide file tree
Showing 8 changed files with 213 additions and 0 deletions.
2 changes: 2 additions & 0 deletions config/sets/phpunit-code-quality.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use Rector\PHPUnit\CodeQuality\Rector\MethodCall\AssertTrueFalseToSpecificMethodRector;
use Rector\PHPUnit\CodeQuality\Rector\MethodCall\NarrowIdenticalWithConsecutiveRector;
use Rector\PHPUnit\CodeQuality\Rector\MethodCall\RemoveExpectAnyFromMockRector;
use Rector\PHPUnit\CodeQuality\Rector\MethodCall\SingleWithConsecutiveToWithRector;
use Rector\PHPUnit\CodeQuality\Rector\MethodCall\UseSpecificWillMethodRector;
use Rector\PHPUnit\CodeQuality\Rector\MethodCall\UseSpecificWithMethodRector;

Expand All @@ -46,6 +47,7 @@

// narrow with consecutive
NarrowIdenticalWithConsecutiveRector::class,
SingleWithConsecutiveToWithRector::class,

// specific asserts
AssertCompareOnCountableWithMethodToAssertCountRector::class,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\SingleWithConsecutiveToWithRector\Fixture;

use PHPUnit\Framework\TestCase;
use Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\SingleWithConsecutiveToWithRector\Source\SomeMockedClass;

final class RepeatedSame extends TestCase
{
public function test()
{
$someServiceMock = $this->createMock(SomeMockedClass::class);
$someServiceMock->expects($this->exactly(3))
->method('prepare')
->withConsecutive(
[1],
);
}
}

?>
-----
<?php

namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\SingleWithConsecutiveToWithRector\Fixture;

use PHPUnit\Framework\TestCase;
use Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\SingleWithConsecutiveToWithRector\Source\SomeMockedClass;

final class RepeatedSame extends TestCase
{
public function test()
{
$someServiceMock = $this->createMock(SomeMockedClass::class);
$someServiceMock->expects($this->exactly(3))
->method('prepare')
->with(
[1],
);
}
}

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

namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\SingleWithConsecutiveToWithRector\Fixture;

use PHPUnit\Framework\TestCase;
use Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\NarrowIdenticalWithConsecutiveRector\Source\SomeMockedClass;

final class SkipMultipleValues extends TestCase
{
public function test()
{
$someServiceMock = $this->createMock(SomeMockedClass::class);
$someServiceMock->expects($this->exactly(3))
->method('prepare')
->withConsecutive(
[1],
[2],
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\SingleWithConsecutiveToWithRector;

use Iterator;
use PHPUnit\Framework\Attributes\DataProvider;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class SingleWithConsecutiveToWithRectorTest extends AbstractRectorTestCase
{
#[DataProvider('provideData')]
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

public static function provideData(): Iterator
{
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule.php';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\SingleWithConsecutiveToWithRector\Source;

class SomeMockedClass
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\PHPUnit\CodeQuality\Rector\MethodCall\SingleWithConsecutiveToWithRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rule(SingleWithConsecutiveToWithRector::class);
};
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @see \Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\NarrowIdenticalWithConsecutiveRector\NarrowIdenticalWithConsecutiveRectorTest
*/
final class NarrowIdenticalWithConsecutiveRector extends AbstractRector
{
public function __construct(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php

declare(strict_types=1);

namespace Rector\PHPUnit\CodeQuality\Rector\MethodCall;

use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Identifier;
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @see \Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\SingleWithConsecutiveToWithRector\SingleWithConsecutiveToWithRectorTest
*/
final class SingleWithConsecutiveToWithRector extends AbstractRector
{
public function __construct(
private readonly TestsNodeAnalyzer $testsNodeAnalyzer
) {
}

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Change single withConsecutive() to with() call',
[
new CodeSample(
<<<'CODE_SAMPLE'
use PHPUnit\Framework\TestCase;
final class SomeTest extends TestCase
{
public function run()
{
$this->personServiceMock->expects($this->exactly(3))
->method('prepare')
->withConsecutive(
[1],
);
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
use PHPUnit\Framework\TestCase;
final class SomeTest extends TestCase
{
public function run()
{
$this->personServiceMock->expects($this->exactly(3))
->method('prepare')
->with([1]);
}
}
CODE_SAMPLE
),
]
);
}

/**
* @return array<class-string<MethodCall|StaticCall>>
*/
public function getNodeTypes(): array
{
return [MethodCall::class];
}

/**
* @param MethodCall|StaticCall $node
*/
public function refactor(Node $node): MethodCall|StaticCall|null
{
if (! $this->testsNodeAnalyzer->isInTestClass($node)) {
return null;
}

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

if (count($node->getArgs()) !== 1) {
return null;
}

$firstArg = $node->getArgs()[0];

// use simpler with() instead
$node->name = new Identifier('with');
$node->args = [new Arg($firstArg->value)];

return $node;
}
}

0 comments on commit 7d29f98

Please sign in to comment.