Fork me on GitHub
a Sensio Labs Product

Remote Void (v2.18.1) edition

Rule php_unit_construct

PHPUnit assertion method calls like ->assertSame(true, $foo) should be written with dedicated method like ->assertTrue($foo).

Warning

Using this rule is risky.

Fixer could be risky if one is overriding PHPUnit’s native methods.

Configuration

assertions

List of assertion methods to fix.

Allowed values: a subset of ['assertSame', 'assertEquals', 'assertNotEquals', 'assertNotSame']

Default value: ['assertEquals', 'assertSame', 'assertNotEquals', 'assertNotSame']

Examples

Example #1

Default configuration.

--- Original
+++ New
@@ -1,9 +1,9 @@
 <?php
 final class FooTest extends \PHPUnit_Framework_TestCase {
     public function testSomething() {
-        $this->assertEquals(false, $b);
-        $this->assertSame(true, $a);
-        $this->assertNotEquals(null, $c);
-        $this->assertNotSame(null, $d);
+        $this->assertFalse($b);
+        $this->assertTrue($a);
+        $this->assertNotNull($c);
+        $this->assertNotNull($d);
     }
 }

Example #2

With configuration: ['assertions' => ['assertSame', 'assertNotSame']].

--- Original
+++ New
@@ -2,8 +2,8 @@
 final class FooTest extends \PHPUnit_Framework_TestCase {
     public function testSomething() {
         $this->assertEquals(false, $b);
-        $this->assertSame(true, $a);
+        $this->assertTrue($a);
         $this->assertNotEquals(null, $c);
-        $this->assertNotSame(null, $d);
+        $this->assertNotNull($d);
     }
 }

Rule sets

The rule is part of the following rule sets:

@PhpCsFixer:risky

Using the @PhpCsFixer:risky rule set will enable the php_unit_construct rule with the default config.

@Symfony:risky

Using the @Symfony:risky rule set will enable the php_unit_construct rule with the default config.

The logo is © 2010-2014 Sensio Labs