Write better unit tests in PHP thanks to a new set of rules dedicated to PHPUnit

Hello PHP developers,

A couple of weeks ago, the Java developers were happy to discover 33 rules dedicated to their favorite unit test tools (JUnit, AssertJ).
Now it’s your turn PHP developers to get access to 13 rules that will help write better tests with PHPUnit.

Here is the full list of the new rules:

Bug Rules:

  • S5779: Assertion failure exceptions should not be ignored (Critical)
  • S5783: Only one method invocation is expected when testing checked exceptions (Critical)
  • S5915: Assertions should not be made at the end of blocks expecting an exception (Critical)
  • S5863: Assertions should not compare an object to itself (Major)

Code Smell Rules:

  • S2187: TestCases should contain tests (Blocker)
  • S2699: Tests should include assertions (Blocker)
  • S3360: Test class names should end with “Test” (Blocker)
  • S2701: Literal boolean values and nulls should not be used in equality assertions (Critical)
  • S5785: PHPUnit assertTrue/assertFalse should be simplified to the corresponding dedicated assertion (Major)
  • S1607: A reason should be provided when skipping a test (Major)
  • S3415: Assertion arguments should be passed in the correct order (Major)
  • S5899: Test methods should be discoverable (Major)
  • S5935: Framework-provided functions should be used to test exceptions (Minor)

For more information, you can check the changelog .

This contributes to our objective to provide more features to help you understand the quality of your tests.

These features are already available on SonarCloud, and will be included soon in SonarQube 8.5 and SonarLint.


1 Like