Content · Web Development · PHP

Using Mutation Testing with Infection PHP

Infection mutates PHP code and evaluates whether the test suite can detect behavioral changes.

1 views Published Oct 17, 2026 Updated Oct 17, 2026 admin

Infection mutates PHP code and evaluates whether the test suite can detect behavioral changes.

What It Does


Mutation testing measures whether tests detect deliberate code changes; it requires coverage support. Test invariants and edge cases, not just one happy path. Boundary assertions are more likely to detect mutations.

Requirements and Compatibility


Use a supported PHP runtime and install any third-party tool or extension explicitly shown in the examples. Commands should be run from the project root unless stated otherwise.

Syntax, Inputs and Results


The examples separate configuration, the smallest working operation and a realistic follow-up task. Adapt file paths and dependency versions to your own project.

Example 1: Basic Use


Mutation testing measures whether tests detect deliberate code changes; it requires coverage support.

composer require --dev infection/infection
XDEBUG_MODE=coverage vendor/bin/infection --threads=2

Example 2: Another Case


Test invariants and edge cases, not just one happy path. Boundary assertions are more likely to detect mutations.

<?php
use PHPUnit\Framework\TestCase;
final class DiscountTest extends TestCase {
    public function testDiscountNeverExceedsPrice(): void {
        $discount = static fn (int $price, int $off): int => max(0, $price - $off);
        self::assertSame(0, $discount(30, 40));
        self::assertGreaterThanOrEqual(0, $discount(100, 5));
    }

    public function test2ArticleBoundary(): void { self::assertSame(8, intdiv(17, 2)); }
}

Practical Example


Coverage requires a driver such as Xdebug with coverage mode enabled.

XDEBUG_MODE=coverage vendor/bin/phpunit --coverage-text
XDEBUG_MODE=coverage vendor/bin/phpunit --coverage-html build/coverage
XDEBUG_MODE=coverage vendor/bin/infection --min-msi=60

For production, apply the compatibility and edge-case limits described below.

Important Behavior and Edge Cases


Mutation scores depend on the selected mutators and test coverage.

Comparison with Related Functions and Tools


SubjectKey difference
Unit tests vs integration testsUnit tests isolate one behavior; integration tests use actual interfaces, databases or framework components.

Common Beginner Mistakes


MistakeCorrect approach
Writing tests without meaningful assertionsAssert an exact result or expected exception, including at least one boundary case.
Skipping the prerequisite of this specific workflowInstall or configure the named tool and confirm the example can actually run.
Ignoring edge conditionsMutation scores depend on the selected mutators and test coverage.

Key Takeaways


  • Infection mutates PHP code and evaluates whether the test suite can detect behavioral changes.
  • Mutation scores depend on the selected mutators and test coverage.
  • Keep application configuration, source code and validation tests under version control.

Use these examples as a starting point, then validate the result with the actual PHP build, dependencies and inputs used by your application.

Share this resourceSend this page to someone who may find it useful.