Cara menggunakan phpunit doesnotperformassertions

Add

use Rector\Config\RectorConfig;
use Rector\PHPUnit\Rector\Class_\ArrayArgumentToDataProviderRector;
use Rector\PHPUnit\ValueObject\ArrayArgumentToDataProvider;

return static function (RectorConfig $rectorConfig): void {
    $rectorConfig->ruleWithConfiguration(ArrayArgumentToDataProviderRector::class, [
        ArrayArgumentToDataProviderRector::ARRAY_ARGUMENTS_TO_DATA_PROVIDERS => [
            new ArrayArgumentToDataProvider('PHPUnit\Framework\TestCase', 'doTestMultiple', 'doTestSingle', 'number'),
        ],
    ]);
};
9 annotation test of the class for faster jump to test. Make it FQN, so it stays in the annotation, not in the PHP source code.

  • class:
     use PHPUnit\Framework\TestCase;
    
     class SomeServiceTest extends TestCase
     {
    -    public function test()
    +    /**
    +     * @dataProvider provideData()
    +     */
    +    public function test(int $number)
         {
    -        $this->doTestMultiple([1, 2, 3]);
    +        $this->doTestSingle($number);
    +    }
    +
    +    public function provideData(): \Iterator
    +    {
    +        yield [1];
    +        yield [2];
    +        yield [3];
         }
     }
    0

+/**
+ * @see \SomeServiceTest
+ */
 class SomeService
 {
 }

 use PHPUnit\Framework\TestCase;

 class SomeServiceTest extends TestCase
 {
 }


AnnotationWithValueToAttributeRector

Change annotations with value to attribute

🔧configure it!

  • class:
     use PHPUnit\Framework\TestCase;
    
     class SomeServiceTest extends TestCase
     {
    -    public function test()
    +    /**
    +     * @dataProvider provideData()
    +     */
    +    public function test(int $number)
         {
    -        $this->doTestMultiple([1, 2, 3]);
    +        $this->doTestSingle($number);
    +    }
    +
    +    public function provideData(): \Iterator
    +    {
    +        yield [1];
    +        yield [2];
    +        yield [3];
         }
     }
    1

use Rector\Config\RectorConfig;
use Rector\PHPUnit\Rector\Class_\AnnotationWithValueToAttributeRector;
use Rector\PHPUnit\ValueObject\AnnotationWithValueToAttribute;

return static function (RectorConfig $rectorConfig): void {
    $rectorConfig->ruleWithConfiguration(
        AnnotationWithValueToAttributeRector::class,
        [new AnnotationWithValueToAttribute('backupGlobals', 'PHPUnit\Framework\Attributes\BackupGlobals', [
            true,
            false,
        ])]
    );
};

↓

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

-/**
- * @backupGlobals enabled
- */
+#[BackupGlobals(true)]
 final class SomeTest extends TestCase
 {
 }


ArrayArgumentToDataProviderRector

Move array argument from tests into data provider [configurable]

🔧configure it!

  • class:
     use PHPUnit\Framework\TestCase;
    
     class SomeServiceTest extends TestCase
     {
    -    public function test()
    +    /**
    +     * @dataProvider provideData()
    +     */
    +    public function test(int $number)
         {
    -        $this->doTestMultiple([1, 2, 3]);
    +        $this->doTestSingle($number);
    +    }
    +
    +    public function provideData(): \Iterator
    +    {
    +        yield [1];
    +        yield [2];
    +        yield [3];
         }
     }
    2

use Rector\Config\RectorConfig;
use Rector\PHPUnit\Rector\Class_\ArrayArgumentToDataProviderRector;
use Rector\PHPUnit\ValueObject\ArrayArgumentToDataProvider;

return static function (RectorConfig $rectorConfig): void {
    $rectorConfig->ruleWithConfiguration(ArrayArgumentToDataProviderRector::class, [
        ArrayArgumentToDataProviderRector::ARRAY_ARGUMENTS_TO_DATA_PROVIDERS => [
            new ArrayArgumentToDataProvider('PHPUnit\Framework\TestCase', 'doTestMultiple', 'doTestSingle', 'number'),
        ],
    ]);
};

