testing php applications

Code Coverage

Code coverage reports which executable lines were exercised while tests ran. It is useful for finding untested areas, but a high percentage does not prove that assertions are meaningful or that important scenarios are covered.

Use Coverage As A Map

Look for important branches with no tests: error handling, authorisation, payment failure, migration paths, and edge conditions. Coverage helps direct review attention.

Do Not Chase A Vanity Number

A test that executes a line without asserting the outcome can increase coverage without increasing confidence. Set thresholds only when they improve behaviour and remain maintainable.

XDEBUG_MODE=coverage vendor/bin/phpunit --coverage-text

Common Mistakes

  • Treating 100 percent coverage as proof of correctness.
  • Adding low-value tests only to satisfy a number.
  • Ignoring branch and failure-path gaps.
  • Running expensive coverage collection on every local feedback loop.

What To Practise

  • Generate a coverage report.
  • Use it to find meaningful gaps.
  • Explain why coverage is not confidence by itself.

Practice

Practice: Review A Coverage Gap

Describe how to respond when a checkout service has high line coverage but no test for payment failure.

Requirements

  • Identify the missing business risk.
  • Add a failure-path scenario.
  • Assert customer-visible and persisted outcomes.
  • Avoid adding tests only for percentage.
Show solution

The missing branch matters because payment failure changes user and order state.

Given the payment gateway declines the charge When the customer submits checkout Then the order is not marked paid and the customer receives a safe failure message

The new test is justified by risk, not by the coverage percentage alone.