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

Found 24 rules:

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)
     {
-    }
 }

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)
     {
     }
 }

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;

RemoveOverrideFinalConstructTestCaseRector

Remove override final construct test case

 use PHPUnit\Framework\TestCase;

 final class SomeClass extends TestCase
 {
-    public function __construct()
-    {
-        parent::__construct(static::class);
-    }
 }

AllowMockObjectsForDataProviderRector

Add #[AllowMockObjectsWithoutExpectations] attribute to PHPUnit test classes that have methods with data providers and mock objects without expectations

 use PHPUnit\Framework\Attributes\DataProvider;
 use PHPUnit\Framework\TestCase;

+#[\PHPUnit\Framework\Attributes\AllowMockObjectsWithoutExpectations]
 final class SomeClass extends TestCase
 {
     #[DataProvider('someDataProvider')]
     public function test()
     {
         $someMock = $this->createMock(\stdClass::class);
         $someMock->expects('method')->willReturn(true);
     }

     public static function someDataProvider(): iterable
     {
         yield [1];
         yield [2];
         yield [3];
     }
 }

AllowMockObjectsWhereParentClassRector

Add #[AllowMockObjectsWithoutExpectations] attribute to PHPUnit test classes with a 3rd party test case, that provides any mocks

 use Symfony\Component\Form\Test\TypeTestCase;

+#[\PHPUnit\Framework\Attributes\AllowMockObjectsWithoutExpectations]
 final class SomeTest extends TypeTestCase
 {
 }

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);
     }
 }

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());
     }
 }

AllowMockObjectsWithoutExpectationsAttributeRector

Add #[AllowMockObjectsWithoutExpectations] attribute to PHPUnit test classes with mock properties used in multiple methods but one, to avoid irrelevant notices in tests run

 use PHPUnit\Framework\TestCase;
+use PHPUnit\Framework\Attributes\AllowMockObjectsWithoutExpectations;
+
+#[AllowMockObjectsWithoutExpectations]
 final class SomeTest extends TestCase
 {
     private \PHPUnit\Framework\MockObject\MockObject $someServiceMock;

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

     public function testOne(): void
     {
-        // use $this->someServiceMock
+        $this->someServiceMock->expects($this->once())
+            ->method('someMethod')
+            ->willReturn('someValue');
     }

     public function testTwo(): void
     {
-        // use $this->someServiceMock
+        $this->someServiceMock->expects($this->once())
+            ->method('someMethod')
+            ->willReturn('anotherValue');
     }
 }

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());
     }
 }

TicketAnnotationToAttributeRector

Change annotations with value to attribute

 use PHPUnit\Framework\TestCase;
+use PHPUnit\Framework\Attributes\Ticket;

-/**
- * @ticket 123
- */
+#[Ticket('123')]
 final class SomeTest extends TestCase
 {
 }

CoversAnnotationWithValueToAttributeRector

Change covers annotations with value to attribute

 use PHPUnit\Framework\TestCase;
+use PHPUnit\Framework\Attributes\CoversClass;
+use PHPUnit\Framework\Attributes\CoversFunction;

-/**
- * @covers SomeClass
- */
+#[CoversClass(SomeClass::class)]
+#[CoversFunction('someFunction')]
 final class SomeTest extends TestCase
 {
-    /**
-     * @covers ::someFunction()
-     */
     public function test()
     {
     }
 }

RequiresAnnotationWithValueToAttributeRector

Change Requires annotations with values to attributes

 use PHPUnit\Framework\TestCase;

-/**
- * @requires PHP > 8.4
- * @requires PHPUnit >= 10
- */
-
+#[\PHPUnit\Framework\Attributes\RequiresPhp('> 8.4')]
+#[\PHPUnit\Framework\Attributes\RequiresPhpunit('>= 10')]
 final class SomeTest extends TestCase
 {
-    /**
-     * @requires setting date.timezone Europe/Berlin
-     */
+    #[\PHPUnit\Framework\Attributes\RequiresSetting('date.timezone', 'Europe/Berlin')]
     public function test()
     {
     }
 }

DependsAnnotationWithValueToAttributeRector

Change depends annotations with value to attribute

 use PHPUnit\Framework\TestCase;

 final class SomeTest extends TestCase
 {
     public function testOne() {}

-    /**
-     * @depends testOne
-     */
+    #[\PHPUnit\Framework\Attributes\Depends('testOne')]
     public function testThree(): void
     {
     }
 }

TestWithAnnotationToAttributeRector

Change @testWith() annotation to #[TestWith] attribute

 use PHPUnit\Framework\TestCase;
+use PHPUnit\Framework\Attributes\TestWith;

 final class SomeFixture extends TestCase
 {
-    /**
-     * @testWith ["foo"]
-     *           ["bar"]
-     */
+    #[TestWith(['foo'])]
+    #[TestWith(['bar'])]
     public function test(): void
     {
     }
 }

DataProviderAnnotationToAttributeRector

Change dataProvider annotations to attribute

 use PHPUnit\Framework\TestCase;

 final class SomeTest extends TestCase
 {
-    /**
-     * @dataProvider someMethod()
-     */
+    #[\PHPUnit\Framework\Attributes\DataProvider('someMethod')]
     public function test(): void
     {
     }
 }

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']);
     }
 }

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');
     }
 }

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');
     }
 }

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 }}!');
     }
 }

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');
     }
 }

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);
     }
 }

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);
     }
 }

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
     {
     }
 }