↓

 use PHPUnit\Framework\TestCase;

 class SomeServiceTest extends TestCase
 {
-    public function test()
+    /**
+     * @dataProvider provideData()
+     */
+    public function test(int $number)
     {
-        $this->doTestMultiple([1, 2, 3]);
+        $this->doTestSingle($number);
+    }
+
+    public function provideData(): \Iterator
+    {
+        yield [1];
+        yield [2];
+        yield [3];
     }
 }


AssertCompareToSpecificMethodRector

Turns vague php-only method in PHPUnit TestCase to more specific

  • class:
     use PHPUnit\Framework\TestCase;
    
     class SomeServiceTest extends TestCase
     {
    -    public function test()
    +    /**
    +     * @dataProvider provideData()
    +     */
    +    public function test(int $number)
         {
    -        $this->doTestMultiple([1, 2, 3]);
    +        $this->doTestSingle($number);
    +    }
    +
    +    public function provideData(): \Iterator
    +    {
    +        yield [1];
    +        yield [2];
    +        yield [3];
         }
     }
    3

-$this->assertSame(10, count($anything), "message");
+$this->assertCount(10, $anything, "message");


-$this->assertNotEquals(get_class($value), SomeInstance::class);
+$this->assertNotInstanceOf(SomeInstance::class, $value);


AssertComparisonToSpecificMethodRector

Turns comparison operations to their method name alternatives in PHPUnit TestCase

  • class:
     use PHPUnit\Framework\TestCase;
    
     class SomeServiceTest extends TestCase
     {
    -    public function test()
    +    /**
    +     * @dataProvider provideData()
    +     */
    +    public function test(int $number)
         {
    -        $this->doTestMultiple([1, 2, 3]);
    +        $this->doTestSingle($number);
    +    }
    +
    +    public function provideData(): \Iterator
    +    {
    +        yield [1];
    +        yield [2];
    +        yield [3];
         }
     }
    4

-$this->assertTrue($foo === $bar, "message");
+$this->assertSame($bar, $foo, "message");


 use PHPUnit\Framework\TestCase;
+use Prophecy\PhpUnit\ProphecyTrait;

 final class ExampleTest extends TestCase
 {
+    use ProphecyTrait;
+
     public function testOne(): void
     {
         $prophecy = $this->prophesize(\AnInterface::class);
     }
 }
0


AssertEqualsParameterToSpecificMethodsTypeRector

Change

 use PHPUnit\Framework\TestCase;

 class SomeServiceTest extends TestCase
 {
-    public function test()
+    /**
+     * @dataProvider provideData()
+     */
+    public function test(int $number)
     {
-        $this->doTestMultiple([1, 2, 3]);
+        $this->doTestSingle($number);
+    }
+
+    public function provideData(): \Iterator
+    {
+        yield [1];
+        yield [2];
+        yield [3];
     }
 }
5 method parameters to new specific alternatives

  • class:
     use PHPUnit\Framework\TestCase;
    
     class SomeServiceTest extends TestCase
     {
    -    public function test()
    +    /**
    +     * @dataProvider provideData()
    +     */
    +    public function test(int $number)
         {
    -        $this->doTestMultiple([1, 2, 3]);
    +        $this->doTestSingle($number);
    +    }
    +
    +    public function provideData(): \Iterator
    +    {
    +        yield [1];
    +        yield [2];
    +        yield [3];
         }
     }
    6

 use PHPUnit\Framework\TestCase;
+use Prophecy\PhpUnit\ProphecyTrait;

 final class ExampleTest extends TestCase
 {
+    use ProphecyTrait;
+
     public function testOne(): void
     {
         $prophecy = $this->prophesize(\AnInterface::class);
     }
 }
1


AssertEqualsToSameRector

Turns

 use PHPUnit\Framework\TestCase;

 class SomeServiceTest extends TestCase
 {
-    public function test()
+    /**
+     * @dataProvider provideData()
+     */
+    public function test(int $number)
     {
-        $this->doTestMultiple([1, 2, 3]);
+        $this->doTestSingle($number);
+    }
+
+    public function provideData(): \Iterator
+    {
+        yield [1];
+        yield [2];
+        yield [3];
     }
 }
