Back to Php Cs Fixer

Rule ``php_unit_construct``

doc/rules/php_unit/php_unit_construct.rst

3.95.12.4 KB
Original Source

=========================== Rule php_unit_construct

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

Warnings

This rule is RISKY


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

This rule is CONFIGURABLE

You can configure this rule using the following option: assertions.

Configuration

assertions


List of assertion methods to fix.

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

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

Examples
--------

Example #1
~~~~~~~~~~

*Default* configuration.

.. code-block:: diff

   --- Original
   +++ New
    <?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']]``.

.. code-block:: diff

   --- Original
   +++ New
    <?php
    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 <./../../ruleSets/PhpCsFixerRisky.rst>`_
- `@Symfony:risky <./../../ruleSets/SymfonyRisky.rst>`_

References
----------

- Fixer class: `PhpCsFixer\\Fixer\\PhpUnit\\PhpUnitConstructFixer <./../../../src/Fixer/PhpUnit/PhpUnitConstructFixer.php>`_
- Test class: `PhpCsFixer\\Tests\\Fixer\\PhpUnit\\PhpUnitConstructFixerTest <./../../../tests/Fixer/PhpUnit/PhpUnitConstructFixerTest.php>`_

The test class defines officially supported behaviour. Each test case is a part of our backward compatibility promise.