Find the best Rector rule to solve your problem. Searching through 1011 rules.

Found 15 rules:

AssertIsTypeMethodCallRector

Replaces Assert::isType() calls with type-specific Assert::is*() calls

 use PHPUnit\Framework\TestCase;

 final class SomeClass extends TestCase
 {
     public function testMethod(): void
     {
-        $this->assertThat([], $this->isType('array'));
+        $this->assertThat([], $this->isArray());
     }
 }
PACKAGE:  phpunit/phpunit >=11.5

AssertContainsOnlyMethodCallRector

Replaces Assert::assertContainsOnly() calls with type-specific Assert::assertContainsOnly*() calls

 use PHPUnit\Framework\TestCase;

 final class SomeClass extends TestCase
 {
     public function testMethod(): void
     {
-        $this->assertContainsOnly('string', ['a', 'b']);
+        $this->assertContainsOnlyString(['a', 'b']);
     }
 }
PACKAGE:  phpunit/phpunit >=11.5

CreateStubInCoalesceArgRector

Use createStub() over createMock() when used as argument/array item coalesce ?? fallback

 use PHPUnit\Framework\TestCase;

 final class SomeTest extends TestCase
 {
     public function test()
     {
         $mockObject = $this->>get('service');
-        $this->someMethod($mockObject ?? $this->createMock(SomeClass::class));
+        $this->someMethod($mockObject ?? $this->createStub(SomeClass::class));
     }

     private function someMethod($someClass)
     {
-    }
 }
PACKAGE:  phpunit/phpunit >=11.0

CreateStubOverCreateMockArgRector

Use createStub() over createMock() when used as argument or array value and does not add any mock requirements

 use PHPUnit\Framework\TestCase;
+
 final class SomeTest extends TestCase
 {
     public function test()
     {
-        $this->someMethod($this->createMock(SomeClass::class));
+        $this->someMethod($this->createStub(SomeClass::class));
     }

     private function someMethod($someClass)
     {
     }
 }
PACKAGE:  phpunit/phpunit >=11.0

MockObjectVarToStubRector

Update the @var docblock of a property changed to a Stub native type, from MockObject to Stub

 /**
- * @var FieldModel|MockObject
+ * @var FieldModel|\PHPUnit\Framework\MockObject\Stub
  */
 private \PHPUnit\Framework\MockObject\Stub $leadFieldModel;
PACKAGE:  phpunit/phpunit >=11.0

PropertyCreateMockToCreateStubRector

Change mock object property that is never mocked to createStub()

 use PHPUnit\Framework\TestCase;

 final class SomeTest extends TestCase
 {
-    private \PHPUnit\Framework\MockObject\MockObject $someServiceMock;
+    private \PHPUnit\Framework\MockObject\Stub\Stub $someServiceMock;

     protected function setUp(): void
     {
-        $this->someServiceMock = $this->createMock(SomeService::class);
+        $this->someServiceMock = $this->createStub(SomeService::class);
     }

     public function testOne(): void
     {
         $someObject = new SomeClass($this->someServiceMock);
     }

     public function testTwo(): void
     {
         $someObject = new AnotherClass($this->someServiceMock);
     }
 }
PACKAGE:  phpunit/phpunit >=11.0

ExpressionCreateMockToCreateStubRector

Replace createMock() assigned to variable that is only used as arg with no expectations, to createStub()

 use PHPUnit\Framework\TestCase;

 final class SomeTest extends TestCase
 {
     public function test(): void
     {
-        $mock = $this->createMock(SomeClass::class);
+        $mock = $this->createStub(SomeClass::class);

         $someObject = new SomeClass($mock);
         $this->assertSame($mock, $someObject->getDependency());
     }
 }
PACKAGE:  phpunit/phpunit >=11.0

NamedArgumentForDataProviderRector

Change the array-index names to the argument name of the dataProvider

 use PHPUnit\Framework\TestCase;

 final class SomeTest extends TestCase
 {
     public static function dataProviderArray(): array
     {
         return [
             [
-                'keyA' => true,
-                'keyB' => false,
+                'changeToKeyA' => true,
+                'changeToKeyB' => false,
             ]
         ];
     }

     #[\PHPUnit\Framework\Attributes\DataProvider('dataProviderArray')]
     public function testFilter(bool $changeToKeyA, bool $changeToKeyB): void
     {
     }
 }