7 into stricter
 use PHPUnit\Framework\TestCase;

 class SomeServiceTest extends TestCase
 {
-    public function test()
+    /**
+     * @dataProvider provideData()
+     */
+    public function test(int $number)
     {
-        $this->doTestMultiple([1, 2, 3]);
+        $this->doTestSingle($number);
+    }
+
+    public function provideData(): \Iterator
+    {
+        yield [1];
+        yield [2];
+        yield [3];
     }
 }
8 for scalar values in PHPUnit TestCase

  • class:
     use PHPUnit\Framework\TestCase;
    
     class SomeServiceTest extends TestCase
     {
    -    public function test()
    +    /**
    +     * @dataProvider provideData()
    +     */
    +    public function test(int $number)
         {
    -        $this->doTestMultiple([1, 2, 3]);
    +        $this->doTestSingle($number);
    +    }
    +
    +    public function provideData(): \Iterator
    +    {
    +        yield [1];
    +        yield [2];
    +        yield [3];
         }
     }
    9

 use PHPUnit\Framework\TestCase;
+use Prophecy\PhpUnit\ProphecyTrait;

 final class ExampleTest extends TestCase
 {
+    use ProphecyTrait;
+
     public function testOne(): void
     {
         $prophecy = $this->prophesize(\AnInterface::class);
     }
 }
2


AssertFalseStrposToContainsRector

Turns

-$this->assertSame(10, count($anything), "message");
+$this->assertCount(10, $anything, "message");
0/
-$this->assertSame(10, count($anything), "message");
+$this->assertCount(10, $anything, "message");
1 comparisons to their method name alternatives in PHPUnit TestCase

  • class:
    -$this->assertSame(10, count($anything), "message");
    +$this->assertCount(10, $anything, "message");
    2

 use PHPUnit\Framework\TestCase;
+use Prophecy\PhpUnit\ProphecyTrait;

 final class ExampleTest extends TestCase
 {
+    use ProphecyTrait;
+
     public function testOne(): void
     {
         $prophecy = $this->prophesize(\AnInterface::class);
     }
 }
3


AssertInstanceOfComparisonRector

Turns instanceof comparisons to their method name alternatives in PHPUnit TestCase

  • class:
    -$this->assertSame(10, count($anything), "message");
    +$this->assertCount(10, $anything, "message");
    3

 use PHPUnit\Framework\TestCase;
+use Prophecy\PhpUnit\ProphecyTrait;

 final class ExampleTest extends TestCase
 {
+    use ProphecyTrait;
+
     public function testOne(): void
     {
         $prophecy = $this->prophesize(\AnInterface::class);
     }
 }
4


 use PHPUnit\Framework\TestCase;
+use Prophecy\PhpUnit\ProphecyTrait;

 final class ExampleTest extends TestCase
 {
+    use ProphecyTrait;
+
     public function testOne(): void
     {
         $prophecy = $this->prophesize(\AnInterface::class);
     }
 }
5


AssertIssetToSpecificMethodRector

Turns isset comparisons to their method name alternatives in PHPUnit TestCase

  • class:
    -$this->assertSame(10, count($anything), "message");
    +$this->assertCount(10, $anything, "message");
    4

 use PHPUnit\Framework\TestCase;
+use Prophecy\PhpUnit\ProphecyTrait;

 final class ExampleTest extends TestCase
 {
+    use ProphecyTrait;
+
     public function testOne(): void
     {
         $prophecy = $this->prophesize(\AnInterface::class);
     }
 }
6


 use PHPUnit\Framework\TestCase;
+use Prophecy\PhpUnit\ProphecyTrait;

 final class ExampleTest extends TestCase
 {
+    use ProphecyTrait;
+
     public function testOne(): void
     {
         $prophecy = $this->prophesize(\AnInterface::class);
     }
 }
7


AssertNotOperatorRector

Turns not-operator comparisons to their method name alternatives in PHPUnit TestCase

  • class:
    -$this->assertSame(10, count($anything), "message");
    +$this->assertCount(10, $anything, "message");
    5

 use PHPUnit\Framework\TestCase;
