diff --git a/config/sets/phpunit-code-quality.php b/config/sets/phpunit-code-quality.php index 71e0c8d4..647516fe 100644 --- a/config/sets/phpunit-code-quality.php +++ b/config/sets/phpunit-code-quality.php @@ -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; @@ -46,6 +47,7 @@ // narrow with consecutive NarrowIdenticalWithConsecutiveRector::class, + SingleWithConsecutiveToWithRector::class, // specific asserts AssertCompareOnCountableWithMethodToAssertCountRector::class, diff --git a/rules-tests/CodeQuality/Rector/MethodCall/SingleWithConsecutiveToWithRector/Fixture/repeated_same.php.inc b/rules-tests/CodeQuality/Rector/MethodCall/SingleWithConsecutiveToWithRector/Fixture/repeated_same.php.inc new file mode 100644 index 00000000..f256b11b --- /dev/null +++ b/rules-tests/CodeQuality/Rector/MethodCall/SingleWithConsecutiveToWithRector/Fixture/repeated_same.php.inc @@ -0,0 +1,43 @@ +createMock(SomeMockedClass::class); + $someServiceMock->expects($this->exactly(3)) + ->method('prepare') + ->withConsecutive( + [1], + ); + } +} + +?> +----- +createMock(SomeMockedClass::class); + $someServiceMock->expects($this->exactly(3)) + ->method('prepare') + ->with( + [1], + ); + } +} + +?> diff --git a/rules-tests/CodeQuality/Rector/MethodCall/SingleWithConsecutiveToWithRector/Fixture/skip_multiple_values.php.inc b/rules-tests/CodeQuality/Rector/MethodCall/SingleWithConsecutiveToWithRector/Fixture/skip_multiple_values.php.inc new file mode 100644 index 00000000..81cf84b3 --- /dev/null +++ b/rules-tests/CodeQuality/Rector/MethodCall/SingleWithConsecutiveToWithRector/Fixture/skip_multiple_values.php.inc @@ -0,0 +1,20 @@ +createMock(SomeMockedClass::class); + $someServiceMock->expects($this->exactly(3)) + ->method('prepare') + ->withConsecutive( + [1], + [2], + ); + } +} diff --git a/rules-tests/CodeQuality/Rector/MethodCall/SingleWithConsecutiveToWithRector/SingleWithConsecutiveToWithRectorTest.php b/rules-tests/CodeQuality/Rector/MethodCall/SingleWithConsecutiveToWithRector/SingleWithConsecutiveToWithRectorTest.php new file mode 100644 index 00000000..67face27 --- /dev/null +++ b/rules-tests/CodeQuality/Rector/MethodCall/SingleWithConsecutiveToWithRector/SingleWithConsecutiveToWithRectorTest.php @@ -0,0 +1,28 @@ +doTestFile($filePath); + } + + public static function provideData(): Iterator + { + return self::yieldFilesFromDirectory(__DIR__ . '/Fixture'); + } + + public function provideConfigFilePath(): string + { + return __DIR__ . '/config/configured_rule.php'; + } +} diff --git a/rules-tests/CodeQuality/Rector/MethodCall/SingleWithConsecutiveToWithRector/Source/SomeMockedClass.php b/rules-tests/CodeQuality/Rector/MethodCall/SingleWithConsecutiveToWithRector/Source/SomeMockedClass.php new file mode 100644 index 00000000..12d95a97 --- /dev/null +++ b/rules-tests/CodeQuality/Rector/MethodCall/SingleWithConsecutiveToWithRector/Source/SomeMockedClass.php @@ -0,0 +1,7 @@ +rule(SingleWithConsecutiveToWithRector::class); +}; diff --git a/rules/CodeQuality/Rector/MethodCall/NarrowIdenticalWithConsecutiveRector.php b/rules/CodeQuality/Rector/MethodCall/NarrowIdenticalWithConsecutiveRector.php index 90ac40e8..7b8a84c5 100644 --- a/rules/CodeQuality/Rector/MethodCall/NarrowIdenticalWithConsecutiveRector.php +++ b/rules/CodeQuality/Rector/MethodCall/NarrowIdenticalWithConsecutiveRector.php @@ -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( diff --git a/rules/CodeQuality/Rector/MethodCall/SingleWithConsecutiveToWithRector.php b/rules/CodeQuality/Rector/MethodCall/SingleWithConsecutiveToWithRector.php new file mode 100644 index 00000000..4524f8c3 --- /dev/null +++ b/rules/CodeQuality/Rector/MethodCall/SingleWithConsecutiveToWithRector.php @@ -0,0 +1,100 @@ +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> + */ + 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; + } +}