Find the best Rector rule to solve your problem
Change assertEquals()/assertSame() method using float on expected argument to new specific alternatives.
-$this->assertSame(10.20, $value);
-$this->assertEquals(10.200, $value);
+$this->assertEqualsWithDelta(10.20, $value, PHP_FLOAT_EPSILON);
+$this->assertEqualsWithDelta(10.200, $value, PHP_FLOAT_EPSILON);
Turns strpos
/stripos
comparisons to their method name alternatives in PHPUnit TestCase
-$this->assertFalse(strpos($anything, "foo"), "message");
+$this->assertNotContains("foo", $anything, "message");
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 }}!');
}
}
Turns not-operator comparisons to their method name alternatives in PHPUnit TestCase
-$this->assertTrue(!$foo, "message");
+$this->assertFalse($foo, "message");
Replaces use of assertSame and assertEquals on Countable objects with count method
-$this->assertSame(1, $countable->count());
+$this->assertCount(1, $countable);
Turns property_exists
comparisons to their method name alternatives in PHPUnit TestCase
-$this->assertFalse(property_exists(new Class, "property"));
-$this->assertTrue(property_exists(new Class, "property"));
+$this->assertClassHasAttribute("property", "Class");
+$this->assertClassNotHasAttribute("property", "Class");
Changes ->with() to more specific method
class SomeClass extends PHPUnit\Framework\TestCase
{
public function test()
{
$translator = $this->createMock('SomeClass');
$translator->expects($this->any())
->method('trans')
- ->with($this->equalTo('old max {{ max }}!'));
+ ->with('old max {{ max }}!');
}
}
Turns true/false comparisons to their method name alternatives in PHPUnit TestCase when possible
-$this->assertTrue(is_readable($readmeFile), "message");
+$this->assertIsReadable($readmeFile, "message");
Changes $mock->will() call to more specific method
class SomeClass extends PHPUnit\Framework\TestCase
{
public function test()
{
$translator = $this->createMock('SomeClass');
$translator->expects($this->any())
->method('trans')
- ->will($this->returnValue('translated max {{ max }}!'));
+ ->willReturnValue('translated max {{ max }}!');
}
}
Turns accidentally flipped assert order to right one, with expected expr to left
<?php
namespace RectorPrefix202411;
use PHPUnit\Framework\TestCase;
class SomeTest extends TestCase
{
public function test()
{
$result = '...';
- $this->assertSame($result, 'expected');
+ $this->assertSame('expected', $result);
}
}
\class_alias('SomeTest', 'SomeTest', \false);
Turns vague php-only method in PHPUnit TestCase to more specific
-$this->assertSame(10, count($anything), "message");
+$this->assertCount(10, $anything, "message");
Turns assertEquals()
into stricter assertSame()
for scalar values in PHPUnit TestCase
-$this->assertEquals(2, $result);
+$this->assertSame(2, $result);
Turns instanceof comparisons to their method name alternatives in PHPUnit TestCase
-$this->assertTrue($foo instanceof Foo, "message");
+$this->assertInstanceOf("Foo", $foo, "message");
Change single-value withConsecutive() to with() call, willReturnOnConsecutiveCalls() to willReturn() call
use PHPUnit\Framework\TestCase;
final class SomeTest extends TestCase
{
public function run()
{
$this->personServiceMock->expects($this->exactly(3))
->method('prepare')
- ->withConsecutive(
- [1],
- );
+ ->with([1]);
}
}
Change assertNotEmpty() on an object to more clear assertInstanceof()
use PHPUnit\Framework\TestCase;
class SomeClass extends TestCase
{
public function test()
{
$someObject = new stdClass();
- $this->assertNotEmpty($someObject);
+ $this->assertInstanceof(stdClass::class, $someObject);
}
}
Turns assertTrue() + isset() comparisons to more precise assertArrayHasKey() method
-$this->assertTrue(isset($anything["foo"]), "message");
+$this->assertArrayHasKey("foo", $anything, "message");
Narrow identical withConsecutive() and willReturnOnConsecutiveCalls() to single call
use PHPUnit\Framework\TestCase;
final class SomeTest extends TestCase
{
public function run()
{
$this->personServiceMock->expects($this->exactly(3))
->method('prepare')
- ->withConsecutive(
- [1],
- [1],
- [1],
- )
- ->willReturnOnConsecutiveCalls(
- [2],
- [2],
- [2],
- );
+ ->with([1])
+ ->willReturn([2]);
}
}
Turns preg_match
comparisons to their method name alternatives in PHPUnit TestCase
-$this->assertSame(1, preg_match("/^Message for ".*"\.$/", $string), $message);
+$this->assertRegExp("/^Message for ".*"\.$/", $string, $message);
Narrow single-value match willReturnCallback() to with() and willReturn() call
use PHPUnit\Framework\TestCase;
final class SomeTest extends TestCase
{
public function run()
{
$matcher = $this->exactly(1);
$this->personServiceMock->expects($matcher)
- ->willReturnCallback(function (...$parameters) use ($matcher) {
- match ($matcher->getInvocationCount()) {
- 1 => $this->assertSame([1], $parameters),
- };
- });
+ ->with(1, $parameters);
}
}
Turns same bool and null comparisons to their method name alternatives in PHPUnit TestCase
-$this->assertSame(null, $anything);
+$this->assertNull($anything);
Turns comparison operations to their method name alternatives in PHPUnit TestCase
-$this->assertTrue($foo === $bar, "message");
+$this->assertSame($bar, $foo, "message");
Change $this->assertSame(true, ...) to assertTrue()
use PHPUnit\Framework\TestCase;
final class SomeTest extends TestCase
{
public function test()
{
$value = (bool) mt_rand(0, 1);
- $this->assertSame(true, $value);
+ $this->assertTrue($value);
}
}
Simplify unnecessary foreach check of instances
-foreach ($foos as $foo) {
- $this->assertInstanceOf(SplFileInfo::class, $foo);
-}
+$this->assertContainsOnlyInstancesOf(\SplFileInfo::class, $foos);
Changes PHPUnit calls from self::assert*() to $this->assert*()
use PHPUnit\Framework\TestCase;
final class SomeClass extends TestCase
{
public function run()
{
- self::assertEquals('expected', $result);
+ $this->assertEquals('expected', $result);
}
}
Make properties in tests with intersection mock object either object type or mock type
use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\MockObject\MockObject;
final class MockingEntity extends TestCase
{
- private SimpleObject|MockObject $someEntityMock;
+ private MockObject $someEntityMock;
protected function setUp(): void
{
$this->someEntityMock = $this->createMock(SimpleObject::class);
}
}
Replace testWith annotation to data provider.
+public function dataProviderSum()
+{
+ return [
+ [0, 0, 0],
+ [0, 1, 1],
+ [1, 0, 1],
+ [1, 1, 3]
+ ];
+}
+
/**
- * @testWith [0, 0, 0]
- * @testWith [0, 1, 1]
- * @testWith [1, 0, 1]
- * @testWith [1, 1, 3]
+ * @dataProvider dataProviderSum
*/
-public function testSum(int $a, int $b, int $expected)
+public function test(int $a, int $b, int $expected)
{
$this->assertSame($expected, $a + $b);
}
Turn property used only in setUp() to variable
use PHPUnit\Framework\TestCase;
class SomeServiceTest extends TestCase
{
- private $someServiceMock;
-
public function setUp(): void
{
- $this->someServiceMock = $this->createMock(SomeService::class);
+ $someServiceMock = $this->createMock(SomeService::class);
}
}
Turns array return to yield in data providers
use PHPUnit\Framework\TestCase;
final class SomeTest implements TestCase
{
public static function provideData()
{
- return [
- ['some text']
- ];
+ yield ['some text'];
}
}
Change __construct() method in tests of PHPUnit\Framework\TestCase
to setUp(), to prevent dangerous override
use PHPUnit\Framework\TestCase;
final class SomeTest extends TestCase
{
private $someValue;
- public function __construct(?string $name = null, array $data = [], string $dataName = '')
+ protected function setUp()
{
+ parent::setUp();
+
$this->someValue = 1000;
- parent::__construct($name, $data, $dataName);
}
}
Remove empty test methods
class SomeTest extends \PHPUnit\Framework\TestCase
{
- /**
- * testGetTranslatedModelField method
- *
- * @return void
- */
- public function testGetTranslatedModelField()
- {
- }
}
Replace @test with prefixed function
class SomeTest extends \PHPUnit\Framework\TestCase
{
- /**
- * @test
- */
- public function onePlusOneShouldBeTwo()
+ public function testOnePlusOneShouldBeTwo()
{
$this->assertSame(2, 1+1);
}
}
Change data provider in PHPUnit test case to newline per item
use PHPUnit\Framework\TestCase;
final class ImageBinaryTest extends TestCase
{
/**
* @dataProvider provideData()
*/
public function testGetBytesSize(string $content, int $number): void
{
// ...
}
public static function provideData(): array
{
- return [['content', 8], ['content123', 11]];
+ return [
+ ['content', 8],
+ ['content123', 11]
+ ];
}
}