Enable Xdebug's coverage mode to measure which executable statements and branches PHPUnit tests exercise.
What It Does
Coverage requires a driver such as Xdebug with coverage mode enabled. Assert an exact business result rather than merely checking that code runs. Exercise a branch that coverage can record.
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
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
Example 2: Another Case
Assert an exact business result rather than merely checking that code runs. Exercise a branch that coverage can record.
<?php
use PHPUnit\Framework\TestCase;
final class PriceTest extends TestCase {
public function testTaxIsAdded(): void {
$total = static fn (int $cents): int => $cents + intdiv($cents * 10, 100);
self::assertSame(1100, $total(1000));
}
public function test2ArticleBoundary(): void { self::assertTrue(str_starts_with("invoice-42", "invoice-")); }
}
Practical Example
Test invariants and edge cases, not just one happy path. Exercise a branch that coverage can record.
<?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 test3ArticleBoundary(): void { self::assertTrue(str_starts_with("invoice-42", "invoice-")); }
}
For production, apply the compatibility and edge-case limits described below.
Important Behavior and Edge Cases
Keep profiling and step debugging disabled on ordinary production traffic.
Comparison with Related Functions and Tools
| Subject | Key difference |
|---|---|
| Unit tests vs integration tests | Unit tests isolate one behavior; integration tests use actual interfaces, databases or framework components. |
Common Beginner Mistakes
| Mistake | Correct approach |
|---|---|
| Writing tests without meaningful assertions | Assert an exact result or expected exception, including at least one boundary case. |
| Skipping the prerequisite of this specific workflow | Install or configure the named tool and confirm the example can actually run. |
| Ignoring edge conditions | Keep profiling and step debugging disabled on ordinary production traffic. |
Key Takeaways
- Enable Xdebug's coverage mode to measure which executable statements and branches PHPUnit tests exercise.
- Keep profiling and step debugging disabled on ordinary production traffic.
- 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.