PACKAGE:  phpunit/phpunit >=11.0

ExpectsParamToMockObjectRector

Change param type of a private test method to MockObject, when expects() is called on it

+use PHPUnit\Framework\MockObject\MockObject;
 use PHPUnit\Framework\TestCase;

 final class SomeTest extends TestCase
 {
-    private function prepareUserMock(SomeUser $user): void
+    private function prepareUserMock(MockObject $user): void
     {
         $user->expects($this->once())
             ->method('getId');
     }
 }
PACKAGE:  phpunit/phpunit >=11.0

MockObjectArgCreateStubToCreateMockRector

Change createStub() to createMock(), when the created variable is passed to a method that requires MockObject type

 use PHPUnit\Framework\MockObject\MockObject;
 use PHPUnit\Framework\TestCase;

 final class SomeTest extends TestCase
 {
     public function test()
     {
-        $someMock = $this->createStub(SomeClass::class);
+        $someMock = $this->createMock(SomeClass::class);
         $this->prepareMock($someMock);
     }

     private function prepareMock(MockObject $someMock): void
     {
         $someMock->expects($this->once())
             ->method('someMethod');
     }
 }
PACKAGE:  phpunit/phpunit >=11.0

RemoveExpectAnyFromMockRector

Remove expect($this->any()) from mocks as it has no added value

 use PHPUnit\Framework\TestCase;

 class SomeClass extends TestCase
 {
     public function test()
     {
         $translator = $this->getMock('SomeClass');
-        $translator->expects($this->any())
-            ->method('trans')
+        $translator->method('trans')
             ->willReturn('translated max {{ max }}!');
     }
 }
PACKAGE:  phpunit/phpunit >=11.0

AddIntersectionParamToMockObjectParamRector

Add a MockObject intersection @param docblock with the mocked class, based on the mock passed in the private method call

 use PHPUnit\Framework\MockObject\MockObject;
 use PHPUnit\Framework\TestCase;

 final class SomeTest extends TestCase
 {
     public function test(): void
     {
         $someService = $this->createMock(SomeService::class);
         $this->prepareServiceMock($someService);
     }

+    /**
+     * @param \SomeService&\PHPUnit\Framework\MockObject\MockObject $someService
+     */
     private function prepareServiceMock(MockObject $someService): void
     {
         $someService->expects($this->once())
             ->method('getId');
     }
 }
PACKAGE:  phpunit/phpunit >=11.0

AddStubIntersectionVarToStubPropertyRector

Add a Stub intersection @var docblock with the stubbed class to a native Stub property

 use PHPUnit\Framework\TestCase;

 final class SomeTest extends TestCase
 {
+    /**
+     * @var \PHPUnit\Framework\MockObject\Stub&\SomeService
+     */
     private \PHPUnit\Framework\MockObject\Stub $someServiceStub;

     protected function setUp(): void
     {
         $this->someServiceStub = $this->createStub(SomeService::class);
     }
 }
PACKAGE:  phpunit/phpunit >=11.0

AddIntersectionVarToMockObjectPropertyRector

Add a MockObject intersection @var docblock with the mocked class to a native MockObject property

 use PHPUnit\Framework\TestCase;

 final class SomeTest extends TestCase
 {
+    /**
+     * @var \PHPUnit\Framework\MockObject\MockObject&\SomeService
+     */
     private \PHPUnit\Framework\MockObject\MockObject $someServiceMock;

     protected function setUp(): void
     {
         $this->someServiceMock = $this->createMock(SomeService::class);
     }
 }
PACKAGE:  phpunit/phpunit >=11.0

BareCreateMockAssignToDirectUseRector

Add explicit instance assert between above nullable object pass

 use PHPUnit\Framework\TestCase;

 final class SomeTest extends TestCase
 {
     public function test()
     {
-        $someObject = $this->createMock(SomeClass::class);
-        $this->process($someObject);
+        $this->process($this->createMock(SomeClass::class));
     }
-

     private function process(SomeClass $someObject): void
     {
     }
 }
PACKAGE:  phpunit/phpunit >=11.0