+use Prophecy\PhpUnit\ProphecyTrait;

 final class ExampleTest extends TestCase
 {
+    use ProphecyTrait;
+
     public function testOne(): void
     {
         $prophecy = $this->prophesize(\AnInterface::class);
     }
 }
8


 use PHPUnit\Framework\TestCase;
+use Prophecy\PhpUnit\ProphecyTrait;

 final class ExampleTest extends TestCase
 {
+    use ProphecyTrait;
+
     public function testOne(): void
     {
         $prophecy = $this->prophesize(\AnInterface::class);
     }
 }
9


AssertPropertyExistsRector

Turns

-$this->assertSame(10, count($anything), "message");
+$this->assertCount(10, $anything, "message");
6 comparisons to their method name alternatives in PHPUnit TestCase

  • class:
    -$this->assertSame(10, count($anything), "message");
    +$this->assertCount(10, $anything, "message");
    7

+/**
+ * @see \SomeServiceTest
+ */
 class SomeService
 {
 }

 use PHPUnit\Framework\TestCase;

 class SomeServiceTest extends TestCase
 {
 }
0


AssertRegExpRector

Turns

-$this->assertSame(10, count($anything), "message");
+$this->assertCount(10, $anything, "message");
8 comparisons to their method name alternatives in PHPUnit TestCase

  • class:
    -$this->assertSame(10, count($anything), "message");
    +$this->assertCount(10, $anything, "message");
    9

+/**
+ * @see \SomeServiceTest
+ */
 class SomeService
 {
 }

 use PHPUnit\Framework\TestCase;

 class SomeServiceTest extends TestCase
 {
 }
1


+/**
+ * @see \SomeServiceTest
+ */
 class SomeService
 {
 }

 use PHPUnit\Framework\TestCase;

 class SomeServiceTest extends TestCase
 {
 }
2


AssertResourceToClosedResourceRector

Turns

-$this->assertNotEquals(get_class($value), SomeInstance::class);
+$this->assertNotInstanceOf(SomeInstance::class, $value);
0 into stricter
-$this->assertNotEquals(get_class($value), SomeInstance::class);
+$this->assertNotInstanceOf(SomeInstance::class, $value);
1 for resource values in PHPUnit TestCase

  • class:
    -$this->assertNotEquals(get_class($value), SomeInstance::class);
    +$this->assertNotInstanceOf(SomeInstance::class, $value);
    2

+/**
+ * @see \SomeServiceTest
+ */
 class SomeService
 {
 }

 use PHPUnit\Framework\TestCase;

 class SomeServiceTest extends TestCase
 {
 }
3


AssertSameBoolNullToSpecificMethodRector

Turns same bool and null comparisons to their method name alternatives in PHPUnit TestCase

  • class:
    -$this->assertNotEquals(get_class($value), SomeInstance::class);
    +$this->assertNotInstanceOf(SomeInstance::class, $value);
    3

+/**
+ * @see \SomeServiceTest
+ */
 class SomeService
 {
 }

 use PHPUnit\Framework\TestCase;

 class SomeServiceTest extends TestCase
 {
 }
4


+/**
+ * @see \SomeServiceTest
+ */
 class SomeService
 {
 }

 use PHPUnit\Framework\TestCase;

 class SomeServiceTest extends TestCase
 {
 }
5


AssertSameTrueFalseToAssertTrueFalseRector

Change

-$this->assertNotEquals(get_class($value), SomeInstance::class);
+$this->assertNotInstanceOf(SomeInstance::class, $value);
4 ...) to
-$this->assertNotEquals(get_class($value), SomeInstance::class);
+$this->assertNotInstanceOf(SomeInstance::class, $value);
5

  • class:
    -$this->assertNotEquals(get_class($value), SomeInstance::class);
    +$this->assertNotInstanceOf(SomeInstance::class, $value);
    6

+/**
+ * @see \SomeServiceTest
+ */
 class SomeService
 {
 }

 use PHPUnit\Framework\TestCase;

 class SomeServiceTest extends TestCase
 {
 }
6


AssertTrueFalseInternalTypeToSpecificMethodRector

Turns true/false with internal type comparisons to their method name alternatives in PHPUnit TestCase