Infection PHP is an AST based (Abstract Syntax Tree) PHP Mutation Testing Framework. From the documentation “Mutation Testing is a fault-based testing technique which provides a testing criterion called the Mutation Score Indicator (MSI). The MSI can be used to measure the effectiveness of a test set in terms of its ability to detect faults.”

The framework which works as a CLI tool allow to runs the test suit, mutates the source code with a set of predefined mutators (mutation operators), and collects the results of killed, escaped mutants and timeouts.

For example, given you have a Form class with hasErrors() method :

// Form.php
public function hasErrors(): bool
{
    return count($this->errors) > 0;
}

Infection will create the following mutants:

  • Conditional boundary mutator
public function hasErrors(): bool
{
-    return count($this->errors) > 0;
+    return count($this->errors) >= 0;
}
  • Conditional negotiation mutator
public function hasErrors(): bool
{
-    return count($this->errors) > 0;
+    return count($this->errors) < 0;
}
  • and Integer 0-1, 1-0 mutator
public function hasErrors(): bool
{
-    return count($this->errors) > 0;
+    return count($this->errors) > 1;
}

The mutation score will be :

  • Mutation Score Indicator (MSI): 47%
  • Mutation Code Coverage (MCC): 67%
  • Covered Code MSI: 70%
MSI is 47%. This means that 47% of all generated mutations were detected (i.e. kills, timeouts or fatal errors). The MSI is the primary Mutation Testing metric. Given the code coverage of 65%, there is a 18% difference so Code Coverage was a terrible quality measurement in this example.

MCC is 67%. On average it should be within the same ballpark as your normal code coverage.

MSI for code that is actually covered by tests was 70% (ignoring not tested code). This shows you how effective the tests really are.

Infection requires PHP 7+, xDebug or phpdbg enabled. More information at https://infection.github.io

LEAVE A REPLY

Please enter your comment!
Please enter your name here