PHP 7: Maintaining and Upgrading Legacy Apps · Getting Legacy Code Under Test

PHPUnit for PHP 7 Projects

Choose the right PHPUnit version and configure a test suite for an old codebase.

Create a free account to save this page, take notes and track your progress. Sign in

PHPUnit versions track PHP versions: PHPUnit 9 supports PHP 7.3–8.x and is the usual choice for PHP 7.3/7.4 code; PHPUnit 10+ requires PHP 8.1. PHPUnit 9 uses docblock annotations (@dataProvider, @covers) and non-static data providers are allowed. Add a phpunit.xml.dist, a bootstrap that loads the autoloader (and any legacy globals), and start with a few high-value tests.

<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="tests/bootstrap.php" colors="true" failOnRisky="true" failOnWarning="true">
  <testsuites>
    <testsuite name="unit"><directory>tests/Unit</directory></testsuite>
    <testsuite name="integration"><directory>tests/Integration</directory></testsuite>
  </testsuites>
  <coverage processUncoveredFiles="false">
    <include><directory suffix=".php">src</directory></include>
  </coverage>
</phpunit>

Common pitfalls

  • Installing PHPUnit 11 on a PHP 7.4 project (it won't install or run).
  • Tests that need the full app bootstrap, making them slow.
  • Ignoring risky tests (tests without assertions).

Key points

  • PHPUnit 9 for PHP 7.3/7.4
  • Separate unit and integration suites
  • Bootstrap only what tests need

Further reading: PHPUnit 9.6 documentation

Practice questions

Which PHPUnit version should a PHP 7.4 project use?

Answer

PHPUnit 9.x — PHPUnit 10 and later require PHP 8.1+. Upgrade PHPUnit after upgrading PHP.

Why split unit and integration test suites?

Answer

Unit tests run fast on every change; integration tests (database, HTTP) are slower and need infrastructure, so they can run separately in CI.

Open in the app to mark lessons complete, take notes and track your progress.

Was this page helpful?