Find the best Rector rule to solve your problem
Turns method names to new ones.
$someObject = new SomeExampleClass;
-$someObject->oldMethod();
+$someObject->newMethod();
Change annotation to attribute
use Symfony\Component\Routing\Annotation\Route;
class SymfonyRoute
{
- /**
- * @Route("/path", name="action") api route
- */
+ #[Route(path: '/path', name: 'action')] // api route
public function action()
{
}
}
Change annotations with value to attribute
use PHPUnit\Framework\TestCase;
+use PHPUnit\Framework\Attributes\Ticket;
-/**
- * @ticket 123
- */
+#[Ticket('123')]
final class SomeTest extends TestCase
{
}
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()
{
}
}
Change annotations with value to attribute
use PHPUnit\Framework\TestCase;
+use PHPUnit\Framework\Attributes\BackupGlobals;
-/**
- * @backupGlobals enabled
- */
+#[BackupGlobals(true)]
final class SomeTest extends TestCase
{
}
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
{
}
}
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
{
}
}
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
{
}
}
Remove "setMethods()" method as never used, move methods to "addMethods()" if non-existent or @method magic
use PHPUnit\Framework\TestCase;
final class SomeTest extends TestCase
{
public function test()
{
$someMock = $this->getMockBuilder(SomeClass::class)
- ->setMethods(['run'])
->getMock();
}
}
Refactor deprecated withConsecutive() to willReturnCallback() structure
use PHPUnit\Framework\TestCase;
final class SomeTest extends TestCase
{
public function run()
{
- $this->personServiceMock->expects($this->exactly(2))
+ $matcher = $this->exactly(2);
+
+ $this->personServiceMock->expects($matcher)
->method('prepare')
- ->withConsecutive(
- [1, 2],
- [3, 4],
- );
+ ->willReturnCallback(function (...$parameters) use ($matcher) {
+ if ($matcher->numberOfInvocations() === 1) {
+ self::assertEquals([1, 2], $parameters);
+ }
+
+ if ($matcher->numberOfInvocations() === 2) {
+ self::assertEquals([3, 4], $parameters),
+ };
+ });
}
}
Change data provider methods to public
use PHPUnit\Framework\TestCase;
final class SomeTest extends TestCase
{
/**
* @dataProvider provideData()
*/
public function test()
{
}
- protected static function provideData()
+ public static function provideData()
{
yield [1];
}
}
Add Prophecy trait for method using $this->prophesize()
use PHPUnit\Framework\TestCase;
+use Prophecy\PhpUnit\ProphecyTrait;
final class ExampleTest extends TestCase
{
+ use ProphecyTrait;
+
public function testOne(): void
{
$prophecy = $this->prophesize(\AnInterface::class);
}
}
Change data provider methods to static
use PHPUnit\Framework\TestCase;
final class SomeTest extends TestCase
{
/**
* @dataProvider provideData()
*/
public function test()
{
}
- public function provideData()
+ public static function provideData()
{
yield [1];
}
}