Find the best Rector rule to solve your problem
Turns method names to new ones.
$someObject = new SomeExampleClass;
-$someObject->oldMethod();
+$someObject->newMethod();
Change assertContains()/assertNotContains() with non-strict comparison to new specific alternatives
-final class SomeTest extends \PHPUnit\Framework\TestCase
+final class SomeTest extends TestCase
{
public function test()
{
$objects = [ new \stdClass(), new \stdClass(), new \stdClass() ];
- $this->assertContains(new \stdClass(), $objects, 'message', false, false);
- $this->assertNotContains(new \stdClass(), $objects, 'message', false, false);
+ $this->assertContainsEquals(new \stdClass(), $objects, 'message');
+ $this->assertNotContainsEquals(new \stdClass(), $objects, 'message');
}
}
Use explicit API for expecting PHP errors, warnings, and notices
final class SomeTest extends \PHPUnit\Framework\TestCase
{
public function test()
{
- $this->expectException(\PHPUnit\Framework\TestCase\Deprecated::class);
- $this->expectException(\PHPUnit\Framework\TestCase\Error::class);
- $this->expectException(\PHPUnit\Framework\TestCase\Notice::class);
- $this->expectException(\PHPUnit\Framework\TestCase\Warning::class);
+ $this->expectDeprecation();
+ $this->expectError();
+ $this->expectNotice();
+ $this->expectWarning();
}
}
Refactor "*TestListener.php" to particular "*Hook.php" files
namespace App\Tests;
-use PHPUnit\Framework\TestListener;
-
-final class BeforeListHook implements TestListener
+final class BeforeListHook implements \PHPUnit\Runner\BeforeTestHook, \PHPUnit\Runner\AfterTestHook
{
- public function addError(Test $test, \Throwable $t, float $time): void
+ public function executeBeforeTest(Test $test): void
{
- }
-
- public function addWarning(Test $test, Warning $e, float $time): void
- {
- }
-
- public function addFailure(Test $test, AssertionFailedError $e, float $time): void
- {
- }
-
- public function addIncompleteTest(Test $test, \Throwable $t, float $time): void
- {
- }
-
- public function addRiskyTest(Test $test, \Throwable $t, float $time): void
- {
- }
-
- public function addSkippedTest(Test $test, \Throwable $t, float $time): void
- {
- }
-
- public function startTestSuite(TestSuite $suite): void
- {
- }
-
- public function endTestSuite(TestSuite $suite): void
- {
- }
-
- public function startTest(Test $test): void
- {
echo 'start test!';
}
- public function endTest(Test $test, float $time): void
+ public function executeAfterTest(Test $test, float $time): void
{
echo $time;
}
}
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),
+ };
+ });
}
}